Set event for 30 buttons
-
Hi all, I have 30 buttons and 1 textbox. When i click button, button's text will be displayed in textbox. It means i must write event for each button and i have 30 functions for 30 buttons. That's too long. So how could i solve that by 1 function? (it means when 1 click a button, program will detect which button was clicked and get that button's text )
-
Hi all, I have 30 buttons and 1 textbox. When i click button, button's text will be displayed in textbox. It means i must write event for each button and i have 30 functions for 30 buttons. That's too long. So how could i solve that by 1 function? (it means when 1 click a button, program will detect which button was clicked and get that button's text )
ctlqt12 wrote:
It means i must write event for each button and i have 30 functions for 30 buttons.
No, it doesn't. The sender property on an event is the control that raised it. So, write one event that casts that to a Button, grabs the text and sets it to the textbox.
Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog "I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
-
Hi all, I have 30 buttons and 1 textbox. When i click button, button's text will be displayed in textbox. It means i must write event for each button and i have 30 functions for 30 buttons. That's too long. So how could i solve that by 1 function? (it means when 1 click a button, program will detect which button was clicked and get that button's text )
Have you noticed the Handles clause at the end of the method header for a button click??
Private Sub Button1_Click(blah, blah,) Handles Button1.Click
Well, you can tack on as many buttons as you want like this:
Private Sub Button1_Click(blah, blah,) Handles Button1.Click, Button2.Click, Button3.Click, ...
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007 -
Hi all, I have 30 buttons and 1 textbox. When i click button, button's text will be displayed in textbox. It means i must write event for each button and i have 30 functions for 30 buttons. That's too long. So how could i solve that by 1 function? (it means when 1 click a button, program will detect which button was clicked and get that button's text )
-
Hi all, I have 30 buttons and 1 textbox. When i click button, button's text will be displayed in textbox. It means i must write event for each button and i have 30 functions for 30 buttons. That's too long. So how could i solve that by 1 function? (it means when 1 click a button, program will detect which button was clicked and get that button's text )
after tagging the events as Dave Kreskowiak told with the handler detect the particular button dim b a button = ctype(sender,button) select case b.name case "" ..... end select
-
Hi all, I have 30 buttons and 1 textbox. When i click button, button's text will be displayed in textbox. It means i must write event for each button and i have 30 functions for 30 buttons. That's too long. So how could i solve that by 1 function? (it means when 1 click a button, program will detect which button was clicked and get that button's text )
use follwing code Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, ...Button30.Click textbox1.text=sender.text '(or textbox1.text=textbox1.text & sender.text) end sub Hope this helps
Rupesh Kumar Swami Software Engineer, Integrated Solution, Bikaner (India)
-
Hi all, I have 30 buttons and 1 textbox. When i click button, button's text will be displayed in textbox. It means i must write event for each button and i have 30 functions for 30 buttons. That's too long. So how could i solve that by 1 function? (it means when 1 click a button, program will detect which button was clicked and get that button's text )
Hi, In which language are you writing the code for this? vb or vb.net? If in case of classic vb, use buttons as control array and write a single function. if in case of vb.net, write a event as follows :
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button5.Click, Button4.Click, Button3.Click, Button2.Click Dim kBtn As Button kBtn = sender TextBox1.Text = kBtn.Text End Sub
Here you can use the source button's properties using kBtn variable. Thanks, Kiran Kumar