简体   繁体   English

在 C#.Net Windows 表格中更改 Label 颜色

[英]Changing Label Color in C# .Net Windows Form

I have the following problem:我有以下问题:

I have a form from which i call a second one.我有一个表格,我从中调用了第二个表格。 First i check if Data isn't empty, if so i print a MessageBox and don't show the form.首先我检查数据是否不为空,如果是,我打印一个 MessageBox 并且不显示表单。

        private void Form1_Click(object sender, EventArgs e)
        {
            //Create an Instance every Time clicked on the Button
            var Form_2 = new Form_2();

            if (Form2.get_data() == true)
            {
                Form_2.Show();
            }
            else
            {
                MessageBox.Show("No Data!", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

I also have a Label underneath the Button which should change, if user pressed the yes Button of the MessageBox that will show up on the second Form.我还有一个 Label 在 Button 下方,如果用户按下 MessageBox 的 yes 按钮,它将显示在第二个表单上。

On the second Form i have the possibility to print some of the data visualized on the Form via MessageBox with Yes/No options.在第二个表单上,我可以通过带有是/否选项的 MessageBox 打印表单上可视化的一些数据。 So i created a private static bool variable in which i save the status, so i change it to true if a User pressed the Yes Button on the MessageBox.所以我创建了一个私有 static bool 变量,我在其中保存状态,所以如果用户按下 MessageBox 上的 Yes 按钮,我将其更改为 true。

private static bool printstatus;

private void btn_print_Click(object sender, EventArgs e)
        {

            DialogResult result_diag = MessageBox.Show("Do you want to print?", "Print Label", MessageBoxButtons.YesNo, MessageBoxIcon.Question);

            switch (result_diag)
            {
                case DialogResult.Yes:
                    var print_diag = new PrintDialog();
                    print_diag.ShowDialog();
                    set_printing_status();

                    break;

                case DialogResult.No:
                    //Do Nothing (only close MessageBox)
                    break;
            }
        }

        public void set_printing_status()
        {
            printstatus = true;
        }

        public bool get_printing_status()
        {
            return printstatus;
        }

Then when closing the Form I create an Instance of Form_1, so the update status function that will change the labels will be called.然后在关闭表单时,我创建了一个 Form_1 的实例,因此将调用将更改标签的更新状态 function。 In this function I call from Form_2 the get_printing_status Method and save tha result into a bool value:在这个 function 中,我从 Form_2 调用 get_printing_status 方法并将结果保存到布尔值中:

public void set_printing_status()
        {

            bool print_status = false;
            print_status = Form_2.get_printing_status();

            if (print_status == true)
            {
                label_status_normal.Text = "Status: printed";
                label_status_normal.BackColor = Color.LimeGreen;
                label_status_normal.Invalidate();
                label_status_normal.Update();
            }
            else if (print_status == false)
            {
                label_status_normal.Text = "Status: not printed";
                label_status_normal.BackColor = Color.Red;
                label_status_normal.Invalidate();
                label_status_normal.Update();
            }
}

But it wont change nothing it reaches the Methode to update the status in Form but no Text or BackColor will be changed.但它不会改变它到达 Methode 以更新 Form 中的状态,但不会更改 Text 或 BackColor。 What am I doing wrong?我究竟做错了什么?

You should avoid creating new instances of forms inside another control, that will probably generate leaks or data-loss.您应该避免在另一个控件中创建 forms 的新实例,这可能会产生泄漏或数据丢失。 Try to use the same instance, that will probably reach the effect you desire.尝试使用相同的实例,这可能会达到您想要的效果。

Other solution is to use UserControls, they are effective as forms, with the plus of avoiding new windows poping up and communication between classes are lot easier.其他解决方案是使用 UserControls,它们与 forms 一样有效,另外还可以避免弹出新的 windows 并且类之间的通信更加容易。

If you really want to use one form, just create a single instance or class as i mentioned, then you can maintain data or use as you wish.如果您真的想使用一种形式,只需创建一个实例或我提到的 class,那么您可以根据需要维护数据或使用。 You can create methods to acess, edit and clear all data inside the forms.您可以创建方法来访问、编辑和清除 forms 中的所有数据。 Just treat it like any other class and it will be fine.只需像对待任何其他 class 一样对待它,就可以了。

While the suggested two-forms design looks a bit suspicious, to do what you what check out Invoke method - as far as I remember - it can accept a method name (like foo - without () ), which will be executed in the target form's "environment" (note that this explication is largely simplified).虽然建议的两种形式的设计看起来有点可疑,但要做你检查Invoke方法的事情 - 据我所知 - 它可以接受方法名称(如foo - 不带() ),它将在目标中执行表单的“环境”(请注意,这个解释在很大程度上被简化了)。

You can set the "Owner" of Form2 to your existing Form1 when you call Show() .您可以在调用Show()时将 Form2 的“所有者”设置为现有的 Form1。

Change:改变:

Form_2.Show();

To:至:

Form_2.Show(this); // note the "this" in parenthesis!

Now over in Form2, you can CAST the .Owner property to Form1 and call your desired method:现在在 Form2 中,您可以将.Owner属性投射到 Form1 并调用您想要的方法:

// ... in Form2 ...
// ... other code ...
Form1 f1 = (Form1)this.Owner;
f1.set_printing_status();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM