Listview所选项目的值 [英] Listview selected item value

查看:101
本文介绍了Listview所选项目的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#使用Windows应用程序.

在我的项目中,我有一个ListView(Listview1),2个文本框(txtbox1,txtbox2)和1个按钮(btnsubmit)控件.

ListView包含五个项目(Apple,Orange,Graphs,Banana,Papaya).

从列表视图中,我必须选择(使用鼠标)最上面的第一项(苹果).然后我输入txtbox1的一些值(例如:123),再次从列表视图中选择第二个项目(橙色),然后输入txtbox1的一些不同值(例如:789),再次可以从列表视图中选择第三个项目(图形),然后我在txtbox1中输入了一些值(例如:456).

我所期望的是,如果我从列表视图中选择第一项(苹果),则txtbox2将显示值123,如果我从列表视图中选择第二项(橙色),则txtbox2将显示值789,如果我选择了第三项(来自Listview的图表),txtbox3将显示值456.

我希望每个人都能理解我的问题.如果有人不明白,我准备再次解释.我需要这个解决方案非常紧急,请尽快给我解决方案

问候
Vasanth

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

推荐答案

执行此操作的正确方法是使用数组.

由于解决方案很紧急,因此您可以使用以下快速技巧来做到这一点.

以下假设单击按钮时将保存该值.

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.");
            }
        }


亲爱的朋友,

经过长时间的讨论,我得到了帖子上方的解决方案.

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


这篇关于Listview所选项目的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆