如何将表单1上的列表框链接到表单到列表框 [英] How can I link a listbox on form 1 to form to listbox

查看:66
本文介绍了如何将表单1上的列表框链接到表单到列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两种形式。我希望第一个表单上的列表框将其结果显示在第二个表单上的列表框中。需要一些帮助来解决这个问题。

解决方案

在C#中你无法访问其他表单控件,但我们知道静态成员可以通过其他类访问。所以你应该在第一种形式中定义一个静态变量来存储它中的项目,如下所示:

在第一种形式中decalre以下

  public   static 列表< string> myList =  new 列表< string>(); 



接下来,在第一个表单的情况下添加Listbox( listbox1)List中的项目

  for  int  i =  0 ; i <  listBox1.Items.Count; i ++)
myList .Add(listBox1.Items [i] .ToString());



和第二种形式,您可以通过以下方式访问此变量并添加到第二个Form的listbox2

  for  int  i =  0 ; i <  Form1.myList.Count; i ++)
listBox2.Items.Add(Form1 .myList [I]);


I have two forms. I want listbox on the first form to show its result to listbox on the second form. Need some assistance on how to do that.

解决方案

In C# you can't access to other Forms' Controls, but we know that static Members are accessible through other classes. so you should define a static variable in first form to store items in it like this:
In First form decalre the following

public static List<string> myList = new List<string>();


Next in an event of first form add the Listbox(listbox1) items in List

for (int i = 0; i < listBox1.Items.Count; i++)
     myList.Add(listBox1.Items[i].ToString());


and in second form you can access this variable by the following and add to listbox2 of second Form

for (int i = 0; i < Form1.myList.Count; i++)
     listBox2.Items.Add(Form1.myList[i]);


这篇关于如何将表单1上的列表框链接到表单到列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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