[RESOLVED] change button color back to transparent? - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 26 of 26

Thread: [RESOLVED] change button color back to transparent?

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

    Re: change button color back to transparent?

    Ok. Can you also show the code where you are calling the message boxes? I couldn't see it in any of the previous posts. Basically I was expecting to see something showing those messages in btnSkill_CheckedChanged and in rbtnSkill_CheckedChanged. By the way is there a specific reason why the handler for the 'Click' event on btnSkill is called btnSkill_CheckedChanged? I'm just curious about that...

  2. #17
    Join Date
    Jan 2010
    Posts
    130

    Re: change button color back to transparent?

    The button handler was I given that name cas I just copy pasted the rbtn event handler function and changed a couple of things :P Ill change it to btnSkill_Click!

    The code calling my messageboxes isnt actually IN the event handlers, it's in the CreateTblMainSkill() method! Thats whats so strange... For some reason the event handler is calling up (part of?) the CreateTblMainSkill method. My only explanation for this is that the event handler is declared in CreateTblMainSkill() so maybe it's jumping back to there... I'm confused :P

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

    Re: change button color back to transparent?

    At what point in the page lifecycle is the 'CreateTblMainSkill' method called?

  4. #19
    Join Date
    Jan 2010
    Posts
    130

    Re: change button color back to transparent?

    This is the order of method calling:

    Code:
    Page_Load(): calls InitializeEvaluation()
    InitializeEvaluation() : calls CreateTblMain()
    CreateTblMain() : calls SplitENumGrade()
    SplitENumGrade() : calls CreateTblMainSkill()
    CreateTblMainSkill() : contains both event handlers. It doesnt call any other methods up.
    The method that calls up CreateTblMainSkill() looks like this:
    Code:
            protected void SplitENumGrade()
            {
                try {
                    // The first grade system (eNumGradeID=1) in the array list must be used. I.e. position 0
                    string eNumGradeTotal = eNumGrade[0].Grades;
                    // Delimiter removes the comma from between the array
                    char[] delimiters = new char[] { ',' };
                    eNumGradesList = eNumGradeTotal.Split(delimiters, StringSplitOptions.RemoveEmptyEntries);
    
                    foreach (string grade in eNumGradesList)
                    {
                        lblENumGrade = new Label();
                        cellENumGrade = new TableCell();
    
                        // Position cells and specify details
                        lblENumGrade.Text = "  " + grade;
                        lblENumGrade.CssClass = "lblENumGrade";
                        cellENumGrade.Controls.Add(lblENumGrade);
                        cellENumGrade.BorderColor = System.Drawing.Color.Blue; // dont want top to be colored or bottom! specify!!!
                        rowHeader.Controls.Add(cellENumGrade);
                    }
                    CreateTblMainSkill();
                }
                catch (IndexOutOfRangeException i)
                {
                    errorPage.DisplayErrorMsg(i.Message);
                }
                catch (Exception)
                {
                    errorPage.DisplayErrorMsg();
                }
            }

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

    Re: change button color back to transparent?

    Hi Stephsh,

    I think it is better to go back to the beginning. I think I can help you with you were trying to do from the start. Basically you want to click a button have its background colour changed to blue. When you click on another button the previously clicked button will go back to its normal background color, whilst the currently clicked button will have the blue background color. Is this correct?

  6. #21
    Join Date
    Jan 2010
    Posts
    130

    Re: change button color back to transparent?

    Completely correct!

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

    Re: change button color back to transparent?

    Code:
        <asp:Button ID="button1" runat="server" Text="One" OnClick="ButtonClickHandler" />
        <asp:Button ID="button2" runat="server" Text="Two" OnClick="ButtonClickHandler" />
        <asp:Button ID="button3" runat="server" Text="Three" OnClick="ButtonClickHandler" />
    Code:
    public partial class HighlightButtons : System.Web.UI.Page
    {
        private string _selectedButton = String.Empty;
    
        protected void ButtonClickHandler(object sender, EventArgs e)
        {
            Button selectedButton = sender as Button;
            _selectedButton = selectedButton.ID;
        }
    
        protected void Page_PreRender(object sender, EventArgs e)
        {
            button1.BackColor = button1.ID == _selectedButton ? System.Drawing.Color.Blue : System.Drawing.Color.Transparent;
            button2.BackColor = button2.ID == _selectedButton ? System.Drawing.Color.Blue : System.Drawing.Color.Transparent;
            button3.BackColor = button3.ID == _selectedButton ? System.Drawing.Color.Blue : System.Drawing.Color.Transparent;
        }
    }
    This is a small sample. I know your scenario is much more complex but I would still apply the same principle. Apply the standard background to all and then override the one that has just been pressed.
    Last edited by nelo; January 28th, 2010 at 05:27 AM. Reason: Corrected a spelling mistake...:)

  8. #23
    Join Date
    Jan 2010
    Posts
    130

    Re: change button color back to transparent?

    This method definitely seems like a much better one then mine. The problem however is that the true or false statement in pageRender always seems to return the value false because all my buttons now permanently turn transparent! they never turn light blue...

    As a test I added an if statement in page render calling a messagebox if the btnSkill.ID is equal to the ID saved in _selectedButton. The messagebox did not appear so my assumption is that all buttons have been given the same ID, namely that of the last ID. How do I stop this?

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

    Re: change button color back to transparent?

    I've just gone through some of the code you posted previously. I noticed that you have a for loop but you don't seem to use the object you're getting in the loop...
    Code:
                // 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
    You don't seem to be using the 'skill' object declared in the loop. You are accessing the criterium collection by index. That doens't appear to be a problem as you increment the index. However it sees odd that you have a 'foreach' loop in the first place...I would recommend changing that. If the buttons are getting the same ID is because they are being given the same id. I've noticed that you create a new instance of the controls which is good. Step through the code with the debugger to make sure the buttons are being given the id that you expect. Put a break point in that line of code and let me know what happens...

  10. #25
    Join Date
    Jan 2010
    Posts
    130

    Unhappy Re: change button color back to transparent?

    That's funny, I just noticed that this morning and changed it before I read your post! Great minds think alike :P

    It's interesting what happens when I debug it. It starts of alright with each seperate button being created, each receiving a seperate ID and text, and then the page_PreRender method declares the buttons transparent. The problem arises when I click on a certain button (I choose the first btn, namely "Zusammenarbeit"). The CreateTblMainSkill method is then RECALLED and the system goes through the whole for loop giving the button a different ID and text each time until it reaches the end of the array "criterium"!

    This is what is outputted:

    Code:
    CreateTblMainSkill() :
    btnSkill.ID : 6
    btnSkill.Text = Zusammenarbeit
    
    btnSkill.ID : 10
    btnSkill.Text = Auffassungsgabe
    
    (and so forth 8 more times...)
    
    
    Page_PreRender(): 
    btnSkill.Text = "Fertigkeit"
    
    I then clicked on first button:
    
    
    CreateTblMainSkill():
    btnSkill.ID : 6
    btnSkill.Text = Zusammenarbeit
    
    btnSkill.ID : 10
    btnSkill.Text = Auffassungsgabe
    
    (and so forth 8 more times...)
    
    
    Page_PreRender(): 
    btnSkill.Text = "Fertigkeit"

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

    Re: change button color back to transparent?

    Have you tried using the 'IsPostBack' property? I'm not sure it will work given that you are creating the controls dinamically but it is worth a try...Basically if it is a post back don't call the CreateTblMainSkill.

Page 2 of 2 FirstFirst 12

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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center