Click here to Skip to main content
15,878,814 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using windows Application using C#.

In My project, I have one ListView (Listview1), 2 text boxes (txtbox1,txtbox2) and 1 button (btnsubmit) controls.

ListView contains Five items (Apple,Orange,Graphs,Banana,Papaya).

From the Listview, I have to select(Using Mouse)top most first item (Apple). Then I entered the txtbox1 some values (Ex:123), Again I can select second item (Orange) from Listview then I entered the txtbox1 some different values (Ex:789), Again I can Select third item (Graphs) form Listview, then I entered the txtbox1 some values (Ex:456).

What I expect, if I select the first item (Apple) from Listview, the txtbox2 will shows the value 123, if I select the second item (Orange) from Listview the txtbox2 will shows the value 789, if I select the third item (Graphs) from Listview the txtbox3 will shows the value 456.

I hope every one understood my Question. If any one not understand i am ready to explain again. I need this solution very urgent give me the solution as soon as possible

Regards
Vasanth
Posted
Updated 17-Nov-19 3:59am
Comments
vasanthkumarmk 15-Aug-12 6:56am    
If any one not understand my explanation please feel free ask, I ready to explain. But give solution very soon.
BobJanova 15-Aug-12 9:52am    
Not sure it was you but when voting on a solution, please remember 5* = good and 1* = bad.

Also, 'urgent' means 'people will skip over your question because you're too demanding'. If it's that urgent, you should already know enough to complete your task instead of expecting strangers to do it for you.
vasanthkumarmk 15-Aug-12 9:53am    
k

The proper way to do this would be using arrays.

As the solution is urgent, you may use the following quick hack to do this.

The following assumes the value gets saved when the button is clicked.

C#
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtbox1.Text = "";
            
            // Added to prevent errors when nothing was selected
            if (listView1.SelectedItems.Count > 0)
            {
                if (listView1.SelectedItems[0].Tag != null)
                {
                    txtbox2.Text = listView1.SelectedItems[0].Tag.ToString();
                }
                else
                {
                    txtbox2.Text = "";
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (listView1.SelectedItems.Count > 0)
            {
                listView1.SelectedItems[0].Tag = txtbox1.Text;
                txtbox1.Text = "";
                MessageBox.Show("Value for " + listView1.SelectedItems[0].Text + " set.");
            }
            else
            {
                MessageBox.Show("Please select an item before assigning a value.");
            }
        }
 
Share this answer
 
v2
Comments
vasanthkumarmk 15-Aug-12 6:57am    
ok thanks, i can implement and check out this code..
vasanthkumarmk 15-Aug-12 7:04am    
if (Listview1.SelectedItems[0].Tag != null) This line shows this type of error

" InvalidArgument=Value of '0' is not valid for 'index'.
Parameter name: index "
Reelix 15-Aug-12 7:20am    
This will happen if you deselect every item - No items are selected, and as such, index 0 is invalid.

Simply add a:

if (listView1.SelectedItems.Count > 0)

clause to make sure at least 1 item is selected.
vasanthkumarmk 15-Aug-12 7:22am    
k i can change this
vasanthkumarmk 15-Aug-12 7:24am    
textBox2.Text = Listview1.SelectedItems[0].Tag.ToString(); This line shows this type of error
Null Reference Exception was unhandled
Object reference not set to an instance of an object.
Dear Friends,

After long discussion, I got the solution of above my post.

C#
private void button1_Click(object sender, EventArgs e)
       {
           if (Listview1.SelectedItems.Count > 0)
           {
               Listview1.SelectedItems[0].Tag = txtbox1.Text;
               txtbox1.Text = "";
               txtbox2.Text = Listview1.SelectedItems[0].Tag.ToString();
               string a = Listview1.SelectedItems[0].Tag.ToString();
               MessageBox.Show("File Name of " + Listview1.SelectedItems[0].Text + " set " + a.ToString() + "  Milli Seconds");
                             
           }
           else
           {
               MessageBox.Show("Please select an item before assigning a value.");
           }
       }

       private void Listview1_SelectedIndexChanged(object sender, EventArgs e)
       {
           try
           {
               if (Listview1.SelectedItems.Count > 0)
               {

                   txtbox2.Text = Listview1.SelectedItems[0].Tag.ToString();

               }
           }
           catch { } 
       }
 
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