How to make a combo box password - vb.net

How do I make a combo box password or button password? I got this code and it does not work.
Public Class Form1
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.Text = "5" Then
If ComboBox2.Text = "3" Then
If ComboBox3.Text = "2" Then
If ComboBox4.Text = "6" Then
Button1.Enabled = True
End If
End If
End If
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("Dony", MsgBoxStyle.Information, "Dony")
Application.Exit()
End Sub
When combo box's item is 5, then it will allow the other combo box to be 3 and if the second combo box is 3 it will allow the third combo box to be 6 and if that happens it will allow the fourth combo box to be 2 and then if that happens the button is enabled.
Anyone help?

Your function run only when you change the selected index in your ComboBox1. Button1 will only be enabled if you choose your value for Combobox1 after you have selected all the good values for the others ComboBoxes. You should put this code in another function that you can call every time you select a value in one of your four ComboBoxes.

First of all: your code will only be executed after the first Combobox get changed. So it will only check the second, the third and the fourth position AFTER the first ComboBox get changed.
The solution for your problem is to add the other ComboBoxes as a Handler for your function. Also you listen to the wrong event. SelectedIndexChanged only get triggered if the user choose another answer from the list. But what if the user typed in the code? The correct event is the TextChanged event which also trigger the function if the user type in the code.
Private Sub CheckCode(sender As Object, e As EventArgs) Handles ComboBox1.TextChanged, ComboBox2.TextChanged, ComboBox3.TextChanged, ComboBox4.TextChanged
If ComboBox1.Text = "5" Then
If ComboBox2.Text = "3" Then
If ComboBox3.Text = "2" Then
If ComboBox4.Text = "6" Then
Button1.Enabled = True
End If
End If
End If
End If
End Sub
Personally, I would not handle it that way, but it definitely solves your problem.

i solved the problem.
The code is
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If NumericUpDown1.Value = 5 And NumericUpDown2.Value = 3 And NumericUpDown3.Value = 2 And NumericUpDown4.Value = 6 Then
Me.Close() 'Command here.
End If
End Sub
If Anyone wants to know.

You must have a code in other comboboxes, not only on the first one.
your code must be like this:
if combobox1.text = 5 and combobox2.text = 3 and combobox3.text = 2 and combobox4.text = 6 then
button1.enabled = true
else
button1.enabled = false
endif
all of the comboboxes has the same code, i think. just try it :)

Related

how can i show specific values in the combobox based on the checkbox state

I want to show related values in the combo box when the user selects the checkboxes.
Private Sub Form2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
If CheckBox1.Checked = True Then
ComboBox3.GetItemText("10")
End If
If CheckBox2.Checked = True Then
ComboBox3.Items.Add("20")
End If
End Sub
Any code suggestion?
On the CheckedChanged event, you may use the following code:
Private Sub CheckBoxes_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged
If sender.Text = "Select 10" Then
ComboBox1.SelectedItem = "10"
Else
ComboBox1.SelectedItem = "20"
End If
End Sub
This is coded only for two CheckBoxes. The ComboBox1.SelectedItem tries to find if there's an option given in the string available in the ComboBox control. An example output is given:
Hope it helps!

Adding text after a particular text or word in textbox vb.net

I have a combo box and textbox.
The combo box will have options for user to chose.
Textbox1 has text:- Troubleshooting Steps: set in properties of the textbox.
If user selects: Rebooted PC from combo box list, then presses a button it will add the text after Troubleshooting Steps:.
Example: Troubleshooting Steps: Rebooted PC
what i want to be able to do is add multiple selects one after other.
Example:
Troubleshooting Steps: Rebooted PC- Problem returned after reboot- Replaced part
I did some googling and found this code which ive changed to do part of it.
But the problem i have it adds the last text selected Troubleshooting Steps: pushing the first text selected to the end.
Whats Happening Example:
Troubleshooting Steps: (3rd)Replaced part- (2nd)Problem returned after reboot- (1st)Rebooted PC
Code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim text As String = TextBox1.Text
Dim index As Integer = text.IndexOf("Troubleshooting Steps:")
Dim countChars As Integer
countChars = "Troubleshooting Steps:".Length
If index >= 0 Then
text = text.Insert(index + countChars, ComboBox1.Text)
TextBox1.Text = text
End If
End Sub
here's the updated one, have a good one!
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'you can set the TextBox text either here or in the properties panel
TextBox1.Text = "Troubleshooting Steps: "
TextBox2.Text = "Follow up: "
End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
If ComboBox1.SelectedIndex < 0 Then
'MsgBox("Your message if nothing is selected in ComboBox1 when the button is pressed")
Else
TextBox1.Text = TextBox1.Text & " - " & ComboBox1.SelectedItem.ToString
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
If ComboBox2.SelectedIndex < 0 Then
'MsgBox("Your message if nothing is selected in ComboBox2 when the button is pressed")
Else
TextBox2.Text = TextBox1.Text & " - " & ComboBox2.SelectedItem.ToString
End If
End Sub

How do you store an output from a combobox in VB.NET?

I am trying to make my code so that I get an output from my ComboBox and then store it then the variable "Planet", how would I go about this? I have tried Planet=ComboBox1 but this does not work.
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim Planet As String
Planet = ComboBox1
If Planet =
End If
End Sub
I have now used:
Planet = Convert.ToString(ComboBox1)
but I get the output "System.Windows.Forms.ComboBox, Items.Count: 8", I have 8 Strings in this ComboBox and it seems that is what it is outputting. When I select an item in the ComboBox I click on one of the planets from a drop down list, which is what I need to retun.
Sorry did not debugged it before SelectedText is the wrong property, you should only use it to retrieve the text that the user selected inside the textbox portion of the combobox. You do get all text selected after changing the index but that doesn't happen until after this event runs. Use SelectedItem.ToString() instead. here's the code:
' I have added a combobox and a lable and the code is written on SelectedIndexChanged Event
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedItem.ToString = "Text1" Then
Label1.Text = "Text1"
End If
If ComboBox1.SelectedItem.ToString = "Text2" Then
Label1.Text = "Text2"
End If
End Sub
Here is the cod that may help:
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Dim Planet As String
Planet = ComboBox1.SelectedValue.Tostring()
If Planet = "ConditionalValue" Then
'Your Code if True
ELSE
'YOUR CODE IF FALSE
End If
End Sub

vb.net - enable/disable a button depending on user input

As I'm putting the finishing touches on my program I'm having some troubles.
Theres several user inputs and a submit button, once the inputs has been filled I wish to enable the submit button, else the button should be disabled. This is what I have:
Private Sub ButtonControl(sender As System.Object, e As System.EventArgs) Handles Input1.Validated
If Input1.Text = "" Then
ButtonSubmit.Enabled = False
ElseIf Input1.Text <> "" Then
ButtonSubmit.Enabled = True
End If
End Sub
The thing is it disables nomatter what and then it doesnt enable when my input is filed
Your code will work if you have another control that can receive the focus. Control Validation occurs on the loss of focus. If you need to have just one focusable item active you will need to use either KeyPress, KeyDown or Textchanged events to enable your button, also make sure that the CausesValidation property of your TextBox is true.
I would also make the method more generic so you could call it from multiple textbox's by using the sender object to access the textbox that raised the event. Also if you have a True/False condition you only need to do the comparison in the first if statement and then you just use an else not an elseif.
for example:
Private Sub ButtonControl(sender As System.Object, e As System.EventArgs) Handles Input1.Validated
If DirectCast(sender, TextBox).Text = "" Then
ButtonSubmit.Enabled = False
Else
ButtonSubmit.Enabled = True
End If
End Sub
You can also use the String.IsNullOrWhiteSpace Method to check if just spaces have been entered if you are using the 4.0 framework or above. Like this TextChanged EventHandler.
Private Sub ButtonControl(sender As Object, e As EventArgs) Handles Input1.TextChanged
If String.IsNullOrWhiteSpace(DirectCast(sender, TextBox).Text) Then
ButtonSubmit.Enabled = False
Else
ButtonSubmit.Enabled = True
End If
End Sub
You are going to need to use the TextBox "TextChanged" event, and be sure to set each Textbox AutoPostback="True". You can use an UpdatePanel to make the postbacks that occur on each Textbox you wish to validate less obnoxious to your end-user.
So, your textbox (if you have many, make sure they all have the OnTextChanged="ValidateForm":
<asp:TextBox ID="Input1" runat="server" OnTextChanged="Validate_TextChanged" />
Inside your textchanged ("ValidateForm") event (which each Textbox is attached to), one quick to implement route to do would just be
' Validation inside this event
Protected Sub Validate_TextChanged(sender As Object, e As EventArgs)
if Input1.text <> "" AndAlso Input2.text <> "" AndAlso ' ...etc.
End Sub
If you go the route of the UpdatePanel, you may find this useful.
This is the kind of thing I would do:
Private Sub TextBoxes_TextChanged( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles _
TextBox1.TextChanged, _
TextBox2.TextChanged, _
TextBox3.TextChanged
Dim textBoxes = { TextBox1, TextBox2, TextBox3 }
Button1.Enabled = textBoxes.All(Function (tb) tb.Text <> "")
End Sub
You can then add as many text boxes in to the textBoxes array as you need to check. Just make sure that the text boxes to the Handles list.
Private Sub TextBox1_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text.Length > 0 Then
Me.Button1.Enabled = True
Else
Me.Button1.Enabled = False
End If
End Sub
Do that code in the Input1_TextChanged event
This is what I did and worked:
Dim CheckInput1 As Boolean
Private Sub Input1_TextChanged(sender As Object, e As EventArgs) Handles Input1.TextChanged, Input1.Enter
CheckInput1 = True
If Input1.Text = "" Then
CheckInput1 = False
End If
If CheckInput1 = False Then
ButtonSubmit.Enabled = False
ElseIf CheckInput1 = True Then
ButtonSubmit.Enabled = True
End If
End Sub
There must be a more efficient code that this but I think this solves your problem.

Don't know what I am doing wrong in controls

Ok Here is what I have so far. I am trying to do the checkbox so that way only one is check at a time. But for some reason they both will check. And another thing I have the labels on there but in the radiobuttons no matter which one you check and submit it only stats the bottom one. Can someone help me and tell me what I am doing wrong...
Public Class Form1
Dim Message As String
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load, MaskedTextBox1.TextChanged
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
If TextBox1.Text <> "" Then
ErrorProvider1.Clear()
Label1.Text = TextBox1.Text
Else
ErrorProvider1.SetError(TextBox1, "Please enter text")
End If
End Sub
Private Sub MonthCalendar1_DateChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DateRangeEventArgs) Handles MonthCalendar1.DateChanged
Me.Text = Me.MonthCalendar1.SelectionRange.Start.ToShortDateString
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Label2.Text = MaskedTextBox1.Text
Label3.Text = RadioButton1.Text
Label3.Text = RadioButton2.Text
Label3.Text = RadioButton3.Text
Label4.Text = CheckBox1.Text
Label5.Text = CheckBox2.Text
Label6.Text = MonthCalendar1.Text
Dim message As String
message = ""
If CheckBox1.Checked Then
message += "first one clicked"
End If
If CheckBox2.Checked Then
message += " second one clicked"
End If
MessageBox.Show(message)
End Sub
Private Sub RadioButton1_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RadioButton3.CheckedChanged, RadioButton2.CheckedChanged, RadioButton1.CheckedChanged
Dim radMessage As RadioButton = CType(sender, RadioButton)
If radMessage.Checked Then
Message = radMessage.Text
End If
End Sub
End Class
Ok and this is suppose to be what my commands are suppose to be doing..
Create a new window Project .change the title of the form to Lab3.drag the following controls to your form,textbox,masked textbox (set to phone number),3 radiobuttons in a groupbox(container control),2 checkboxes, Month calendar,all on the left of the form,add a label for each control on the right, add an errorprovider for the textbox and insure it has data entered, add tooltips to every control, add a button
Task
When the user mouses over any control on the left they get a tooltip, change the text on the button to "Submit", when the user clocks the button, the info from the controls on the left appears in the label on the right,if no data is entered in the textbox use the errorprovider control to prompt the user for data, only use 1 label for all three radiobuttons, Only display each check box data if the check boxes checked, make sure to add code to erase the appropriate label if the user UNCHECKS a check box, set the tab order of the controls so you can tab though them in order down the left side, look on the internet to find out how to rettrieve the selected date from the calendar control when the user clicks the button.
I am not expecting anyone to do this for me. I am trying to do the code but i know i mest up some where. I am still a newbie at this so sorry.
I don't expect this answer to solve all of your problems but hopefully it should prove a starting point.
Firstly, why do you want only one checkbox to be selected at any moment? I can't see that as a requirement in your task? By default checkboxes do not exhibit that behaviour (that is what radio buttons are for), but they can be made to. To do this you would need to look at the selected_change event of the checkbox and when one is selected, deselect all the others. However before you do that, make sure it is what you are after before progressing.
With the radio buttons issue, I am assuming you want to set the text of the selected radio button into a label? Well currently you are assigning the selected text into a field, Message, but you are not using this anywhere else. When you set up your labels you have the following:
Label3.Text = RadioButton1.Text
Label3.Text = RadioButton2.Text
Label3.Text = RadioButton3.Text
Whereas I am guessing you need something along the lines of
Label3.Text = Message
At the moment you are setting the same Label text three times and only the third radio button text will be displayed.
On another note I would possibly think about using different variable names that Message as it could result in some confusion later on.
Hope that gets you somewhere