Can I maintain vb.net windows form designer code in .vb? - vb.net

Can I maintain vb.net windows form designer code in .vb?
i.e I wanted to ask whether I can maintain only one .vb file for both form design and logic part, instead of having one designer.vb for form design part and .vb file for logic part?

Here is a functional sample:
_
Partial Class Form1
Inherits System.Windows.Forms.Form
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
Public Sub Intiialize()
'Form overrides dispose to clean up the component list.
InitializeComponent()
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
End Sub
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.Text = "Form1"
End Sub
End Class

Related

VB.net global actions

I am writing an application in VB.net (using Visual Studio 2022) which has a lot of text boxes. I am wondering if there is a way to write a routine so any time any of the textboxes have the "enter" action called it will highlight the text contained in the textbox, as opposed to writing the code for each individual textbox "enter" routine?
Assuming that "have the "enter" action called" means when it receives focus, the simplest option would be to create your own custom control that includes that functionality, then use that control instead of regular TextBoxes. Creating custom controls is pretty simple when you want to add to an existing control rather than start from srcatch.
Public Class TextBoxEx
Inherits TextBox
Protected Overrides Sub OnEnter(e As EventArgs)
MyBase.OnEnter(e)
SelectAll()
End Sub
End Class
Once you have that class in your project, it's just a matter replacing existing TextBox refrerences in the designer code with that class. If your project targets .NET Framework, you'll have to Show All Files in the Soluion Explorer to access the designer code file. The designer code for a form with a TextBox will look something like this:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(12, 12)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(100, 20)
Me.TextBox1.TabIndex = 0
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(800, 450)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents TextBox1 As TextBox
End Class
You just need to replace this:
Friend WithEvents TextBox1 As TextBox
with this:
Friend WithEvents TextBox1 As TextBoxEx
and this:
Me.TextBox1 = New System.Windows.Forms.TextBox()
with this:
Me.TextBox1 = New TextBoxEx()
You can even use the Find & Replace functionality in VS to find all the instances you need to replace. Once that's done, everything else is automatic.
Just be sure to have backed up your code before messing with the designer files, in case you break them. Of course, you should be using source control so you should just be able to undo changes to go back to your last good code.

ListViewGroup shows items but not headers

I have read through several forums and tried different options. Most of the answers were this, which makes me assume that would be the issue. I placed Application.EnableVisualStyles()
at the beginning of InitializeComponent() in the .Designer.vb file as seen below:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()>
Partial Class MetadataCollectionGUI
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()>
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()>
Private Sub InitializeComponent()
Application.EnableVisualStyles() ' <--- See Here
Me.ListViewMetaDataCollection = New System.Windows.Forms.ListView()
Me.ColumnTags = CType(New System.Windows.Forms.ColumnHeader(), System.Windows.Forms.ColumnHeader)
etc... etc...
End Sub
Etc...
End Class
Did I misunderstand how to execute that particular solution or is the issue something different?
I found out what the issue was. Go to the project properties and enable the application framework. Now you can enabled the XP visual styles.
Thank you Hursey & jmcilhinney, I would not have figured it out without you all!

Inform programmer with a message when about to use a Usercontrol

First of all I am working with Visual Studio 2017. I have create a UserControl which requires a borderless form. Is there any way to popup a message if programmer try to place this control into a non-borderless form? Something like the message box which appears in design view when we give a wrong value at Properties Window of any control.
EDIT
I noticed that if I add a simple MsgBox into my UserControl's Public Sub New and then add this UserControl to a Form, message box appears.
Public Sub New()
InitializeComponent()
MsgBox("Test Message")
End Sub
But, how can I check if parent form is borderless or not? Something like this example below, which of course doesn't work inside Public Sub New because there is no parent yet!!!
Public Sub New()
InitializeComponent()
if Not MyBase.ParentForm.FormBorderStyle = FormBorderStyle.None Then
MsgBox("Test Message")
End If
End Sub
Well after some tests I managed to solved it...
Into UserControl's Load event (where ParentForm is acceptable), I check first if UserControl is in DesignMode (else user will gets messages everytime application starts) and then, if ParentForm is not FormBorderStyle.None, user informed by a MessageBox.
Private Sub UserControl_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If Not Site Is Nothing AndAlso Site.DesignMode Then
If Not ParentForm.FormBorderStyle = FormBorderStyle.None Then
MessageBox.Show("This control works better with a borderless form!!!", "Control Info", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
End If
End Sub

Cannot view Designer

Please help! I can not view the Control Parameter Adjustment Form in my VB project, other three forms are normal.
How can I fix this problem?
Here is the Designer.vb code:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Control_Parameter_Adjustment
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label()
Me.GroupBox1 = New System.Windows.Forms.GroupBox()
Me.Label31 = New System.Windows.Forms.Label()
......
End Class
At first, I apologize for my bad english.
This worked for me: If you add an empty form to your project, you can double clik on it, and you can see the following:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
You can't see that class definition in the other form, probably because this class have dissapeared (I can't explain you why).
Also and probably, you can find the form_Load event in the form code.
Simply rewrite this class, changing the Form Name in class definition. Then, you can see the designer. The icon also change.

Changing the color of a selected item in a listbox/listview

I'm reasonably new to programming and soon I will be creating a program and would like to change the colour of a selected row via a button click.
I have been attempting this but I don't even know where to begin. If someone could point me in the correct direction it would be much appreciated.
Thanks ^.^
You could simply create a new control, then change the designer to inherit from the listbox control.
the designer code would then look something like this (vs2010):
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class UserControl1
Inherits ListBox
'UserControl overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
components = New System.ComponentModel.Container()
End Sub
End Class
To see the designer code, select "Show All Files" (button at the top of your solution explorer). Then expand the new control node in your solution explorer.
then change the line:
Inherits System.Windows.Forms.UserControl
to:
Inherits ListBox
and finally delete delete the line:
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Hope that helps.