简体   繁体   English

如何在列表框中显示所选数据并在文本框中显示?

[英]How to display selected data in Listbox and display in Textbox?

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 selects an item in the list box and clicks 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();
}

A Form is a class like any other: you can add properties and you can setup accessors. 表单是一个与其他类一样的类:您可以添加属性,也可以设置访问器。

Use a property on the delivery form which fill a textbox when changed. 使用传递表单上的属性,该属性在更改时会填充文本框。

All you have to do now is set this value from the main form and show the delivery form. 您现在要做的就是从主表单中设置此值并显示交货表单。

Delivery Form: 交付形式:

class FrmDelivery: Form
{
    TextBox text1; // Initialize this as usual
    public string DisplayText
    {
       get { return text1.Text; }
       set { text1.Text = Value; }
    }
} 

Main Form: 主要形式:

private void btnEditDelivery_Click(object sender, EventArgs e)
{
    FrmDelivery frm = new FrmDelivery();
    frm.DisplayText = "Whatever Value you want";
    frm.ShowDialog();
}

You could also declare text1 as public, but I don't like given more control than needed. 您也可以将text1声明为public,但是我不希望获得超出所需的控制权。 Always pick the most restrictive way. 始终选择最严格的方法。

There are some ways to do this,one is you can use static field to pass the value of selecteditem of listbox to Delivery form 有几种方法可以执行此操作,一种是可以使用静态字段将列表框的selecteditem的值传递到“传递”表单

like this : 像这样 :

form1(in selectedindexchanged event of listbox): form1(在listbox的selectedindexchanged事件中):

public static string listboxselecteditem=listbox1.selecteditem;//here you add selected item of listbox

and then in Delivery form you do : 然后以“交付形式”执行:

textbox1.Text=form1.listboxselecteditem;//add value of selected item in listbox to textbox in Delivery form

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

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