开发者

Disable And Change Button Image On Click in C#

I have a button that on click I would like to be disabled and it's background image to be changed to null here is the code I have that happens on button click

    private void levelOne1001_Click(object sender, EventArgs e)
    {
        levelOne1001.Enabled = false;
        levelOne1001.BackgroundImage = null;
        scoreClass.genRandomNumber(100);
        scoreClass.val开发者_如何转开发OfQuestion = 100;
        q1001 = true;
        openQuestionForm();
    }

And here is the code from openQuestionForm();

    private void openQuestionForm()
    {
        QuestionForm qForm = new QuestionForm();
        scoreClass.iCount++;
        qForm.Show();
        this.Hide();
    }

And here is where I call this form back up

     Level1Form l1Form = new Level1Form();
     l1Form.Show();

How the process works is Button on Original form is clicked goes to a Question form, button on Question form is clicked it goes back to Original Form. But when I go back to the original form the button is still enabled and the image is still there. Is there any way to fix this?

EDIT: Forgot to say this was in WinForms


You are instantiating a new Level1Form, so it's returning to its default state, causing the button to return to its default state. There are a few possible approaches:

Add a parameter to Level1Form's constructor that indicates what state the button should be in, something like

 Level1Form(bool enableButton) {
     initComponent();

     if(!enableButton) {
         levelOne1001.Enabled = false;
         levelOne1001.BackgroundImage = null;            
     }
  }

Or, grab the same form again and reuse it. You will need to keep a reference to it somewhere and tell it to show itself again. Alternately, you can grab it out of Application.OpenForms


You're creating a new Level1Form instance, which has nothing to do with the existing instance that you modified.

You need to re-show the original instance.


You need to remember your initial form instance in a member outside the method and call show on it.

Level1Form l1Form;

private void FirstTimeCreate()
{
   l1Form = new Level1Form();
}

private void Reshow() 
{
     l1Form.Show();
}


First add ImageList1 to your design page. Then click on the arrow on top of the imageList, then click "choose images" and add all the different kind of images that you want your button to change upon clicking on it. Then write the below code:

int im = 3;
        private void levelOne1001_Click(object sender, EventArgs e)
            {
levelOne1001.BackgroundImage = imageList1.Images[im];
switch (im)
            {
                case 0:
                case 1:
                case 2:
                    im++;
                    break;
                default:
                    im =0; // assuming you have 3 images; if you are at image 3 and click then let it go to image with index 0 (which is the beginning).
                    break;
            }
                // levelOne1001.Enabled = false; // you can add this code above as you see fit
            }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜