窗体互相传值问题

zxc544164 2008-12-07 07:20:26
窗体1有一个listbox和一个按钮.
窗体2又一个listbox 和 两个按钮.
窗体1的按钮是用来弹出窗体2的.并且传数据给窗体2的listbox.
窗体2的按钮1用来添加数据.按钮2用来删除数据.
并且当窗体2关闭了时候又把数据传回给窗体1的listbox.此时窗体1的listbox和窗体2传来的数据一样多.或一样小./

麻烦给点代码看看.
...全文
100 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
zxc544164 2008-12-08
  • 打赏
  • 举报
回复
listbox能显示多列吗
vpjian 2008-12-07
  • 打赏
  • 举报
回复
不需要用委托

---------
设置Form2 的listBox1 的Modifiers属性为Public
在Form2加增加一个“确定”按钮设置 DialogResult属性 为OK

在Form1中这样写代码就行了。


Form2 f2 = new Form2();
f2.listBox1.Items.AddRange(listBox1.Items);
if (f2.ShowDialog() == DialogResult.OK)
{
listBox1.Items.Clear();
listBox1.Items.AddRange(f2.listBox1.Items);
}
zxc544164 2008-12-07
  • 打赏
  • 举报
回复
委托事件是什么呀.?

还没学到呢..能说说马?
要自己定义的呀?
sumerainping 2008-12-07
  • 打赏
  • 举报
回复
以前在csdn看到一个兄弟说窗体间传值,委托和事件才是王道。呵呵
scy251147 2008-12-07
  • 打赏
  • 举报
回复
mark,呵呵·~~学习下,一楼的利用委托,貌似我还不会
猿敲月下码 2008-12-07
  • 打赏
  • 举报
回复
简单点的把控件传过去:
Form1中:
Form2 frm=new Form(listbox);
frm.Show();

Form2中:

listbox LB;
public Form2(listbox lb)

{

InitializeComponent();
LB=lb;//把Form1的lb传给LB,这样你就可以在Form2中用LB了,在这里修改也就是修改Form1中的lb
}
zjs1109 2008-12-07
  • 打赏
  • 举报
回复
你可以去我的CSDN上我转载有比较全面的窗体传值,这里给点小例子你看。
说明:Form1为主窗体,包含控件:文本框textBoxFrm1,多选框checkBoxFrm1和按钮buttonEdit;

Form2为子窗体,包含控件:文本框textBoxFrm2,多选框checkBoxFrm2和按钮buttonOK,buttonCancel。


当我们新建一个窗体的时候,设计器会生成默认的构造函数:

public Form2()

{

InitializeComponent();

}

它不带参数,既然我们要把Form1中的一些数据传到Form2中去,为什么不在Form2的构造函数里做文章呢?

假设我们要实现使Form2中的文本框显示Form1里textBoxFrm1的值,修改子窗体的构造函数:

public Form2(string text)

{

InitializeComponent();

this.textBoxFrm2.Text = text;

}

增加Form1中的修改按钮点击事件,处理函数如下:

private void buttonEdit_Click(object sender, System.EventArgs e)

{

Form2 formChild = new Form2(this.textBoxFrm1.Text);

formChild.Show();

}

我们把this.textBoxFrm1.Text作为参数传到子窗体构造函数,以非模式方式打开,这样打开的formChild的文本框就显示了”主窗体”文本,是不是很简单,接下来我们传一个boolean数据给子窗体。

Public Form2(string text,bool checkedValue)

{

InitializeComponent();

this.textBoxFrm2.Text = text;

this.checkBoxFrm2.Checked = checkedValue;

}

在主窗体中的修改按钮点击处理,我采用了打开模式窗口的方式,其实在这个例子中看不出有什么分别,

private void buttonEdit_Click(object sender, System.EventArgs e)

{

Form2 formChild = new Form2(this.textBoxFrm1.Text,this.checkBoxFrm1.Checked);

formChild.ShowDialog();

}

结果在预料之中,但是这里明显存在不足,在子窗体里的数据修改后不能传给主窗体,也就是说主窗体不受子窗体的影响。而在实际的开发过程中我们经常使用子窗体来修改主窗体里面的数据,那怎么解决呢?

在.NET中有两种类型,值类型和引用类型。值类型是从ValueType继承而来,而ValueType又是从Object继承;对于引用类型它直接继承Object类型。这下让我们看看怎样通过Form2来修改Form1里的数据。

还是让我们来修改Form2的代码。

Private TextBox textBoxFrm12;

private CheckBox checkBoxFrm12;

public Form2(TextBox heckbo,CheckBox heckbox)

{

InitializeComponent();

this.textBoxFrm2.Text = heckbo.Text;

this.checkBoxFrm2.Checked = heckbox.Checked;

this.textBoxFrm12 = heckbo;

this.checkBoxFrm12 = heckbox;

}

现在我们传了两个引用类型的数据:TextBox类型,和CheckBox;另外在Form2中增加了两个类数据成员textBoxFrm12、checkBoxFrm12用来分别保存构造函数传来的变量,不过他们并不属于Form2的Controls容器。修改Form2的确定按钮点击事件函数:

private void buttonOK_Click(object sender, System.EventArgs e)

{

this.textBoxFrm12.Text = this.textBoxFrm2.Text;

this.checkBoxFrm12.Checked = this.checkBoxFrm2.Checked;

this.Close();

}

上面的代码我们通过把textBoxFrm2的Text和checkBoxFrm2.Checked赋给textBoxFrm12和checkBoxFrm12完成了对主窗体中的textBoxFrm1和checkBoxFrm2的修改,因为textBoxFrm1和textBoxFrm12是同一个引用,而checkBoxFrm2和checkBoxFrm12也是。

到这里为止功能是实现了,但是总觉得不是很合理,让两个窗体控件传来传去,现在我举一个恰当一点的例子。

修改了两个窗体:



<第二个例子>

说明:在这个例子中我们的两个窗体都加了一个ListBox用来显示ArrayList中的内容。

主窗体中控件:listBoxFrm1,buttonEdit;

子窗体中控件:listBoxFrm2,textBoxAdd,buttonAdd,buttonEdit,buttonOK。

这次我们用ArrayList来作为传递数据,在Form1中定义类数据成员:

private ArrayList listData1;

在构造函数中增加了对listData1进行内存分配,并生成数据最终绑定到listBoxFrm1,

public Form1()

{

InitializeComponent();

this.listData1 = new ArrayList();

this.listData1.Add(DotNet);

this.listData1.Add(C#);

this.listData1.Add(Asp.net);

this.listData1.Add(WebService);

this.listData1.Add(XML);

this.listBoxFrm1.DataSource = this.listData1;

}

另外,对修改按钮点击事件处理函数的修改如下:

private void buttonEdit_Click(object sender, System.EventArgs e)

{

Form2 formChild = new Form2(this.listData1);

formChild.ShowDialog();

this.listBoxFrm1.DataSource = null;

this.listBoxFrm1.DataSource = this.listData1;

}

相对与主窗体,对子窗体作相应修改,也在Form2中增加了类数据成员:

private ArrayList listData2;

用来保存对主窗体中listData1的引用。

修改构造函数:

public Form2(ArrayList listData)

{

InitializeComponent();

this.listData2 = listData;

foreach(object o in this.listData2)

{

this.listBoxFrm2.Items.Add(o);

}

}

这里让listData2同listData1指向同一个引用;另外没有对listBoxFrm进行绑定,采用了填充。

好了,下面是对数据操作的时候了。

添加处理函数代码如下:

private void buttonAdd_Click(object sender, System.EventArgs e)

{

if(this.textBoxAdd.Text.Trim().Length>0)

{

this.listData2.Add(this.textBoxAdd.Text.Trim());

this.listBoxFrm2.Items.Add(this.textBoxAdd.Text.Trim());

}

else

MessageBox.Show(请输入添加的内容!);

}

删除处理代码如下:

private void buttonDel_Click(object sender, System.EventArgs e)

{

int index = this.listBoxFrm2.SelectedIndex;

if(index!=-1)

{

this.listData2.RemoveAt(index);

this.listBoxFrm2.Items.RemoveAt(index);

}

else

MessageBox.Show(请选择删除项或者没有可删除的项!);

}

退出Form2子窗体:

private void buttonOK_Click(object sender, System.EventArgs e)

{

this.Close();

}

编译运行程序,在子窗体中对数据进行修改,关闭后,主窗体就会显示更新后的数据。

这里有一点要提醒一下,比较两个例子,我们都传的是引用类型,一个是String,另一个是ArrayList,为什么string类型不能修改主窗体的数据呢?其实在.Net中对string类型的修改并不是修改原来的值,原来的值没有变化,而是重新生成一个新的字符串,下面是一个很好的说明。

public class ZZConsole

{

[STAThread]

static void Main(string[] args)

{

string str1 = abc;

string str2 = str1;

str1 = 123;

Console.WriteLine(str1);

Console.WriteLine(--------------);

Console.WriteLine(str2);

Console.WriteLine(--------------);

ArrayList al1 = new ArrayList();

al1.Add(abc);

ArrayList al2 = al1;

al2.Add(123);

foreach(object o in al1)

Console.WriteLine((string)o);

Console.WriteLine(--------------);

foreach(object o in al2)

Console.WriteLine((string)o);

Console.ReadLine();

}

}

devilli 2008-12-07
  • 打赏
  • 举报
回复
在子窗体类中声明委托和对应的事件
如:
public delegate void ChangeUserId(object sender, EventArgs e);
public class Test
{
public event ChangeUserId NewEvent;

//你的更改用户ID的方法体
public void ChangeUser()
{
if(NewEvent!=null)
NewEvent(this, new EventArgs());
}
}

父窗体中挂起该事件,代码如下:
public void onLoad()
{
Test test1;
test1.NewEvent += new ChangeUserId(process_Event);
}

void process_Event()
{

//...即可实现事件触发后的方法了
}

http://topic.csdn.net/u/20080626/17/1742fccb-1513-4543-9f9f-045660d24666.html

http://topic.csdn.net/u/20080708/08/B5CA5F5E-1C08-4D71-8AE0-04864BFB28B0.html

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧