I have an issue with my radio buttons messagebox vb.net - vb.net

If RB_Male.Checked Or RB_Female.Checked = False Then
MessageBox.Show("Please select your gender")
ElseIf RB_Male.Checked = False Or RB_Female.Checked = True Then
ElseIf RB_Male.Checked = True Or RB_Female.Checked = False Then
End If
I have an issue as in, I want my error message to only pop up when both radio buttons are not selected. The code above allows the messagebox to pop up even when one of the radio buttons are selected which I don't want happening. Any help will be greatly appreciated.

To check if neither button is checked:
If Not (RB_Male.Checked AndAlso RB_Female.Checked) Then

If (NOT RB_Male.Checked) ANDALSO (NOT RB_Female.Checked) Then
MessageBox.Show("Please select your gender")
ElseIf RB_Male.Checked = False Or RB_Female.Checked = True Then
ElseIf RB_Male.Checked = True Or RB_Female.Checked = False Then
End If

Related

How to create a button with several commands (hangman game) in visual basic 2019

I want to make a hangman game in visual basic 2019. I have a button for each letter, if it's the right letter it will appear on the unknown word but if it's the worng letter I want to click in the button and the hangman body part will appear. My problem is to program which body part of him will appear.
I'm a really begginer in visual basic, so please help me with easy terms that I will understand. thank you.
my body parts are pictureboxes. the first one is the hang thing with the head. the second picture box is exactly the same plus th belly part. the third one is the same as the previous one plus onde arm, ...
I was trying to put all in the same button like
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If CABEÇA.Visible = True Then
TRONCO.Visible = True
CABEÇA.Visible = False
Else
CABEÇA.Visible = True
End If
If TRONCO.Visible = True Then
TRONCO.Visible = False
BESQ.Visible = True
Else TRONCO.Visible = True
End If
If BESQ.Visible = True Then
BESQ.Visible = False
BDIR.Visible = True
End If
If BDIR.Visible = True Then
BDIR.Visible = False
PESQ.Visible = True
End If
If PESQ.Visible = True Then
PESQ.Visible = False
PDIR.Visible = True
MessageBox.Show("you lost")
End If
End Sub
cabeça=head; tronco=belly, besq=left arm, bdir= right arm, pesq=left leg, pdir= right leg
this scheme is not possible, off course, because or it will give the head or it will pass trhough the code and give me the last image.
Is there any way of making each image appear once I click the button (first click-first image, second clicks-second one, third click-third one)
If not, I'm open to different suggestions.
Set all of your pictures to visible=false at the beginning of the program and add the following to your button event:
If cabeça.Visible = False Then
cabeça.Visible = True
ElseIf tronco.Visible = False Then
tronco.Visible = True
ElseIf besq.Visible = False Then
besq.Visible = True
ElseIf bdir.Visible = False Then
bdir.Visible = True
ElseIf pesq.Visible = False Then
pesq.Visible = True
ElseIf pdir.Visible = False Then
pdir.Visible = True
MsgBox("You lose!")
Else
MsgBox("Game Over")
End If

Setting Form Property .AllowEdits on Subforms does not seem to work

I am using the following code as event handler for the button cmd_Edit on my main form:
Private Sub cmd_Edit_Click()
If intCanEdit = False Then
If MsgBox("Sollen vorhandene Prozeduren verändert werden ?", vbYesNo, "Frage") = vbNo Then Exit Sub
Me.AllowEdits = True
Me.AllowAdditions = True
Dim sbfrm As Control
For Each sbfrm In Me.Controls
With sbfrm
Select Case .ControlType
Case acSubform
.Form.AllowEdits = True
.Form.AllowAdditions = True
End Select
End With
Next sbfrm
intCanEdit = True
Else
Me.AllowEdits = False
Me.AllowAdditions = False
For Each sbfrm In Me.Controls
With sbfrm
Select Case .ControlType
Case acSubform
**.Form.AllowEdits = False**
.Form.AllowAdditions = False
End Select
End With
Next sbfrm
intCanEdit = False
End If
cmd_Edit.Caption = IIf(intCanEdit, "Click to Save", "Click to Edit")
cmd_Edit.BackColor = IIf(intCanEdit, vbRed, vbGreen)
End Sub
The form loads with intCanEdit set to False. When i click the button once (setting it to true) everything works as expected, when i click it again (setting it to false again) i get an error (Runtime error 2455) with the Debugger sending me to the line i marked with asterisks in the above code.
Does anybody have an idea why i can set the property to True with my code, but get an error when i try to set the same property back to False? :(

setting radio button checked when form loading (vb net)?

I want to select one of my RadioButton from a different Group Boxes (only one radio button needs to be check from all radio buttons) programmatically (when the form is loaded).
I'm loading datagridview value to a variable.
Now, I want to select the radio button corresponding to the value in the variable. .
I tried simple code using else if and select case. But both are not working?
{ Dim SpecimenName As String = frmMain.DataGridView1.Rows(0).Cells(4).Value.ToString
If SpecimenName = "" Then
'RadioButton1.AutoCheck()
ElseIf SpecimenName Like "Michanical" Then
RadioButton1.PerformClick()
ElseIf SpecimenName Like "Manage" Then
RadioButton2.PerformClick()
ElseIf SpecimenName Like "Civil" Then
RadioButton3.Checked = True
ElseIf SpecimenName Like "Electronics" Then
RadioButton4.Checked = True
ElseIf SpecimenName Like "Supply" Then
RadioButton5.Checked = True
ElseIf SpecimenName Like "Landscaping" Then
RadioButton6.Checked = True
ElseIf SpecimenName Like "P&D" Or SpecimenName Like "Project" Then
RadioButton7.Checked = True
End If}
Set RadioButton1.Checked = True in the Form_Shown event.
Form_Load will not work if that is where you are setting the checked values.

Visual Basic "Do While" loop

I am coding a basic program that allows users to calculate useful data on a shape (2d and 3d) based on information such as radius length, side length, height, etc.
I would like to create a settings MENU that allows users to set how many decimal places they would like the answer to go to, so what I did was
Private Sub btnSettings_Click(sender As Object, e As EventArgs) Handles btnSettings.Click
If btnSettings.Text = "Settings" Then
btnSettings.Text = "Back"
ElseIf btnSettings.Text = "Back" Then
btnSettings.Text = "Settings"
End If
Do While btnSettings.Text = "Back"
'makes IO elements invisible
lblEnter.Visible = False
lblEnter2.Visible = False
lblData1.Visible = False
lblData2.Visible = False
lblData3.Visible = False
lblAnswer1.Visible = False
lblAnswer2.Visible = False
lblAnswer3.Visible = False
txtEnter.Visible = False
txtEnter2.Visible = False
btnClearTxt1.Visible = False
lblEnter3.Visible = False
txtEnter3.Visible = False
chkBox.Visible = False
btnCalculate.Visible = False
btnClear.Visible = False
'Makes shape selection elements invisible
picCircle.Visible = False
picSquare.Visible = False
picTriangle.Visible = False
rdoCircle.Visible = False
rdoSquare.Visible = False
rdoTriangle.Visible = False
btn3D.Visible = False
'Changes texts on necessary elements
lblSelectShape.Text = "Settings"
Loop
End Sub
As you can see, when "btnSettings" shows the text "Settings", the elements on the screen are VISIBLE to the user, however as soon as "btnSettings" changes text to "Back" (indicating the user is inside the settings menu) all the elements on the screen disappear, making room for the elements the settings menu will have. However, while debugging the program whenever I hit the settings button the program crashes.
Any help? Thanks
Use a Select Case for this:
Select Case shapeType
Case shapeType.Circle
'logic for this
'...
End Select

ActiveX List Boxes will not "Size and Move" with their parent cells

I'm new at VBA so sorry in advance if this is a silly question. I have a Worksheet with ActiveX List boxes. The worksheet also has Toggle Switches. The toggle switches are set up to Hide Rows and ActiveX boxes when not depressed and Show Rows and ActiveX boxes when depressed. I'd like to save the file with all of the Toggle switches not depressed so that the user can un-hide only the rows and boxes that they need. Everything works properly until I save the file with all rows hidden. After the save all of the boxes change locations. I've tried setting the boxes to "Move and Size with cell", "Move but don't size with cell", and "Don't more or Size with cell" in the preferences. The same thing happens with all options. Below is my toggle switch code. Is there something in there causing this to happen?
Private Sub ToggleButton1_Click()
If ToggleButton1.Value = True Then
'This area contains the things you want to happen
'when the toggle button is not depressed
Range("101:183").EntireRow.Hidden = False
Sheet1.Range("94:144").EntireRow.Hidden = False
'This hides the listboxes since they can not move and
'size with cells
Sheet11.OLEObjects("ListBox1").Visible = True
Sheet11.OLEObjects("ListBox2").Visible = True
Sheet11.OLEObjects("ListBox3").Visible = True
Sheet11.OLEObjects("ListBox4").Visible = True
Sheet11.OLEObjects("ListBox5").Visible = True
Sheet11.OLEObjects("ListBox6").Visible = True
Sheet11.OLEObjects("ListBox7").Visible = True
Sheet11.OLEObjects("ListBox8").Visible = True
Sheet11.OLEObjects("ListBox9").Visible = True
Sheet11.OLEObjects("ListBox10").Visible = True
Sheet11.OLEObjects("ListBox11").Visible = True
Sheet11.OLEObjects("ListBox12").Visible = True
Sheet11.OLEObjects("ListBox13").Visible = True
Sheet11.OLEObjects("ListBox14").Visible = True
Sheet11.OLEObjects("ListBox15").Visible = True
Sheet11.OLEObjects("ListBox16").Visible = True
Sheet11.OLEObjects("ListBox17").Visible = True
Sheet11.OLEObjects("ListBox18").Visible = True
Else
'This area contains the things you want to happen
'when the toggle button is depressed
Range("101:183").EntireRow.Hidden = True
Sheet1.Range("94:144").EntireRow.Hidden = True
Sheet11.OLEObjects("ListBox1").Visible = False
Sheet11.OLEObjects("ListBox2").Visible = False
Sheet11.OLEObjects("ListBox3").Visible = False
Sheet11.OLEObjects("ListBox4").Visible = False
Sheet11.OLEObjects("ListBox5").Visible = False
Sheet11.OLEObjects("ListBox6").Visible = False
Sheet11.OLEObjects("ListBox7").Visible = False
Sheet11.OLEObjects("ListBox8").Visible = False
Sheet11.OLEObjects("ListBox9").Visible = False
Sheet11.OLEObjects("ListBox10").Visible = False
Sheet11.OLEObjects("ListBox11").Visible = False
Sheet11.OLEObjects("ListBox12").Visible = False
Sheet11.OLEObjects("ListBox13").Visible = False
Sheet11.OLEObjects("ListBox14").Visible = False
Sheet11.OLEObjects("ListBox15").Visible = False
Sheet11.OLEObjects("ListBox16").Visible = False
Sheet11.OLEObjects("ListBox17").Visible = False
Sheet11.OLEObjects("ListBox18").Visible = False
End If
End Sub
I know this isn't the answer to your question (I haven't even looked at it yet), but i just felt like giving you this code, this is the exact code you provided and will function the same way, just looks a tiny bit clearer (actually as it also removes the if statement it prolly even performs at like 1/1000000 of a millisecond faster also =D)
Private Sub ToggleButton1_Click()
Dim boolToggleValue As Boolean
Dim i As Integer
boolToggleValue = ToggleButton1.Value
'This area contains the things you want to happen
'when the toggle button is not depressed
Range("101:183").EntireRow.Hidden = Not boolToggleValue
Sheet1.Range("94:144").EntireRow.Hidden = Not boolToggleValue
'This hides the listboxes since they can not move and
'size with cells
With Sheet11
For i = 1 To 18
.OLEObjects("ListBox" & i).Visible = boolToggleValue
Next i
End With
End Sub