Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
VB.NET Programically added new form with button - Click even't doesn't see textbox?
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.
5 Answers
- MarkGLv 79 years ago
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
- GardnerLv 79 years ago
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.
Source(s): VB.NET Programmer - binzLv 45 years ago
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.
- 9 years ago
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.
- How do you think about the answers? You can sign in to vote the answer.