Click here to Skip to main content
15,915,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have several buttons on a TableLayoutPanel that have certain tags assigned to them to distinguish them from one another.
Like for instance, if a button is a cake icon, then its tag is "cake", so on and so forth.

What I'm trying to achieve is that if the user clicks on the correct button/item, there will be some sort of a popup message, etc.

Here's what I have so far:

C#
private void Buttons_Click(System.Object sender, System.EventArgs e)
        {
            Button btn = sender as Button;
            string tag = Convert.ToString(btn.Tag);

        //if the problem is cake, and the button has "cake" as its tag, then it's correct
            if (tag.Equals(problem))
            {
                //Add Score\
                MessageBox.Show("Correct!");
            }

            else
            {
                MessageBox.Show("Error");
                second -= 2;
            }
        }


The message boxes don't pop up though. (I'm not that sure of this code as I just ran across this while searching for a possible solution)
Posted
Comments
Arun Banik 13-Sep-14 12:08pm    
Message boxes from code behind does not work with certain browsers. You can validate the same at client side using JavaScript or JQuery. It will be simple. You can create a custom popup window to show the message or using DIV element to the message.

You can create a floating DIV shown in this page ... http://www.encodedna.com/2012/12/floating-div-using-css.htm
BillWoodruff 13-Sep-14 23:55pm    
This question is about WinForms programming with C#: it has nothing do to with HTML, JavaScript, jQuery, etc.
BillWoodruff 13-Sep-14 12:44pm    
Show us the code where you assign the Click EventHandler to the Buttons.

1 solution

Since you don't get any MessageBox, and there is one on each side of the if...else, there are two obvious possible causes:
1) The button Tag property is null: you will get a null reference exception on the if statement as Convert.ToString will return null for a null input. (I'd check as a standard action anyway)
2) The click method is not getting executed at all.

The second is probably the one you are meeting. So check that the event handler is hooked up to each and every Button.Click event...because I'm guessing you didn't do that bit, and it's not automatic.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900