简体   繁体   English

如何在文本框中显示来自Listview的数据?

[英]how to show data from Listview in Textbox?

I'm writing a Phone Book application. 我正在编写电话簿应用程序。

I have a listview in which you can view the saved data in the database. 我有一个列表视图,您可以在其中查看数据库中保存的数据。 I want to add an edit option. 我想添加一个编辑选项。

More specifically: When I mark a row in the table with subscribers and phone numbers and hit the Edit button a window with two text boxes is shown on the screen. 更具体地说:当我在表格中用订户和电话号码标记一行并单击“编辑”按钮时,屏幕上将显示带有两个文本框的窗口。 In the first text box is the name and in the second - a phone number of the marked entry. 在第一个文本框中是名称,在第二个文本框中是标记条目的电话号码。

Can you tell me how to do that? 你能告诉我怎么做吗?

you can do like this... 你可以这样...

Yes, but TextBox and ListView are in different windows and I cannot access it each other 是的,但是TextBox和ListView在不同的窗口中,我无法互相访​​问

in form2 drag and drop two textboxes and do like this.... 在form2中,拖放两个文本框并执行以下操作。

Just create a property on the Form2 class and set it before you show Form2. 只需在Form2类上创建一个属性,然后在显示Form2之前进行设置即可。

public class Form2
{
    public string Name
    {
        get { return textbox1.Text; }
        set { textbox1.Text = value; }
    }
    public string phonenumber
    {
        get { return textbox2.Text; }
        set { textbox2.Text = value; }

    }

 }

public class Form1
{

  private void btnedit_Click(object sender, eventargs e)
  {
      for (int i = 0; i < lv.Items.Count; i++)
      {
        // is i the index of the row you selected?
        if (lv.Items[i].Selected == true)
        {  
          //I show here the second field text (SubItems[1].Text) from the selected row(Items[i]) 
                Message.Show(lv.Items[i].SubItems[1].Text);
                break;
        }            
      }
      Form2 frm2 = new Form2();
      frm2.Name= text1;
      frm2.phonenumber = text2;
      frm2.Show();
      this.Hide();  //// if you want to hide the form1
    }
  }
}

I hope it will helps you.... 希望对您有帮助。

use MouseClick Event for example, 例如,使用MouseClick事件,

private void ListBox1_MouseClick(System.Object sender, System.Windows.Forms.MouseEventArgs e)  
{    
    this.TextBox1.Text = this.ListBox1.SelectedItem;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM