处理多个表单之间的数据

处理多个表单之间的数据,第1张

概述处理多个表单之间数据

我正在制作一个生成pdf文件的程序。 在最后一代文件之前,我想让用户select编辑文件的一部分(即将创build的graphics的标题)。 当用户点击一个button导出pdf时,我希望它以新的forms显示出来。 这是我正在尝试做什么的概述…

private voID button4_Click(object sender,EventArgs e) // Test pdf Code! { Form2 Newpdf = new Form2(chart3.Titles[chart3.Titles.IndexOf("header")].Text.ToString().Substring(0,chart3.Titles[chart3.Titles.IndexOf("header")].Text.ToString().Length - 4)); Newpdf.Show(); if (Newpdf.Selected == true) { // Create pdf,open save file dialog,etc } }

这里是通过这个button点击打开的窗体…

public partial class Form2 : Form { public bool Selected { get; set; } public String Graphname { get; set; } public Form2(String filename) { InitializeComponent(); textBox1.Text = filename; Graphname = filename; Selected = false; } public voID button1_Click(object sender,EventArgs e) { Graphname = textBox1.Text; this.Selected = true; // After the button is selected I want the code written above to continue execution,however it does not! } }

到现在为止,当我点击Form2中的button时,什么都没有发生,这两个表单之间的沟通还有一些我不了解的东西!

2个WCF服务进程可以监听相同的端口吗?

c#TrimEnd删除更多需要?

如何获取已安装的更新和修补程序的列表?

{“目录名称无效”} Win32Exception未处理

closureswindows资源pipe理器的特定实例

检查设备更改(添加/删除)事件

在windows上的C#中加载一个linuxdynamic库(.so)

在C#中打印后台处理程序API设置标志?

孤立的存储

无法findtypes或命名空间“Mono”

你应该改变你的Form2.Graphname像下面

public String Graphname { get { return textBox1.Text } }

然后更改您的新的Form2创建像下面,测试它,因为我没有通过VS运行这个,但应该工作:)

private voID button4_Click(object sender,EventArgs e) // Test pdf Code! { // why on earth were you doing .Text.ToString()? it's already string... Form2 Newpdf = new Form2(chart3.Titles[chart3.Titles.IndexOf("header")].Text.Substring(0,chart3.Titles[chart3.Titles.IndexOf("header")].Text.Length - 4)); // show as a dialog form,so it will wait for it to exit,and set this form as parent Newpdf.ShowDialog(this); if (Newpdf.Selected == true) { // get the name from the other form string filename = Newpdf.Graphname; // Create pdf,etc } }

你的问题的答案很简单。

Newpdf.Show();

Show()不会暂停调用表单的执行。 因此,验证Selected属性(如果为true)下面的检查将永远不会正确执行,因为该检查在表单开始出现时已经达到并验证。 ShowDialog()会暂停执行并等待被调用的窗体关闭。

那一边 我会建议其他两种方式之一之间的沟通形式;

使用全局变量。 在公共模块中声明一个保存图形名称的变量。 调用对话框,要求用户使用ShowDialog()输入名称,因为这会暂停执行调用表单,直到调用表单返回结果。

if(Form.ShowDialog() == DialogResult.OK) { // Save pdf,using Title in global variable }

确保在Close()之前将DialogResult设置为被调用的形式。

将调用表单的实例变量传递给被调用的名称输入表单,并将其保存到构造函数中。 这样,如果将图名称属性公开为公共属性,则应该可以从关闭表单的代码中的被调用表单访问它,这是您的:

public voID button1_Click(object sender,EventArgs e) { callingFormInstance.GraphnameProperty = textBox1.Text; Close(); }

希望有所帮助。 干杯!

总结

以上是内存溢出为你收集整理的处理多个表单之间的数据全部内容,希望文章能够帮你解决处理多个表单之间的数据所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1279009.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-09
下一篇2022-06-09

发表评论

登录后才能评论

评论列表(0条)

    保存