简体   繁体   中英

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.

        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.

On the second Form i have the possibility to print some of the data visualized on the Form via MessageBox with Yes/No options. 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.

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. In this function I call from Form_2 the get_printing_status Method and save tha result into a bool value:

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. What am I doing wrong?

You should avoid creating new instances of forms inside another control, that will probably generate leaks or data-loss. 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.

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. You can create methods to acess, edit and clear all data inside the forms. Just treat it like any other class and it will be fine.

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).

You can set the "Owner" of Form2 to your existing Form1 when you call Show() .

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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