How to use get, set in Windows Presentation Foundation (WPF) - c#

I have two windows form, and I can transfer data between them by using (get, set), the code is in below:
Form1.cs
public partial class Form1 : Form
{
public TextBox tb1
{
get { return textBoxinForm1; }
set { textBoxinForm1 = value; }
}
private void buttonSendtoForm2_Click(object sender, EventArgs e)
{
Form2 form2 = (Form2)Application.OpenForms["Form2"];
form2.tb2.Text = textBoxinForm1.Text;
}
}
Form2.cs
public partial class Form2 : Form
{
public TextBox tb2
{
get { return textBoxinForm2; }
set { textBoxinForm2 = value; }
}
private void buttonSendtoForm1_Click(object sender, EventArgs e)
{
Form1 form1 = (Form1)Application.OpenForms["Form1"];
form1.tb1.Text = textBoxinForm2.Text;
}
}
When I try to do the same but with windows (WPF), I have error below "textBoxinForm2", "textBoxinForm1"and "value". So, how to fix that..

The corresponding WPF code to get a reference to a Window1 class would be this:
private void buttonSendtoForm1_Click(object sender, EventArgs e)
{
Window1 form1 = Application.Current.Windows.OfType<Window1>().FirstOrDefault();
form1.tb1.Text = textBoxinForm2.Text;
}

Related

How to access a combobox text of the form1 from another user control

I'm working on a project that contains multiple pages in the same window and i made that using multiple User Controls docked on top of each other and when you click the page button it shows up using: userControl1.Show(); / userControl1.Hide();.
I'm facing some problems when i try to access a combobox text that is located on the form1, i have tried using public strings such as:
public string comPort;
private void cbPort_SelectedIndexChanged(object sender, EventArgs e)
{
comPort = cbPort.Text;
}
and trying to access it from the other user control using:
Form1 mainForm;
public FiremwareFlasher()
{
InitializeComponent();
mainForm = (Form1)this.FindForm();
}
and then:
MessageBox.Show(mainForm.comPort);
But i got that error:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Any ideas?
You could define string property and use Application.OpenForms method to access the property.
Here is a code example you can refer to.
In form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
comboBox1.Items.Add("test1");
comboBox1.Items.Add("test2");
comboBox1.Items.Add("test3");
comboBox1.Items.Add("test4");
testUcl1.Hide();
}
public string cmbtext
{
get
{
return comboBox1.Text;
}
set
{
comboBox1.Text = value;
}
}
bool t = true;
private void button1_Click(object sender, EventArgs e)
{
if(t)
{
testUcl1.Show();
t = !t;
}
else
{
testUcl1.Hide();
t = !t;
}
}
}
In Usercontrol:
public partial class TestUcl : UserControl
{
public TestUcl()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form1 form1 = (Form1)Application.OpenForms["Form1"];
textBox1.Text = form1.cmbtext;
}
}
Test Result:

Can't transfer tbox from 1 form to another tbox in another form c#

I have 2 forms, Form1 is the parent and ALog is the child. My goal is to have a textbox's text from Form1 (form1textbox) contents transfer over to a textbox on ALog (alogcheckbox)
This has to be done on the formload event on Alog and when the form shows from a button click on Form1
This is what I have currently:
Form1:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public string LabelText
{
get { return form1textbox.Text; }
set { form1textbox.Text = value; }
}
private void button1_Click(object sender, EventArgs e)
{
ALog alogform = new ALog();
alogform.Show();
}
}
ALog:
public partial class ALog : Form
{
public ALog()
{
InitializeComponent();
}
public Form Alog;
private void button1_Click(object sender, EventArgs e)
{
}
private void ALog_Load(object sender, EventArgs e)
{
this.Form1.LabelText = textBox1.Text;
}
}
I've seen other questions similar to mine and answers as well, but I can't seem to manage to get this to work.
Any help is appreciated, thanks.
You want to add a constructor to ALog that takes the value, and initialize it that way.
ALog becomes:
public partial class ALog : Form
{
public ALog(string value)
{
InitializeComponent();
this.alogcheckbox.Text = value;
}
public Form Alog;
private void button1_Click(object sender, EventArgs e)
{
}
}
And from Form1:
private void button1_Click(object sender, EventArgs e)
{
ALog alogform = new ALog(form1textbox.Text);
alogform.Show();
}

Get data from Form 2 To Form1 And close

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

How to send date from a textbox to listview of another form

Please, i need help to send data from a textbox to a listview (column quantity) of another form.
I have in form1
namespace officine
{
public partial class FormClav : Form
{
public FormClav()
{
InitializeComponent();
}
......
private void validation_Click(object sender, EventArgs e)
{
// need code here
// onclik thisend textbox1 to listview1 in formOrd
}
I have a lisview1 in a second form
I fill this listview from a dabase after getting bar code.
Then I call form1 (a numeric keyboard) to change quality column..
So I need to send data from Textbox1 (in FormClav ) to lisview1 in FormOrdo
namespace officine
{
public partial class FormOrdo : Form
{
.......
private void loadproduct()
{
listView1.Items.Clear();
cn.Open();
cmd.CommandText = "select * from vente";
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
string[] row = { dr[1].ToString(), dr[2].ToString()};
var listViewItem = new ListViewItem(row);
listView1.Items.Add(listViewItem);
}
}
cn.Close();
}
any idea pls?
You can set a public variable in FormClav and put there the value that you need.
public partial class FormClav : Form
{
public FormClav()
{
InitializeComponent();
}
public string yourvalue = ""
}
private void validation_Click(object sender, EventArgs e)
{
yourvalue = textbox1.text;
}
Try passing a delegate when you instantiate the second form that points to a thread-safe method in the first form that can update your listview.
Hello to past textbox value to another form use this code:
private void validation_Click(object sender, EventArgs e)
{
form2 a = new form2();
a.MdiParent = this.MdiParent; // sets form2 as 'parent window'
a.value = yourTextBox.Text; // sets variable 'value' in form2 equal to yourTextBox value after button is clicked
a.Show(); //opens form2
}
In form 2 generate get set value
public string value { get; set; }
Now you can operate with value easily.
e.g. listView1.Items.Add(value);
On form load adding to listView:
private void Form2_Load(object sender, EventArgs e)
{
listView1.Items.Add(value);
}
Edit:
public partial class Form1 : Form
{
Form2 Frm2;
public Form1()
{
InitializeComponent();
Frm2 = new Form2(this);
}
private void button1_Click(object sender, EventArgs e)
{
Frm2.Show();
Frm2.textBox1.Text = "From Form1";
}
}
public partial class Form2 : Form
{
Form1 Frm1;
public Form2(Form1 F)
{
InitializeComponent();
Frm1 = F;
}
private void button1_Click(object sender, EventArgs e)
{
Frm1.textBox1.Text = "From Form2";
Frm1.listView1.Items.Add(textBox1.Text);
}
}

Passing a value from one form to another form

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

Categories