Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with creating multiple events

Status
Not open for further replies.

Galarun

Programmer
Jun 28, 2005
24
US
Hello programmers,

I'm creating a cribbage game using Visual Studio Visual C# as well as cards.dll. I'm creating a graphical interface where it displays the cards in each hand. My problem is is that I want to make it so that when you click and hold the mouse (MouseDown) on any card in the hand it will become highlighted.

I could create events for each individual card in the hand, but that would be very tedious and seemingly inefficient. What I'm asking is if there is a way to create an event that fires for each card in the hand w/o having to create many duplicate events. Essentially, when I add a card to a hand I want it to have the event mentioned above, but whenever you create an event you have to specify exactly what control it relates to. I want it so that whenever you add a card it automatically creates the event for that card. Can anybody help or lead me in a direction to solve this problem?

Thanks a ton,

-- Andrew
 
public void AddCardToHand()
{
//Get your card control (whatever the card object is called) in our case we'll call it ctlCard;

ctlCard = GetCard();
ctlCard.MouseDown += new MouseEventHandler(ctlCard_MouseDown);

}

private void ctlCard_MouseDown(object sender, MouseEventArgs e)
{
//Do your card down stuff here
}

When you want to add a card to the hand, just call our AddCardToHand() method and the event will automatically be attached to the control.
 
Oh, so you're saying that for everycard that is added to the hand it will run off the same event?
 
That is correct.

for instance:

public void SetUpTextBoxes()
{
//Just for fun, we'll add 100 textboxes to a form
for (int i = 0; i < 100; i++)
{
TextBox txt = new TextBox();
txt.Text = "TextBox " + i;
txt.Dock = DockStyle.Top;
this.Controls.Add(txt);

txt.Click += new EventHandler(txt_Click);
}
}

private void txt_Click(object sender, EventArgs e)
{
//When any one of the 100 text boxes is clicked
//A messagebox will display its name.
TextBox txt = (TextBox)sender;
MessageBox.Show(txt.Text);
}


As an added bonus: if you select a panel, a textbox, a button, and a label all at once, go to the Events page (the lightning bolt) and double click on the "Click" event, you can have all of those controls point to the same event. This limits you though if you want to detect which control sent it. Its good for things like:

private void ControlCollection_Click(object sender, EventArgs e)
{
//When one of the controls on your usercontrol is clicked, make the control fire its own custom click event

this.MyCustomControlClicked(this, EventArgs.Empty);
}
 
Wow, I didn't know that you could have multiple controls fire off the same event. I'm not a fan of using the design feature in Visual Studio 'cause I enjoy the coding aspect a lot more. If you wanted to code that would it be like this?

control1.Click += new MouseEventHandler(control_Click);
control2.Click += MouseEventHandler(control_Click);
control3.Click += MouseEventHandler(control_Click);
control4.Click += MouseEventHandler(control_Click);

My syntax is probably a bit off (beginner at event programming) so I'm not sure if I need the "new" keyword or not, but my question is is can I just make every control fire off on the same event like above?
 
I believe you do need the "new" keyword. But yes you can do this. HOWEVER - it is not the best coding practice.
 
Why not? Isn't that what you're doing when you're adding a new Event Handler for each card? (Like your example above). What would be the better way of going about this?

Btw, thanks a lot for your responses.
 
To use the same event handler on objects of the same type is fine. In fact - it's a great thing to do. It keeps your code manageable.

It's not a "bad" practice to use the same event on different TYPES of controls, but it does make your code a little less understandable.

Imagine you want to determine which object called the event using "object sender". You have to find out what kind of control to cast it to and then use it based on that control type. In my mind it just seems a little bit like a hack.
 
Ah I understand now. Thanks a lot for your advice JurkMonkey.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top