All.
I want to receive checkbox1 data from form1 to form2. I used get and set methods to receive meaning of variable. I used this code for this. But it didn't work. Why? Where is the problem?
form1.cs
...
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 test = new Form2();
test.checkBox1 = checkBox1.Checked;
test.Show();
}
}
}
form2.cs
...
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
private bool data7;
public Form2()
{
InitializeComponent();
}
public bool checkBox1
{
get { return data7; }
set { value = data7; }
}
private void Form2_Load(object sender, EventArgs e)
{
if (data7 == true)
{
label1.Text = "true";
}
else
{
label1.Text = "false";
}
}
}
}
set { value = data7; }
should be
set { data7 = value; }
Your set method is wrong. It should be
set { data7 = value; }
Form2 apparently stores the value in the variable data7, but not in a CheckBox. You would have to do something like this to actually store it in a CheckBox
public bool checkBox1
{
get { return myCheckBox.Checked; }
set { myCheckBox.Checked = value; }
}
Another problem is returning the result of user input to Form1. Since you call Form2 with test.Show(); the code after this statment continues immediately, without wating for Form2 to close. Call test.ShowDialog(); instead.
Another option for returning a result while not blocking Form1 is to use an event. Using this definition
public class Form2ResultEventArgs : EventArgs
{
public Form2ResultEventArgs(bool checked)
{
this.Checked = checked;
}
public bool Checked { get; private set; }
}
In Form2 you would define an event like this.
public event EventHandler<Form2ResultEventArgs> Form2Result;
private OnForm2Result(bool checked)
{
var handler = Form2Result;
If (handler != null) {
handler(this, new Form2ResultEventArgs(checked));
}
}
// Assuming that you have a OK button on Form2
private OkButton_Click (...)
{
OnForm2Result(myCheckBox.Checked);
this.Close();
}
In Form1
var test = new Form2();
test.Form2Result += ProcessResult;
test.Show();
...
private void ProcessResult(object sender, Form2ResultEventArgs e)
{
bool result = e.Checked;
...
}
UPDATE
If you only want to set a label, why not just do this
In Form2
public void SetDisplay(bool value) {
label1.Text = value.ToString();
}
In Form1
var test = new Form2();
test.SetDisplay(checkBox1.Checked);
test.Show();
Note that InitializeComponent is called in the constructor of Form2 and therefore the label exists after new Form2(). No need to do that in Form2_Load.
I would like to suggest that when the bool data7 is created that is set a value of false, or true I don't care, cause when the form is first loaded if the user has not set CheckBox1 the Form will crash. And Yes I know the OP, in the Code given, did set the variable.
private bool data7 = false;
I have included Fur Dworetzkys Answer in my suggestion Below
...
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
//give the variable a default value
private bool data7=false;
public Form2()
{
InitializeComponent();
}
public bool checkBox1
{
get { return data7; }
//Here is Furs Correction
set { data7 = value; }
}
private void Form2_Load(object sender, EventArgs e)
{
if (data7 == true)
{
label1.Text = "true";
}
else
{
label1.Text = "false";
}
}
}
}
if you are not doing anything (e.g. validation) with data7 in get or set, then just get rid of them.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public bool checkBox1 { get; set; }
private void Form2_Load(object sender, EventArgs e)
{
if (checkBox1)
{
label1.Text = "true";
}
else
{
label1.Text = "false";
}
}
}
Related
public partial class Form2 : Form
{
public bool male {get; set;}
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
maleGender_rb1 = male;
}
}
I want to display the radio buttton result from form1 to form2 using also radio buttons
The best solution is to setup an event in the child form and for the RadioButton controls subscribe to CheckedChanged. The CheckedChange event determines which RadioButton was checked and sends notification to any listeners.
Child form:
namespace PassStringFromChildToParentForm
{
public partial class ChildForm : Form
{
public delegate void OnPassData(bool isMale);
public event OnPassData PassData;
public ChildForm()
{
InitializeComponent();
MaleRadioButton.Checked = true;
MaleRadioButton.CheckedChanged += RadioButton_CheckedChanged;
FemaleRadioButton.CheckedChanged += RadioButton_CheckedChanged;
}
private void RadioButton_CheckedChanged(object sender, EventArgs e)
{
var radioButton = (RadioButton)sender;
if (radioButton.Checked)
{
PassData?.Invoke(radioButton == MaleRadioButton);
}
}
public void UpdateRadioButton(bool isMale)
{
if (isMale)
{
MaleRadioButton.Checked = true;
}
else
{
FemaleRadioButton.Checked = true;
}
}
}
}
Main Form:
Here we subscribe to the child form event and determine which RadioButton to check based on the bool value passed.
namespace PassStringFromChildToParentForm
{
public partial class MainForm : Form
{
private ChildForm _childForm;
public MainForm()
{
InitializeComponent();
MaleRadioButton.CheckedChanged += RadioButton_CheckedChanged;
FemaleRadioButton.CheckedChanged += RadioButton_CheckedChanged;
}
private void RadioButton_CheckedChanged(object sender, EventArgs e)
{
if (Application.OpenForms.OfType<ChildForm>().Count() != 1) return;
var radioButton = (RadioButton)sender;
if (!radioButton.Checked) return;
if (!radioButton.Checked) return;
_childForm.UpdateRadioButton(MaleRadioButton.Checked);
}
private void ShowChildForm_Click(object sender, EventArgs e)
{
_childForm = new ChildForm();
_childForm.PassData += ChildForm_PassData; ;
_childForm.Show(this);
}
private void ChildForm_PassData(bool isMale)
{
if (isMale)
{
MaleRadioButton.Checked = true;
}
else
{
FemaleRadioButton.Checked = true;
}
}
}
}
You can't convert bool to RadioButton. I think you need to use Checked property of radio button to pass boolean value to it.
For example:
maleGender_rb1.Checked = true;// or false
I have a form and a class. In the form, I have a textbox and an empty label. A user is supposed to type something in the textbox then press a button. I want the textbox value and empty label to be passed to the class where I have a function that passes whatever value was in the textbox into the label, then empties the textbox again. When I type something in the textbox and click the button though, nothing happens. This is basically what I have.
Form:
public partial class Form1 : Form
{
private Class _class;
public Form1()
{
InitializeComponent();
_class = new Class();
}
public string TextBox
{
get { return TextBox1.Text; }
set { TextBox1.Text = value; }
}
public string Label
{
get { return Label1.Text; }
set { Label1.Text = value; }
}
private void btn1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != string.empty)
{
_class.Function();
}
else
{
Something else
}
}
}
Class:
class Class
{
public void Function()
{
using (var form = new Form1())
{
string TextBox = form.TextBox;
string Label = form.Label;
if (Label == string.Empty)
{
Label = TextBox;
TextBox = string.Empty;
}
else
{
Something else
}
}
}
}
In order to modify your current Form you need to pass the object to the function, it is not correct to create a new Form every time the function is called. So I guess you are trying to do something like this:
public partial class Form1 : Form {
private Class _class;
public Form1()
{
InitializeComponent();
_class = new Class();
}
public string TextBox
{
get { return TextBox1.Text; }
set { TextBox1.Text = value; }
}
public string Label
{
get { return Label1.Text; }
set { Label1.Text = value; }
}
private void btn1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != string.Empty)
{
_class.Function(this);
}
else
{
//Something else
}
}
Class:
class Class {
public void Function(Form1 form)
{
string TextBox = form.TextBox;
string Label = form.Label;
if (Label == string.Empty)
{
Label = TextBox;
TextBox = string.Empty;
}
else
{
//Something else
}
}
Anyways, there are better ways to modify properties of Forms Controls. You should take a look at PropertyBinding, it let you bind a property of a class with a Control Form
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;
}
I am developing window application POS. Requirement is: when user click on button 'seach item' on _mainform(form 1) then it open _Searchform(form2) and on the search form it display result in listview with select item button on form3 and close _searchform(form2). The item that we selected from listview , we add in _mainform(form1) listview.
I try to implement this functionality with delegate and event. On form3 (search result form) i have declared delegate and event and subscribe that even on form1(main form). but when i run application event on form 1 dot get fired.
following this code:
_mainform(form1):
namespace KasseDelegate
{
public delegate void ListViewUpdatedEventHandler(object sender, ListViewUpdatedEventArgs e);
public partial class Form1 : Form
{
private Form3 frm3;
public Form1()
{
InitializeComponent();
frm3 = new Form3();
frm3.ListViewUpdated += new ListViewUpdatedEventHandler(Frm3_ListViewUpdated1);
}
private void Frm3_ListViewUpdated1(object sender, ListViewUpdatedEventArgs e)
{
MessageBox.Show("hi");
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2();
frm2.Show();
}
}
}
_searchform(form2) :
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (SQLiteConnection con = new SQLiteConnection(Properties.Settings.Default.ConnectionString))
{
con.Open();
SQLiteCommand cmd = new SQLiteCommand("select * from varer where varenummer=#Varenummer", con);
cmd.Parameters.AddWithValue("#Varenummer", "101");
SQLiteDataReader dr = cmd.ExecuteReader();
Form3 frm3 = new Form3(dr);
frm3.Show();
}
}
}
form 3:
namespace KasseDelegate
{
public partial class Form3 : Form
{
public event ListViewUpdatedEventHandler ListViewUpdated;
SQLiteDataReader dr1;
public Form3()
{
InitializeComponent();
}
public Form3(SQLiteDataReader dr)
{
InitializeComponent();
dr1 = dr;
}
private void Form3_Load(object sender, System.EventArgs e)
{
if (dr1 != null)
{
while (dr1.Read() == true)
{
ListViewItem LVI = new ListViewItem();
LVI.SubItems.Add(dr1[0].ToString());
LVI.SubItems.Add(dr1[1].ToString());
LVI.SubItems.Add(dr1[2].ToString());
LVI.SubItems.Add(dr1[3].ToString());
LVI.SubItems.Add(dr1[4].ToString());
listView1.Items.Add(LVI);
}
}
}
private void button2_Click(object sender, System.EventArgs e)
{
string sVareNummer = listView1.SelectedItems[0].SubItems[1].Text;
string sBeskrivelse = listView1.SelectedItems[0].SubItems[2].Text;
string pris = listView1.SelectedItems[0].SubItems[4].Text;
string enpris = listView1.SelectedItems[0].SubItems[5].Text;
if (ListViewUpdated != null)
{
ListViewUpdated(this, new ListViewUpdatedEventArgs() { VareNummer1 = sVareNummer, Beskrivelse1 = sBeskrivelse, Pris1 = pris, Enpris1 = enpris });
}
}
}
public class ListViewUpdatedEventArgs : System.EventArgs
{
private string VareNummer;
private string Beskrivelse;
private string pris;
private string enpris;
public string VareNummer1
{
get
{
return VareNummer;
}
set
{
VareNummer = value;
}
}
public string Beskrivelse1
{
get
{
return Beskrivelse;
}
set
{
Beskrivelse = value;
}
}
public string Pris1
{
get
{
return pris;
}
set
{
pris = value;
}
}
public string Enpris1
{
get
{
return enpris;
}
set
{
enpris = value;
}
}
}
}
So how i can get values of listview selected item from form3(displayitem) to form1.(mainform).
If I understand it correctly you have Form1 - which launches Form2 which in turn launches Form3. On selecting some item on Form3 you need to update it back on Form1.
So declare a delegate on Form1. Pass this delegate to Form2 [Form2 constructor should accept this as a default parameter - so that if Form2 is triggered from some other place we need not have hard dependency on delegate.]
maintain a private variable of this delegate type on Form2 and while launching Form3 pass the same delegate to Form3 constructor.
On Form3 you have the delegate now which is holding a reference to a method on From1, so whenever an item is selected you can assign this delegate on Form3 which will fire the method on Form1.
so here is an working example.
public delegate void MyDelegate(string selectedItem);
public class Form1
{
private MyDelegate delegate1;
public Form1()
{
delegate1 = new MyDelegate(ShowSelectedItem);
var form2 = new Form2(delegate1);
}
public void LaunchForm2()
{
}
private void ShowSelectedItem(string result)
{
}
}
public class Form2
{
private MyDelegate form2Delegate;
public Form2(MyDelegate del = null)
{
form2Delegate = del;
var form3 = new Form3(form2Delegate);
}
public void LaunchForm3()
{
}
}
public class Form3
{
private MyDelegate form3Delegate;
public Form3(MyDelegate del = null)
{
form3Delegate = del;
SelectedItemTriggered("tes");
}
public void SelectedItemTriggered(string selectedItem)
{
form3Delegate(selectedItem);
//This will trigger method ShowSelectedItem of Form1
}
}
You could set up an event in your Form3 and access it from outside like this:
public partial class Form2 : Form
{
public Form2()
{
Form3 new3 = new Form3();
// Access the event of form3 from outsite
new3.DisplayedItemChanged += ItemChanged;
}
public void ItemChanged(object sender, EventArgs e)
{
// This will be triggered.
}
}
public class Form3 : Form
{
// Create an event
public event EventHandler DisplayedItemChanged;
public void button1_Click(object sender, EventArgs e)
{
if (this.DisplayedItemChanged != null)
{
// Raise the event and pass any object
this.DisplayedItemChanged(yourObjectToPass, e);
}
}
}
According your comments below:
public partial class Form1 : Form
{
public Form1()
{
// This one is an instance of Form3
Form3 newForm = new Form3();
newForm.SomethingHappens += RaiseHere;
}
public void RaiseHere(object sender, EventArgs e)
{
// Do something...
}
}
public partial class Form2 : Form
{
public Form2()
{
// This one is NOT the same as on Form1. Its a NEW form.
Form3 newForm = new Form3();
newForm.Show();
}
}
public partial class Form3 : Form
{
public event EventHandler SomethingHappens;
public Form3()
{
//...
}
}
This wont work, never. You have to use the same instance:
Form3 newForm = new Form3();
newForm.SomethingHappens += RaiseHere;
newForm.Show();
If this isnt clear for you I cant help, sorry:
I have two Forms:
Form1
Form2
Whenever I check/uncheck a CheckBox checkBox1 on Form2 I want to update textbox1.Readonly that is on Form1. If both textbox1 and checkbox1 had been on the same Form it would be easy go:
private void checkBox1_CheckedChanged(object sender, EventArgs e) {
textbox1.Readonly = checkBox1.Checked;
}
What shall I do in my case when textbox1 and checkbox1 are on different Forms?
You can put it like this:
public partial class Form1: Form {
...
// textBox1 is private (we can't access in from Form2)
// so we'd rather create a public property
// in order to have an access to textBox1.Readonly
public Boolean IsLocked {
get {
return textBox1.Readonly;
}
set {
textBox1.Readonly = value;
}
}
}
...
public partial class Form2: Form {
...
private void checkBox1_CheckedChanged(object sender, EventArgs e) {
// When checkBox1 checked state changed,
// let's find out all Form1 instances and update their IsLocked state
foreach (Form fm in Application.OpenForms) {
Form1 f = fm as Form1;
if (!Object.RefrenceEquals(f, null))
f.IsLocked = checkBox1.Checked;
}
}
}
You should use events and delegates.
On Form2, We're create a delegate and event
public delegate void OnCheckedEventHandler(bool checkState);
public event OnCheckedEventHandler onCheckboxChecked;
public void checkBox1_Checked(object sender, EventArgs e)
{
if (onCheckboxChecked != null)
onCheckboxChecked(checkBox1.Checked);
}
And on Form1, we're realize this event:
void showForm2()
{
Form2 f2 = new Form2();
f2.onCheckboxChecked += onCheckboxChecked;
f2.Show();
}
public void onCheckboxChecked(bool checkState)
{
textBox1.ReadOnly = checkState;
}
For simplier and more flexible
Form1:
public class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 tmpFrm = new Form2();
tmpFrm.txtboxToSetReadOnly = this.txtMyTextBox; //send the reference of the textbox you want to update
tmpFrm.ShowDialog(); // tmpFrm.Show();
}
}
FOrm2:
public class Form2 : System.Windows.Forms.Form
{
public Form2()
{
InitializeComponent();
}
TextBox _txtboxToSetReadOnly = null;
public TextBox txtboxToSetReadOnly
{
set{ this._txtboxToSetReadOnly = value; }
get {return this._txtboxToSetReadOnly;}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
if( this._txtboxToSetReadOnly != null) this._txtboxToSetReadOnly.ReadOnly = checkbox1.Checked;
/*
or the otherway
if( this._txtboxToSetReadOnly != null) this._txtboxToSetReadOnly.ReadOnly = !checkbox1.Checked;
*/
}
}