Multiple controls to call the same function

  • Thread starter Thread starter Rob Meade
  • Start date Start date
R

Rob Meade

Hi all,

Hoping someone can advise me on this one.

I have a form with about 10 text boxes. A user will be entering numbers in
each box, I have written a small function that will correct the format of
the numbers, so if a user enters 5 it becomes 5.00, if they enter 5.5 it
becomes 5.50 and so on.

At present I have this as a test and am calling it only on the one text box,
but I want to be able to call it from all of the boxes, I'm using the .Leave
method to run the function, but it has the specific text box in the Handles
part of the code. Whilst I appreciate that you can have multiple controls
listed after Handles etc, I thought there was probably a better way.

Any info appreciated,

Regards

Rob
 
The Leave eventhandler has a sender object. You can cast this to TextBox in
order to get the instance that raised the event, use this for further
processing.

Chris
 
Have you thought about creating this as a custom control and adding the
function within the custom control?
 
...
Have you thought about creating this as a custom control and adding the
function within the custom control?

Hello Brian, thanks for the reply.

I must admit that I hadn't no, I've only used custom controls in the web
stuff I've done, and then typically only for template stuff across our
applications.

Not fully in the know here see, especially with the Windows side of stuff.

Regards

Rob
 
...
The Leave eventhandler has a sender object. You can cast this to TextBox in
order to get the instance that raised the event, use this for further
processing.

Hello Christopher, thanks for your reply.

This sounds quite interesting, would this then though be applicable to 'all'
text boxes on my form and not just the ones I need to be validated etc.

The same problem actually has arisen in a new problem too as I want a button
on the form that will reset it, this is nice and easy in a web page, I've
not seen anything quite so simple yet for the Windows side of stuff, I guess
I would need something similar to the above to 'clear' all of the relevent
text boxes?

Regards

Rob
 
...
The Leave eventhandler has a sender object. You can cast this to TextBox in
order to get the instance that raised the event, use this for further
processing.

Hello again Christopher,

I've had a little go, but dont fully understand your 'cast' referrence, seem
to be having some difficulties.

Regarding the clearing of the form I managed to find this example online
which has worked:

Dim ctr As Control
For Each ctr In Me.Controls
If TypeOf ctr Is TextBox Then
DirectCast(ctr, TextBox).Text = ""
End If
Next ctr

I noted the word 'cast' there also and wondered if something similar would
work for my initial problem, however, I dont understand what I need to do at
the end of the line which says Handles... I surely need something after
that but cannot find what I need...something as open ended as TextBox.Leave
didnt work...unless I specify which textbox I cant get it to work, do I need
to specify a 'collection' of textboxes here?

Any further info appreciated,

Regards

Rob
 
By casting the sender object, you can use the same eventhandler for many
instances. You could also use the same event handler for different types of
objects, e.g. Click event on button and TextBox. In the event handler you
would have to test what type of object the sender is before performing any
action.

In C# you explicitly add the event handler to an object, so adding the same
eventhandler to multiple instances is not a problem, I have not idea how you
would do it in VB.NET. If it's not possible, you could create a seperate
function you could call from each individual event handler.

The following C# code illustrates what I mean;

// add event handlers
textBox1.Leave += new EventHandler(TextBox_Leave);
textBox2.Leave += new EventHandler(TextBox_Leave);

private void TextBox_Leave(object sender, EventArgs e)
{
TextBox tb = sender as TextBox; // if it cannot be cast, it will return
null
if(tb != null)
{
// do something with the textbox
}
}


Chris
 
...
By casting the sender object, you can use the same eventhandler for many
instances. You could also use the same event handler for different types of
objects, e.g. Click event on button and TextBox. In the event handler you
would have to test what type of object the sender is before performing any
action.

That sounds like exactly what I want to do!
// add event handlers
textBox1.Leave += new EventHandler(TextBox_Leave);
textBox2.Leave += new EventHandler(TextBox_Leave);

Chris, I have seen some code that had an AddHandler - you think that might
be what I need?

This is a bit of text from the Visual Studio help file..

AddHandler button.Click, AddressOf Me.Button_Clicked

This the same kinda thing?

Thanks for such a prompt response by the way, you help is appreciated.

Regards

Rob
 
Hi Christopher,

I managed to stick this together with your help (wasn't sure where I had to
put the line of code in the end but found the MyForm_Load sub seemed to work
:o)

AddHandler txtHourlyDailyRate.Leave, AddressOf TextBox_Leave
AddHandler txtHoursDaysWorkedPerWeek.Leave, AddressOf TextBox_Leave

Works an absolute charm, many, many thanks for your help.

Regards

Rob
 
Back
Top