项目中有3个表单时如何调用表单,C#中表单之间的数据传输? [英] How to call a form when there are 3 forms in a project and data Transfer between the forms in C#?

查看:43
本文介绍了项目中有3个表单时如何调用表单,C#中表单之间的数据传输?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的项目 form1、form2、form3 中有 3 个表单,现在运行顺利,我在我的项目 form4 中又添加了一个.前三个表单已经通过 ShowDialog() 链接起来了.

我不想碰 Program.cs 文件.

我如何首先将表格 4 称为启动表格?较早的表格 1 是我的项目中出现的第一个表格.

此外,我的 form1 rdb1 和 rdb2 中有 2 个单选按钮.在我的 form2 中,我将 openFileDialog 附加到按钮 Select .现在我希望当用户在 form1 中选择 rdb1 时,form2 中的 openFileDialog 过滤器应该打开只有.XML"作为扩展名的文件,当在 Form1 中选择 rdb2 时,则在 Form2 中只能打开.TXT"文件.

无法在智能感知中找到语法,你能帮忙吗?

提前致谢..

解决方案

你能解释为什么你不想动你的 Program.cs 文件吗?这正是您更改启动表单的地方.

更改:

Application.Run(new Form1());

到:

Application.Run(new Form4());

其次,您可以使用 Filter 属性在 Open- 和 SaveFileDialog 上设置过滤器.将其设置为如下值:

XML 文件|*.xml

或文本:

文本文件|*.txt

编辑添加:

要从另一种形式执行此操作:

class Form1 {表格 2 表格 2;void Form1_Load(对象发送者,EventArgs e){表格 2 = 新表格 2();}void rdb1_CheckedChanged(对象发送者,EventArgs e){如果(rdb1.Checked)form2.openFileDialog1.Filter = "XML 文件|*.xml";别的form2.openFileDialog1.Filter = "文本文件|*.txt";}}

确保您已将 Form2 设计器上 openFileDialog1 的 Modifiers 属性设置为Public"或Internal",以允许从类本身外部访问它.

I have 3 forms in my project form1 , form2 , form3 and it was running smoothly now i have added one more in my project form4 . The first three forms are already linked up through ShowDialog().

I don't want to touch Program.cs file.

How can i call form 4 first as start up form ? Earlier form 1 was the first form to appear in my project.

Also i have 2 radio buttons in my form1 rdb1 and rdb2. In my form2 i have openFileDialog attached to a button Select . Now i want when the user selects rdb1 in form1 then the filter of openFileDialog in form2 should open files with only ".XML" as extension and when rdb2 is selected in Form1 then in Form2 only ".TXT" files can be opened.

I am unable to find the syntax for this in intellisense can you please help out?

Thanks in advance..

解决方案

Can you explain why you don't want to touch your Program.cs file? This is exactly where you change the start-up form.

Change the:

Application.Run(new Form1());

to:

Application.Run(new Form4());

Secondly, you can set the filters on Open- and SaveFileDialog using the Filter property. Set it to a value like this:

XML Files|*.xml

Or for text:

Text Files|*.txt

Edited to add:

To do this from another form:

class Form1 {
  Form2 form2;

  void Form1_Load(object sender, EventArgs e) {
    form2 = new Form2();
  }

  void rdb1_CheckedChanged(object sender, EventArgs e) {
    if (rdb1.Checked) 
      form2.openFileDialog1.Filter = "XML Files|*.xml";
    else
      form2.openFileDialog1.Filter = "Text Files|*.txt";
  }
}

Make sure you have set the Modifiers property of the openFileDialog1 on the Form2 designer to "Public" or "Internal" to allow access to it from outside the class itself.

这篇关于项目中有3个表单时如何调用表单,C#中表单之间的数据传输?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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