Programically added new form with button - Click even't doesn't see textbox

simpleonline

Active member
Joined
Sep 13, 2011
Messages
33
Programming Experience
Beginner
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.
__________________
 
Well my first question is why do it that way when it would be far simpler just to create the window at design and use a display sub? But if it's absolutely necessary can you not declare the components globally rather than at the sub level where they are instanced?
 
Sorry here is the code and a little more explanantion to what I am talking about.

I created a new form programically and then put the text box and button on that new form.

Here is the code. Thanks

VB.NET:
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim NewWindow As New System.Windows.Forms.Form
        Dim TextBox1 As New System.Windows.Forms.TextBox
        Dim Submit1 As New System.Windows.Forms.Button
 
        NewWindow.Size = New System.Drawing.Size(416, 289)
        NewWindow.Text = "Folked"
 
        'TextBox1
        '
        TextBox1.Location = New System.Drawing.Point(27, 141)
        TextBox1.Name = "TextBox1"
        TextBox1.Size = New System.Drawing.Size(342, 20)
        TextBox1.TabIndex = 1
 
        'Button1
        '
        Submit1.Location = New System.Drawing.Point(139, 167)
        Submit1.Name = "Button1"
        Submit1.Size = New System.Drawing.Size(80, 23)
        Submit1.TabIndex = 2
        Submit1.Text = "Submit"
        Submit1.UseVisualStyleBackColor = True
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(400, 253)
        NewWindow.Controls.Add(Submit1)
        NewWindow.Controls.Add(TextBox1)
 
        NewWindow.Name = "Form1"
        NewWindow.Text = "Form1"
        NewWindow.ResumeLayout(False)
        NewWindow.PerformLayout()
 
        AddHandler Submit1.Click, AddressOf Button_click
        NewWindow.Show()
 
    End Sub
 
    Private Sub Button_click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        MessageBox.Show("Hello World")
 
 
 
 
    End Sub
 
 
End Class

This part works great. I can click the button and the my new window appears with the text box and the button on it and when I click the button it shows me the message "Hello World".

Now let's say that I want to display whatever is in the textbox of that new form in textbox1.text.

I get the error that the textbox1 is not defined. I tried including the name

"NewWindow.Textbox1.text"
"Textbox1.text"

But I'm not sure what it doesn't see the textbox.

2envmkp.jpg
 
Yup, declare TextBox1 globally. This means that it is available from any part of the program. Any thing you Dim in a sub is only available in that sub.


Public Class Form1
Dim TextBox1 As New System.Windows.Forms.TextBox


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim NewWindow As New System.Windows.Forms.Form
Dim Submit1 As New System.Windows.Forms.Button
 
The only this is that I am going to be creating 100 new forms each with the textbox and a button. That's the reason why I chose to create it on the fly. If I create it globally would that interfere with the disposal of the window?

What I'm doing is creating the textbox and the button and then the user will type what ever is in the label (haven't gotten to the label yet) and the value of the textbox will be put someone else and then the windows form will be disposed and the next window will appear, etc.
 
You need to create a class for your form, and declare it's textbox that you need to read from as Public or Friend. Then you can instanciate as many as you want:

Dim arrMyForm() As MyFormClass

For i = 0 to 99
    arrMyForm(i) = New MyFormClass
    MessageBox.Show(arrMyForm(i).TextBox1.Text)
    ...
Next


and PLEASE stop calling textboxes "TextBox33414513562156". It's stupid and confusing to everyone, including you.
 
The only this is that I am going to be creating 100 new forms each with the textbox and a button. That's the reason why I chose to create it on the fly. If I create it globally would that interfere with the disposal of the window?

What I'm doing is creating the textbox and the button and then the user will type what ever is in the label (haven't gotten to the label yet) and the value of the textbox will be put someone else and then the windows form will be disposed and the next window will appear, etc.
Why not create 1 form in the designer, then at runtime create the 100 instances of it?

Is there actually a purpose to creating the whole thing at runtime?
 
Why not create 1 form in the designer, then at runtime create the 100 instances of it?

You know that's an excellent question. I never thought of it that way. I always figured there would be an easier way to implement this. Thanks guys.
 

Latest posts

Back
Top