具有2个计数器形式和一个主C#的计数器点击器 [英] Counter clicker with 2 counter form and one main C#

查看:108
本文介绍了具有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#的计数器点击器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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