I have this class
Account.cs
namespace EasyFtp
{
class Account
{
public String Username;
public String Password;
public String FtpServer;
}
}
and i have MainWindow Form (Main window for my application ) and logForm with 3 textbox and button. I want to log to my ftp server before show my mainwindow , so i have to showdialog my logform and when thus user press button it get all information from logform and pass it to my mainwindow and save data in object of Account class ; my question is how i pass the data.
MainWindow.cs
namespace EasyFtp
{
public partial class MainWindow: Form
{
private Account myaccount;
LogInForm g;
public MainWindow()
{
InitializeComponent();
g = new LogInForm();
g.ShowDialog();
}
/* how i continue the code */
}
}
LogInForm
namespace EasyFtp
{
public partial class LogInForm : Form
{
public LogInForm()
{
InitializeComponent();
}
private void OKButton_Click(object sender, EventArgs e)
{
/*log in code (not created yet)*/
this.Dispose();
}
}
}
Update:
Your Main window
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
textBox1.Text = "original text";
}
private void button1_Click(object sender, EventArgs e)
{
new Form2().ShowDialog();
}
}
Your dialog-form, that will change the values in Form1
public Form2()
{
InitializeComponent();
// Get the text from Form1
textBoxOrg.Text = Application.OpenForms["Form1"].Controls["textBox1"].Text;
}
private void button1_Click(object sender, EventArgs e)
{
// Change the text on Form1
Application.OpenForms["Form1"].Controls["textBox1"].Text = textBox1.Text;
}
You probably wan't to change value on public properties instead of UI-elements.
Another, and cleaner way, is to pass fields to the form with a ref
In my app in several time i have to call a window(class). the work of this window is to show the meaning of a word.when i again call that window a new window shows but the previous one also shows.
I have two form named form1,form2.
Form1 is like that:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string a = textBox1.Text;
Form2 s = new Form2(a);// it will be called as many time as i click
s.Show();
}
}
Form2 is like that:
public partial class Form2 : Form
{
public Form2(string s)
{
InitializeComponent();
label1.Text = s;
}
}
what i want is that inside form1 if i call form2 it shows but if i call form2 again the previous form2 window will be closed automatically and new form2 window will be shown instead of previous one.
How can i do that????
Here's an example of storing the Form2 reference at class level, as mentioned by the others already:
public partial class Form1 : Form
{
private Form2 f2 = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (f2 != null && !f2.IsDisposed)
{
f2.Dispose();
}
string a = textBox1.Text;
f2 = new Form2(a);
f2.Show();
}
}
I think you should consider using singleton pattern.
You can implement it like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string a = textBox1.Text;
Form2.ShowMeaning(a);// it will be called as many time as you click
}
}
and Form2
public partial class Form2 : Form
{
private static readonly Form2 _formInstance = new Form2();
private Form2()
{
InitializeComponent();
}
private void LoadMeaning(string s)
{
label1.Text = s;
}
//Override method to prevent disposing the form when closing.
protected override void OnClosing(CancelEventArgs e)
{
e.Cancel = true;
this.Hide();
}
public static void ShowMeaning(string s)
{
_formInstance.LoadMeaning(s);
_formInstance.Show();
}
}
Hope it helps.
I have created a Winform namedForm1 with a RichTextBox namedrichTextBox1. Also I have created a method called update which does the work of displaying message in the richTextBox1. When I tried to invoke it from Class1 it is not working. Whereas I am to see the message in the MessageBox whereas not in the richTextBox1. Here is the piece of code.
Code: Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public void update(string message)
{
richTextBox1.AppendText("mess: " + message);
MessageBox.Show(message);
}
private void Form1_Load(object sender, EventArgs e)
{
Class1 sample = new Class1();
}
}
Class1.cs
public class Class1
{
public Class1()
{
Form1 form = new Form1();
form.update("Sampe");
}
}
try this:
Class1.cs
class Class1
{
public Class1()
{
Form1._Form1.update("Sampe");
}
}
Form1.cs
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
_Form1 = this;
}
public static Form1 _Form1;
public void update(string message)
{
richTextBox1.AppendText("mess: " + message);
MessageBox.Show(message);
}
private void Form1_Load(object sender, EventArgs e)
{
Class1 sample = new Class1();
}
}
Basically when a user enters data into a textfield on Form2, I want that data to be stored into a variable then when users selects the button enter, Form2 will hide and Form1 will appear. I then want Form1 to display the data entered in the textfield from Form2 in a new textfield.
This is my attempt, but it doesn't work
On Form 2...
public string Player1 {get; set;}
private void pvpPlay_Click(object sender, EventArgs e)
{
Player1 = txtPlayer1.Text;
Form1 op = new Form1();
op.Show();
this.Hide();
}
Then on Form1 to call this I put...
Form2 f = new Form2();
txtTest.Text = f.Player1;
But it doesn't work. Hopefully someone knows the answer.
I would prefer the using of a simple Callback function like this:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public String SomeProperty { get; set; }
private void OpenFormButton_Click(object sender, EventArgs e)
{
var secondForm = new Form2()
{
GetSomeProperty = () => { return SomeProperty ;};
};
this.Hide(); //The best way to hide!
secondForm.Show();
}
private void Form1_Load(object sender, EventArgs e)
{
SomeProperty = "HELLO WORLD";
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Func<string> GetSomeProperty
{
get;
set;
}
private void Form2_Load(object sender, EventArgs e)
{
MessageBox.Show(GetSomeProperty.Invoke());
}
}
Everytime you call GetSomeProperty.Invoke(); the Func will call the get accessor and return it from the first Form
What you can do is overload the Form1 constructor.
public Form1(string s)
{
txtTest.Text=s;
}
When calling from Form2
Form1 op = new Form1(Player1);
I have two forms named form1 and form2:
form1 is made of a label and a button.
form2 is made of a textBox and a button
When I click the form1 button, this will show up form2. Any inputs in textBox should be written back to form1.label once I hit the button in form2.
I have the code below but it doesn't work.
// Code from Form 1
public partial class Form1 : Form
{
public void PassValue(string strValue)
{
label1.Text = strValue;
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2();
objForm2.Show();
}
}
// Code From Form 2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 objForm1 = new Form1();
objForm1.PassValue(textBox1.Text);
this.Close();
}
}
And a screenshot:
How can I realize that?
You don't access your form1, from which you created form2. In form2 button1_Click you create new instance of Form1, which is not the same as initial. You may pass your form1 instance to form2 constructor like that:
// Code from Form 1
public partial class Form1 : Form
{
public void PassValue(string strValue)
{
label1.Text = strValue;
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2(this);
objForm2.Show();
}
}
// Code From Form 2
public partial class Form2 : Form
{
Form1 ownerForm = null;
public Form2(Form1 ownerForm)
{
InitializeComponent();
this.ownerForm = ownerForm;
}
private void button1_Click(object sender, EventArgs e)
{
this.ownerForm.PassValue(textBox1.Text);
this.Close();
}
}
Like mentioned in other posts, you won't be able to reference the original Form1 by creating a new instance of Form1. You can pass Form1 into Form2's constructor or expose Form2's text as a public property, but I usually prefer using delegates for this to maintain loose coupling.
// Code from Form 1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2();
objForm2.PassValue += new PassValueHandler(objForm2_PassValue);
objForm2.Show();
}
public void objForm2_PassValue(string strValue)
{
label1.Text = strValue;
}
}
// Code From Form 2
public delegate void PassValueHandler(string strValue);
public partial class Form2 : Form
{
public event PassValueHandler PassValue;
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (PassValue != null)
{
PassValue(textBox1.Text);
}
this.Close();
}
}
When you are doing:
Form1 objForm1 = new Form1();
objForm1.PassValue(textBox1.Text);
... you are creating a new Form1 and calling the PassValue method on the wrong Form1 object. Instead, you could do:
public partial class Form1 : Form
{
// This is the text that will be entered in form2
public String form2text;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
// Show form2
Form2 objForm2 = new Form2(this);
objForm2.ShowDialog();
// When form2 is closed, update the label text on form1
label1.Text = form2text;
}
}
public partial class Form2 : Form
{
// This is the instance of Form1 that called form2
private Form1 form1caller;
public Form2(Form1 form1caller)
{
InitializeComponent();
this.form1caller = form1caller;
}
private void button1_Click(object sender, EventArgs e)
{
// Pass the textBox value to form1 before closing form2
form1caller.form2text = textBox1.Text;
this.Close();
}
}
I just tried this code and it works, sure it will help you.
in the first form (Form1) type below:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2(textBox1.Text);
f.ShowDialog();
}
}
in the second form (Form2) use below codes:
public partial class Form2 : Form
{
public Form2( string st)
{
InitializeComponent();
textBox1.Text = st;
}
private void Form2_Load(object sender, EventArgs e)
{
}
}
You could do this:
class Form2
{
public string ReturnedText = "";
private void button1_Click(object sender, EventArgs e)
{
ReturnedText = textbox1.Text;
Close();
}
}
and in form1
Form2 objForm2 = new Form2();
objForm2.ShowDialog();
string ret = objForm2.ReturnedText;
You should pass reference on form1 to form2 instead of creating new instance in this code:
private void button1_Click(object sender, EventArgs e)
{
Form1 objForm1 = new Form1(); // ← this is another form1, not that you see
objForm1.PassValue(textBox1.Text);
this.Close();
}
The way that I normally approach this requirement is as follows:
I place a public property on the Form2 class:
public string ValueFromForm1 { get; set; }
//In the constructor, or other relevant method, I use the value
public Form2()
{
form2LabelToDisplayForm1Value.Text = ValueFromForm1;
}
In order to return something to Form1, you need to add a public property to the Form1 class to receive the value, and then send a reference to the form to Form2, so that Form2 can set the value:
//Add reference property to Form2 class
public Form1 CallingForm { get; set; }
//Form2 can access the value on Form1 as follows:
private someMethod()
{
this.CallingForm.ValueFromForm2 = "Info coming from form 2";
}
then
//Add public property to Form1 class
public string ValueFromForm2 { get; set; }
//When Form2 is created, set the reference property
Form2 objForm2 = new Form2();
objForm2.CallingForm = this;
objForm2.Show();
Since Form2 now has a reference to the Form1 that created, there is no need to call new Form1() anywhere in Form2. All Form2 has to do is set the value on the reference, and then close itself.
This is what you are going to do:
// Code from Form 1
public partial class Form1 : Form
{
public string MyValue { get; set; }
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 objForm2 = new Form2();
objForm2.textBox1.Text = MyValue;
objForm2.MainForm = this;
objForm2.Show();
}
}
// Code From Form 2
public partial class Form2 : Form
{
public Form1 MainForm { get; set; }
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MainForm.MyValue = textBox1.Text;
MainForm.Show();
this.Close();
}
}
Form 1 code...:-
namespace Passing_values_from_one_form_to_other
{
public partial class Form1 : Form
{
string str;
private String value1;//taking values from form no _of_test_cases
public string value
{
get { return value1; }
set { value1 = value; }
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = str;
}
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
str = f2.passvalue;
}
}
}
Form 2 code....:-
namespace Passing_values_from_one_form_to_other
{
public partial class Form2 : Form
{
private string str;
public string passvalue
{
get { return str; }
set { str = value; }
}
public Form2()
{
InitializeComponent();
}
private void Btn_Ok1_Click(object sender, EventArgs e)
{
passvalue = textBox1.Text;
this.Close();
}
}
}
directly execute it u will get the clear picture....same way u can pass values from one form to other...
post your comments if you face any issues...
hope this will help...
or else you can refer this video...
http://www.youtube.com/watch?v=PI3ad-TebP0