如何使用列表框将数据从一个窗口传输到另一个窗口。 [英] How to transfer data from one window to another using listboxes.

查看:97
本文介绍了如何使用列表框将数据从一个窗口传输到另一个窗口。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



我正在尝试将用户输入的列表框中的数据传输到另一个页面上的另一个列表框中。例如,我希望将Window1 Listbox1中的ID传输到Window2 Listbox2。我已经设置了一个类来存储数据,但我不确定如何传输数据。 (编辑我不知道如何将ID从一个窗口转移到另一个窗口的任何代码)



亲切的问候



我尝试过:



等级:



 class TrainF 
{
private List< TrainF> _list = new List< TrainF>();

public void Add(TrainF Details)
{
_list.Add(Details);
}



//设置要存储的火车预订数据。
私有字符串TrainID;
公共字符串ID
{
get
{
返回TrainID;
}
设置
{
TrainID = value;
}
}







主要代码:

 TrainF aPerson = new User(); 
aPerson.ID = ID.Text;



尝试
{
if(TxtTrainID.Text == )
{
抛出新的System.ArgumentException(你必须输入你的火车ID);
}
}
catch(ArgumentException ex)
{
MessageBox.Show(ex.Message);
}
LstTrain.Items.Add(TxtTrainID.Text);

解决方案

处理此问题的正确方法是分离业务逻辑,即应用程序域的基本要素,来自所有用户交互的东西。



这意味着:

- 创建适当的数据结构(使用一个或多个类)来代表您的业务;这与表单,控件等无关。

- 将这些数据结构传递给需要它们的表单。



结合业务和交互如果不是一个大混乱,东西必然会创建不可维护的代码。



作为绝对最小值,使用OriginalGriff在他的3篇文章系列中解释的属性(在解决方案2和3),传递业务数据。不要传递控件,因为这会在您的表单之间创建复杂的相互依赖关系,这些关系必然会随着时间的推移而发展。



:)


< blockquote>有很多方法可以在Forms之间构建数据交换。您选择哪一个可能涉及用户交互(显示模态),需要验证用户输入等。



OriginalGriff在表格上有三篇很棒的系列文章形成交互和数据交换:[ ^ ]



另见:[ ^ ]



这是一种方式:



假设:



1.你有两张表格


2.一个如果是主表格



3.另一个表格是在运行时由主表格创建的



在MainForm中:

  private  OtherForm otherForm; 

private ListBox otherFormListBox;

private void MainForm_Load(对象发​​件人,EventArgs e)
{
otherForm = new OtherForm;
otherFormListBox = otherForm.OFListBox;

otherForm.Show(); // 在此处显示?
}

public void TransferData()
{
// 做正确的事
}

在OtherForm中:

  public  ListBox OFListBox; 

private ListBox otherFormListBox;

private void OtherForm_Load(对象发​​件人,EventArgs e)
{
otherForm = new OtherForm;
OFListBox = listBox1;
}

在这个例子中,你在另一个Form中公开ListBox,所以Main Form可以访问它。


对我来说最简单的方法是要使用具有两个表单都可以访问的属性的静态类,您甚至可以使用Program.cs来实现此目的。

请参见此处的说明:https://www.dotnetperls.com/static [ ^ ]

 static class Train 
{
public static string Id {get;组; }
}


Hello,

I am trying to transfer my data from listbox that the user has entered into a different listbox on another page. For example I want the ID from Window1 Listbox1 to be transferred to Window2 Listbox2. I have set up a class to store the data but I am unsure how to transfer the data over. (Edit I don't know any of the code on how to transfer the ID from one window to another)

Kind regards

What I have tried:

Class:

class TrainF
    {
        private List<TrainF> _list = new List<TrainF>();

        public void Add(TrainF Details)
        {
            _list.Add(Details);
        }



        //Setting the data for the train booking to be stored.
        private string TrainID;
        public string ID
        {
            get
            {
                return TrainID;
            }
            set
            {
                TrainID = value;
            }
        }




Main code:

TrainF aPerson = new User();
            aPerson.ID = ID.Text;


try
           {
               if (TxtTrainID.Text == "")
               {
                   throw new System.ArgumentException("You must enter your a train ID");
               }
           }
           catch (ArgumentException ex)
           {
               MessageBox.Show(ex.Message);
           }
           LstTrain.Items.Add(TxtTrainID.Text);

解决方案

The right way to handle this in general is by separating the business logic, i.e. the essentials of your application domain, from all user interaction stuff.

This means:
- create appropriate data structures (using one or more classes) to represent your business; this is independent of forms, controls, etc.
- pass these data structures to the forms that need them.

Combining business and interaction stuff is bound to create unmaintainable code, if not one big mess.

As an absolute minimum, use properties as explained by OriginalGriff in his 3-article series (referenced in solutions2 and 3), to pass business data around. Do not pass Controls, as that would create complex interdependencies between your forms, which are bound to evolve over time.

:)


There are many ways to structure interchange of data between Forms. Which one you choose may involve user-interaction (showing modally), need for validation of user input, etc.

OriginalGriff has a great series of three articles here on Form to Form interaction and data exchange: [^]

See, also: [^]

Here's one way:

Assuming:

1. You have two Forms

2. One if them is the Main Form

3. The other Form is created at run-time by the Main Form

In the MainForm:

private OtherForm otherForm;

private ListBox otherFormListBox;

private void MainForm_Load(object sender, EventArgs e)
{
     otherForm = new OtherForm;
     otherFormListBox = otherForm.OFListBox; 

     otherForm.Show(); // show it here ?  
}

public void TransferData()
{
     // do the right thing
}

In the OtherForm:

public ListBox OFListBox;

private ListBox otherFormListBox;

private void OtherForm_Load(object sender, EventArgs e)
{
     otherForm = new OtherForm;
     OFListBox = listBox1;
}

In this example, you expose the ListBox in the other Form, so the Main Form can access it.


For me the simplest way is to use a Static class with properties that can be accessed by both forms, you can even use Program.cs for this purpose.
See explanation here: https://www.dotnetperls.com/static[^]

static class Train
{
    public static string Id { get; set; }
}


这篇关于如何使用列表框将数据从一个窗口传输到另一个窗口。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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