1,105,331 Community Members

how to display selected data in listbox and display in textbox?

Member Avatar
littledevils326
Newbie Poster
7 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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();
    }
Member Avatar
Mike Askew
Posting Shark
927 posts since Nov 2010
Reputation Points: 132 [?]
Q&As Helped to Solve: 85 [?]
Skill Endorsements: 25 [?]
Featured
 
0
 

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

Member Avatar
littledevils326
Newbie Poster
7 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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;

        }
Member Avatar
vicprogrammer61
Newbie Poster
2 posts since Sep 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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;
        }
    }
Member Avatar
Mike Askew
Posting Shark
927 posts since Nov 2010
Reputation Points: 132 [?]
Q&As Helped to Solve: 85 [?]
Skill Endorsements: 25 [?]
Featured
 
0
 

@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.

Member Avatar
littledevils326
Newbie Poster
7 posts since Nov 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

@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

Member Avatar
vicprogrammer61
Newbie Poster
2 posts since Sep 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: