just started on VB, hope you can help me.
I'm doing a project about a game called TFT, where each item is made with two components. My project consists of 9 buttons, and by pressing two of them (could be the same one), I want to be able to get the specific item (as a msgbox).
Don't know if there's a better way to do it but I'm open to suggestions!
Here's an example that changes the "Check" button to Green when Button3 is selected, followed by Button7 being selected, otherwise it changes the "Check" button to Red.
The buttons are arranged in a standard keypad grid:
Button1 Button2 Button3
Button4 Button5 Button6
Button7 Button8 Button9
Code:
Public Class Form1
Private FirstButton As Button = Nothing
Private SecondButton As Button = Nothing
Private Buttons As List(Of Button)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Buttons = New List(Of Button)(
{Button1, Button2, Button3,
Button4, Button5, Button6,
Button7, Button8, Button9})
Buttons.ForEach(Sub(b) b.Text = "")
End Sub
Private Sub AllButtons_Click(sender As Object, e As EventArgs) _
Handles Button1.Click, Button2.Click, Button3.Click,
Button4.Click, Button5.Click, Button6.Click,
Button7.Click, Button8.Click, Button9.Click
Dim curButton As Button = DirectCast(sender, Button)
If IsNothing(FirstButton) Then
FirstButton = curButton
FirstButton.Text = "1"
ElseIf IsNothing(SecondButton) Then
SecondButton = curButton
If curButton Is FirstButton Then
curButton.Text = "1, 2"
Else
SecondButton.Text = "2"
End If
Else
FirstButton.Text = ""
SecondButton.Text = ""
FirstButton = curButton
FirstButton.Text = "1"
SecondButton = Nothing
End If
End Sub
Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
If FirstButton Is Button3 AndAlso SecondButton Is Button7 Then
btnCheck.BackColor = Color.Green
Else
btnCheck.BackColor = Color.Red
End If
End Sub
End Class
Running example:
Related
I have a Form with a Panel containig 5 RadioButton
I've only handled the Click event of all RadioButtons.
When I use arrow keys to move from a RadioButton to another, the Click event is raised at each move.
Is it possible to only Select the RadioButton and Click it using SpaceBar?
I would you use the Form.KeyDown event of the parent form and do something like this:
Private Sub Form1_KeyPress(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
REM Check if space is the button pressed
If e.KeyCode = Keys.Space Then
REM See if a Radio Button has focus to know which was selected
If RadioButton1.ContainsFocus Then
RunMyCode(RadioButton1)
ElseIf RadioButton2.ContainsFocus Then
RunMyCode(RadioButton2)
End If
End If
End Sub
''' <summary>
''' Method which executes the code you want run when a radio button is selected
''' </summary>
''' <param name="rdButtona"></param>
Private Sub RunMyCode(ByRef rdButtona As RadioButton)
REM Check which radio button was selected and execute the apprpriate code
If rdButtona.Equals(RadioButton1) Then
MsgBox("Radio Button 1")
ElseIf rdButtona.Equals(RadioButton2) Then
MsgBox("Radio Button 2")
End If
End Sub
To prevent the RadioButton from being checked, handle the RadioButton.Click event and uncheck the RadioButton using radioButton1.checked = false. If you handle the RadioButton.CheckChanged event, radioButton1.checked = false will raise another CheckChanged event which we don't need. I would also do something there to show which radio button has been selected. In My example I change the text color but you can do whatever you want.
Private Sub RadioButton2_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton2.CheckedChanged
RadioButton2.Checked = False
ResetColors()
RadioButton2.ForeColor = Color.DarkRed
End Sub
Private Sub RadioButton3_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton3.CheckedChanged
RadioButton3.Checked = False
ResetColors()
RadioButton3.ForeColor = Color.DarkRed
End Sub
Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged
RadioButton1.Checked = False
ResetColors()
RadioButton1.ForeColor = Color.DarkRed
End Sub
Private Sub ResetColors()
RadioButton2.ForeColor = Color.Black
RadioButton1.ForeColor = Color.Black
RadioButton3.ForeColor = Color.Black
End Sub
Of course the code could be simplified, but this is the general idea.
The following code runs RadioButtons Click Event only on mouse click (or on spacebar press) and not on arrow keys press.
It also gives focus to RadioButton selected with Arrow Key
Dim HandleRBtnClick As Boolean = True
Private Sub RadioButtons_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles RadioButton1.Click, RadioButton2.Click, RadioButton3.Click, RadioButton4.Click
If Not HandleRBtnClick Then
With CType(sender, RadioButton)
.Checked = False
.Focus()
End With
Exit Sub
End If
'Code for RadioButton Checked (on mouse click or spacebar pressed)
Me.TextBox1.Text = CType(sender, RadioButton).Name
End Sub
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, keyData As Keys) As Boolean
If keyData = Keys.Left OrElse
keyData = Keys.Right OrElse
keyData = Keys.Up OrElse
keyData = Keys.Down Then
HandleRBtnClick = False
Else
HandleRBtnClick = True
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Okay, I am currently have an app where I can draw on it. I use radio buttons to select the color and drawing size of the "Pen" for the drawing. I would like to get rid of these radio buttons and use a MenuStrip on a MDI form to affect the color and size of the pen on a new child form within the MDI form.
Currently, this is what I have for the form that I can draw on that includes the radio buttons.
Public Class Form1
Private shouldPaint As Boolean = False
Dim paintColor As Color
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.MdiParent = ParentMdiForm
'Color Radio Buttons
Me.redRadio.Tag = Color.Red
Me.blueRadio.Tag = Color.Blue
Me.greenRadio.Tag = Color.Green
Me.blackRadio.Tag = Color.Black
Me.blackRadio.Checked = True
'Size Radio Buttons
Me.smallRadio.Checked = True
End Sub
'Draw while mouse button is pressed
Private Sub Painter_MouseDown(sender As Object, e As MouseEventArgs) Handles MyBase.MouseDown
shouldPaint = True
End Sub
'Stop drawing when mouse button is not pressed
Private Sub Painter_MouseUp(Sender As Object, e As MouseEventArgs) Handles MyBase.MouseUp
shouldPaint = False
End Sub
'Change the size of the pen
Private Sub Painter_MouseMove(sender As Object, e As MouseEventArgs) Handles MyBase.MouseMove
If (shouldPaint) Then
If smallRadio.Checked = True Then
Using g As Graphics = CreateGraphics()
g.FillEllipse(New SolidBrush(paintColor), e.X, e.Y, 4, 4)
End Using
ElseIf mediumRadio.Checked = True Then
Using g As Graphics = CreateGraphics()
g.FillEllipse(New SolidBrush(paintColor), e.X, e.Y, 8, 8)
End Using
ElseIf largeRadio.Checked = True Then
Using g As Graphics = CreateGraphics()
g.FillEllipse(New SolidBrush(paintColor), e.X, e.Y, 12, 12)
End Using
End If
End If
End Sub
Private Sub RadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles redRadio.CheckedChanged, blueRadio.CheckedChanged, greenRadio.CheckedChanged, blackRadio.CheckedChanged
If CType(sender, RadioButton).Checked = True Then
paintColor = CType(CType(sender, RadioButton).Tag, Color)
End If
End Sub
Private Sub SizeRadioButton_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles smallRadio.CheckedChanged, mediumRadio.CheckedChanged, largeRadio.CheckedChanged
If CType(sender, RadioButton).Checked = True Then
End If
End Sub
End Class
My question is, how do I use the menustrip to select the size for my drawings? I figured out how to do it with the color but I cannot figure out the size. I am just not understanding how to code this.
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
At the moment, I have a button that sends a value to another form and displays result in a label. The problem is, I have 20 buttons that are labeled a to w that need to be coded and I am stumped as to how I can pass values from multiple buttons. Would it be a case statement in the form being passed to? I am a new user to VB.Net and still finding my way so any help would be gratefully received. I have included code sample for the first button 'A'. Thanks
frmMain
Private Sub btnA_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles btnA.MouseDown
If (e.Button = MouseButtons.Right) Then
'Dim curButton As Button = DirectCast(sender, Button)
'frmRacks.buttonName = curButton.Name 'Dynamic alternative to frmRacks.buttonName = "A"
frmRacks.buttonName = "A"
frmRacks.Show()
ElseIf (e.Button = MouseButtons.Left) Then
MessageBox.Show("To be coded")
End If
End Sub
frmRacks
Public Class frmRacks
Public buttonName As String
Private Sub racksfrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblRacks.Text = buttonName
End Sub
EDIT: New project
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim button1 As Button = New Button
Dim button2 As Button = New Button
Dim button3 As Button = New Button
With button1
.Name = "button1"
.Left = 0
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button1
End With
With button2
.Name = "button2"
.Left = 100
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button2
End With
With button3
.Name = "button3"
.Left = 200
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button3
End With
Me.Controls.Add(button1)
Me.Controls.Add(button2)
Me.Controls.Add(button3)
End Sub
Private Sub btn_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim curButton As Button = DirectCast(sender, Button)
Dim curButtonName As String = curButton.Name 'This string would change on account of the button you have clicked
Form2.buttonName = curButtonName
Form2.Show()
'MessageBox.Show("You clicked the button called " & curButtonName.ToUpper)
End Sub
End Class
Public Class Form2
Public buttonName As String
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
lblRacks.Text = buttonName
End Sub
End Class
Here you have a sample code which hopefully will help you to get clearer ideas:
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim button1 As Button = New Button
Dim button2 As Button = New Button
Dim button3 As Button = New Button
With button1
.Name = "button1"
.Left = 0
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button1
End With
With button2
.Name = "button2"
.Left = 100
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button2
End With
With button3
.Name = "button3"
.Left = 200
AddHandler .MouseDown, AddressOf btn_MouseDown
'Add remaining properties for button3
End With
Me.Controls.Add(button1)
Me.Controls.Add(button2)
Me.Controls.Add(button3)
End Sub
Private Sub btn_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Dim curButton As Button = DirectCast(sender, Button)
Dim curButtonName As String = curButton.Name 'This string would change on account of the button you have clicked
MessageBox.Show("You clicked the button called " & curButtonName.ToUpper)
End Sub
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