-
Example Code for: Re: How can I control a label on one form from another?
Patrick,
Here is the code for referencing a form called from outside the form.
-Al
'-------------------------------------------------------------------
'This is a vb class that controls both forms
'(this would be your controller code)
Public Class Class1
Public Shared Sub Main()
Dim frmForm2 As Form2 = New Form2()
Dim frmForm1 As Form1 = New Form1()
frmForm1.Show()
frmForm2 = New Form2()
frmForm2.Process(frmForm1)
Application.Run(frmForm2)
End Sub
End Class
'------------------------------------------------------------------
'Form1 has no added code, but should have a label.
Public Class Form1
Inherits System.Windows.Forms.Form
#Region
'VS created code removed for readability.
#End Region
End Class
'-------------------------------------------------------------------
'Form2 should have a button1 and following code.
Public Class Form2
Inherits System.Windows.Forms.Form
Private mForm As Form
#Region
'VS created code removed for readability.
#End Region
Public Sub Process(ByRef frm As Form)
'Get a reference to the existing form passed in from Class1.
mForm = frm
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click
Dim objControl As Control
'Iterate the controls.
For Each objControl In mForm.Controls
'Find the contol by name.
If objControl.Name = "Label1" Then
objControl.Text = "New Text"
End If
Next
End Sub
End Class
"Al Guten" <agut3@yahoo.com> wrote:
>
>Hi Patrick,
>
>No the code you have in Form1 button click should go in the code (like Sub
>Main) that creates both forms. I assumed that Form1 and Form2 are both started
>independently from some other code.
>
>If one form creates the other, you don't need any of this and you can use
>your created reference.
>
>Let me know if you are still having problems and I will email you exact
code.
>What I typed in was just snippets.
>
>Al
>
>"Patrick Hill" <phill@stroock.com> wrote:
>>
>>Al,
>>Thanks for your response. Now I put this code into form1's button click:
>> Dim x as Form2 = New Form2
>> x.Setup(Form1)
>> x.Show()
>>and I have this in form2:
>>Private mForm As Form
>>
>>Private Sub Button1_Click
>> 'You can hardcode the control if it never changes, but iterating
>> 'And searching for the control would be best.
>> mForm.Controls.Item(0).Text = "Hello"
>>End Sub
>>
>>Public Sub Setup(ByRef frm As Form)
>> 'frm will be the referenced Form1
>> mForm = frm
>>End Sub
>>But when I try to run it I get an error on this line
>>x.Setup(Form1) the error says:
>>C:\WINNT\Profiles\phill\Personal\Visual Studio Projects\Test3\Form1.vb(68):
>>'Form1' is a type and cannot be used as an expression.
>>
>>Any ideas?
>>Thanks
>>Patrick
>>
>>"Al Guten" <agut3@yahoo.com> wrote:
>>>
>>>Oops - First post went too quick.
>>>
>>>Here is an unglamorous method of doing what you need. I assume both forms
>>>are being called from a single application. You can pass a reference of
>>form1
>>>to form2. Code would look something like:
>>>
>>>In the Main Application
>>>'Instantiate Form2 and pass reference to form1
>>>Dim x as Form2 = New Form2
>>>x.Setup(Form1)
>>>x.Show()
>>>
>>>In Form2:
>>>
>>>Private mForm As Form
>>>
>>>Private Sub Button1_Click
>>> 'You can hardcode the control if it never changes, but iterating
>>> 'And searching for the control would be best.
>>> mForm.Controls.Item(0).Text = "Hello"
>>>End Sub
>>>
>>>Public Sub Setup(ByRef frm As Form)
>>> 'frm will be the referenced Form1
>>> mForm = frm
>>>End Sub
>>>
>>>
>>>
>>>"Patrick Hill" <phill@stroock.com> wrote:
>>>>
>>>>Ok Now I know that forms are no longer gobal but how do I access say
a
>>label
>>>>on one form from another form? I'm trying something that USE to be very
>>>simple
>>>>by saying form1.label1.caption
>>>>Now here is the code I have in form1 form 1 has a label and a command
>button.
>>>>The code in the button is as follows:
>>>>
>>>>Dim frm2 as new form2
>>>>frm2.show()
>>>>
>>>>Now in form2 I have a coomand button that all I want it to do is change
>>>the
>>>>text property of the label on form1. Can someone anyone just show me
>how
>>>>to do this. Its really simple and its blowing my mind I have tried this
>>>>dim frm1 as NEW form1
>>>>frm1.label1.text="Hello"
>>>>frm1.show
>>>>now this creates a NEW instance of the form but I want to use the form
>>thats
>>>>already loaded I don't want a new instance of the form..I then tried
>>>>Dim frm1 as form1
>>>>frm1.label1.text="Hello"
>>>>and that didn't work.
>>>>Any help would be great! thanks!
>>>>
>>>
>>
>
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks