Question:
VB.NET Programically added new form with button - Click even't doesn't see textbox?
simpleonline12
2012-06-26 07:20:39 UTC
I have created my new window, textbox, and button and I can have my button display a messagebox.show("Hello") when I set the code under the click event.

My Window has a button and a textbox.

My issue is when I try to reference my new window's textbox in the code under that click event that I get an error message stating my new window is not declared.

How do you get around that? The code is going to create the new window, the button, and the textbox and then I am trying to have the coded added to the button at runtime but because it doesn't exist yet it's firing an error when I try to build it.

I tried adding the addeventhandler after the windows was created in the code but still a no go.
Five answers:
?
2012-06-26 20:18:01 UTC
Here is some example code that demonstrtes how to dynamically create buttons and assigne event handlers. In addition the created buttons are also copied into an array which gives you an alternate means of access a dynamically created button. Thie code uses the array to rename all the buttons if the form is clicked instead of a button. Clicking a button will change the button color to red or green.



Public Class Form1



Private m_btnArray(4, 4) As Button



Private Sub Form1_Load(ByVal sender As System.Object, _

ByVal e As System.EventArgs) _

Handles MyBase.Load





Me.Width = 65 * 7 '(width of 5 buttons + margins)

Me.Height = (35 * 7) + 35

createArray() 'add 5x5 array of buttons





End Sub

Private Sub createArray()

Dim newbtn As Button

Dim x, y, cnt, btnTop, btnLeft As Integer





btnTop = 0 'initalize



For y = 0 To 4

btnTop += 35

btnLeft = 0 'initalize



For x = 0 To 4

btnLeft += 65

newbtn = New Button

Me.Controls.Add(newbtn) 'add new button to form

AddHandler newbtn.Click, AddressOf multiButton_Click

With newbtn

.Name = "myBTN_" & cnt

.Width = 60

.Height = 30

.Text = "Btn_" & cnt



.Top = btnTop

.Left = btnLeft



.Visible = True

.Enabled = True



End With



m_btnArray(x, y) = newbtn



cnt += 1



Next

Next



End Sub



Private Sub multiButton_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) _

Handles MyBase.Click

Dim btn As New Button

Dim x, y, cnt As Integer

Dim txt As String

Static Dim nameCnt As Integer







If btn.GetType Is sender.GetType Then

'sender is a button

btn = sender



If btn.BackColor = Color.Red Then

btn.BackColor = Color.LightGreen

Else

btn.BackColor = Color.Red

End If



Else

'assume the form has been clicked, change text of all buttons

For y = 0 To 4

For x = 0 To 4

txt = nameCnt & "Btn_" & cnt

m_btnArray(x, y).Text = txt

cnt += 1

Next

Next

nameCnt += 1

End If



End Sub

End Class
Gardner
2012-06-26 10:12:13 UTC
Build the window using the VB.NET form designer. Include everything in it that you need. Then the click event on your other form can simply create a new instance of that form and display it.
binz
2016-10-04 02:17:33 UTC
including buttons & text fabric container controls on the fly isn't any huge deal. The trick is, how are you going to make the button do some thing as quickly because it somewhat is further. you will might desire to build an unquestionably code generator for that. Is it workable? particular. Is it basic? So so. you will possibly might desire to comprise the C# compiler on your distribution so as that code might desire to be compiled as created via your application.
Kashyap Patel
2012-06-26 07:41:53 UTC
your new form should be public declared. and if you want to access controls on the available that from need to define properties or declare that control protected to public.
Aashish
2012-06-26 07:38:14 UTC
object reference not set to an instance of an object


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...