Home > Software design >  Why is a control not initializing? In one Form with two classes
Why is a control not initializing? In one Form with two classes

Time:12-27

I try to edit my text until it is compare able for others. Many tasks have not lead to be recoginzeable. First of all i still work not more than with two classes. And my main language i use is not english. Class one, Form1 holds :

Class Form1

Sub New()
    InitializeComponent()

End Sub

End Class

Class two, can be named other than class Form1, named Second_Class holds :

Class Second_Class
'Dim MainForm As New Form1 'object
WithEvents Button1 As New Button
Public Sub New()
    Development()
End Sub

Sub Development()
    Button1 = New Button With {
        .Size = New Size(40, 40),
        .Location = New Point(30, 30),
        .Text = "Click me"
    }
    Form1.Controls.Add(Button1)
End Sub
End Class

By pressing the start without debugger key, the solution can also be achieved with the ctrl F5 key, a form is starting. But the form starts without a button.

CodePudding user response:

Still a lot of work is do be done on this part of the planet. I live in Baden, Loweraustria, and searched alot for easy steps with visual basic dot net, VB.NET. One approach to avoid side bars to maintain readability through my code is to add a new class to my form project. I named it 'SecondProjectClass' . Then i changed in the code window the name to 'Form1' and placed infront of the Public Class name 'Partial'.

Partial Public Class Form1 'second file with filename SecondProjectClass

End Class

The Form1 classfile holds:

Public Class Form1
WithEvents ButtonOne As New Button

Public Sub New()
    InitializeComponent()
    Development(ButtonOne)
    Me.Controls.Add(ButtonOne)
End Sub
End Class

The second file named SecondProjectClass holds:

Partial Public Class Form1
Sub Development(ButtonOne As Button)
    With ButtonOne
        .Size = New Size(40, 40)
        .Location = New Point(30, 30)
        .Text = "Click me"
    End With
End Sub
End Class
  • Related