CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2010
    Posts
    130

    [RESOLVED] bool value does not change!

    I am still having trouble with my button event handler... When a button is clicked btnSkillSelected should turn to true and the button should turn light blue. When another button is clicked (case 1 in If statement) then the button previously clicked should be returned to it's transparent color. The latest selected button should then turn light blue. For some reason the value of btnSkillSelected always keeps the same value: false.

    Code:
    public bool btnSkillSelected;
    
    protected void Page_Load(object sender, EventArgs e)
    {
                btnSkillSelected = false; // I TRIED COMMENTING THIS OUT BUT GOT SAME RESULT!
    }
    
    // If a btn has been enabled before
                if (btnSkillSelected == true)
                {
                    MessageBox("Test: A button is already selected!");
    
                    // Find the previously selected btn
                    for (int i = 0; i < criterium.Length; i++)
                    {
                        if (btnSkill.ID == Convert.ToString(selectedSkillID))
                        {
                            // Disable the previously selected button
                            btnSkill.BackColor = System.Drawing.Color.Transparent;
                            btnSkill.Enabled = true;
                        }
                    }
                }
                else
                {
                    MessageBox("Test: Button selected for first time");
                }
    
                // True for both methods:
    
                // Show that a button has been enabled
                btnSkillSelected = true;
    
                // Change state of currently selected btn
                btnSkill = (Button)sender;
                selectedSkillID = int.Parse(btnSkill.ID);
                btnSkill.BackColor = System.Drawing.Color.LightBlue;
                btnSkill.Enabled = false;
    }

  2. #2
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: bool value does not change!

    Where are you setting the btnSkillSelected to true? I can see it in the code but is not clear what method it is in. Secondly you talk about what should happen to the previously selected button. Have you actually done anything to preserve the state? You have note that with a web application you have to consciously (and carefully) consider how you preserve the state in between postbacks. So of that is done for you (e.g. the ControlState for example) but application data has to maintained by you. In this case the btnSkillSelected always gets reset. You have to bear in that for each request (whether the initial request or a postback) a new instance of the page class is created. So you will need to take some steps to preserve the state of key variables/data you are interested in.

  3. #3
    Join Date
    Jan 2010
    Posts
    130

    Question Re: bool value does not change!

    Sorry, copy pasting error.This was the first part

    Code:
    protected void btnSkill_CheckedChanged(object sender, EventArgs e)
    {
                // If a btn has been enabled before
                if (btnSkillSelected == true)
    (...)
    1. Yes, the state of the previously selected button has been saved in selectedSkillID (it saves the button's ID). When this method is called, the button who has an ID identical to that saved in selectedSkillID is turned back to its original transparent color. The ID of the currently selected button is then saved in selectedSkillID and the button is turned light blue (last lines).

    --> I think I made an error here and the currently selected button is set transparent as opposed to the previously selected one. The currently selected button is then later on turned light blue. Any ideas how I can change the color of the previously selected button?

    2. Thank you. I made them global static vars and now the values dont get reset.

    Btw thanks for all the help you've given me for my previous posts! I am completely new to C# so the questions must seem very obvious to you but they've sure kept me busy for a while

  4. #4
    Join Date
    Nov 2002
    Location
    .NET 3.5 VS2008
    Posts
    1,039

    Re: bool value does not change!

    Quote Originally Posted by stephsh
    Btw thanks for all the help you've given me for my previous posts! I am completely new to C# so the questions must seem very obvious to you but they've sure kept me busy for a while
    I'm glad to be of help. There's a lot to learn about C#, ASP.NET and development in general. I remember a time when these things were not obvious to me so I'm happy to make your learning experience a little bit less painful

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured