CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 26
  1. #1
    Join Date
    Jan 2010
    Posts
    130

    [RESOLVED] change button color back to transparent?

    I've spent ages trying to work this problem out... When I click on a button the background should change colour. If another button has already been clicked previously (btnSkillSelected = true) then this button should first be turned back transparent. This does not however happen, instead it stays light blue!

    // When a btn is checked the following method is executed
    private void btnSkill_CheckedChanged(object sender, EventArgs e)
    {
    // If a btn has been enabled before
    if (btnSkillSelected == false)
    {
    MessageBox("1st time a btn is selected!");
    // save selected btnSkill
    selectedSkillID = int.Parse(btnSkill.ID);
    }
    else
    {
    MessageBox("not the 1st time a btn is 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;
    }
    }
    }
    // 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;
    // Create side table
    CreateTblCriterium(selectedSkillID);
    }

  2. #2
    Join Date
    Jan 2010
    Posts
    130

    Question Re: change button color back to transparent?

    The problem lies with the button ID.

    When I create my set of buttons I want to give each button a seperate button ID (the first btn ID should equal the first criterium ID, the second btn should equal the second criterium ID, etc). What my code however does is it gives each button the same ID as that of the first criterium ID, then it changes the btn ID to that of the second criterium ID, then to the third criterium ID and so forth. This means that all the buttons have the same ID so I can no longer call up a specific button to change it's background color! How do I ensure each button gets a seperate ID?

    Code:
     protected void CreateTblMainSkill()
             {
                 int i = 0;
    
                foreach(object skill in criterium)
                {
                    btnSkill = new Button();
                    cellSkill = new TableCell();
                    rowSkill = new TableRow();
                    btnSkill.ID = Convert.ToString(criterium[i].ID); // PROB LIES HERE
                    btnSkill.Text = Server.HtmlDecode(criterium[i].Skill);
                    btnSkill.BackColor = System.Drawing.Color.Transparent;
                    btnSkill.BorderColor = System.Drawing.Color.Transparent;
                    cellSkill.Controls.Add(btnSkill);
                    rowSkill.Controls.Add(cellSkill);
    
                    // On button click display side table for desired skill
                    btnSkill.Click += new EventHandler(btnSkill_CheckedChanged);
    Any help would be much appreciated!

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

    Re: change button color back to transparent?

    I'm not sure how you are getting the same ID unless the items in 'criterium' have the same ID. Can you explain further or put more of the code?

  4. #4
    Join Date
    Jan 2010
    Posts
    130

    Re: change button color back to transparent?

    I'm not too sure how that works either!

    As a test I inserted a MessageBox straight after the btn ID is declared and asked for the btn ID to be returned. I then clicked on the first button. The MessageBox popped up 10 times, each time returning me the name of a different item from criterium (in the order that the items are stored in the array criterium). That's how I came up with the assumption below!

    I performed the same test with my radiobuttons (who are aligned next to each button and should also have the same ID as the buttons) to see if I would get a similar result. Strangely when I click on either a button or a radiobutton BOTH messageboxes appear instead of just the desired one (i.e. when clicking on a radiobutton the messagebox with the radiobutton ID should appear and vice versa)! There also seems to be a conflict of event handlers here!

    Here is the rest of my code:
    Code:
             protected void CreateTblMainSkill()
             {
                 int i = 0;
    
                // Create a cell with a skill in each row
                foreach(object skill in criterium)
                {
                    btnSkill = new Button();
                    cellSkill = new TableCell();
                    rowSkill = new TableRow();
                    btnSkill.ID = Convert.ToString(criterium[i].ID); // Give button same ID as skill iD // I THINK PROB LIES HERE
                    btnSkill.Text = Server.HtmlDecode(criterium[i].Skill);
                    btnSkill.BackColor = System.Drawing.Color.Transparent;
                    btnSkill.BorderColor = System.Drawing.Color.Transparent;
                    cellSkill.Controls.Add(btnSkill);
                    rowSkill.Controls.Add(cellSkill);
    
                    // On button click display side table for desired skill
                    btnSkill.Click += new EventHandler(btnSkill_CheckedChanged);
    
                    // For every skill add rbtn for each ENumGrade
                    for (int c = 0; c < eNumGradesList.Length; c++)
                    {
                        cellRbtn = new TableCell();
                        rbtnSkill = new RadioButton();
                        rbtnSkill.ID = Convert.ToString(criterium[i].Skill) + " , " + c; // rbtn ID = skillName + " , " + eNumGrade
                        rbtnSkill.Checked = false;
                        // Autopostback ensures page notifies server of event taking place in web control
                        rbtnSkill.AutoPostBack = true;
                        rbtnSkill.GroupName = Convert.ToString(criterium[i].Skill); // Ensures only one rbtn in each group can be clicked
                        //MessageBox("rbtnSkill group name: " + rbtnSkill.GroupName); // why does this come up again? to do with autopostback?
                        cellRbtn.Controls.Add(rbtnSkill);
                        cellRbtn.BorderColor = System.Drawing.Color.Blue;
                        rowSkill.Controls.Add(cellRbtn);
    
                        // On rbtn click control how many have been clicked
                        rbtnSkill.CheckedChanged += new EventHandler(rbtnSkill_CheckedChanged);
    
                        // If this is NOT the first time accessing this page, keep desired rbtn checked
                        // Note: settings stay even when closed window!
                        if (!IsPostBack)
                        {
                            if (rbtnSkill.ID == Convert.ToString(Input_master.selectedSkillID)) 
                            {
                                rbtnSkill.Checked = true;
                            }
                        }
                    }
    
                    // Add the following rows to the stage
                    tblMain.Rows.Add(rowSkill);
                    i++;
                }
            }
    
            protected void btnSkill_CheckedChanged(object sender, EventArgs e)
            {
                // If a btn has been enabled before
                if (Input_master.btnSkillSelected == true)
                {
                    // Find the previously selected btn
                    for (int i = 0; i < criterium.Length; i++)
                    {
                        if (btnSkill.ID == Convert.ToString(Input_master.selectedSkillID))
                        {
    
                            MessageBox("TEST!");
                            // Disable the previously selected button: NOT WORKING! !!!!!
                            btnSkill.BackColor = System.Drawing.Color.Transparent;
                            btnSkill.Enabled = true;
    
                            // try turning the namin container (i.e. ALL buttons) transparent
                            //btnSkill.NamingContainer = "skill";
                                    // Framework-KlassenbibliothekControl..::.OnParentEnabledChanged-Methode : think only works with windows forms
                                  
                        }
                    }
                }
                else
                {
                    MessageBox("1st time a btn is selected!");
                }
    
                // True for both methods:
    
                // Show that a button has been enabled
                Input_master.btnSkillSelected = true;
    
                // Change state of currently selected btn
                btnSkill = (Button)sender;
                Input_master.selectedSkillID = int.Parse(btnSkill.ID);
                btnSkill.BackColor = System.Drawing.Color.LightBlue;
                btnSkill.Enabled = false;
    
                // Create side table
                CreateTblCriterium(Input_master.selectedSkillID);
            }
    
            protected void rbtnSkill_CheckedChanged(object sender, EventArgs e)
            {
                SetTotalRbtnSelected();
    
                // Save state of page on basis that everything has been completed. Override when rbtn becomes unchecked.
                if (Input_master.totalRbtnSelected == criterium.Length)
                {
                    Input_master.completedPageNoten = true;
                    MessageBox("ALL RBTNS SELECTED");
                }
                else
                {
                    Input_master.completedPageNoten = false;
                    MessageBox("RBTNS SELECTED: " + Input_master.GetTotalRbtnSelected()); // not working!
                }
            }
    
            protected void GetCriterium()
            {
                try
                {
                    criterium = WebServiceConnection.webService.getAllCriteria();
                    eNumGrade = WebServiceConnection.webService.getAllENumGrade();
                }
                catch (UnauthorizedAccessException)
                {
                    Azubiportal.ErrorPage.SetError("UnauthorizedAccessException");
                    errorPage.DisplayErrorMsg();
                }
                catch (IndexOutOfRangeException)
                {
                    Azubiportal.ErrorPage.SetError("IndexOutOfRangeException");
                    errorPage.DisplayErrorMsg();
                }
                catch (NullReferenceException)
                {
                    Azubiportal.ErrorPage.SetError("NullReferenceException");
                    errorPage.DisplayErrorMsg();
                }
                catch (Exception)
                {
                    errorPage.DisplayErrorMsg();
                }
            }
    
             // Set the total nr of rbtns selected on the page: not working!
            protected void SetTotalRbtnSelected()
            {
                try {
                    int i = 0;
    
                        // First split rbtnSkill GroupName to only return beginning part
                        //string rbtnSkillName = rbtnSkill.Group;
    
                    //MessageBox("string: " + rbtnSkillName);
    
                    //// Delimiter removes the comma from between the array
                    //char[] delimiters = new char[] { ',' };
                    //rbtnSkillNameList = rbtnSkillName.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
    
                    //string rbtnSkillBeginName = rbtnSkillNameList[0]; // First part of name
    
                    //MessageBox("string: " + rbtnSkillBeginName);
    
    
                        // while rbtnSkill's group name is equal to that of the first group of skills
                        while (rbtnSkill.GroupName == Convert.ToString(criterium[i].Skill))
                        {
                            // if the rbtnSkill from that group is checked
                            if (rbtnSkill.Checked == true)
                            {
                                Input_master.IncrementTotalRbtnSelected();
                                MessageBox("Incremented total Rbtns : " + Input_master.GetTotalRbtnSelected());
                            }
                        }
            }
    
    (...)

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

    Re: change button color back to transparent?

    Hi,

    Could you possibly put the rendered html in an attachment? I would like to see how it is rendered to get a better understanding of what is happening. Thanks.

  6. #6
    Join Date
    Jan 2010
    Posts
    130

    Post Re: change button color back to transparent?

    A physical dialog box is generated each time so Im not sure how to give it to you as HTML... I uploaded a screenshot of the messagebox generated when I clicked on the first button (the number corresponds to the name/number of the first skill ID). I also attached a screenshot displaying the effect of having clicked first on the first button and then on the second button. The first button should have turned back to its transparent color once I clicked on the second button.

    The text on the message boxes appear as follows:
    Code:
    Button skill ID: 6  
    RbtnSkill group name: Zusammenarbeit (x4)
    Button skill ID: 7 RbtnSkill group name: Auffassungsgabe (x4) 
    Button skill ID: 8 
    RbtnSkill group name: Transferverm&ouml;gen (x4) 
    ...And so forth...
    As you can see the first, third and fifth message are generated by the Button Event Handler while the rest are generated by the Radiobutton Event Handler. The latter displays each message four times since there are four radiobuttons on the same row which should all have the same ID.
    Attached Images Attached Images   

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

    Re: change button color back to transparent?

    Quote Originally Posted by stepsh
    A physical dialog box is generated each time so Im not sure how to give it to you as HTML
    I meant the rendered html for the the whole page (i.g. right click -> view source)

  8. #8
    Join Date
    Jan 2010
    Posts
    130

    Post Re: change button color back to transparent?

    oops! I attached it!
    Attached Files Attached Files

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

    Re: change button color back to transparent?

    Thanks.

    The first thing I would try is replace this line...
    Code:
    rbtnSkill.ID = Convert.ToString(criterium[i].Skill) + " , " + c;
    with this...
    Code:
    rbtnSkill.ID = Convert.ToString(criterium[i].Skill) + c;
    Last edited by nelo; January 27th, 2010 at 10:47 AM. Reason: Added code tags...

  10. #10
    Join Date
    Jan 2010
    Posts
    130

    Re: change button color back to transparent?

    Thanks but I already tried that before and it didnt help

    I tried it a second time to be on the safe side but again no success...

  11. #11
    Join Date
    Apr 2007
    Location
    Florida
    Posts
    403

    Re: change button color back to transparent?

    Quote Originally Posted by stephsh View Post
    The problem lies with the button ID.

    When I create my set of buttons I want to give each button a seperate button ID (the first btn ID should equal the first criterium ID, the second btn should equal the second criterium ID, etc). What my code however does is it gives each button the same ID as that of the first criterium ID, then it changes the btn ID to that of the second criterium ID, then to the third criterium ID and so forth. This means that all the buttons have the same ID so I can no longer call up a specific button to change it's background color! How do I ensure each button gets a seperate ID?

    Code:
     protected void CreateTblMainSkill()
             {
                 int i = 0;
    
                foreach(object skill in criterium)
                {
                    btnSkill = new Button();
                    cellSkill = new TableCell();
                    rowSkill = new TableRow();
                    btnSkill.ID = Convert.ToString(criterium[i].ID); // PROB LIES HERE
                    btnSkill.Text = Server.HtmlDecode(criterium[i].Skill);
                    btnSkill.BackColor = System.Drawing.Color.Transparent;
                    btnSkill.BorderColor = System.Drawing.Color.Transparent;
                    cellSkill.Controls.Add(btnSkill);
                    rowSkill.Controls.Add(cellSkill);
    
                    // On button click display side table for desired skill
                    btnSkill.Click += new EventHandler(btnSkill_CheckedChanged);
    Any help would be much appreciated!

    In the code you pasted, you're not incrementing 'i' anywhere I can see. Which would set each btnSkill.ID to the same criterium[0] index.

  12. #12
    Join Date
    Jan 2010
    Posts
    130

    Re: change button color back to transparent?

    I increment "i" further down in that method. If you check the rest of the code which I later posted you will see that i increment it (check last line of this code):

    Code:
    (...)
                    btnSkill.Click += new EventHandler(btnSkill_CheckedChanged);
    
                    // For every skill add rbtn for each ENumGrade
                    for (int c = 0; c < eNumGradesList.Length; c++)
                    {
                        cellRbtn = new TableCell();
                        rbtnSkill = new RadioButton();
                        rbtnSkill.ID = Convert.ToString(criterium[i].Skill) + " , " + c; // rbtn ID = skillName + " , " + eNumGrade
                        rbtnSkill.Checked = false;
                        // Autopostback ensures page notifies server of event taking place in web control
                        rbtnSkill.AutoPostBack = true;
                        rbtnSkill.GroupName = Convert.ToString(criterium[i].Skill); // Ensures only one rbtn in each group can be clicked
                        //MessageBox("rbtnSkill group name: " + rbtnSkill.GroupName); // why does this come up again? to do with autopostback?
                        cellRbtn.Controls.Add(rbtnSkill);
                        cellRbtn.BorderColor = System.Drawing.Color.Blue;
                        rowSkill.Controls.Add(cellRbtn);
    
                        // On rbtn click control how many have been clicked
                        rbtnSkill.CheckedChanged += new EventHandler(rbtnSkill_CheckedChanged);
    
                        // If this is NOT the first time accessing this page, keep desired rbtn checked
                        // Note: settings stay even when closed window!
                        if (!IsPostBack)
                        {
                            if (rbtnSkill.ID == Convert.ToString(Input_master.selectedSkillID)) 
                            {
                                rbtnSkill.Checked = true;
                            }
                        }
                    }
    
                    // Add the following rows to the stage
                    tblMain.Rows.Add(rowSkill);
    
                    i++; // HERE i is incremented!
                }
         }

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

    Re: change button color back to transparent?

    Ok. Let's go back a little bit. Please excuse I just want to get a very clear picture of what is happening without having to put the code in my local environment. Let's try and answer these questions...

    1. What happens when you press the skill button (i.e. btnSkill)?
    2. What happens when you press the frist rbtnSkill? What happens when you press another rbtnSkill?
    3. What happens when you press the skill button followed by any of the rbtnSkill buttons?
    4. What happens when you press any of the rbtnSkill followed by the skill button?

    I've got an idea I just want to confirm the facts.

    Thanks.

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

    Re: change button color back to transparent?

    Quote Originally Posted by stephsh View Post
    Thanks but I already tried that before and it didnt help

    I tried it a second time to be on the safe side but again no success...
    I don't think its a good idea to format the radio button ids with the spaces and comma. Looking at the code and the rendered html suggests that you had in the first place was not the problem. I would take away the space and comma. But that's just a personal preference...
    Last edited by nelo; January 27th, 2010 at 11:31 AM. Reason: change some of the words to make it more understandle to a wider audience

  15. #15
    Join Date
    Jan 2010
    Posts
    130

    Re: change button color back to transparent?

    1. What happens when you press the skill button (i.e. btnSkill)?

    The following messageboxes are generated:
    Code:
    Button skill ID: 6  
    RbtnSkill group name: Zusammenarbeit (x4)
    Button skill ID: 7 RbtnSkill group name: Auffassungsgabe (x4) 
    Button skill ID: 8 
    RbtnSkill group name: Transferverm&ouml;gen (x4) 
    (...)
    The button then becomes highlighted in light blue and disabled.

    2. What happens when you press the first rbtnSkill?
    Exactly the same messages as above are generated. The appropriate rbtn becomes selected.

    What happens when you press another rbtnSkill?
    Exactly the same messages as above are generated. The second rbtn becomes selected. If rbtn is in the same row, then the previous rbtn is deselected. If not then in total 2 rbtns are now selected.

    3. What happens when you press the skill button followed by any of the rbtnSkill buttons?
    First situation 1 happens (answered in q1) and then situation 2 (answered in q2).

    4. What happens when you press any of the rbtnSkill followed by the skill button?
    First situation 2 happens and then situation 1.

    --> My order of selection, doesnt seem to change anything. I keep receiving the same messages, in the same order, but the relevant button or radiobutton does select itself.

Page 1 of 2 12 LastLast

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