[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;
}
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.
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.
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).
Re: Trying to get value from one form to populate in another form
Originally Posted by Arjay
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.
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.
Re: Trying to get value from one form to populate in another form
Originally Posted by kabben
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.
Re: Trying to get value from one form to populate in another form
Originally Posted by BigEd781
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 );
}
}
Re: Trying to get value from one form to populate in another form
Originally Posted by Arjay
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...
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.