Then I create another form and send the variable to that form in a constructor like this:
form f2 = new Form2(account);
f2.ShowDialog();
In the form-class f2 I have a button where I write what I want to withdraw from the account.
private void withDraw_Click(object sender, EventArgs e
{
deposit = Int.Parse(txtDeposit.Text);
}
The constructor:
public Form2(int account)
{
InitializeComponent();
account = account + deposit;
}
What I want is to write the amount to deposit in the account in the textbox.
That amount I want to add to the account-variable.
Then I want to return the account to the mainform.
But I dont now how to do that or maybe I could use some global variable?
Nitesh KejriwalPosted Oct 15, 2012, 7:34 AM
Nitesh KejriwalPosted Oct 15, 2012, 9:10 AM
Lars PerssonPosted Oct 15, 2012, 8:41 AM
I tried this:
public void ShowForm(ref int saldo)
{
_saldo = saldo;
this.ShowDialog();
saldo = _saldo;
label1.Text = saldo.ToString();
}
My guess was that first you put the value from saldo into the local variable _saldo.
Then the form with button and a textBox is shown.
You click on the button and add to the saldo.
The local variable is then copied to the referenced saldo.
Shouldn't you then be able to show the saldo (account) in a label?
Lars PerssonPosted Oct 15, 2012, 8:11 AM
Lars PerssonPosted Oct 15, 2012, 8:08 AM
that is, when I write f2 and a dot the ShowForm doesn't show up.
What could be wrong?
Lars PerssonPosted Oct 15, 2012, 7:36 AM
Lars PerssonPosted Oct 15, 2012, 7:30 AM
Nitesh KejriwalPosted Oct 15, 2012, 7:15 AM
Form1
private double account;
form f2 = new Form2();
f2.ShowForm(ref account);
MessageBox.show(account.ToString());
Form2
private double _account;
public void ShowForm(ref double acount)
{
_account = acount;
this.ShowDialog();
acount = _account;
}
private void withDraw_Click(object sender, EventArgs e)
{
_account = _account - Int.Parse(txtDeposit.Text);
}
private void deposit_Click(object sender, EventArgs e)
{
_account = _account + Int.Parse(txtDeposit.Text);
}