Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi everyone.
I am developing a small application , and I am thinking to pass a textbox value from another form to another. I know how to pass data directly where there are two forms, but in my case I have also an intermediate form ( where user can choose cash or credit card, and depend on the choice, then is opened the form(frm_cash or frm_credit). After I click the button OK one the form( ( frm_cash or frm_credit )) I want the textbox from ( frm_cash or frm_credit ) to be displayed into frm1 textbox. If someone could help me about this one?

What I have tried:

Didn't tried anything, cause I know to pass directly, but not when there is also an intermediate form
Posted
Updated 9-Jun-20 4:29am
Comments
CHill60 9-Jun-20 11:12am    
How are you passing the information "directly"??
[no name] 9-Jun-20 12:07pm    
Form2 fr = new Form2();
fr.showDialog();
textbox1 = frm.TotalValue;

and in form2
Public stirng TotalVlaue();

private void btn_ok(object sender, EventArgs e)
{
TotalValue = txttotal.Text;
This.Dispose();
} This is directly without intermediate form

The problem is when there is a intermediate forms, cause I can't set the textbox to wait from the frm2 data like this
Form2 fr = new Form2();
fr.showDialog();
textbox1 = frm.TotalValue;

Because we don't know which form will open form 3 or form 4(it depents from frm2 user choice)

Follow these tips/tricks:
Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]

BTW: You don't even need intermediate form! If you need to collect different data depending on user choice, you can create one form in which user will provide corresponding data.

Imagine, you've got a Card class and an CardType enum which defines card type:

C#
public enum CardType
{
    None = 0,
    Cash = 1,
    Credit = 2
};

public class Card
{
    string cardNo = string.Empty;
    CardType ct = CardType.Cash;
    string owner = string.Empty;

    public Card(string _cn, CardType _ct, string _owner)
    {
        cardNo = _cn;
        ct = _ct;
        owner = _owner;
    }

    public string Number { get => cardNo; set => cardNo = value; }
    public CardType CardType { get => ct; set => ct = value; }
    public string Owner { get => owner; set => owner = value; }
}



Then, in the second form, user have to select card type andd provide the information about card number and its owner. That's all!
[EDIT]

In case, when you use 3 forms, and one is main, second is intermediate and third is final form, the second form have to act as a dialogbox.

second form
C#
private CardType result = CardType.None;

public IntermediateForm()
{
    InittializeComponents();
}

public CardType 
{
    return result;
}

//use some event to set result
//if(...)
//result = CardType.Cash;
//else
//result = CardType.Credit;


main form
C#
IntermediateForm dlg = new IntermediateForm();
DialogResult ans = ans = dlg.ShowDialog();
if(ans = DialogResult.Cancel) return;
//determine what form to call
CardType ct = dlg.CardType;
if(ct == CardType.Cash)
//call CashForm
else
//call CreditForm
 
Share this answer
 
v3
Comments
[no name] 9-Jun-20 10:42am    
@Maciej Los, the intermediate form is the part of the project. In the intermediate form the user( seller) need to select from Cash or Mix payments method based on client payment. If the intermediate form wouldn't exist, I wouldn't need to ask here. And your example given, doesn't link with what I asked or the problem
CHill60 9-Jun-20 11:14am    
Did you read the articles on the links? Part 3 covers your situation perfectly.
[no name] 9-Jun-20 11:50am    
Non of these covers the situation perfectly .In these links the communication are between two forms( its the easy way). I don't know if you read the question complete. In these articles that you are showing me, the form one waits data from form 2. But in my case form one don't know which form will open (form 3 or form 4) cause there is a intermediate form( form2) where you choose which form will open
Maciej Los 10-Jun-20 2:15am    
Well...
See updated answer (after EDIT).
I would create a 'Transaction" class, and use it to contain all the possible information about this monetary transaction. Instantiate it in the first step (form) of your transaction, and pass it from form to form as you go, adding data in each form.

This way, when you need to display text data in the third form, you can just get it from your transaction object.

Ok. Create your class (this is very raw - just for example purposes):
public class Transcation
    {
        public String data1 = "";
        public String dada2 = "";
    }


In Form1, instantiate your new object:

public partial class Form1 : Form
    {
        private Transcation t = new Transcation();

        public Form1()
        {
            InitializeComponent();
        }

        private void ContinueToNextStep()
        {
            // Update your object's data:
            t.data1 = "some data";

            Form2 f2 = new Form2(t);

            f2.Show();
        }
    }


In subsequent forms, modify the constructor so that it accepts your transaction object, and then access the object's data when you need to, like so:

public partial class Form2 : Form
    {
        private Transcation t;

        public Form2(Transcation t)
        {
            this.t = t;
            InitializeComponent();
            this.textBox1.Text = t.data1;
        }
    }



Rinse and repeat for every form in your chain, adding data as you need to and accessing it as above.

- Pete
 
Share this answer
 
v5
Comments
[no name] 9-Jun-20 10:37am    
pdoxader , any simple example how to display the class data on form ( after being filled) , one the close of form 3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900