I got 2 forms (form1, form2). I can pass values from form2 to form1, but the main problem is, when I just close the form2, he will pass the values anyway, so I got empty values in form1. For example: I add a value of 2000 euros to form 1 textbox&label, and then when I open one more time the form2, and leave the texbox clear and radiobutt unchecked,then close the form2 with the red X closing button, the form1 values of 2000 euros will disappear. Here's the source code of the passing values and buttons:
FORM2
private string pss;
public string Passvalue
{
get { return pss; }
set { pss = value; }
}
private string pss2;
public string Passvalue2
{
get { return pss2; }
set { pss2 = value; }
}
public void Btn1_Click(object sender, EventArgs e)//The passvalue button
{
string eur="EUR";
Passvalue = ukupnaCifraTB.Text;//textbox form2=the number sender
ukupnaCifraTB.Text = String.Empty;
if (radioButton1.Checked)
{
radioButton1.Text = eur;
Passvalue2 = radioButton2.Text;
}
this.Close();
}
Here's form1:
private string backvalue;
public string BackedValue
{
get { return backvalue; }
set { backvalue = value; }
}
private string backedText;
public string BackedText
{
get { return backedText; }
set { backedText = value; }
}
public void Btn1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.ShowDialog();
trenutnoStanjeTB.Text = f2.Passvalue;//trenutnostanjeTB=textbox(form1)=gets number from form2
DinEuLab1.Text = f2.Passvalue2;//dineulab1=label form1=gets the eur text
DinEuLab2.Text = f2.Passvalue2;
}
What I need to change/add to resolve my problem? I want that when I once pass the values, and then open the form2 and close it, to not send the empty values to form1.
a simple query for emptiness could resolve your issue. At the point where your Form2 is closed and you try to assign the Passvalues
to the Textboxes
of Form1
, just check whether there are valid values in f2.Passvalue
and f2.Passvalue2
. If they are not empty assign them, otherwise let the old value remain
trenutnoStanjeTB.Text = !String.IsNullOrWhiteSpace(f2.Passvalue) ? f2.Passvalue : trenutnoStanjeTB.Text;
DinEuLab1.Text = !String.IsNullOrWhiteSpace(f2.Passvalue2) ? f2.Passvalue2 : DinEuLab1.Text;
and so on