how do I declare a control in windows forms

byStangz Stanley Grimes 20 Reputation points
2024-11-29T21:56:29.22+00:00

<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()

    CommandAction = New Button()

    Baudratestr = New TextBox()

    Portnamestr = New TextBox()

    ResponseStr = New TextBox()

    BaudRate = New Label()

    PortNamelbl = New Label()

    commandlbl = New Label()

    SuspendLayout()

    ' 

    ' Button1

    ' 

    CommandAction.Location = New Point(122, 120)

    CommandAction.Name = "Button1"

    CommandAction.Size = New Size(75, 23)

    CommandAction.TabIndex = 0

    CommandAction.Text = "Action"

    CommandActionUse.VisualStyleBackColor = True

    ' 

    '  Baudratestr 

    ' 

    Baudratestr.Location = New Point(60, 23)

    Baudratestr.Name = " Baudratestr "

    Baudratestr.Size = New Size(236, 23)

    Baudratestr.TabIndex = 1

    ' 

    ' PortnameStr

    ' 

    Portnamestr.Location = New Point(60, 52)

    Portnamestr.Name = "PortnameStr"

    Portnamestr.Size = New Size(236, 23)

    Portnamestr.TabIndex = 2

    ' 

    ' mmandstr 

    ' 

    mmandstr.Location = New Point(60, 81)

    mmandstr.Name = "mmandstr "

    mmandstr.Size = New Size(236, 23)

    mmandstr.TabIndex = 3

    ' 

    ' BaudRate

    ' 

    BaudRate.AutoSize = True

    BaudRate.Location = New Point(12, 26)

    BaudRate.Name = "BaudRate"

    BaudRate.Size = New Size(13, 15)

    BaudRate.TabIndex = 4

    BaudRate.Text = "a"

    ' 

    ' PortNamelbl

    ' 

    PortNamelbl.AutoSize = True

    PortNamelbl.Location = New Point(13, 52)

    PortNamelbl.Name = "PortNamelbl"

    PortNamelbl.Size = New Size(41, 15)

    PortNamelbl.TabIndex = 5

    PortNamelbl.Text = "PortNamelbl"

    ' 

    ' commandlbl

    ' 

    commandlbl.AutoSize = True

    commandlbl.Location = New Point(12, 81)

    commandlbl.Name = "commandlbl"

    commandlbl.Size = New Size(41, 15)

    commandlbl.TabIndex = 6

    commandlbl.Text = "commandlbl"

    ' 

    ' Form1

    ' 

    AutoScaleDimensions = New SizeF(7.0F, 15.0F)

    AutoScaleMode = AutoScaleMode.Font

    ClientSize = New Size(800, 450)

    Controls.Add(commandlbl)

    Controls.Add(PortNamelbl)

    Controls.Add(BaudRate)

    Controls.Add(mmandstr)

    Controls.Add(Portnamestr)

    Controls.Add(Baudratestr)

    Controls.Add(CommandAction)

    Name = "Form1"

    Text = "Form1"

    ResumeLayout(False)

    PerformLayout()

End Sub

Friend WithEvents Button1 As Button

Friend WithEvents Baudratestr As TextBox

Friend WithEvents Portnamestr As TextBox

Friend WithEvents Commandstr As TextBox

Friend WithEvents BaudRatelbl As Label

Friend WithEvents PortNamelbl As Label

Friend WithEvents commandlbl As Label
Windows
Windows
A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.
5,652 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,760 questions
Microsoft Managed Desktop
Microsoft Managed Desktop
A cloud-based service that brings together Microsoft 365 Enterprise and adds these features: User device deployment; IT service management and operations; and Security monitoring and response.
59 questions
0 comments No comments
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 32,961 Reputation points Microsoft Vendor
    2024-12-02T07:29:08.7066667+00:00

    Hi @byStangz Stanley Grimes

    Locate the Windows Form Designer generated code region in the Designer.vb file and add your control declaration as a class member:

    Private WithEvents MyButton As System.Windows.Forms.Button
    

    In the InitializeComponent() method, initialize the control, set its properties, and add it to the form's Controls collection:

    Me.MyButton = New System.Windows.Forms.Button()
    Me.MyButton.Text = "Click Me"
    Me.MyButton.Size = New System.Drawing.Size(100, 50)
    Me.MyButton.Location = New System.Drawing.Point(50, 50)
    Me.MyButton.Name = "MyButton"
    ' Add the button to the form
    Me.Controls.Add(Me.MyButton)
    

    Best Regards.

    Jiachen Li


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.