具有2个计数器形式和一个主C#的计数器点击器

编程入门 行业动态 更新时间:2024-10-09 19:17:02
本文介绍了具有2个计数器形式和一个主C#的计数器点击器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

这适用于排队系统。当单击下一个按钮时,它位于frmCounter1和frmCounter2中,必须在主要形式的首选文本框中处理增量编号。 我有什么试过:

// counter1 namespace QueuingSystem { public partial class Counter1 :表格 { int count = 0; 主视图=新Main(); public Counter1() { InitializeComponent(); } private void btnNext1_Click(object sender,EventArgs e) { count ++; label1.Text = count.ToString(); view.label4.Text = count.ToString(); } private void Counter1_Load(object sender,EventArgs e) { view.Show(); } } }

// counter2 namespace QueuingSystem { public partial class Counter2:Form { int count = 0; 主视图=新Main(); public Counter2() { InitializeComponent(); } private void btnNext2_Click(object sender,EventArgs e) { count ++; label1.Text = count.ToString(); view.label5.Text = count.ToString(); } private void Counter2_Load(object sender,EventArgs e) { view.Show(); } } }

解决方案

而不是使用正常方式的事件,你也可以简单地将你的计数变量放在程序中作为静态变量。 然后你可以简单地引用它as: Program.count 在表单中,例如:

使用System; 使用System.Windows.Forms; namespace QueuingSystem { static class Program { public static int count = 123; ///< summary> ///应用程序的主要入口点。 ///< / summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }

并在您的表格中:

使用System.Windows.Forms的; namespace QueuingSystem { public partial class Form1:Form { public Form1() { InitializeComponent() ; this.Text = Program.count.ToString(); } } }

摆脱对主表单的引用,这是一个坏主意 - 而且它不会起作用,因为它是表单的不同实例,而不是你正在显示的那个。 相反,使用事件来告诉主表单中的孩子表格做了一些事情,需要注意:在两种表格之间传递信息,第2部分:儿童到家长 [ ^ ] 它可能看起来很复杂,但它并不是真的 - 它是完全相同的机制,你一直用它来处理按钮,文本框和你的其他一切形式。

this is for a queuing system. When the next button is clicked which is in frmCounter1 and frmCounter2 the increment number must be processed in their preferred textbox which is in main form. What I have tried:

//counter1 namespace QueuingSystem { public partial class Counter1 : Form { int count = 0; Main view = new Main(); public Counter1() { InitializeComponent(); } private void btnNext1_Click(object sender, EventArgs e) { count++; label1.Text = count.ToString(); view.label4.Text = count.ToString(); } private void Counter1_Load(object sender, EventArgs e) { view.Show(); } } }

//counter2 namespace QueuingSystem { public partial class Counter2 : Form { int count = 0; Main view = new Main(); public Counter2() { InitializeComponent(); } private void btnNext2_Click(object sender, EventArgs e) { count++; label1.Text = count.ToString(); view.label5.Text = count.ToString(); } private void Counter2_Load(object sender, EventArgs e) { view.Show(); } } }

解决方案

Instead of using events, which is the normal way, you can also simply put your count variable in Program as a Static variable. Then your can simply refer to it as: Program.count in your forms, example:

using System; using System.Windows.Forms; namespace QueuingSystem { static class Program { public static int count = 123; /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }

And in your Form:

using System.Windows.Forms; namespace QueuingSystem { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.Text = Program.count.ToString(); } } }

Get rid of the reference to your main form, that's a bad idea - and it won't work as that's a different instance of the form, not the one you are displaying. Instead, use Events to "tell" the Main form that the child form has done something and needs attention: Transferring information between two forms, Part 2: Child to Parent[^] It may look complicated, but it isn't really - it's exactly the same mechanism, you use all the time to process the buttons, text boxes, and everything else on your forms.

更多推荐

具有2个计数器形式和一个主C#的计数器点击器

本文发布于:2023-10-18 15:32:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1504637.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:计数器   形式

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!