C# School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C# Expert!

Join 300,444 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,529 people online right now. Registration is fast and FREE... Join Now!




Assigning multiple activators to one control

 

Assigning multiple activators to one control

L1989

30 Jun, 2009 - 09:06 PM
Post #1

New D.I.C Head
*

Joined: 21 Jun, 2009
Posts: 26

I want to assign multiple activators to one control button, say, I want a label to add one every time I click one it with the left mouse button and minus one every time I click one it with the right mouse button.

Any help would be appreciated

User is offlineProfile CardPM
+Quote Post


PsychoCoder

RE: Assigning Multiple Activators To One Control

30 Jun, 2009 - 09:09 PM
Post #2

Dyslexics Untie!
Group Icon

Joined: 26 Jul, 2007
Posts: 14,714



Thanked: 501 times
Dream Kudos: 11450
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

My Contributions
Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Post your code like this: code.gif

Thanks.
User is offlineProfile CardPM
+Quote Post

L1989

RE: Assigning Multiple Activators To One Control

30 Jun, 2009 - 10:06 PM
Post #3

New D.I.C Head
*

Joined: 21 Jun, 2009
Posts: 26

CODE

private void lblpcolor1_Click(object sender, EventArgs e)
        {


            if (intClick1 == 10)
            {
                intClick1 = 1;
            }
            else
            {
                intClick1++;
            }


            switch (intClick1)
            {


                case 1:
                    lblpcolor1.BackColor = Color.Red;
                    break;
                case 2:
                    lblpcolor1.BackColor = Color.Orange;
                    break;
                case 3:
                    lblpcolor1.BackColor = Color.Yellow;
                    break;
                case 4:
                    lblpcolor1.BackColor = Color.Green;
                    break;
                case 5:
                    lblpcolor1.BackColor = Color.Cyan;
                    break;
                case 6:
                    lblpcolor1.BackColor = Color.Blue;
                    break;
                case 7:
                    lblpcolor1.BackColor = Color.Purple;
                    break;
                case 8:
                    lblpcolor1.BackColor = Color.Black;
                    break;
                case 9:
                    lblpcolor1.BackColor = Color.White;
                    break;
                case 10:
                    lblpcolor1.BackColor = Color.Gray;
                    break;

            }


As in this case, this is set by default that the label I have assigned will change color every time I click on it with my left mouse button, it can loop between the 10 colors but can't go backwards if I happen to click an extra time, what I want to do is to assign the right click button of the mouse to it so that it'll go backwards when I click on the label with my right mouse button
User is offlineProfile CardPM
+Quote Post

janne_panne

RE: Assigning Multiple Activators To One Control

30 Jun, 2009 - 10:25 PM
Post #4

D.I.C Addict
****

Joined: 9 Jun, 2009
Posts: 527



Thanked: 107 times
My Contributions
This is how I would do it:

CODE

        int color = 1;

        private void lblpcolor1_Click(object sender, EventArgs e)
        {
            // Convert event to MouseEventArgs so we can find out which mousebutton was clicked
            MouseEventArgs me = (MouseEventArgs)e;

            if (me.Button == MouseButtons.Left)
                color++;
            else if (me.Button == MouseButtons.Right)
                color--;
            

            if (color > 10)
                color = 1;
            else if (color < 1)
                color = 10;

            ChangeLabelColor(lblpcolor1);
        }

        private void ChangeLabelColor(Label lbl)
        {
            switch (color)
            {
                case 1:
                    lbl.BackColor = Color.Red;
                    break;
                case 2:
                    lbl.BackColor = Color.Orange;
                    break;
                case 3:
                    lbl.BackColor = Color.Yellow;
                    break;
                case 4:
                    lbl.BackColor = Color.Green;
                    break;
                case 5:
                    lbl.BackColor = Color.Cyan;
                    break;
                case 6:
                    lbl.BackColor = Color.Blue;
                    break;
                case 7:
                    lbl.BackColor = Color.Purple;
                    break;
                case 8:
                    lbl.BackColor = Color.Black;
                    break;
                case 9:
                    lbl.BackColor = Color.White;
                    break;
                case 10:
                    lbl.BackColor = Color.Gray;
                    break;

            }
        }


I hope this helps you. Also don't hesitate to ask if there is anything confusing in my solution.
User is offlineProfile CardPM
+Quote Post

L1989

RE: Assigning Multiple Activators To One Control

30 Jun, 2009 - 10:40 PM
Post #5

New D.I.C Head
*

Joined: 21 Jun, 2009
Posts: 26

QUOTE(janne_panne @ 30 Jun, 2009 - 10:25 PM) *

This is how I would do it:

CODE

        int color = 1;

        private void lblpcolor1_Click(object sender, EventArgs e)
        {
            // Convert event to MouseEventArgs so we can find out which mousebutton was clicked
            MouseEventArgs me = (MouseEventArgs)e;

            if (me.Button == MouseButtons.Left)
                color++;
            else if (me.Button == MouseButtons.Right)
                color--;
            

            if (color > 10)
                color = 1;
            else if (color < 1)
                color = 10;

            ChangeLabelColor(lblpcolor1);
        }

        private void ChangeLabelColor(Label lbl)
        {
            switch (color)
            {
                case 1:
                    lbl.BackColor = Color.Red;
                    break;
                case 2:
                    lbl.BackColor = Color.Orange;
                    break;
                case 3:
                    lbl.BackColor = Color.Yellow;
                    break;
                case 4:
                    lbl.BackColor = Color.Green;
                    break;
                case 5:
                    lbl.BackColor = Color.Cyan;
                    break;
                case 6:
                    lbl.BackColor = Color.Blue;
                    break;
                case 7:
                    lbl.BackColor = Color.Purple;
                    break;
                case 8:
                    lbl.BackColor = Color.Black;
                    break;
                case 9:
                    lbl.BackColor = Color.White;
                    break;
                case 10:
                    lbl.BackColor = Color.Gray;
                    break;

            }
        }


I hope this helps you. Also don't hesitate to ask if there is anything confusing in my solution.




I've changed a bit to apply to my code, thanks a lot for the help.

Also I would like to ask if I use this "private void ChangeLabelColor(Label lbl)" can I apply it to other labels with the same function?

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 01:37AM

Live C# Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month