Jump to content
Xtreme .Net Talk

HELP ME...I want a "Hello" message to be displayed with out repeating the same


Recommended Posts

Posted

HELP ME...I want a "Hello" message to be displayed with out repeating the same

 

:confused:

I got 5 Buttons placed on a form

 

When I press each button I want a "Hello" message to be displayed with out repeating the same

code behind each Button_Click event.

Plz..help me ..

ima
Posted

Private Sub ShowMessage(ByVal PassedValue)

MessageBox.Show(PassedValue)

End Sub

 

Private Sub Button1_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles Button1.Click

ShowMessage("Button1")

End Sub

 

Private Sub Button2_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles Button2.Click

ShowMessage("Button2")

End Sub

 

Private Sub Button3_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles Button3.Click

ShowMessage("Button3")

End Sub

 

Private Sub Button4_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles Button4.Click

ShowMessage("Button4")

End Sub

 

Private Sub Button5_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles Button5.Click

ShowMessage("Button5")

End Sub

George C.K. Low

Posted
Private Sub ShowMessage(ByVal PassedValue)

MessageBox.Show(PassedValue)

End Sub

 

Private Sub Button1_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles Button1.Click

ShowMessage("Button1")

End Sub

 

Private Sub Button2_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles Button2.Click

ShowMessage("Button2")

End Sub

 

Private Sub Button3_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles Button3.Click

ShowMessage("Button3")

End Sub

 

Private Sub Button4_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles Button4.Click

ShowMessage("Button4")

End Sub

 

Private Sub Button5_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles Button5.Click

ShowMessage("Button5")

End Sub

 

Or...

 

  Private Sub Button1_Click(ByVal sender As System.Object,   
  ByVal e As System.EventArgs) Handles Button1.Click, _
Button2.Click, Button3.Click, Button4.Click, Button5.Click
       Dim clickedButton as Button = DirectCast(sender, Button)
       Messagebox.Show(clickedButton.Text)
   End Sub

"It may be roundly asserted that human ingenuity cannot concoct a cipher which human ingenuity cannot resolve."

 

- Edgar Allan Poe, 1841

 

I long to accomplish great and noble tasks, but it is my chief duty to accomplish humble tasks as though they were great and noble. The world is moved along, not only by the mighty shoves of its heroes, but also by the aggregate of the tiny pushes of each honest worker.

 

- Helen Keller

  • 2 weeks later...
Posted
Private Sub Button1_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles Button1.Click, _

Button2.Click, Button3.Click, Button4.Click, Button5.Click

Dim clickedButton as Button = DirectCast(sender, Button)

Messagebox.Show(clickedButton.Text)

End Sub

 

but if the buttons were placed on different forms.How it is done?

ima
Posted
but if the buttons were placed on different forms.How it is done?

 

Thats a whole 'nother question which is a much different beast than the first.

 

You asked about 5 buttons on the same form.

 

Now you're starting to get into Delegates, event listeners etc.

 

If you're not hardcore into OOP, I'd suggest just using a public subprocedure somewere (like in a module) and just call the procedure from each of the 5 button.click events.

 

   Public Function ShowButtonText(ByVal btn As Button) As String
       Return btn.Text
   End Function

   Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       MsgBox(ShowButtonText(Button1))
   End Sub

 

personally I think in this case, as you presented it, it would just be easier to write one line of code in each of the buttons click events

 

   Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
       MsgBox(sender.text)
   End Sub

 

Every way I can think of doing it differently involves much more code...

 

Anyone have a simpler idea?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...