PDA

View Full Version : Multiple events to raise one function...



Ogre
2007-01-23, 07:54 PM
I am still a novice .Net user...I just have a few questions, but first let me describe what I am trying to do...

I am creating a Quote entry database...I would like to have one line shown, then when the + button is clicked I would like another line to show up...Each line has one delete button (where my problem lies)...I need to create an event for each delete button, but pointing to the same Sub (since they all do the same thing, just to different rows)...Mind you that each delete button is created when the + button is clicked...

If I am going about this the wrong way, please let me know of a better one...Thanks in advance for the help...

Bobby C. Jones
2007-01-24, 03:13 PM
Ogre,
When you hook into each buttons click event simply pass the same function as the delegate.



this.cancelButton1.Click += new System.EventHandler(this.onCancelButtonClick);


this.cancelButton2.Click += new System.EventHandler(this.onCancelButtonClick);

Ogre
2007-02-05, 05:19 PM
OK...This is what I have when adding a new checkbox...Please let me know if I am going about this the wrong way...Thanks in advance...



Dim temp As New CheckBox
temp.Location = New System.Drawing.Point(515, 22 + (52 * (intTotalLineItems - 1)))
temp.Size = New Size(66, 17)
temp.Text = "Remove"
temp.Name = "chkRem" & intTotalLineItems.ToString
temp.TextAlign = ContentAlignment.MiddleLeft
temp.Click += New System.EventHandler(Me.chkRem1_CheckedChanged)
Me.pnlQuotes.Controls.Add(temp)


It gives me 2 errors on the "temp.click +=" line...It says:


'Public Event Click(sender as object, e as System.EventArgs)' is an event, and cannot be called directly. Use a 'RaiseEvent' statement to raise an event.

And:


'System.EventHandler' is a delegate type and requires a single 'addressof' ecpression as the only argument to the constructor

~Ray

Bobby C. Jones
2007-02-05, 05:51 PM
Sorry Ray,
I gave you the C# syntax. The easiest thing to do is to let the forms editor do the work for you. I created a form with three check boxes. I selected the first box and in the events tab of the Properties palette I double clicked the Click event to create an empty event handler. Then I selected the other boxes and assigned their Click event to the existing handler.

When you open your code window you'll see how VB handles delegates, a bit differently than C#.

Ogre
2007-02-05, 05:56 PM
Sorry Ray,
I gave you the C# syntax. The easiest thing to do is to let the forms editor do the work for you. I created a form with three check boxes. I selected the first box and in the events tab of the Properties palette I double clicked the Click event to create an empty event handler. Then I selected the other boxes and assigned their Click event to the existing handler.

When you open your code window you'll see how VB handles delegates, a bit differently than C#.
I see what you are saying, but I am adding a new control in mid program...I need to add the event to the checkbox...I have a feeling it is possible...Thanks for the help...

Bobby C. Jones
2007-02-05, 06:11 PM
I see what you are saying, but I am adding a new control in mid program...I need to add the event to the checkbox...I have a feeling it is possible...Thanks for the help...
Ray,
Now I see. You'll need to look into the AddHandler statement.

Ogre
2007-02-05, 07:23 PM
Ray,
Now I see. You'll need to look into the AddHandler statement.
IT WORKS!!!!!!!!! GREATNESS!!!! Thanks a lot for the help!!!!!!

Bobby C. Jones
2007-02-05, 07:53 PM
IT WORKS!!!!!!!!! GREATNESS!!!! Thanks a lot for the help!!!!!!I'm glad it worked :-)