Hi,

i got a form with 10 button and i want to get the button name of the mouse click in a string variable

i tried this:

private void Button_Click(object sender, RoutedEventArgs e)
        {
           string buttonName = "";
           if (sender is Button)
           {
               buttonName = ((Button)sender).Name;
               MessageBox.Show(buttonName);
           }
        }

but i get no message;

Also i was thinking something like a OnMouseClick event but i didn't play with the events and handlers too much and i don't know how to use them.

you want to show the name of button or caption/text of button in msgbox when you click it ? please rephrase it .

when i mouse click a button on the form i want to catch it's name in a string variable so everytime i click a button i want that string variable to get the buttons name.

well do like this

'declare the variable 
dim myname as string 
'now use this code at the button click event 
myname = yourbutton.Name.ToString
msgbox(myname)

Regards

Couldn't help but notice there is no "Handles" clause on

private void Button_Click(object sender, RoutedEventArgs e)

so perhaps nothing is happening because the code isn't attached to the button.

Hi,
1st of all, you have to create an array of all buttons, accessible inside a class (or form).
Then use access to them with only one event:

Public Partial Class Form1
	Inherits Form
	'I have put 3 buttons on my form!!
	Private buttons As Button()
	Public Sub New()
		InitializeComponent()

		'now lets create a common code for all:
		buttons = New Button() {button1, button2, button3}
		For i As Integer = 0 To buttons.Length - 1
			buttons(i).Name = "My button " & (i + 1)
			'specify name of the button
			buttons(i).Text = "Pres button " & (i + 1)
			'specify the text on the button
			buttons(i).Click += New EventHandler(AddressOf Buttons_Click)
		Next
	End Sub

	Private Sub Buttons_Click(sender As Object, e As EventArgs)
		Dim button As Button = TryCast(sender, Button)
		If button IsNot Nothing Then
			MessageBox.Show("You pressed " + button.Name)
		End If
	End Sub
End Class

Edited 4 Years Ago by Mitja Bonca: n/a

'declare the variable 
dim myname as string 
'now use this code at the button click event 
myname = yourbutton.Name.ToString
'how do i know which one the 10 buttons i click on 
msgbox(myname)

The variable needs to keep the name of the last pressed button.

So if i press button1,2 and 5 the variable should keep "btn1" after the first press, "btn2" after the second button, and "btn5"after third press and so on.

use a Static inside your Sub.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click, Button2.Click, Button3.Click
        Static btnName As String = "" '// stores .Name of btn.
        With CType(sender, Button)
            If btnName = "" Then MsgBox(.Name) Else MsgBox("previous btn: " & btnName)
            btnName = .Name '// add value to your string "AFTER".
        End With
    End Sub

Edited 4 Years Ago by codeorder: n/a

Here is the solution (actually two of them):

'I have put 3 buttons on my form!!
Private buttons As Button()
Private pressedButton As Button
'OR:
Private _pressedButton As String
Public Sub New()
	InitializeComponent()

	'now lets create a common code for all:
	buttons = New Button() {button1, button2, button3}
	For i As Integer = 0 To buttons.Length - 1
		buttons(i).Name = "My button " & (i + 1)
		'specify name of the button
		buttons(i).Text = "Pres button " & (i + 1)
		'specify the text on the button
		buttons(i).Click += New EventHandler(AddressOf Buttons_Click)
	Next
End Sub

Private Sub Buttons_Click(sender As Object, e As EventArgs)
	Dim button As Button = TryCast(sender, Button)
	If button IsNot Nothing Then
		pressedButton = button
		_pressedButton = button.Name

		'show it here:
		RetreivePressedButton()
	End If
End Sub

Private Sub RetreivePressedButton()
	'you can call this method every time after click on button:
	MessageBox.Show("Pressed button was: " & Convert.ToString(pressedButton.Name) & ". And it still keeps this name in variable")
	'OR:
	MessageBox.Show("Pressed button was: " & _pressedButton & ". And it still keeps this name in variable")
End Sub

Using a common event handler will work as long as the buttons have similar functions. If they do not then to keep track of the last button pressed you should only have to declare a class level string such as

public partial class Form1 : Form
 
    Public lastButton As String
    .
    .
    .
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
        lastButton = sender.Name

Just add the last line to each button handler.

I like Mitja's idea of

Private pressedButton As Button

That way you have access to all of the button info for the last pressed button, not just the button name.

Hi,

i got a form with 10 button and i want to get the button name of the mouse click in a string variable

Think this is what you want to do

Private Sub Button1_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Button1.MouseClick, Button2.MouseClick, Button3.MouseClick, Button4.MouseClick, Button5.MouseClick, Button6.MouseClick, Button7.MouseClick, Button8.MouseClick, Button9.MouseClick, Button10.MouseClick
Dim strName As String = ""
Dim text As Button = sender
strName = text.Name
Label1.Text = strName
End Sub

This question has already been answered. Start a new discussion instead.