Need to create a textbox on the fly

I need to be able to create a textbox on Form1 from a click event on Form2. Further it has to go on the active tab of a tabcontrol in Form 1. I am using VB.Net 2003. I do not understand how to do this. I tried to do it to keep it simple at first and have it appear just on Form2 (the active form) but it does not work. I am lost. Please help.

Thanks PB

What I have:

Form1 with:
TabControl1 (this is the destination of the textbox)

Form2:

Dim tb As Object

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim tabAttLbl As Label
        tabAttLbl.Size = New Size(48, 16)
        tabAttLbl.Location = New Point(8, 8)
        tabAttLbl.Text = txtName.Text
End Sub

How do I get this to work?

Thanks

PB
PBLackAsked:
Who is Participating?
 
Éric MoreauSenior .Net ConsultantCommented:
PBLack,

This code is working for me:

    Private f As Form2

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        f = New Form2
        f.Show()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Stop
        Dim tabAttLbl As New Label
        tabAttLbl.Size = New Size(48, 16)
        tabAttLbl.Location = New Point(8, 8)
        tabAttLbl.Text = "This is a test"
        f.TabControl1.SelectedTab.Controls.Add(tabAttLbl)
    End Sub
0
 
Éric MoreauSenior .Net ConsultantCommented:
Hi PBLack,

you alos need to set it Visible = True

Cheers!
0
 
Éric MoreauSenior .Net ConsultantCommented:
PBLack,

When you press the button on Form2, do you have a reference to the loaded instance of Form1 ?
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
PBLackAuthor Commented:
Yes clsGlobals.Form1 in a singleton class
0
 
PBLackAuthor Commented:
Hi I tried that and I get the following:

An unhandled exception of type 'System.NullReferenceException' occurred myapp.exe
0
 
PBLackAuthor Commented:
Oh my mistake no I do not have reference to Form1 in the button on Form2. Please explain how to do that. I have this in clsGlobals.vb

Public Class clsGlobals
#Region "Singleton Forms"
    Public Shared Form1 As Form1
    Public Shared Form2 As Form2
    Public Shared Form3 As Form3
    Public Shared Form4 As Form4
    Public Shared Form5 As Form5
    Public Shared Form6 As Form6
    Public Shared Form7 As Form7
#End Region
End Class
0
 
PBLackAuthor Commented:
I apologize. I am still learning VB.Net and make a lot of bonehead mistakes.

PB
0
 
Éric MoreauSenior .Net ConsultantCommented:
PBLack,
> An unhandled exception of type 'System.NullReferenceException' occurred myapp.exe

You are missing a NEW in :
Dim tabAttLbl As New Label
0
 
Éric MoreauSenior .Net ConsultantCommented:
PBLack,
> Public Shared Form1 As Form1

Do you create an instance (NEW) somewhere ?
0
 
PBLackAuthor Commented:
Someday I will learn the proper syntax. Thank you for the help. It works great!

PB
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.