indexing button in vb.net 2010 - vb.net

my code is as follows:
Private Sub btn1_Click(sender As System.Object, e As System.EventArgs) Handles btn1.Click, btn2.Click, btn3.Click
Dim objBtn() As Object = {btn1, btn2, btn3}
Dim btn As Button
With objBtn
btn = CType(objBtn(x), Button)
If btn.FlatStyle = FlatStyle.Standard Then
btn.FlatStyle = FlatStyle.Flat
btn.FlatAppearance.BorderColor = Color.OrangeRed
Else
btn.FlatStyle = FlatStyle.Standard
End If
End With
End Sub
What must i do for the program to control the value of x automatically? that is, suppose i click on btn1, value of x must become 0; if i click on btn2, value of x must become 1 and so on. Thank you.

This will do what you want. :D
x = objBtn.IndexOf(sender)

I think you are trying to (badly) implement a NumericUpDown control.
http://msdn.microsoft.com/en-us/library/729xt55s.aspx
Jus drag&drop it into your Form, maybe set its properties, and it will be ready to go.

It looks like you want to set the state of one button differently from the others in the set.
Here's code to help you to do that.
Private Sub btn1_Click(sender As System.Object, e As System.EventArgs) Handles _
btn1.Click, btn2.Click, btn3.Click
Dim buttons() As Button = {btn1, btn2, btn3}
For Each btn in buttons
If btn Is sender Then
btn.FlatStyle = FlatStyle.Standard
Else
btn.FlatStyle = FlatStyle.Flat
btn.FlatAppearance.BorderColor = Color.OrangeRed
End If
Next
End Sub

Related

How can get the correct Handler of an object?

I am making an application that requires generating panels dynamically, and in turn that each panel generated two events, one with left click and another with right mouse click.
The right click is the one that gives me trouble since I have not been able to call a Handler that I have put temporarily in the event of the left click, but now that I see that it works, I want to pass it to the ToolStripMenuItem event, but when it enters event, the sender takes ownership of the ToolStripMenuItem and in this case you would need the property "System.Windows.Forms.Panel" in order to work on the Panel object.
I am not sure if I am doing it correctly, can you support me with any idea how to do it?
Annex the code of what I have developed so far
Public Class Form1
Dim pb, pbdoors As New Panel
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pos As Int32 = 20
Dim contador As Int16 = 1
For i As Int16 = 1 To 3
Dim pb As New Panel With
{
.Width = 120,
.Height = 460,
.Top = 10,
.Left = 10,
.Name = "Panel" & contador,
.Location = New Point(pos, 20)
}
AddHandler pb.Click, AddressOf myClickHandler_b
Me.Panel1.Controls.Add(pb)
pb.BringToFront()
pos = pos + 120
contador = contador + 1
Next
End Sub
End Class
Public Sub myClickHandler_b(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim pos As Integer = Val(TextBox38.Text)
Dim clickedLabel As Panel = DirectCast(sender, Panel)
clickedLabel.Location = New Point((clickedLabel.Location.X + 120), clickedLabel.Location.Y)
TextBox38.Text = pos
End Sub
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
myClickHandler_b(sender, e)
End Sub
In order to recognize which mouse button is clicked you should use MouseClick as event handler.
The piece of code starting with and working on that is: “AddHandler pb.MouseClick……….”
I hope this might help you:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim pos As Int32 = 20
Dim contador As Int16 = 1
For i As Int16 = 1 To 3
Dim pb As New Panel With
{
.Width = 120,
.Height = 460,
.Top = 10,
.Left = 10,
.Name = "Panel" & contador,
.Location = New Point(pos, 20)
}
AddHandler pb.MouseClick, Sub(senderO As Object, eObj As MouseEventArgs)
If eObj.Button = MouseButtons.Left Then
'Do your tasks here
MsgBox("Left button clicked")
ElseIf eObj.Button = MouseButtons.Right Then
'Do your tasks here
MsgBox("Right button clicked")
End If
End Sub
Me.Panel1.Controls.Add(pb)
pb.BringToFront()
pos = pos + 120
contador = contador + 1
Next
End Sub
Winforms only provides a single event (Click) for both mouse buttons. You need to check (and cast, given that signature) the event arguments to know when you have a right click:
Public Sub myClickHandler_b(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim mouseevent As MouseEventArgs = TryCast(e, MouseEventArgs)
If mouseevent IsNot Nothing AndAlso mouseevent.Button = MouseButtons.Right Then
RightClick(TryCast(sender, Panel))
Exit Sub
End If
'Left Click
'Ugh. Val() is not your friend.
Dim pos As Integer = Val(TextBox38.Text)
Dim clickedLabel As Control = DirectCast(sender, Control)
clickedLabel.Location = New Point((clickedLabel.Location.X + 120), clickedLabel.Location.Y)
TextBox38.Text = pos
End Sub
Public Sub RightClick(source As Panel)
End Sub
Now for the second part. In the ToolStripMenuItem1_Click() method, if you have several dynamic panels on your form, how is the method supposed to know which panel it's working with? You need something in this code knows that information, and uses it for the sender argument. Additionally, given the new handling for left vs right clicks, you also need to think carefully about how that will spill over into the click handler.
So ToolStripMenuItem1_Click() should look something (but probably not exactly!) like this:
Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
myClickHandler_b(pb, Nothing)
End Sub

How to disable and get name for control in FlowLayoutPanel from ContextMenuStrip in VB.Net

My program contains buttons in a FlowLayoutPanel.
I want to disable any button when right click on it and click "Disable" in the ContextMenuStrip.
My code is:
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To 30
Dim btn As New Button
btn.Name = i
btn.Text = i
btn.ContextMenuStrip = ContextMenuStrip1
FlowLayoutPanel1.Controls.Add(btn)
Next
End Sub
End Class
declare a public varibale for keeping a control
public ctrl as button = nothing
you can create a right click by putting this code on mouse down...
If e.Button <> Windows.Forms.MouseButtons.Right Then Return
Dim cms = New ContextMenuStrip
ctrl = sender
Dim item1 = cms.Items.Add("Disable")
item1.Tag = 1
AddHandler item1.Click, AddressOf Disable
end if
and in the diable sub you can code like this...
Private Sub Disable()
ctrl.enabled = false
End Sub

Toggle button color by calling function in vb.net

I'm new to vb.net. I have 20 buttons in one form. When I click any of one button, it color should be changed.
I can code for all button like following. But I need a function, when i call that function, the color should be changed. Please help me and give me full coding
Private Sub btnR1X1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnR1X1.Click
If (btnR1X1.BackColor = Color.White) Then
btnR1X1.BackColor = Color.Gray
ElseIf (btnR1X1.BackColor = Color.Gray) Then
btnR1X1.BackColor = Color.White
End If
End Sub
I have assumed that you are using VB.Net. Assuming that is the case, you should edit your question to remove the vb6 tag.
You can write a function that will toggle the BackColor of any control.
Private Sub ToggleColor(ctrl As Control)
If ctrl.BackColor = Color.White Then ctrl.BackColor = Color.Gray Else ctrl.BackColor = Color.White
End Sub
You can call that function from a Button's click handler like this
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ToggleColor(CType(sender, Control))
End Sub
However, if all you want to do when any of the buttons is clicked is to toggle the BackColor, you can use a single event handler for the click event of every button.
Private Sub Buttons_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click, Button3.Click, Button4.Click 'etc
Dim ctrl as Control = CType(sender, Control)
If ctrl.BackColor = Color.White Then ctrl.BackColor = Color.Gray Else ctrl.BackColor = Color.White
End Sub

How to handle a variable amount of buttons clicked?

I have a pretty basic problem with button clicks in VB.Net I cannot seem to figure out.
First, I am creating a variable amount of buttons and adding them to the parent form.
Private Sub CreateUIObjects()
For i As Integer = 1 To NumberOfButtons
Dim button As Button = New Button()
Me.Controls.Add(button)
Next
End Sub
I know it is possible to handle a fixed amount of buttons clicked with the following code
Private Sub Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Handles Button1.Click, Button2.Click, Button3.Click '... And so on
Dim b As Button = CType(sender, Button)
End Sub
But what do I do with not 3, but a variable amount of buttons?
Some code to experiment with
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
CreateUIObjects(3)
End Sub
Dim myButtonNames As String = "foobar"
Private Sub myButtonsClick(sender As Object, e As EventArgs)
Dim b As Button = DirectCast(sender, Button)
Debug.WriteLine(b.Name)
End Sub
Private Sub CreateUIObjects(NumberOfButtons As Integer)
Static ct As Integer = 0
For i As Integer = 1 To NumberOfButtons
ct += 1
Dim btn As Button = New Button()
btn.Name = myButtonNames & ct.ToString
btn.Text = btn.Name
btn.Location = New Point(ct * 20, ct * 20)
AddHandler btn.Click, AddressOf myButtonsClick
Me.Controls.Add(btn)
Next
End Sub
You can use something like this
AddHandler Button1.Click, AddressOf Button_Click
take a look http://msdn.microsoft.com/en-us/library/ms172877.aspx

Accessing each button's properties after they are dynamically generated in vb.net?

I have used the following code to generate buttons dynamically. I want to know how to code in such a way that if i click one button, there should be some change done to some other button in the same form. Since all the buttons are generated in the loop, i do not know how to call one button elsewhere in the code.
Private Sub random2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
For i = 1 To 16
Dim btn As New Button
AddHandler btn.Click, AddressOf ClickMe
btn.Width = 23
btn.Height = 23
btn.Text = ""
btn.Tag = i
btn.Name = i
btn.Name = "Button" & i
flp.Controls.Add(btn) 'flp stand for flow layout panel
Next
End Sub
Private Sub ClickMe(ByVal Sender As Object, ByVal e As EventArgs)
Dim btn As Button
btn = CType(Sender, Button)
dim str as string = btn.tag
MsgBox(str)
End Sub
End Class
You have added all your dynamically created buttons to the FlowLayoutPanel control collection.
You will find them there with syntax like this
Dim btn As Button = TryCast(flp.Controls("name"), Button)
if btn IsNot Nothing then
btn.Caption = "New Text"
....
End If
Or if you want a button at a specific index
Dim btn As Button = TryCast(flp.Controls(index), Button)