Trouble with forms

CaJack

New member
Joined
Jan 2, 2008
Messages
3
Programming Experience
Beginner
Hi, I’m having trouble with my application. I can’t seem to get forms to communicate together. I’ve started off with a simple test like this:
VB.NET:
Form2.Label1.Text = "hello"
Here’s the code that creates my forms from my main menu class:
(I should explain that my main menu form has a textbox and a button, when the user types something in and clicks the button a new form is created.)
VB.NET:
Public Class MainMenuClass
    Dim mynum As String
    Private frm_MainMenu As MainMenuForm
    Public TestArray As New ArrayList
    Private c_NewForm As NewFormClass

    Public Sub New(ByVal frm As MainMenuForm)
        frm_MainMenu = frm
    End Sub

    Public Sub createNewForm(ByVal Uinput As String)
        c_NewForm = New NewFormClass(Uinput, Me)
        TestArray.Add(c_NewForm)
        c_NewForm.Display()
        
    End Sub

If 2 new forms are created and the user input the names “joe” and “bob” for them, that should be one newform called joe and one called bob. And each has there own newform class. From each of the new forms the user can type into a textbox and click a button to see if there is a form open with that name. I have tried many things and I cant figure out how to pass the name from the textbox to a string to be used like this:
VB.NET:
Dim TestString as String = textbox.1.text
TestString.Label1.Text = "hello"
Any help would be great, thanks.
 
You can give the form a name by setting the Name property. My.Application.OpenForms collection have a string indexer by this name. Example:
VB.NET:
Dim f As New Form
f.Name = "Bob"
f.Show()
My.Application.OpenForms("Bob").Text = "a form named Bob"
 
Thanks for your reply.
I'm having a problem with accessing a label on the created form.

The form is named like this..
VB.NET:
Dim f As New Form
Dim str as string = TextBox1.Text
f.Name = str

as the user chooses the name of the forms created how can I access a control or something within that form. If I want a button on one form to change the text on a label on another form. I cant seem to figure out how to do it..

VB.NET:
Dim alert as string = "Match"
f.label1.text = alert

I'm quite stuck now, and would greatly appreciate any help, thanks.
 
Unless you're able to pinpoint the exact type of your form class you have to use the Controls collection to access the control.
VB.NET:
f.Controls("LabelForInput").Text = "hello"
If you do however know the type you can cast it, FormForMe is here the form class, this class has a Friend field LabelForInput which refer to the label control:
VB.NET:
DirectCast(My.Application.OpenForms("Bob"), FormForMe).LabelForInput.Text = "hello"
 
Back
Top