sureshcd10 Posted March 5, 2004 Posted March 5, 2004 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 .. Quote ima
georgepatotk Posted March 5, 2004 Posted March 5, 2004 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 Quote George C.K. Low
ballisticnylon Posted March 5, 2004 Posted March 5, 2004 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 Quote "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
sureshcd10 Posted March 13, 2004 Author Posted March 13, 2004 Thank u very much for your help. Quote ima
sureshcd10 Posted March 14, 2004 Author Posted March 14, 2004 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? Quote ima
Denaes Posted March 14, 2004 Posted March 14, 2004 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? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.