not sure how to write the code for this. i have two forms, one is the main form which has a listbox containing data which is loaded in from a text file. the other is a delivery form. when the user select an item in the list box and click the edit button, the delivery form should appear with the selected data displayed in the text box of the delivery form at the moment i have something like this

      private Visit theVisit = new Visit();
    private List<Delivery> deliveries = new List<Delivery>();
     private FrmDelivery deliveryForm = new FrmDelivery();

private void updateDelivery()
    {
        lstDeliveries.Items.Clear();            
        List<String> listOfD = theVisit.listDeliveries();
        lstDeliveries.Items.AddRange(listOfD.ToArray());            
    }
        private void btnEditDelivery_Click(object sender, EventArgs e)
    {
        deliveryForm.ShowDialog();
        updateDelivery();
    }

You can either make a public property on the main form revealing the selected value of the list box, or you can pass the selected value of the list box in the constructor for the second form. Those would be the simplest methods

would it work if i put this in my main form?

 public void lstDeliveries_SelectedIndexChanged(object sender, EventArgs e)
        {
            int indexD = lstDeliveries.SelectedIndex;
            Delivery selectedD = theVisit.getDelivery(indexD);
            deliveryForm.delivery = selectedD;

        }

This is how I normally handle passing data between forms or make a public property
You definitely want to check if you are getting an DialogResult.OK by setting a property of a button on the second form as the user can simply just close the form and then you attempt to do your update with incorrect data

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string temp = listBox1.SelectedItem.ToString();
            Form2 theForm = new Form2(temp);
            if (theForm.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show("It Worked !");
            }
        }
    }



public partial class Form2 : Form
    {
        private string theTextPassedIn;
        public Form2(string theText)
        {
            InitializeComponent();
            theTextPassedIn = theText;
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            textBox1.Text = theTextPassedIn;
        }
    }

@little, the form would already have to exist for you to be able to do that, IE you already opened and instantiated it, but yes it would work.

Vic has shown you a demonstration of using the constructor method to pass the value across.

@vic
i have tried it your way but getting error messages
as i have ready got the following at the start

 private Visit theVisit = new Visit();
 private List<Delivery> deliveries = new List<Delivery>();
 private FrmDelivery deliveryForm = new FrmDelivery();

i tried this:

        private void btnEditDelivery_Click(object sender, EventArgs e)
        {
            string indexDelivery = lstDeliveries.SelectedItem.ToString();
            FrmDelivery deliveryForm = new FrmDelivery(indexDelivery);
            deliveryForm.ShowDialog();
            updateDelivery();
        }

and i get the error saying that 'coursework2_FrmDelivery' does not contain a constructor that takes 0 arguments

Hi you do not need the private FrmDelivery deliveryForm = new FrmDelivery(); declaration anymore as you create a new instance of the form everytime you click your button

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.