Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#3.0 C# C#4.0
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 15-Aug-12 1:30am
Comments
vasanthkumarmk at 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 at 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 at 15-Aug-12 9:53am
   
k
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
 
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.");
            }
        }
  Permalink  
v2
Comments
vasanthkumarmk at 15-Aug-12 6:57am
   
ok thanks, i can implement and check out this code..
vasanthkumarmk at 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 at 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 at 15-Aug-12 7:22am
   
k i can change this
vasanthkumarmk at 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.
Reelix at 15-Aug-12 8:25am
   
The:
 
if (listView1.SelectedItems[0].Tag != null)
 
Should compensate for this.
 
I have altered the solution with the above recommendation included.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Dear Friends,
 
After long discussion, I got the solution of above my post.
 
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 { } 
       }
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 545
1 OriginalGriff 285
2 Shweta N Mishra 244
3 Mathew Soji 225
4 BillWoodruff 200
0 OriginalGriff 6,153
1 Sergey Alexandrovich Kryukov 5,758
2 DamithSL 4,958
3 Manas Bhardwaj 4,539
4 Maciej Los 3,735


Advertise | Privacy | Mobile
Web01 | 2.8.1411019.1 | Last Updated 15 Aug 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100