Article Options
Recently Viewed
Premium Sponsor
Premium Sponsor

 »  Home  »  Upgrading  »  Multiple Forms in VB.NET. Part 2 - Accessing Controls on Other Forms
 »  Home  »  Windows Development  »  Win Forms  »  Multiple Forms in VB.NET. Part 2 - Accessing Controls on Other Forms
Multiple Forms in VB.NET. Part 2 - Accessing Controls on Other Forms
by Ged Mead | Published  07/23/2003 | Upgrading Win Forms | Rating:
Ged Mead

Ged Mead (XTab) is a Microsoft Visual Basic MVP who has been working on computer software and design for more than 25 years. His journey has taken him through many different facets of IT. These include training as a Systems Analyst, working in a mainframe software development environment, creating financial management systems and a short time spent on military laptop systems in the days when it took two strong men to carry a 'mobile' system.

Based in an idyllic lochside location in the West of Scotland, he is currently involved in an ever-widening range of VB.NET, WPF and Silverlight development projects. Now working in a consultancy environment, his passion however still remains helping students and professional developers to take advantage of the ever increasing range of sophisticated tools available to them.

Ged is a regular contributor to forums on vbCity and authors articles for DevCity. He is a moderator on VBCity and the MSDN Tech Forums and spends a lot of time answering technical questions there and in several other VB forum sites. Senior Editor for DevCity.NET, vbCity Developer Community Leader and Admin, and DevCity.NET Newsletter Editor. He has written and continues to tutor a number of free online courses for VB.NET developers.

 

View all articles by Ged Mead...
Multiple Forms in VB.NET. Part 2 - Accessing Controls on Other Forms

Article source code: multipleforms2.zip

One of the key mindset changes you have to make as part of your move from VB6 to VB.NET is the status of Windows Forms. In the early days of VB, Forms were King. Now, VB.NET changes the picture and - although they are still important in many applications - Windows Forms should in many cases be viewed as simply another class and be dealt with accordingly.

If you keep this in mind, many of the early learning curve problems of the move to VB.NET are easier to overcome. We dealt with how to load, show, hide and close multiple forms in Multiple Forms in VB.NET: Part 1. You saw there that the answer to most VB6 to VB.NET misunderstandings in this area was to deal with each form as an object of the Form Class.

In this article, we will take an initial look at some ways of accessing other form's controls and passing data between forms; and our approach to this is going to be pretty much the same as in the earlier article.

This is written for fellow .NET Newbies and so it avoids some of the more advanced topics and skills. It is intended only to give you a hand to get started with the basics of accessing one form and its data from another the .NET way, using an object oriented approach. As always with .NET, there's lots of scope for further study!

Accessing Another Form's Controls

When you are dealing with Windows Forms it's quite likely that sooner or later you are going to want to pass the data held in a control on one form over to another form. For example, the text a user has entered in a text box, a selection from a combobox, a checkbox's Checked property, etc. You may also want to insert data into a control on another form.

Remembering that everything in VB.NET is an object, and that we want to leave our VB.Old ways behind us and think OOP, the trick here is to deal with the control we want to access as an object in its own right.

So let's take one of the examples from the first paragraph above and see how we go about achieving this. I'm going to take the first one - passing the text from a textbox in Form2 back to Form1 - as this is relatively common and straightforward.

Example 1: Reading Another Form's TextBox

In this example we'll assume that it's acceptable for the second form to be opened modally (that is, the user can't do anything with the first form until the second form has been closed.) In many cases, this will be fine.

Here are the steps:

  1. Create Two Windows Forms (Form1 and Form2*)
  2. Form1: Add a Button (named 'ShowButton') and a Label (named 'UserLabel').
  3. Form2: Add a TextBox (named 'UsersName') and a Button (named 'CloseButton').

(* It's not usually considered a good idea to use names that don't reveal any information - such as Form1, TextBox1, etc. However, I've kept to Form1 and Form2 in this example as it's useful to make it clear which form is doing the calling and which is being called.)

And now to start the coding. In Form1, we need an Object Variable to manipulate the second form.

Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim F2 As New Form2()  ' Object variable

In the Click_Event handler for the ShowButton, enter this code:

    F2.ShowDialog(Me)  ' Show Form2

There is some more code needed for that button click event, but we'll come back to it in a moment.

In Form2, we create another Object Variable, but this time it will hold a TextBox control, not a Form.

Public Class Form2
    Inherits System.Windows.Forms.Form

    Public Shared NuNameTB As TextBox

Notice that the scope of this variable is 'Public Shared'. This is important as this scope declaration makes the variable visible to the other form (and in fact the rest of the application).

Still in Form2, we put the following code in the Click event of the button:

Private Sub CloseButton_Click(ByVal sender As System.Object_
    ByVal e As System.EventArgsHandles CloseButton.Click
    ' Assign this form's TextBox to the Object Variable
    NuNameTB = UsersName
    ' Now close the form
    Me.Close()
End Sub

With that very simple code, we have made the properties of our TextBox in Form2 available to the user back in Form1. NuNameTB is now effectively a TextBox Object, whose Text property is the same as Form2's UsersName.Text property.

I said we'd have to come back to the code block for the ShowButton Click in Form1. If you have entered all the above code and have run it, you will be back with Form1 on view, but there's no evidence that what I have just said is true. So, here's the proof:-

At the bottom of the Click event for Form1's ShowButton, add this:

    Me.UserLabel.Text = "Current User : " & F2.NuNameTB.Text

Run this now and, once you've closed down Form2, you will see that whatever you typed into the TextBox on Form2 is available and displayed in the label on Form1.

Note that although this example uses the most obvious property of the TextBox (the Text property), you can in fact access any of that TextBox's properties if you need to. By the way, if you're wondering why we've done it this way with an object variable and not accessed the textbox directly, then the plain answer is 'Think OOP'.

Example 2: Using a Property Procedure

So that was a fairly uncomplicated example which will do the job for you in many circumstances. Let's rack up the OOP factor another notch and this time use a Property Procedure as our means of passing the data around. This fits in with one of the cornerstones of OOP - Abstraction.

In my non-technical way, I like to think of Abstraction as the provision of a general purpose front end that continues to function properly even if we tweak about with the code at the 'back end'. In the example that follows, for instance, we could remove the textbox from Form2 and use some other means of feeding data into its UserName Property. The code in Form1 would be oblivious to the change and still work perfectly well.

I am again going to use a textbox as the control we are focusing on, but don't let this give you the mistaken impression that you are limited to textboxes. They just happen to be easy to use as examples. Set up your project as follows:

  1. Create Two Windows Forms (Form1 and Form2)
  2. Form1: Add a Button (named 'ShowButton') and a Label (named 'UserLabel').
  3. Form2: Add a TextBox (named 'UsersNameTextBox').

As in the previous example, we need an Object Variable In Form1 to allow us to show the second form.

Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim F2 As New Form2()  ' Object variable

And we can add this code to Form1's Load event in order to have both forms on view at the same time:

    F2.Show  ' Show Form2

So far, this example and the previous one are very similar. We start to diverge at this point. Let's create that Property on Form2 which will hold the contents of TextBox1 (or whatever else we later choose to store in the UserName property).

In Form2, we will create a ReadOnly Property called UserName. Why Read Only? Well, it's not strictly necessary, but for the purposes of this simple example it will ensure that data cannot be passed back from Form1 and used to populate the TextBox in Form2.

Here's the code to go into Form2:

Public ReadOnly Property UserName() As String
    Get
        Return UsersNameTextBox.Text
    End Get
End Property

As you can see, all this Property does is hold the value of the text that the user enters into the textbox on Form2.

Now, what we want to happen is that when the user clicks on the button in Form1, the label on Form1 will be updated with the contents of the textbox on Form2. (Actually, to be more accurate, the contents of the UserName Property from Form2, remembering what I said earlier about Abstraction).

So in Form1, in the Click_Event handler for the ShowButton, enter this code:

Private Sub ShowButton_Click(ByVal sender As System.Object_
    ByVal e As System.EventArgsHandles ShowButton.Click
    Me.UserLabel.Text = F2.UserName
End Sub

To see this example working, run the project and make sure that you have positioned the two forms so that you can see both on the screen at the same time. Enter a name into the textbox on Form2. Click on the button on Form1. The label on Form1 will be updated with whatever you have entered into that textbox.

Change the textbox entry, click the button again and the label will change accordingly.

OK, now no-one's pretending that this is anything like a real-world scenario; it's a very artificial project, designed only to demonstrate that you can pass the data from the control on one form to a control on another form. Structurally it has more holes than a piece of Gruyere cheese, but it serves its purpose. Hopefully, you can see more realistic ways in which you can employ this basic technique.

The attached Solution includes the two examples from this article.

In the next article, we will look at rather more dynamic ways of getting hold of this cross-forms data. By this I mean the kind of scenarios where Form1 can be automatically updated when data in Form2 changes - no more of this 'pressing a button to make something happen' nonsense. We'll be tying our Form Properties in with Events and Event Handlers for a much slicker, user friendly and professional looking application. But in the meantime the two examples in this article have at least got the basics under our belts.

Related devCity.NET articles:

How would you rate the quality of this article?
1 2 3 4 5
Poor Excellent
Tell us why you rated this way (optional):

Article Rating
The average rating is: No-one else has rated this article yet.

Article rating:4.39423076923076 out of 5
 208 people have rated this page
Article Score139714
Article Series
Comments    Submit Comment

Comment #1  (Posted by Jose Joseph on 07/30/2003)

Good work ...!. It is good for understanding the key concept of the multiple forms. As a beginner, this article is help me work more with the window forms.

I am also interested to know the usage of each dot net controle through your article.

regards

Jose Joseph
 
Comment #2  (Posted by Franko on 08/01/2003)

Very inlightenig but...

Here is my problem

I have Form1 where I fill a dataset (data adapter)

In Form2 I need to use the contents of the dataset opened in Form1 without having to refill it (table very big - takes a lot of time)

Can it be done and how

Thanks

 
Comment #3  (Posted by XTab on 08/01/2003)

Hi Franko, I haven't put this theory to the test, but I know that you can adapt the basic textbox examples for other controls. So, logically, the theory should extend that extra step to an object with no visible GU interface, such as your DataSet example.

I would suggest you try using the "Form Property" method shown in the article to set up a property or properties to link to your DataSet (and therefore, if necessary, on down the levels to it's Tables, Rows and Columns), depending on just what specifics you need to access.

The core concept behind the whole thing is that you actually use code in your second form exactly as if you were on your first form - the only difference being that instead of refering directly to the control (or object?) itself, you refer to the Public Property you have set up - which is what "joins" the two forms. Form1 Control -> Public Property/Properties returning values from Form1's Controls -> Form2 .

If this isn't enough to get you sorted, I think it might be better to post your question to one of our Net Forums, but maybe the above will get you on track.
 
Comment #4  (Posted by Tamer on 08/20/2003)

Good! this is a followup on my last comment for Part1, it address what I exactly needed!
 
Comment #5  (Posted by XTab on 08/20/2003)

Glad you found what you needed! There's also a Part 3 in the pipeline, which takes the skills set in this area a stage further.


 
Comment #6  (Posted by Peet Schultz on 09/02/2003)

Hi There.

I have tried the demo, but have found that when I try to send a value from Form2 back to Form1 the code does not work. Also if you dim Form1 as new Form1 at the very top of Form2, (just below the "Inherits System.Windows.Forms.Form" it will generate an exception. (On my system at least?!) If I put this declaration into say Buton1 Click event on Form2 the code will compile, but the value in the textbox on Form1 does not update. If I put a watch in the code, I can see the value does actually change, but does not get displayed on the From.
 
Comment #7  (Posted by XTab on 09/02/2003)

Hello Peet,

Sorry to hear that you're having problems.

>> have tried the demo, but have found that when I try to send a value from Form2 back to Form1 the code does not work

I've just downloaded the sample solutions from the site myself and run them and they seem to work fine for me. The user name is passed from Form2 back to Form1 in both the Solutions when I ran them a few minutes ago.

Have you actually downloaded the samples and tried them or are your problems arising from Solutions you are writing from scratch, based on the article? If the latter, I suggest you download and run the samples, then once you see how they function, tweak them according to your needs.

>> Also if you dim Form1 as new Form1 at the very top of Form2,....

Don't know why you would want to do this. It isn't shown in the article and isn't necessary to achieve the passing of data from Form2 to Form1.

>> but the value in the textbox on Form1 does not update....

If you decide that you do need a new instance of Form1, then that's what you will get - another instance. So at that point you will have two Form1s . Depending on the circumstances of your project, the compiler may or may not allow this to happen, but if it does then I would guess that you are still sat looking at the first instance of Form1 - but you've just instantiated another one, changed and passed a value to it, but haven't made that second instance visible to the user.

BTW, as you've chosen to name your second instance as "Form1" you've probably made it even more confusing - it's a good idea to use something like Dim F1 as new Form1, rather than the Dim Form1 as New Form1, as you have done.

Anyway, as I say, I think your best course by far is to work your way through the example solutions and get an understanding of those under your belt and then start trying out your variations.

Best of luck

XTab


 
Comment #8  (Posted by Peet Schultz on 09/03/2003)

Hi XTab
Thanks for the quick response.

I have worked through you example and yes it does work the way that it is set up right now. What I am trying to get to work is the following: Let's say Form2 collects my username and password that needs to be validated inside of form1. So on form2 I would have a "submit" button that would pass the values in the Username/Password textboxes from Form2 back to Form1. Now I can see how to retrieve the values using Properties. I can also see that the way that I am trying to do it , is creating a new copy of Form1. What beats me is how to get the value back into a textbox on the original copy of Form1,so that I can then display the currently logged on User.

Hope this makes sense! If not, I can mail the modified project to you.

Regards
Peet Schultz
 
Comment #9  (Posted by XTab on 09/03/2003)

OK, I see the thrust of what you're aiming to do there and it's fairly easily achievable. It would save a bit of time if you will send me your zipped project so far and I'll amend it and mail it back to you. Just send it to xtab@vbcity.com

Cheers
XTab
 
Comment #10  (Posted by an unknown user on 09/11/2003)


 
Comment #11  (Posted by Sybile on 10/30/2003)

Hello,
This article is great for when you know what the form is. However I have a huge application with about 40 maintenance tables. All these have forms with navigation buttons. In VB 6 I would of had an Navigation module in which I enable/disable buttons based on the current position. I tried the same concept in VB.Net but I always get errors. Of course I am completely new to .Net and I could be completely doing this wrong. But what I would like to do is pass the form and dataset information. Enable the buttons and if at first record disable the first and previous buttons. Here is the code I used in a module:

Function MoveInDst(ByVal dst As DataSet, ByVal strTbl As String, ByVal iRec As Integer, ByVal frm As Form)
frm.BindingContext(dst, strTbl).Position = iRec 'this line works fine
' I've tried a few different ways to enable to control but haven't gotten anything to work
frm.Controls("cmdFirst").Enabled = True 'doesn't work
frm.cmdPrevious.Enabled = True 'doesn't work

Maybe I am stuck in VB6 world I don't know but I have spent too much time trying to figure this out. Please help.
Any help or suggestions would be greatly appreciated.
Thank you,
Sybile
 
Comment #12  (Posted by XTab on 10/31/2003)

Hello,
I've copied your question to the VB Net Forum and I see there are now some suggestions made there by other members. You can view them at http://www.vbcity.com/forums/topic.asp?tid=45829. Hope this gets you back on track.
Cheers

XTab
 
Comment #13  (Posted by MikeG on 11/20/2003)

Hi,

You could do the exact same thing as the example.
Create a writeonly property in Form1 as Dataset.
In form2, you could try something like this:

Dim dsForm1 as Dataset = Form1.formDataset.

Now, form2 has a reference of your dataset created in form1.

Hope this help.


 
Comment #14  (Posted by MikeG on 11/20/2003)

> Create a writeonly property in Form1 as Dataset.

This should be a readonly property...

 
Comment #15  (Posted by Jonathan on 11/21/2003)

It is a neat idea to access controls via the property feature. But, how do I access controls from the Parent form(s)?
for example form1 create an instance of form2. how do form2 access form1's controls?

 
Comment #16  (Posted by XTab on 11/27/2003)

Hi Jonathan

The following code seems to work OK . It allows you to access a Form1 textbox from another form.

In Form 1:
Public Class Form1
Inherits System.Windows.Forms.Form
Public Shared F1TB1 As TextBox

# Windows designer code

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
F1TB1 = Me.TextBox1
End Sub

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

End Class
------------------------

In another form:

Me.Label1.Text = Form1.F1TB1.Text
--------------------

Hope this helps




 
Comment #17  (Posted by Mike on 12/01/2003)

I've been looking all over the internet for some article that could explain this too me.
I'm a bit angry that Microsoft has made this so damn hard. I mean in VB6 you could write form1.label1.text = form2.label1.text and that was it.
But thanks too you I got my project working.. Well atleast the part with communicating betwean the forms =)

Thanks alot!
Regards, Mike, Sweden
 
Comment #18  (Posted by maroo on 12/16/2003)

HI what if I want the second form on the first form ( of an on click event) in the same width and properties as the first fom.
In other words the second form has to be open in the form1 rather than as pop window??

Thanks.
 
Comment #19  (Posted by XTab on 12/17/2003)

Do you mean you want it to appear :
in the same location, but on top of Form1 (and still movable)
or
as an MDI Form (such as the 'forms' you get in MS Word
or
as a 'Form within a Form' (where it's a separate form, but if it's moved via the caption bar, any of it that moves outside the confines of the underlying form is not visible ?



 
Comment #20  (Posted by Shane Cronin on 12/18/2003)

Great Article, It was great to finally find an article that had simple code examples, rather than loads of waffle about .NET.

I am having one problem though, that really doesnt have anything to do with article, but i hope someone can help please. I am using the line of code below to convert all input into a textbox to Uppercase, however this is preventing the user from using the backspace key in runtime, how do i enable the backspace key to be used.

Me.txtUserName.SelectedText = e.KeyChar.ToString.ToUpper

Thanks

 
Comment #21  (Posted by XT on 12/22/2003)

You can test for the BackSpace (or any other key, come to that) with code along these lines:
If Not e.KeyChar = Chr(&H8) Then
' In your case, your conversion to Upper Case code
End If

The above code will not try and convert a Backspace, but will still have effect of deleting the last letter typed in.
If you want something different from this, contact me by email - xtab@vbcity.com - and I'll try and help.


 
Comment #22  (Posted by Ed on 10/02/2004)

Very good for the beginner. I have been looking for this for some time. Thanks so much.
 
Comment #23  (Posted by an unknown user on 01/16/2005)
Rating
once again, brilliant. makes it very easy to understand the basic principles of forms
 
Comment #24  (Posted by an unknown user on 01/28/2005)
Rating
Well written article and the techniques can be appreciated. However what concerns me is that you are declaring a public variable in a class and you are referencing a private variable using a property. This type of referencing breaks encapsulation. Encapsulation as you know is information protection or hiding. The idea is that you do not ask an object for its information so that the caller can perform the task, but rather the object which holds the data performs the task. With that in mind, how do you do you achieve information sharing between two forms who collaborate without violating encapsulation?
 
Comment #25  (Posted by an unknown user on 01/29/2005)
Rating
Great Job, your article help me.

Alfredo Mtz.
 
Comment #26  (Posted by adam on 01/31/2005)
Rating
what about CF?? do the examples above work in all flavors of vb.net ???

 
Comment #27  (Posted by an unknown user on 02/08/2005)
Rating
hi erm...i need to know if say i have 2 forms open...and i want to click a button on form 2...and start a timer on form 1...how do i go about doing that? if u could...send your reply to me at hello_lijun87@hotmail.com
 
Comment #28  (Posted by Edward on 02/11/2005)
Rating
I found a soluction to all your problems in regards to passing information from one form to another
This is how it's done

'Create 2 forms
'in form1 create 2 text boxes and one button

Public Class Form1
Inherits System.Windows.Forms.Form
Dim frm As New Form2

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddHandler frm.Button1.Click, AddressOf FillTextBoxes
End Sub

Private Sub FillTextBox(ByVal sender As System.Object, ByVal e As System.EventArgs)
TextBox1.Text = frm.TextBox1.Text
TextBox2.Text = frm.TextBox2.Text
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
frm.Show()
End Sub

'In Form2 Create 2 text boxes and one button and that's all!

Just type in something in to those text boxes and click on the button and all the information will be passed to the Form1
This couldn't be simpler.
 
Comment #29  (Posted by Lindy on 03/19/2005)
Rating
Hey this is awesome, i spent ages in the msdn library finding info about this, this is exactly what I'm looking for. Easy to understand with human terminology!
 
Comment #30  (Posted by an unknown user on 03/28/2005)
Rating
Simple explanation that used a simple example to demonstrate the core lesson without introducing other corrollary issues.
 
Comment #31  (Posted by an unknown user on 04/20/2005)
Rating
Using the same way, when i tried to update my statusbar panel in mdi form class, the text not appearing . pls answer me.
my email id is moncys@gmail.com
 
Comment #32  (Posted by an unknown user on 04/21/2005)
Rating
THANKS SO MUCH IT S GREAT
 
Comment #33  (Posted by Andy Mason on 04/23/2005)
Rating
This is really good for understanding. I do have one question though. When I drag a Textbox onto a form, the auto-generated code behind it (which it recommends not changing) declares them as "Friends WithEvents", to the form. I did however change them to Public Shares. Although this works... is this really the "Right" way to fix it. Or should there be a method (on the form class) that actually interacts with the textboxes, etc? Rather than giving anything and everything full control. Not that it matters for small things, but organizationally I think thats the proper way to do it.
 
Comment #34  (Posted by Betsy on 04/23/2005)
Rating
This is the closest I've come to what I'm looking for, but as a very-much-so newbie, it's still a little over my head. I have a calculation that is displayed in a label on my main form, but I haven't been able to figure out how to display the result in a label on the second form. It seems simple enough, and this article came very close to explaining it, but perhaps I'm just not understanding something quite simple as my label (on the second form) is still coming up empty.
 
Comment #35  (Posted by XTab on 04/23/2005)
Rating
Hi, to get a Reference to your main form (I take this to mean the project's startup form(?) needs a slightly different approach. So, no, you didn't miss a trick - it's just a bit more difficult :-}
Have a look at the Part 4 article, which deals specifically with this: http://www.devcity.net/Articles/100/1/multipleforms4.aspx
 
Comment #36  (Posted by an unknown user on 05/04/2005)
Rating
Just the right amount of information offered in an easily digested format that does not talk down to the reader. The concepts are immediately applicable and the articles in the series all support each other well. Jdang
 
Comment #37  (Posted by an unknown user on 05/05/2005)
Rating
This article is framed very well,Good work and best wishes
 
Comment #38  (Posted by fei_hyblade on 05/11/2005)
Rating
awesome article !. i got some problem,i created 2 forms,on form1,i add 2 text box,in form 2 i add a button and a textbox,i use the code :
dim host as string
host=text1.text
f1.text1.text = host
and got the text1 textbox in form 1 value changed into whatever i type inside text1 box in form2,but when i try to pass the variable into "hostname" variable in form 1, using this code in form 1:
dim hostname as string
hostname = text1.text
text2.text = hostname
it works,but there's nothing in textbox2 :(.what's the problem ? thx.
 
Comment #39  (Posted by an unknown user on 06/10/2005)
Rating
Top post- exactly what I was after. It's gonna be easy getting into DotNet with articles like this one!

- Thief_
 
Comment #40  (Posted by an unknown user on 06/16/2005)
Rating
because I was scarching for this.
when I applied in my system I became succesful.But when I tried to access the object of form one to form2 It shows me a sweet message that unable to get source.
how I will do this .Please mail me to againview@yahoo.com
 
Comment #41  (Posted by an unknown user on 06/25/2005)
Rating
It's easy to understand and very well explained
 
Comment #42  (Posted by an unknown user on 07/12/2005)
Rating
I couldn't be more thankful...
 
Comment #43  (Posted by an unknown user on 07/13/2005)
Rating
This was an excellent and easy to understand article. I was able to implement it quickly without having to search for additional information
 
Comment #44  (Posted by an unknown user on 07/13/2005)
Rating
I don't know why but on my system
Example #2 does not allow me to access
the second form. It has it displayed
but it won't let me select it to add
any data to the text box.

 
Comment #45  (Posted by an unknown user on 07/22/2005)
Rating
works
 
Comment #46  (Posted by an unknown user on 07/26/2005)
Rating
Thanx Ged.You really know your stuff
 
Comment #47  (Posted by megha on 07/28/2005)
Rating
a form having two textboxes and a button.when i enter the name in first textbox(that has to be input from the keyboard) and click the button ,it has to check the local database and get the value needed on the other textbox. how do i achieve this.please help
 
Comment #48  (Posted by David on 08/11/2005)
Rating
wow..This was great Thanks..
I've been looking for this few days !
 
Comment #49  (Posted by an unknown user on 08/16/2005)
Rating
Thanks Ged, I put these concepts to work right away. Chris
 
Comment #50  (Posted by an unknown user on 09/07/2005)
Rating
it was extrmely helpful, specially when the very expensive books can't provide an answer!!!
Thanks a lot
 
Comment #51  (Posted by an unknown user on 09/26/2005)
Rating
Hi Ged,
Could you please explain me how to pass the content of the textbox back to the parent form with the child form still opened?

alainneil@hotmail.com
 
Comment #52  (Posted by an unknown user on 09/29/2005)
Rating
This article is simple but effective
 
Comment #53  (Posted by an unknown user on 10/02/2005)
Rating
Very clear and concise. Articulation at its finest.
 
Comment #54  (Posted by Barnaby Arnott on 10/02/2005)
Rating
Of course there are numerous solutions. And gave full marks in my Rating. However...
For all his diligence, the author skipped over the total solution to one of the easiest methods presented above:
If you are using the modal .ShowDialog method to display your second form, there is a built-in overload to the constructor that will send a pointer to the second form with an instance of the original form. You can then back-reference to variables in the first form using the .Owner property
Those are the technical terms. I'm actually refering to the one line of part two of this article:
" F2.ShowDialog(Me) ' Show Form2"

This example should explain what the previous paragraph attempted to:
#Example:
'Form1 has a single button to modally open the second form
'Form2 has two textboxes, one to show the name of itself, the other to show the name of the owner

'Original form who's variables you need to reference
Public Class Form1
Inherits System.Windows.Forms.Form

'#Region " Windows Form Designer generated code "
'This is unmodified from the designer provides

Private Sub OpenButton1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles OpenButton1.Click
Dim form2 As New Form2
form2.ShowDialog(Me)
End Sub
End Class

'*****************************

Public Class Form2
Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
'Again, unmodified

Private Sub Form2_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
tbSelf.Text = Me.Name
tbOwner.Text = Me.Owner.Name
End Sub
End Class

#EndExample

There are also options if you are not using a modally displayed form via the ShowDialog method.
If you open form2 (of the above example) via Show(), you have two easy methods available other than that explained in the article (overloading the New constructor for Form2). Both options involved the Owner property concept of Windows Forms:
1. Add the instantiated form2 to the OwnedForms array of form1 (actually unnamed* instantiation of Form1) via the AddOwnedForm method:
Private Sub OpenButton1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles OpenButton1.Click
Dim form2 As New Form2
Me.AddOwnedForm(modalForm)
form2.Show()
End Sub

2. Add the original form as the Owner of form2 before calling its Show method. (There can be only one Owner for a form)
Private Sub OpenButton1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles OpenButton1.Click
Dim form2 As New Form2
modalForm.Owner = Me
form2.Show()
End Sub

*The fact that you create a project with a Windows Form and that is the starting point of code execution is the source of confusion on this issue, coupled with the sometimes confusion difference between an instantiated Object versus its abstract Class definition. I am very conscious of case: when it comes to the above, "form2" is the instantiated Object of Class "Form2"; while the class Form1 has no instantiated variable name (although this is what I just called "form1" :) - the closest you can get to referencing the instantiated object of Form1 is to use the keyword Me when in the scope of Form1 code (unless you follow Ged's example and instantiate all forms in a Module).

One last note: I struggled with this concept partly because I lead myself down the wrong line of thinking by trying to use the MDI Child and Parent relationship. I of course learned that this is invalid, unless you use an MDI form... which takes a few other changes to invoke properly and is still not what most people actually want to accomplish (speaking generically)

Hope that helps straighten things out a bit. As always, create a test project to prove these concepts to yourself, and be sure to look at the MSDN Class definitions and the associated Members.

Now to get back to what I was doing...
Barnaby Arnott
www.computingimprovements.com
 
Comment #55  (Posted by Barnaby Arnott on 10/02/2005)
Rating
Arrg, formatting lost - trust me, it's valuable if you can decipher it! Maybe the editors can help me...
Barnaby
 
Comment #56  (Posted by an unknown user on 10/05/2005)
Rating
I know VB and i thought yeah, .net will be the sort of the same.... NOO... no form1.show, etc getting data from another forms objects etc... So thanks and well done :)
 
Comment #57  (Posted by an unknown user on 10/08/2005)
Rating
I was looking for a way to declare a public variable and could not find in my textbook...
 
Comment #58  (Posted by an unknown user on 10/19/2005)
Rating
I am doing this in quite a bit of my vb6 code and am getting ready to make the dive into vb2005.net. Nice to know you are here.. Great instruction.
 
Comment #59  (Posted by an unknown user on 10/28/2005)
Rating
Excellent and simple article. everything is explained very clearly.
thanks for the help
 
Comment #60  (Posted by an unknown user on 11/20/2005)
Rating
I learnt this feature on classes. It's gret and very helpful
 
Comment #61  (Posted by an unknown user on 11/22/2005)
Rating
finally...i have looked everywhere for this info...ths is a simple explination that allows for understanding and a stepping stone for future .net dev. thanks....
 
Comment #62  (Posted by jet on 12/20/2005)
Rating
this posting is absolute on the money and you did a good job explained it, but I have a question about sharing the controls (textbox) between the forms though.

Can you tell me why this happen?

i have a form1 opens up then in another class i used another instance of form1 and called it as secondform. Here is my problem, when i try to pass data to the shared textbox from other classes, not from the Me itself, it always go to the secondform instead of main one, why? Is there anyway i can control which form should receive the data at any given time from any class?

Any help would be greatly appreciated.



 
Comment #63  (Posted by an unknown user on 01/03/2006)
Rating
Hello Sir,
But if i have big application and i don't know that which child form active then i get name but not control on that form . So, i got problem in toolbar for cut.copy , paste from control.
plese give me reply soon ,
vishalbarot_mscit@yahoo.co.in
 
Comment #64  (Posted by an unknown user on 01/08/2006)
Rating
it is importance for me, because i am new vb.net user
 
Comment #65  (Posted by an unknown user on 01/16/2006)
Rating
Thank you very much. That's what all I need. This is a helpful topic.
 
Comment #66  (Posted by jasmine on 01/18/2006)
Rating
thanks XTab.it helped me a lot.im a new vb.net user.but ur article made me very familiar in vb.net.
 
Comment #67  (Posted by an unknown user on 02/15/2006)
Rating
just perfect, as a beginner i liked this article, and also i would like to know if the author have any book to publish, cozz i will the 1st to buy it.
 
Comment #68  (Posted by an unknown user on 02/16/2006)
Rating
The examples are well commented
 
Comment #69  (Posted by an unknown user on 03/12/2006)
Rating
Its very easy to learn. and source code also help to understand easily
 
Comment #70  (Posted by an unknown user on 03/15/2006)
Rating
This was woderfull form, may be because I starting with VB dot net, and I am very familiar with VB 6.0, (by the way I like VB 6.0 much more)
My name is Armando Barrera from Mexico, my email is: abarrera@hasbro.com
 
Comment #71  (Posted by yaya on 03/25/2006)
Rating
Thanks for your details and explanations. It's very helpfull and straight forward.
 
Comment #72  (Posted by an unknown user on 03/27/2006)
Rating
FINE
 
Comment #73  (Posted by an unknown user on 04/11/2006)
Rating
Exactly what the doctor ordered
 
Comment #74  (Posted by an unknown user on 04/18/2006)
Rating
Is there any way to do this for a ListBox? I'm trying to do it for a programming project and I need the whole collection from a ListBox on Form2 to be added over to a ListBox on Form1. I tried just to substitue and it didn;t work, but still this really helps along with the rest of the site ^_^
 
Comment #75  (Posted by angayarkannan on 04/22/2006)
Rating
Sir,it working fine...How can enter multiple inputs at a time?Is datagrid is allowed to enter multiple inputs at a time? please give a solution sir...i am confused too much.. my email id angayar@rediffmail.com
 
Comment #76  (Posted by an unknown user on 04/26/2006)
Rating
Thank you very much...you article helps me a lot

regards
Alonso Ramos abecedario@gmail.com
 
Comment #77  (Posted by an unknown user on 04/26/2006)
Rating
Thank you very much...you article helps me a lot

regards
Alonso Ramos abecedario@gmail.com
 
Comment #78  (Posted by an unknown user on 05/14/2006)
Rating
really good and way of explaining is really good and easily understandable even to the beginners
 
Comment #79  (Posted by an unknown user on 05/25/2006)
Rating
I hae scoured books in the bookstore nd read some Beginning Visual Basic Books, none of which explained this as well as you! Thanks for the breakthrough!

 
Comment #80  (Posted by George on 07/25/2006)
Rating
What about having a button on form1 and when pressed, send data from datagrid to another form to be used as input (Keys) to a maint function.
 
Comment #81  (Posted by an unknown user on 07/29/2006)
Rating
Wonderful Sir ! this truly is an article for Newbies to give a better understanding of Forms. Thank you.
 
Comment #82  (Posted by an unknown user on 08/03/2006)
Rating
Please give many examples in more than one way

 
Comment #83  (Posted by an unknown user on 08/21/2006)
Rating
thats an excellent jump start for a beginner like me, I enjoyed the preciseness of the tutorial
 
Comment #84  (Posted by an unknown user on 09/08/2006)
Rating
!@$!@$!$ FINALLY!!! THX
 
Comment #85  (Posted by an unknown user on 09/25/2006)
Rating
I love this code. It saved my day.
 
Comment #86  (Posted by an unknown user on 10/16/2006)
Rating
Very good article. One question about this line:

At the bottom of the Click event for Form1's ShowButton, add this:

Me.UserLabel.Text = "Current User : " & Form2.NuNameTB.Text


-- Shouldn't it be F2.NuNameTB.Text, as F2 is the 'instance' of the Form2 class, whereas Form2 is the class name.
 
Comment #87  (Posted by an unknown user on 10/23/2006)
Rating
Good basic information which is handy when starting out - well done - not easy to find
 
Comment #88  (Posted by an unknown user on 11/06/2006)
Rating
concise and clear
 
Comment #89  (Posted by an unknown user on 11/06/2006)
Rating
Excellent article. Simple, to the point, elegant. Comment #16 was additionally great. Both the article and the comment resolved my problem.
 
Comment #90  (Posted by an unknown user on 11/06/2006)
Rating
Excellent article. Simple, to the point, elegant. Comment #16 was additionally great. Both the article and the comment resolved my problem.
 
Comment #91  (Posted by an unknown user on 11/10/2006)
Rating
basics persons can easily understood


 
Comment #92  (Posted by an unknown user on 12/05/2006)
Rating
This is very helpful for me.Because this is needed one for me at this time.
 
Comment #93  (Posted by an unknown user on 12/09/2006)
Rating
Thanks for this Explanation it was a great help.
 
Comment #94  (Posted by an unknown user on 01/20/2007)
Rating
I understood it clearly, and it helped alot. Keep it up.
 
Comment #95  (Posted by an unknown user on 01/22/2007)
Rating
thank you thank you very useful :)
 
Comment #96  (Posted by an unknown user on 04/02/2007)
Rating
Fine article
 
Comment #97  (Posted by an unknown user on 04/17/2007)
Rating
Very useful. Helped explain the general process very well.
 
Comment #98  (Posted by an unknown user on 04/20/2007)
Rating
Pretty but would have liked to see examples of more complex objects -
combo boxes, check boxes etc.
 
Comment #99  (Posted by an unknown user on 06/09/2007)
Rating
Good work
it is useful for my project

regards
palanivel.S
 
Comment #100  (Posted by an unknown user on 07/16/2007)
Rating
This was exactly what I needed. I simple and quick method of accessing the properties of one control on a different and using them on another. Perfect!
 
Comment #101  (Posted by an unknown user on 09/02/2007)
Rating
Was searching for this info for quite a while...
 
Comment #102  (Posted by an unknown user on 09/07/2007)
Rating
The author keeps a wavering newby by the hand and leads him (me) to the solution.
Thank you!
 
Comment #103  (Posted by an unknown user on 09/28/2007)
Rating
nice work i was really confused over this
 
Comment #104  (Posted by an unknown user on 10/26/2007)
Rating
Excellent
 
Comment #105  (Posted by GQ on 11/07/2007)
Rating
about comment# 28,, i tried the code, and it worked. but when i try to close form2 (form2.close in button clicked event) at the same time its button was clicked, it passes blank text. i assume that VB executes first the event in form2 after the AddHandler event in form1. is there anyway to resolve this?? i need to close the form2 after form1 gets its values
 
Comment #106  (Posted by an unknown user on 12/10/2007)
Rating
thanks, this article helps me lot.
 
Comment #107  (Posted by Preet on 01/07/2008)
Rating
Hello sir
I have used the same example to find out will it works for me.Actually its not worked for me.The problem is that label "username" doesnot receive any value from the form2.

Please tell me why its not catching the dynamic value.

thanks a lot
Preet
 
Comment #108  (Posted by Preet on 01/07/2008)
Rating
Hello

this paper's coding works for me,actually i was writing form textbox name rather then writing public shared variable name.

Thanks
 
Comment #109  (Posted by an unknown user on 01/07/2008)
Rating
Hello

Well this article helps me,but i want update datagridvalues through form2 i.e. I wanna add new row value in datagrid of form1 through form2.
Can u plz help me.

Thanks in advance
newfriend

 
Comment #110  (Posted by an unknown user on 01/29/2008)
Rating
Hello Ged..this is a very nice tutorial on handling multiple forms..where as my problem is I have a dll which has some forms and I able to open that forms through win application and vbaccess application.the problem is I am not able to make it single instance of a form. each time I click on the button and it generates new instances of the forms present in the dll.
 
Comment #111  (Posted by an unknown user on 01/30/2008)
Rating
sir i want to simple coding just copy one form textbox content into another form textbox by using both type values i.e numerical and string values.
 
Comment #112  (Posted by an unknown user on 02/03/2008)
Rating
Yes! Best things in life come for free. I would pay to get this kind of solutions to my .NET problems. Thanks a lot XTab !
 
Comment #113  (Posted by an unknown user on 02/18/2008)
Rating
Well done mate; I have spent the whole morning trying to obtain a value from another form. I like your style, the way you've kept it simple by breaking it down to different scenarios.
 
Comment #114  (Posted by an unknown user on 02/22/2008)
Rating
Well done mate; I have spent the whole morning trying to obtain a value from another form. I like your style, the way you've kept it simple by breaking it down to different scenarios.
 
Comment #115  (Posted by an unknown user on 04/10/2008)
Rating
This is Easy to understand
 
Comment #116  (Posted by an unknown user on 04/23/2008)
Rating
great!!!! you really helped me alot!!!!
 
Comment #117  (Posted by an unknown user on 05/25/2008)
Rating
its helpful:)
 
Comment #118  (Posted by Ebenezer Aniapam on 06/05/2008)
Rating
I am trying to do same thing but rather with DataSet and Combobox and it is working.I want when user click Form2 it should update Form1
 
Comment #119  (Posted by an unknown user on 10/08/2008)
Rating
b'coz i expect the code in different way in that way it is more helpfull for me and anybody can use this code very easily and it is more understandable so i rated like this
 
Comment #120  (Posted by an unknown user on 11/17/2008)
Rating
100% Privacy, 100% Guarantee!!!
 
Comment #121  (Posted by an unknown user on 11/27/2008)
Rating
Great article. It's amazing how long this this has been popular.
 
Comment #122  (Posted by an unknown user on 12/01/2008)
Rating
carolnoo
 
Comment #123  (Posted by an unknown user on 12/11/2008)
Rating
I am very much satisfied and solved my problems.
 
Comment #124  (Posted by an unknown user on 04/04/2009)
Rating
Excuse me. My home is not a place, it is people.
I am from Azerbaijan and learning to speak English, give true I wrote the following sentence: "Around the world airfare, complex international airline tickets, itineraries for global trips."

Best regards :-(, Luce.
 
Comment #125  (Posted by an unknown user on 05/07/2009)
Rating
The message you are trying to give is clear. But how can you use a shared variable in form2 with instance of the class(form2).
 
Comment #126  (Posted by an unknown user on 05/24/2009)
Rating
The author has done an excellant job of describing how to handle multiple forms
 
Comment #127  (Posted by an unknown user on 05/30/2009)
Rating
Explains it in very simple terms. Other sources were confusing. Thanks.
 
Comment #128  (Posted by an unknown user on 07/04/2009)
Rating
very helpful
 
Comment #129  (Posted by an unknown user on 09/04/2009)
Rating
Simiple example
a) one by object
b) by property
 
Comment #130  (Posted by an unknown user on 10/02/2009)
Rating
Hi all. Everybody likes to go their own way--to choose their own time and manner of devotion.
I am from Lesotho and too poorly know English, give true I wrote the following sentence: "What we display to have is a incorrect abuse network, a stolen cost, one where there's dramatically forgotten engines of who provides to new york."

Thank :-D Rusty.
 
Comment #131  (Posted by Nishant Shrivastava on 10/23/2009)
Rating
Hi the article is good.I have one problem that there is a button in form1 and when you click on that button, form2 should get loaded and form1 should get unloaded.I am also passing few variables from form1 to form2.Variable passing is working fine but when i do me.close in the button_click event and dim frm as new form2 frm.show then the whole project gets unloaded.The code in the button_click event is as follows:
dim frm as new form2
frm.show()
me.close()
----------
But this is not working.I tried different ways like using me.dispose() also but its not working. Either the project gets unloaded or my form1 does not get unloaded and can be seen in the taskbar.What is the way to do it
 
Comment #132  (Posted by an unknown user on 11/10/2009)
Rating
Very interesting...that was good
 
Comment #133  (Posted by an unknown user on 12/03/2009)
Rating
I was having trouble getting the concept and this article was great! It really helped me with my final VB project! thank :)
 
Sponsored Links