Accessing controls on another form.

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Hi

I posted this to the .Net general newsgroup and got a reply from "Morten"
who has given me an answer but in C# a language I have never worked with.
VB.Net is still very new to me, I was hoping someone here in the VB.net
group could help me a little. I have copied the posts to here.

Morten has given me a code snippet in C# but where do I use it and what is
the VB.Net equivalent.

Thanks DaveG

''''''''' Orig Post ''''''''''''''

>>> Hi
>>>
>>> Using VB.net 2003
>>>
>>> I know how to access controls on a 2nd form after declaring the form
>>> in the first.
>>>
>>> But how do I access the controls on the original form from the 2nd
>>> form.
>>>
>>> (in frmMain)
>>> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
>>> As System.EventArgs) Handles Button1.Click
>>> Dim form2 As New Form2
>>> form2.TextBox1.Text = TextBox1.Text
>>> form2.Show()
>>> End Sub
>>>
>>> (in Form2)
>>> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e
>>> As System.EventArgs) Handles Button1.Click
>>> Dim form1 As New Form1
>>> form1.TextBox1.Text = TextBox1.Text
>>> form1.Update()
>>> Me.Close()
>>>
>>> End Sub
>>>
>>>
>>> Form2 textbox updates ok
>>> The textbox in form1 does not update and still holds the original text
>>> before opening Form2
>>>
>>> This is just a simple example I cannot get working, once I can get
>>> this working the I should not have problems with the actual code I
>>> need to use.
>>>
>>> There must be a simple answer to this....???
>>>
>>> Thanks in advance for any help given
>>>
>>> DaveG

>> Hi Dave,
>>
>> You need to pass a reference to your 1st form when declaring the 2nd,
>> typically
>>
>> Form2 f2 = new Form2(this); // Me in VB
>>
>> Overload the constructor of the 2nd form to accept a 1st form
>> parameter. Store the parameter for later use
>>
>> On Sat, 30 Apr 2005 13:29:05 +0200, DaveG <no.spam.dave@xxxxxxxxxxx>
>> wrote:
>>
> Thanks Morten
>
> Now the problem is I have used the overload in normal funtions and subs
> but never with the constructors, so now I'm a little lost, I understand
> the reasons for the overload..... so the New form2(Me) will be excepted
> bur how to implement it is where I am stuck......


Dave, overloading a constructor is done the exact same way, although I'm
not sure how the VB syntax is. Store the Form1 reference for later use.

private Form1 myParent;

public Form2(Form1 f)
{
myParent = f;
}

then simply call

myParent.MethodOrSimilarInForm1()

whenever you need.

.



Relevant Pages

  • Re: Adding a tab to a TabControl1 from another Form
    ... You can find a sub named "New" in the hidden section of your form. ... Add a variable to hold Form1 reference: ... private ParentForm AS Form1 ... Add your own constructor ...
    (microsoft.public.dotnet.languages.vb)
  • Re: how to reference an object that already exists
    ... (shelling to it). ... >One way to do this would be to overload the constructor ... >Public Sub New ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Referencing forms?
    ... Can you provide an example of passing a reference to Form1 in Form2's ... Public Sub New ... using second form's constructor, special property, calling a method, etc. ...
    (microsoft.public.dotnet.framework.windowsforms)
  • Re: How do I Overload Show() Method?
    ... > Could someone please explain how I overload the Showmethod of a ... > Suppose I select a record from a datagrid on Form1, ... > the CustomerID as an 'argument' to the Show method. ... > Sub GetSelectedRecord() ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to pass information, classes between forms in Windows Application mode [ FINALLY!]
    ... access and what you're actually declaring. ... Obviously - you're referring to it in the constructor. ... Form1, but I guess I'm using the wrong lingo again. ... the program did not compile. ...
    (microsoft.public.dotnet.languages.csharp)