dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: [RESOLVED] Trying to get value from one form to populate in another form

  1. #1
    Join Date
    Sep 2010
    Posts
    26

    [RESOLVED] Trying to get value from one form to populate in another form

    I am trying to get a text value from NameForm and pull it in Welcome as a label value.
    What I have so far, when the Welcome page comes up, it does not give the value of the text but if you click on the label field, it pops up with the NameForm screen. After you enter name again on the NameForm screen, it will show in Welcome.

    Screens should be:

    NameForm - enter name and click submit to go to Welcome.
    Welcome - this has a label field in it that should populate with the name that was entered on the NameForm.

    Code:
       public partial class NameForm : Form
        {
            private string fullname = " ";
    
            public string nameValue
            { get { return fullname; } }
    .......
            public void SubNameBtn_Click(object sender, EventArgs e)
            {            
                this.AcceptButton = SubNameBtn;   
    
                User user = new User();
    
                user.FirstName = fNameTxt.Text;
                user.LastName = lNameTxt.Text;
    
                fullname = fNameTxt.Text;    \\using only fNameTxt till figured out
    .........
    Code:
        public partial class Welcome : Form
        {
            public Welcome()
            {
                InitializeComponent();
                CenterToScreen();
            }
    
            private void WelNameLbl_Click(object sender, EventArgs e)
            {
                NameForm frm = new NameForm();
                frm.ShowDialog();
                WelNameLbl.Text = frm.nameValue;
            }

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,247

    Re: Trying to get value from one form to populate in another form

    I'm having trouble following what you are trying to do.

    Here's your requirements:
    NameForm - enter name and click submit to go to Welcome.
    Welcome - this has a label field in it that should populate with the name that was entered on the NameForm.
    Here's what you have:
    What I have so far, when the Welcome page comes up, it does not give the value of the text but if you click on the label field, it pops up with the NameForm screen. After you enter name again on the NameForm screen, it will show in Welcome.
    The requirements seems to indicate that you are starting with a Welcome, then Name, then Welcome. This workflow doesn't seem to match the requirements.

  3. #3
    Join Date
    Sep 2010
    Posts
    26

    Re: Trying to get value from one form to populate in another form

    I start with the NameForm. This asks for first and last name. I hit Submit and WelcomeForm comes up. One this form, there is a label that should fill with the fullname from NameForm.
    What it is doing right now is after hitting submit on NameForm, the WelcomeForm comes up but the label is empty. I think that because I put the code under the lbl_Click event, this is causing the next issue. When I click on the label which is empty, the NameForm comes up again and should not. But, after entering info again on NameForm and hitting submit, the second time the Welcome comes up, it has the name in the label field.

  4. #4
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,247

    Re: Trying to get value from one form to populate in another form

    I believe what you are trying to do - the code you posted doesn't work that way.

    Below is some code that creates a NameForm and opens a Welcome form. You'll need to use the designer to add the textBoxFirstName, textBoxLastName, and buttonSubmit UI items.

    Code:
    using System;
    using System.Windows.Forms;
    
    namespace CGWelcome
    {
        public partial class NameForm : Form
        {
            public NameForm( )
            {
                InitializeComponent( );
            }
    
            private void buttonSubmit_Click( object sender, EventArgs e )
            {
                Welcome.ShowForm( this, String.Format( "{0}, {1}", textBoxLastName.Text, textBoxFirstName.Text ) );
    
    
                // Alternate approach to displaying the welcome form (doesn't use the static ShowForm method we've created)
                // var welcome = new Welcome();
                // welcome.Name = String.Format("{0}, {1}", textBoxLastName.Text, textBoxFirstName.Text );
                // welcome.ShowDialog( this );
            }
    
            private void OnTextChanged( object sender, EventArgs e )
            {
                buttonSubmit.Enabled = textBoxFirstName.Text.Length > 0 && textBoxLastName.Text.Length > 0;
            }
        }
    }
    The code above handles the Submit click and Welcome.ShowForm which is a static method that creates a Welcome form and passes in the parent form and the formatted user name.

    I've also commented out the equivalent code that displays the Welcome form without using a static method.

    You'll notice there is an OnTextChanged handler. I have this wired to the TextChange event for each of the textboxes. By default I have the Enabled property of the Submit button set to false. This handler just enables the Submit button when both the first and last name textboxes have text.

    The Wecome form code:
    Code:
    using System.Windows.Forms;
    
    namespace CGWelcome
    {
        public partial class Welcome : Form
        {
            // Static ShowForm method - creates a modal form.
            // It passes the name and the parent form.
            public static DialogResult ShowForm( IWin32Window owner, string userName )
            {
                // Create an instance of the Welcome form and set the UserName property
                var form = new Welcome { UserName = userName };
    
                // Call the ShowDialog method and return the dialog result
                return form.ShowDialog( owner );
            }
    
            // Expose a public UserName property that sets the name text
            // (notice how we don't expose the textBoxName directly?)
            public string UserName
            {
                get { return textBoxName.Text; }
                set { textBoxName.Text = value; }
            }
    
            public Welcome( )
            {
                InitializeComponent( );
            }
        }
    }
    The public static ShowForm method is an approach I use to make the code that instantiates the form clean.

    The UserName property simply exposes the underlying name textbox control. It is considered to be bad for one form to access controls from another form, so we simply expose a non-control string property.

    Actually, with the use of the static ShowForm method, we didn't really need to expose a UserName property. Instead we could have just accessed the textboxName.Text property within the ShowForm method (and still achieved the desired control encapsulation).
    Attached Files Attached Files

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Trying to get value from one form to populate in another form

    we are not disposing of our Forms Arjay...

  6. #6
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,247

    Re: Trying to get value from one form to populate in another form

    Quote Originally Posted by BigEd781 View Post
    we are not disposing of our Forms Arjay...
    Why do you feel that's necessary?

  7. #7
    Join Date
    Sep 2010
    Posts
    26

    Re: Trying to get value from one form to populate in another form

    Wow!! You are awesome!!! Thanks so much, that works perfectly (almost) :-) Just need an exclamation but I can do that.
    Again, thank you so much!!

  8. #8
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Trying to get value from one form to populate in another form

    Quote Originally Posted by Arjay View Post
    Why do you feel that's necessary?
    Because Form implements IDisposable, which tells you that it maintains references to native resources that will not be cleaned up. Of course, in this case I believe it will clean up after itself in the finalizer, but I see no benefit in making your program less deterministic.

  9. #9
    Join Date
    Oct 2010
    Posts
    12

    Re: Trying to get value from one form to populate in another form

    I know its a little overkill, but you could alternatively create a class with variables and store the data in that class. It would then be usable in other areas of the program. Yes i know its beyond the scope of the example but thought it worth mentioning.

    It certainly makes it easier with persistent data you wish to keep.

    Just thought I'd throw the idea out there, it can be helpful depending on what you are doing.

  10. #10
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Trying to get value from one form to populate in another form

    Quote Originally Posted by kabben View Post
    I know its a little overkill, but you could alternatively create a class with variables and store the data in that class. It would then be usable in other areas of the program. Yes i know its beyond the scope of the example but thought it worth mentioning.

    It certainly makes it easier with persistent data you wish to keep.

    Just thought I'd throw the idea out there, it can be helpful depending on what you are doing.
    Ugh, please don't create global data for the sake of communicating between objects. There are much, *much* better ways to do it.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,247

    Re: Trying to get value from one form to populate in another form

    Quote Originally Posted by BigEd781 View Post
    Because Form implements IDisposable, which tells you that it maintains references to native resources that will not be cleaned up. Of course, in this case I believe it will clean up after itself in the finalizer, but I see no benefit in making your program less deterministic.
    I don't disagree, but have never bothered to do it for forms.

    Simple enough change:

    Code:
    // Static ShowForm method - creates a modal form.
    // It passes the name and the parent form.
    public static DialogResult ShowForm( IWin32Window owner, string userName )
    {
         // Create an instance of the Welcome form and set the UserName property
         using( var form = new Welcome { UserName = userName } )
         {
               // Call the ShowDialog method and return the dialog result
               return form.ShowDialog( owner );
         }
    }
    Thanks for the suggestion.

  12. #12
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Trying to get value from one form to populate in another form

    Quote Originally Posted by Arjay View Post
    I don't disagree, but have never bothered to do it for forms.
    Yeah, if you knowingly decide not to and know what you are doing (as you do), it will be fine. I tend to include it in all of my examples because 1) it is how I write code anyway, and 2) I am assuming that most of the people looking for help here don't really understand this stuff, and it is better if they always dispose of objects that implement IDisposable. It's funny; someone just posted a new thread here in which they exceeded the handle limit in windows because they weren't disposing of their controls. Why they (think they) need 10,000 controls is another matter entirely...

  13. #13
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,247

    Re: Trying to get value from one form to populate in another form

    It's kind of interesting that one of the features of 1.0 .Net was that the garbage collector was suppose to take care of these things.

    For C++ guys like myself, not having to cleanup didn't sit too well.

    Of course, it didn't work too well in 1.0 either, because in 1.1 MS added the IDisposable interface and the finalizer.

    The end result is, with these features and the using block, we could get very similar behavior to a C++ destructor.

    So now we are in a way back to being concious of having to know when to cleanup objects.

  14. #14
    Join Date
    Sep 2010
    Posts
    26

    Thumbs up Re: Trying to get value from one form to populate in another form

    Thank you all! I have made the change and plan to read up on the IDisposable for future reference. You guys are great :-)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)