-1

The goal of the program assignment I'm working on requires me to fill a listbox with items taken from a data file, and then allowing the user to modify parts of the selected item. To do this, I need assistance in figuring out how to pass part of a listbox's selected item in one form to a textbox in another form.

Here's the coding I have for the first form in my program:

public partial class Form1 : Form 
{

    const char DELIM = '\\';
    const string FILENAME = @"C:\Visual Studio 2015\Data Files\Training Workshops data";
    string recordIn;
    string[] Fields;
    static FileStream file = new FileStream(FILENAME, FileMode.Open, FileAccess.Read);
    StreamReader reader = new StreamReader(file);
    public int X;

    public Form1() 
    {

        InitializeComponent();
    }

    public class Workshop 
    {

        public string title { get; set; }
        public int days { get; set; }
        public string categrory { get; set; }
        public double cost { get; set; }

        public string[] categrorynames = 
        { 
            "Application Development", 
            "Databases", 
            "Networking", 
            "System Administration" 
        };
    }

    Workshop shop = new Workshop();       

    private void button1_Click(object sender, EventArgs e) 
    {

        Form2 secondForm = new Form2();
        secondForm.Show();
    }

    private void PopulateList(string filePath) 
    {

        while(recordIn != null) 
        {
            try 
            {
                 recordIn = reader.ReadLine();
                 Fields = recordIn.Split(DELIM);
                 X = Convert.ToInt32(Fields[0]);
                 shop.categrory = shop.categrorynames[X];
                 shop.days = Convert.ToInt32(Fields[1]);
                 shop.title = Fields[3];
                 shop.cost = Convert.ToDouble(Fields[2]);
            }     
            catch (Exception A) 
            {
                if (X < 0 && X > 3) 
                {
                    shop.categrory = "invalid";
                }
                if (shop.days != Convert.ToInt32(Fields[1])) 
                {
                    shop.days = 0;
                }
                if (shop.title != Fields[3]) 
                {
                    shop.title = "invalid";
                }
                if (shop.cost != Convert.ToDouble(Fields[2])) 
                {
                    shop.cost = 0;
                }
            }
        }
    }
}

And below is a link to a screenshot of the second form:

https://i.stack.imgur.com/IRqVh.png

I need to transfer the shop.categrory data of form1's listbox's selected item to the second form's, and the shop.title, shop.days and shop.cost to the corresponding textbox's. From there I can make it so that whatever the user enters in the textboxes would change the data of the selected item when they press the "save and exit" button.

Any help would be appreciated, and if anyone notices an error in the coding I have now, please feel free to point them out.

jsanalytics
  • 13,058
  • 4
  • 22
  • 43
  • Possible duplicate of [How to update textbox in form1 from form2?](http://stackoverflow.com/questions/7969582/how-to-update-textbox-in-form1-from-form2) – Michael Todd Feb 01 '16 at 19:13
  • Is this the actual code that you're using to fill the list? I see nowhere where you fill any kind of list. You have conditions inside a Catch statement and you don't instantiate a new object with each loop of the reader (I assume you have more than one item in your text file that you want to add to your list. – Charles May Feb 01 '16 at 19:21
  • Yeah, sorry, Woj. This was my first post ever on this site and didn't get how to post code right. Thank you for editing it, jstreet. – Michael Boyer Feb 03 '16 at 17:45

1 Answers1

0

Create a Parameterized constructor in form2 that accepts a String, and create it's instance in first form, and pass the value you want to pass to the second form:

private void button1_Click(object sender, EventArgs e) 
{
  Form2 secondForm = new Form2("DATA TO BE SENT");
  secondForm.Show();
}

and in form2 Create a Constructor:

public Form2(string data)
{
  InitializeComponent();
  txtData.Text=data;
}
Jamshaid K.
  • 3,555
  • 1
  • 27
  • 42