Multiple Button Handler

  • Thread starter Thread starter Vayse
  • Start date Start date
V

Vayse

I have seven text boxes, txtDay1 to txtDay7.
I have 7 command buttons, btnExpand1 to btnExpand7

When a button is clicked, it sets the height of the text box to 200.

Private Sub btnExpand1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExpand1.Click

Me.txtDisplay1.Height = 200

End Sub

I'd like to have one routine to handle the all clicks. That is, the routine
should be able to tell which button was clicked.

How would I do this?

Thanks

Vayse
 
Use this

In Visual Studio, if you place your cursor at the end of the Sub
declaration and press the space bar, you can enter several events after
the first like this,

Handles btnExpand1.Click, btnExpand2.Click, btnExpand3.click and so on.

This will work as long as the signatures are the same.
 
Dear Vayse,

you could also have a look at AddHandler. With AddHandler you can 'redirect'
events to methods.
You could redirect all you button click event to one method by doing
multiple AddHandler calls.
Note that this method should have a sender parameter in order to determine
which button was clicked.

Michel van den Berg
 
I missed some of your post, you will need this

Select Case Sender.name
Case "btnExpand1"
txtDisplay1.Height = 200
Case "btnExpand2"
txtDisplay2.Height = 200
End Select
 
Vayse said:
I have seven text boxes, txtDay1 to txtDay7.
I have 7 command buttons, btnExpand1 to btnExpand7

When a button is clicked, it sets the height of the text box to 200.

Private Sub btnExpand1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExpand1.Click

Me.txtDisplay1.Height = 200

End Sub

I'd like to have one routine to handle the all clicks. That is, the
routine should be able to tell which button was clicked.


\\\
Private Sub ExpandButton_Click( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles btnExpand1.Click, btnExpand2.Click, ..., btnExpand7.Click
MsgBox(DirectCast(sender, Button).Name)
End Sub
///
 
I missed some of your post, you will need this

Select Case Sender.name
Case "btnExpand1"
txtDisplay1.Height = 200
Case "btnExpand2"
txtDisplay2.Height = 200
End Select

.... which will unfortunately only work with 'Option Strict Off'.
 
... which will unfortunately only work with 'Option Strict Off'.
What can be overcome with

Select Case DirectCast(sender,button).name

:-)

And it answers than as I saw a not yet answered question from you,

The sender is of the type object. You have forever to tell what exact type
of that object is to use it. (not when you need to use members which are
directly deriving from object as "ToString").

An alternative is Ctype (convert Type) however here it is normally done with
DirectCast.

I hope this helps,

Cor
 
Cor Ligthert said:
An alternative is Ctype (convert Type) however here it is normally done
with DirectCast.

Well, it really doesn't matter whether you use 'CType' or 'DirectCast' here.

;-)
 
Thanks guys. Nearly there. Below is the code I have.
I have two more questions

1) Why isn't the button changing height? Its 22 to start with, and stays at
22 afterwards. This happens even if I do
Me.txtEventDay1.Height = 100

txtEventDay1 will stay the same height.
Is there some redraw I should be doing?

2) The form is laid out so that is an event and a note against each day. If
the user clicks expand, I wish to hide the note and expand the event.
txtEvent is above txtNote is each case. So I was planning on moving the
bottom of txtEvent, so that it would stretch and cover the note, like so
Me.Controls("txtEventDay" & txtControl).Bottom = Me.Controls("txtNoteDay" &
txtControl).Bottom
But txtEventX.Bottom is a read only property. What should I do instead?

Thanks
Vayse


'*************************************************************
Private Sub btnExpand1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExpand1.Click, btnExpand2.Click,
btnExpand3.Click

Dim txtControl As String = DirectCast(sender, Button).Name
txtControl = txtControl.Substring(txtControl.Length - 1, 1)

Me.Controls("txtEventDay" & txtControl).Height = 100

End Sub

'*************************************************************
 
Never mind, got it sorted.
I just had to set the text boxes multiline property to true!

Private Sub btnExpand1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnExpand1.Click, btnExpand2.Click,
btnExpand3.Click

Dim txtControl As String = DirectCast(sender, Button).Name

txtControl = txtControl.Substring(txtControl.Length - 1, 1)

Dim iNewHeight As Integer = Me.Controls("txtNoteDay" & txtControl).Bottom -
Me.Controls("txtEventday" & txtControl).Top

Me.Controls("txtEventDay" & txtControl).Height = iNewHeight

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top