Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,

I am trying to transfer my data from listbox that the user has entered into a different listbox on another page. For example I want the ID from Window1 Listbox1 to be transferred to Window2 Listbox2. I have set up a class to store the data but I am unsure how to transfer the data over. (Edit I don't know any of the code on how to transfer the ID from one window to another)

Kind regards

What I have tried:

Class:

class TrainF
    {
        private List<TrainF> _list = new List<TrainF>();

        public void Add(TrainF Details)
        {
            _list.Add(Details);
        }



        //Setting the data for the train booking to be stored.
        private string TrainID;
        public string ID
        {
            get
            {
                return TrainID;
            }
            set
            {
                TrainID = value;
            }
        }



Main code:
TrainF aPerson = new User();
            aPerson.ID = ID.Text;

try
           {
               if (TxtTrainID.Text == "")
               {
                   throw new System.ArgumentException("You must enter your a train ID");
               }
           }
           catch (ArgumentException ex)
           {
               MessageBox.Show(ex.Message);
           }
           LstTrain.Items.Add(TxtTrainID.Text);
Posted
Updated 20-Dec-18 3:11am

The right way to handle this in general is by separating the business logic, i.e. the essentials of your application domain, from all user interaction stuff.

This means:
- create appropriate data structures (using one or more classes) to represent your business; this is independent of forms, controls, etc.
- pass these data structures to the forms that need them.

Combining business and interaction stuff is bound to create unmaintainable code, if not one big mess.

As an absolute minimum, use properties as explained by OriginalGriff in his 3-article series (referenced in solutions2 and 3), to pass business data around. Do not pass Controls, as that would create complex interdependencies between your forms, which are bound to evolve over time.

:)
 
Share this answer
 
v2
There are many ways to structure interchange of data between Forms. Which one you choose may involve user-interaction (showing modally), need for validation of user input, etc.

OriginalGriff has a great series of three articles here on Form to Form interaction and data exchange: [^]

See, also: [^]

Here's one way:

Assuming:

1. You have two Forms

2. One if them is the Main Form

3. The other Form is created at run-time by the Main Form

In the MainForm:
C#
private OtherForm otherForm;

private ListBox otherFormListBox;

private void MainForm_Load(object sender, EventArgs e)
{
     otherForm = new OtherForm;
     otherFormListBox = otherForm.OFListBox; 

     otherForm.Show(); // show it here ?  
}

public void TransferData()
{
     // do the right thing
}
In the OtherForm:
C#
public ListBox OFListBox;

private ListBox otherFormListBox;

private void OtherForm_Load(object sender, EventArgs e)
{
     otherForm = new OtherForm;
     OFListBox = listBox1;
}
In this example, you expose the ListBox in the other Form, so the Main Form can access it.
 
Share this answer
 
v2
For me the simplest way is to use a Static class with properties that can be accessed by both forms, you can even use Program.cs for this purpose.
See explanation here: https://www.dotnetperls.com/static[^]
static class Train
{
    public static string Id { get; set; }
}
 
Share this answer
 
v3
Comments
BillWoodruff 20-Dec-18 8:58am    
My vote of #3 (which I do not consider a negative vote): I think, for new/newer students using a static class is not a good idea; imho, it does not assist in a future understanding, and use, of SOLID. It's a work-around to get global variables "cheap."

However, I think that certain app design scenarios benefit greatly from use of a static class that functions as a "switchboard;" analogous to the "controller" in an MVC pattern. But, that's way out of scope for this thread.

I think the idea of using the internal Program class for this is actually a bad idea: it's a hack.
RickZeeland 20-Dec-18 9:49am    
I agree that using Program.cs is a bit quick and dirty, but I think mentioning Static is informative because some beginners don't seem to know what Static means and what it's use is !
BillWoodruff 20-Dec-18 10:01am    
Hi Rick, I think the appropriate uses of 'static are for: libraries of related functions that have no external dependencies; extension methods, as part of singleton implementation.

https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/classes-and-structs/static-classes-and-static-class-members

also: https://stackoverflow.com/a/241372

imho, in the context of this question, I think mentioning ;static is liable to just confuse the poster.

... and, I could be: dead WRONG :)

Here you will find a Basic discussion about your request: Transferring information between two forms, Part 1: Parent to Child[^]
 
Share this answer
 

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