With my Form1 mainclass i try to use a second classfile named 'strange' holding a button with event clause. How do i reference the procedures?

Štefan Mihael Rihar 181 Reputation points
2021-12-14T21:23:28.613+00:00

Good evening,
my results with the space i have read in the documentations about visual basic, are not ordered yet.

My entry point today is Visual Studio with a visual basic dot net form tab. Past pressing the eF seven key i have included a second class file to my, not implemented, file structure. The second classfile i named 'strange' .

Considering my environment with responsive webdesign and beyond that with responsive mainboard architecture tabs,
i try to support readability with my visual basic code.

Therefore i started the following codelines in a second classfile :

Public Class Strange  
    Inherits Form  
    Public WithEvents Button1 As Button  
    Private userNameValue As String  
    Public Property UserName() As String  
        Get  
            ' Gets the property value.  
            Return userNameValue  
        End Get  
        Set(ByVal Value As String)  
            ' Sets the property value.  
            userNameValue = Value  
        End Set  
    End Property  
    Public Sub Capitalize()  
        ' Capitalize the value of the property.  
        userNameValue = UCase(userNameValue)  
    End Sub  
    Public Sub New(ByVal UserName As String)  
  
        ' Set the property value.  
        Me.UserName = UserName  
  
        Button1 = New Button With {  
            .Size = New Size(40, 40),  
            .Location = New Point(30, 30),  
            .Text = "Click Me"  
        }  
        Me.Controls.Add(Button1)  
    End Sub  
    Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click  
        ' Create an instance of the class.  
        Dim user As New Strange("Moore, Bobby")  
        ' Capitalize the value of the property.  
        user.Capitalize()  
        ' Display the value of the property.  
        MsgBox("The original UserName is: " & user.UserName)  
        ' Change the value of the property.  
        user.UserName = "Worden, Joe"  
        ' Redisplay the value of the property.  
        MsgBox("The new UserName is: " & user.UserName)  
    End Sub  
End Class  
  

I concentrate my work on two documentation sites. First site is : Walkthrough: Defining Classes (Visual Basic)

the second site is : How to: Create a Windows Forms application from the command line

I have used, the command, InitializeComponent() before. I have still problems to recognize referencing procedures or classes and define member procedures, i think, respective methodes, and classes.

My possibility to make mediafiles for my youtubechannel is not the easiest task. It took a long time to find the, for me, parts to be able to work without the visual studio toolbox. For me there are only two sets, timers and keys.
Developing with oszillations and therefore with dispatchers and not only timer impulses is way beyond my scale.
Also TrueType is out of reach, respektive vektors without boarders (or only ellipses). Pixels are enough.

I try to ask what refernces do i have to type with a mainform class form1 that holds,

Public Class Form1  
    Sub New()  
  
        ' Dieser Aufruf ist für den Designer erforderlich.  
        InitializeComponent()  
  
        ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.  
  
    End Sub  
End Class  

to make a visible use with the first code example in this question?
May a calm weather be guided with this question.

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,507 questions
{count} votes

Accepted answer
  1. Jiachen Li-MSFT 24,436 Reputation points Microsoft Vendor
    2021-12-21T07:50:59.997+00:00

    Hi @Štefan Mihael Rihar ,
    Try the following code to see if it meets your expectations?
    The strange class actually defines the form that pops up after you press the button in Form1.
    Strange.vb

     Public Class Strange  
         Inherits Form  
         Public WithEvents Button1 As Button  
         Private userNameValue As String  
         Public Property UserName() As String  
             Get  
                 ' Gets the property value.  
                 Return userNameValue  
             End Get  
             Set(ByVal Value As String)  
                 ' Sets the property value.  
                 userNameValue = Value  
             End Set  
         End Property  
         Public Sub Capitalize()  
             ' Capitalize the value of the property.  
             userNameValue = UCase(userNameValue)  
         End Sub  
         Public Sub New(ByVal UserName As String)  
          
             ' Set the property value.  
             Me.UserName = UserName  
          
             Button1 = New Button With {  
                 .Size = New Size(40, 40),  
                 .Location = New Point(30, 30),  
                 .Text = "Click Me"  
             }  
             Me.Controls.Add(Button1)  
         End Sub  
         Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click  
             ' Create an instance of the class.  
             Dim user As New Strange("Moore, Bobby")  
             ' Capitalize the value of the property.  
             user.Capitalize()  
             ' Display the value of the property.  
             MsgBox("The original UserName is: " & user.UserName)  
             ' Change the value of the property.  
             user.UserName = "Worden, Joe"  
             ' Redisplay the value of the property.  
             MsgBox("The new UserName is: " & user.UserName)  
         End Sub  
     End Class  
    

    Form1.vb

     Public Class Form1  
         Public WithEvents Button1 As Button  
         Public Second_Class As New Strange  
         Sub New()  
             ' Dieser Aufruf ist für den Designer erforderlich.  
             InitializeComponent()  
             ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.  
             Buuton_One_Procedure_Or_Method()  
         End Sub  
         Sub Buuton_One_Procedure_Or_Method()  
             Button1 = New Button()  
             Button1.Size = New Size(40, 40)  
             Button1.Location = New Point(30, 30)  
             Button1.Text = "Click me"  
             Me.Controls.Add(Button1)  
         End Sub  
         Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
             Dim MyName As String = "name"  
             Dim MyForm As New Strange(MyName) 'This line is used to create a Strange class object  
             MyForm.ShowDialog()  
         End Sub  
     End Class  
    

1 additional answer

Sort by: Most helpful
  1. Jiachen Li-MSFT 24,436 Reputation points Microsoft Vendor
    2021-12-15T02:42:20.387+00:00

    Hi @Štefan Mihael Rihar ,
    The Strange class inherits the Form class, so you can use it like the Form class, but you need to bring the UserName parameter.
    You can add a button in Form1, and then use the following code.
    Following code defines a strange form named MyForm when you click button 1, passes in the value of MyName and shows MyForm.

    Public Class Form1  
        Sub New()  
            ' Dieser Aufruf ist für den Designer erforderlich.  
            InitializeComponent()  
            ' Fügen Sie Initialisierungen nach dem InitializeComponent()-Aufruf hinzu.  
        End Sub  
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click  
            Dim MyName As String = "name"  
            Dim MyForm As New Strange(MyName)  
            MyForm.ShowDialog()  
        End Sub  
    End Class  
    

    Hope the code above could be helpful.
    Best Regards.
    Jiachen Li

    ----------

    If the answer is helpful, please click "Accept Answer" and upvote it.
    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.