Show a ComboBox based on the selected value of another ComboBox [duplicate] - vb.net

I have a little problem that i'm creating a simple search application which have a TEXT box and some combo boxes and radio buttons and a search button.
radio button names "Videos", "Audios", "Pics" etc..
when radiobutton of video is selected, a combobox is appear having options "YouTube", "Metacafe" etc
I want that when i click "Metecafe" item in combobox of video, an other combo box appear having items Like "Entertainment", "How To", "+18" etc(categories of video search).
so "HOW TO SHOW/HIDE AN OTHER COBOBOX WITH THE HELP OF A COMBOBOX ITEM"
hope you have understood my problem.
screen shoot
i don't know it is possible or not becux i'm beginner in VB
my English is not so good so please see below code:)
if combobox1.SelectedItem = "PAKISTAN" Then
combobox2.Visible = True
End if
if combobox1.SelectedItem = "INDIA" Then
combobox3.visible = true
combobox2.visible = false
End if
Obviously this code is wrong, this is a example that what i want to do
Thanks to all in advance..
NOTE:
i have try this codes but its not working..
if ComboBox1.Items(ComboBox1.SelectedIndex).ToString() = "PAKISTAN" Then
ComboBox2.Visible = True
End if
sorry for spell and grammatical mistakes:(

Try using the SelectedItem Property like this
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedItem.ToString = "PAKISTAN" Then
ComboBox2.Visible = True
End If
End Sub
Since you are wanting to check for multiple country's you can use a Select Case Statement like this
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Select Case ComboBox1.SelectedItem.ToString
Case "PAKISTAN"
ComboBox2.Visible = True
ComboBox3.Visible = False
Case "INDIA"
ComboBox3.Visible = True
ComboBox2.Visible = False
Case Else
ComboBox2.Visible = False
ComboBox3.Visible = False
End Select
End Sub

Two things are important here:
(a) Which event you're using to /detect/ that the combobox has changed, and
(b) The code you're using to detect what is selected.
I haven't tested this, and I'm coming from a C# / VB6 background, so bear with me if this isn't 100% correct, but I believe using the following should work:
Inside the SelectedIndexChanged event of ComboBox1, insert your code above, EXCEPT.. Change instances like
combobox1.SelectedItem = "PAKISTAN"
to
ComboBox1.Text = "PAKISTAN"
The SelectedItem property outputs the selected item object itself, which is a variable type that can't be displayed.. not the /text/ of the currently selected item. There are other ways to access the text associated to that item, but ComboBox1.Text is the easiest. ComboBox1.SelectedItem.ToString() would work also.

If ComboBox1.Items(ComboBox1.SelectedIndex).ToString() = "PAKISTAN" Then
ComboBox2.Visible = True
End If

Btw is this a web client or window client?
Assuming if you are implementing a web client app and the conditional logic are simple, you may need to trigger a page reload which can be achieve in your markup by specifying an event and set the property for reload to enable. My initial on this from reading your post is that your GUI might not be refreshing or reloading unless it's your intension to not reload the page or refresh the GUI then this should be handle with javascript instead.
I hope I'm interpreting the issue correctly and hopefully this will give you some idea on how to tackle the issue.

Related

Unchecking a Previously Selected Checkbox when another checkbox is checked

My form has 3 checkboxes available to the User. When the User mistakenly checks one Checkbox and realizes the mistake then checks the correct Checkbox, I need the incorrect checked Checkbox to Uncheck automatically.
I have diligently searched the web but have been unable to find any sites that offer answers for VB 2010. Mostly they provide solutions for HTML, Excel, Java, etc.
Private Sub BeginnerForm_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
StartupForm.Close()
If Me.CheckBox1.Checked Then
Me.CheckBox2.Checked = False
Me.CheckBox3.Checked = False
ElseIf Me.CheckBox2.Checked Then
Me.CheckBox1.Checked = False
Me.CheckBox3.Checked = False
ElseIf Me.CheckBox3.Checked Then
Me.CheckBox1.Checked = False
Me.CheckBox2.Checked = False
End If
End Sub
The results of this code is that it does not uncheck the Checkbox(es). I get no error code, it just does not UNCHECK the Checkbox(es). I have also tried other code entries such as Me.Checkbox2.CheckState.Checked = CheckState.Unchecked to no avail. Trusting you can steer me in the right direction to UNCHECK a Checkbox when another Checkbox is CHECKED.
First of all, as mentioned by daShier, you place the code in the wrong event handler. Since you want to uncheck the other checkboxes when one is being checked, you must put the code in the either Click or CheckChanged event handler of all your checkboxes that take part in this selection logic. I would suggest to put in the CheckChanged event since it will be triggered even when your checkbox check state is changed through code in addition to through mouse or keyboard actions.
' declare variable to keep track of the previous selected checkbox
Dim prevSelectedCheckBox As CheckBox
' all checkboxes to be controlled are handled by this event handler
Private Sub CheckBoxes_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged, CheckBox2.CheckedChanged, CheckBox3.CheckedChanged
' first, identify which checkbox is triggering this event
Dim this As CheckBox = DirectCast(sender, CheckBox)
' if this event is triggered when this checkbox is being checked
If this.Checked Then
' if there is a checkbox previously selected/checked
If prevSelectedCheckBox IsNot Nothing Then
prevSelectedCheckBox.Checked = False ' uncheck it
End If
' now this checkbox is currently the checked one, save it to the variable
prevSelectedCheckBox = this
' if event is triggered by unchecking this checkbox, and this checkbox is the previously checked checkbox
ElseIf this.Equals(prevSelectedCheckBox) Then
' clear the variable since now, there is no checkbox is being checked
prevSelectedCheckBox = Nothing
End If
End Sub
However, if user can only select one option among the options represented by the checkboxes, you better use radio button instead.
It appears you are processing the code in the wrong handler. You are changing things when closing the form. Try moving the code to the CheckBoxx.CheckedChanged handlers:
Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
If CheckBox1.Checked Then
CheckBox2.Checked = False
CheckBox3.Checked = False
End Sub
UPDATE: I realize that your original code that I copied and pasted would cause a loop since the changing of the checkbox states would trigger the other routines. It's not necessary since you only need clear the other boxes if the box your user is clicking is checked.
You will need to add similar code to CheckBox2.CheckedChanged and CheckBox3.CheckedChanged as well.
Also, I should point out that you are trying to make checkboxes act as radio buttons. Why not replace the checkbox controls with radio buttons instead so that VB handles the one-only behavior for you?

Disable Button If Combox Input Deleted

In my project, I have a few textbox inputs, and some combo boxes with maybe 2 indexed items on a form. There's a button I'm disabling on load if no input is supplied to both textbox inputs, and it works great even if I delete out any text. However, I'm having issues with forcing the combo box to behave in the same manner. This work however:
Private Sub cboPickShirts_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cboPickShirts.SelectedIndexChanged
InputCheck_3 = True
If cboPickShirts.SelectedIndex < 0 Then
InputCheck_3 = False
End If
If InputCheck_3 = False Then
btnInputResult.Enabled = False
ElseIf InputCheck_3 = True Then
btnInputResult.Enabled = True
End If
End Sub
I have InputCheck_3 set up as a global variable in a Public Module. On form load, I'm disabling my button and it doesn't enable until I select one of the indexed items. My struggle to get the button disable again if any combo box text is entered and deleted out, leaving it null or empty. Any thoughts on what I'm missing or what I can add to get results? I guess I need a variable or event to notice the change (entering & deletion of text).
The problem you are having is that your SelectedIndexChanged event is not being triggered when you remove the selected item from your ComboBox. I would use the TextChanged event of your TextBox's and ComboBox and give it a common handler and check it that way. Something like this
Private Sub TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged, TextBox2.TextChanged, cboPickShirts.TextChanged
EnableCheck()
End Sub
Private Sub EnableCheck()
btnInputResult.Enabled = (String.IsNullOrEmpty(TextBox1.Text) And String.IsNullOrEmpty(TextBox2.Text) And ComboBox1.SelectedIndex = -1)
End Sub
You can also check that the comboBox is NullorEmpty the same way as the textbox's. As it stands right now the combobox will be enabled when the text no longer matches a selection.
One line code
btnInputResult.Enabled = If((cboPickShirts.SelectedIndex<0),False, True)

Checkbox does does not work in VB.Net form

I have two checkboxes for two groupboxes to enable visibility or invisibility of each one at a time but somehow one is working(chboNew) the other one(chboIssue) doesn't!
here is the code I have written for it, any help would be appreciated:
Private Sub chboIssue_CheckStateChanged(sender As Object, e As EventArgs) Handles chboIssue.CheckStateChanged
If chboIssue.Checked = True Then
gbIssueSearch.Visible = True
gbNewSearch.Visible = False
chboNew.Checked = False
ElseIf chboIssue.Checked = False Then
gbIssueSearch.Visible = False
End If
End Sub
Private Sub chboNew_CheckStateChanged(sender As Object, e As EventArgs) Handles chboNew.CheckStateChanged
If chboNew.Checked = True Then
gbNewSearch.Visible = True
gbIssueSearch.Visible = False
chboIssue.Checked = False
ElseIf chboIssue.Checked = False Then
gbNewSearch.Visible = False
End If
End Sub
If user has to choose between new issue and issue search,one at a time.
Then you should use radio buttons, instead of checkbox.
Checkbox gives idea that user can choose both checkboxes at same time.
Which in your case is not true.
Changing the name of the checkboxes is not going to solve your issue. I noticed that for your chboNew.CheckStateChanged event handler in your elseif clause you are checking if chboIssue is checked, whereas in your other handler for chboIssue both your if/else clauses look at chboIssue. I'm thinking that may be part of your issue. Also, if only one of these boxes is supposed to be checked at a time, you may want to add logic to automatically uncheck the other whenever one is checked. For instance, in your chboNew handler, "If chboNew.Checked = True Then chboIssue = False", and the inverse in your chboIssue handler. Hope this helps.

show/hide another combobox, with the help of combobox items

I have a little problem that i'm creating a simple search application which have a TEXT box and some combo boxes and radio buttons and a search button.
radio button names "Videos", "Audios", "Pics" etc..
when radiobutton of video is selected, a combobox is appear having options "YouTube", "Metacafe" etc
I want that when i click "Metecafe" item in combobox of video, an other combo box appear having items Like "Entertainment", "How To", "+18" etc(categories of video search).
so "HOW TO SHOW/HIDE AN OTHER COBOBOX WITH THE HELP OF A COMBOBOX ITEM"
hope you have understood my problem.
screen shoot
i don't know it is possible or not becux i'm beginner in VB
my English is not so good so please see below code:)
if combobox1.SelectedItem = "PAKISTAN" Then
combobox2.Visible = True
End if
if combobox1.SelectedItem = "INDIA" Then
combobox3.visible = true
combobox2.visible = false
End if
Obviously this code is wrong, this is a example that what i want to do
Thanks to all in advance..
NOTE:
i have try this codes but its not working..
if ComboBox1.Items(ComboBox1.SelectedIndex).ToString() = "PAKISTAN" Then
ComboBox2.Visible = True
End if
sorry for spell and grammatical mistakes:(
Try using the SelectedItem Property like this
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
If ComboBox1.SelectedItem.ToString = "PAKISTAN" Then
ComboBox2.Visible = True
End If
End Sub
Since you are wanting to check for multiple country's you can use a Select Case Statement like this
Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Select Case ComboBox1.SelectedItem.ToString
Case "PAKISTAN"
ComboBox2.Visible = True
ComboBox3.Visible = False
Case "INDIA"
ComboBox3.Visible = True
ComboBox2.Visible = False
Case Else
ComboBox2.Visible = False
ComboBox3.Visible = False
End Select
End Sub
Two things are important here:
(a) Which event you're using to /detect/ that the combobox has changed, and
(b) The code you're using to detect what is selected.
I haven't tested this, and I'm coming from a C# / VB6 background, so bear with me if this isn't 100% correct, but I believe using the following should work:
Inside the SelectedIndexChanged event of ComboBox1, insert your code above, EXCEPT.. Change instances like
combobox1.SelectedItem = "PAKISTAN"
to
ComboBox1.Text = "PAKISTAN"
The SelectedItem property outputs the selected item object itself, which is a variable type that can't be displayed.. not the /text/ of the currently selected item. There are other ways to access the text associated to that item, but ComboBox1.Text is the easiest. ComboBox1.SelectedItem.ToString() would work also.
If ComboBox1.Items(ComboBox1.SelectedIndex).ToString() = "PAKISTAN" Then
ComboBox2.Visible = True
End If
Btw is this a web client or window client?
Assuming if you are implementing a web client app and the conditional logic are simple, you may need to trigger a page reload which can be achieve in your markup by specifying an event and set the property for reload to enable. My initial on this from reading your post is that your GUI might not be refreshing or reloading unless it's your intension to not reload the page or refresh the GUI then this should be handle with javascript instead.
I hope I'm interpreting the issue correctly and hopefully this will give you some idea on how to tackle the issue.

Rookie With Events; Causing Action after hitting F5

So yes I'm very new to creating my own custom events. I can do the basics when I put controls on a form but this one is a little more complex. I have an application that reads in a .TSV and populates a form with controls based on the number of objects it "reads." So for instance: I have a file that contains 10 people objects and my code populates a form with controls for each person. Easy stuff!
Now lets say I have a ComboBox with the items: "Alive", "Deceased", "Unborn". Right next to this I have a textbox for age. Now originally, this textbox is not enabled because the default value for the ComboBox is "Unborn". But lets say when the user selects "Alive", I want that textbox to become enabled so an age can be entered.
Obviously from me asking this and the title of this question, I don't know how to go about doing this. I don't really understand events and I learn by example but the MSDN examples don't quite cut it.
Any help (especially an awesome Step-by-Step guide) would be greatly appreciated.
From what I gather from the comments, you want to add events to a form object that is created at runtime. Use the AddHandler command to the object. Something to the effect of:
AddHandler NameOfFormObject.TypeOfAction, AddressOf HowToHandle
Private Sub HowToHandle(ByVal sender as System.Object, ByVal e As System.EventArgs)
DropDownMenu.enabled = True
End Sub
Doing it this way, you will be able to modify the events of an object created at runtime. In your case, it sounds like you'll want to use the action that Josaph recommended, and end up incorporating both solutions offered, like so
AddHandler ComboBox1.SelectedIndexChanged, AddressOf HowToHandle
Private Sub HowToHandle(ByVal sender as System.Object, ByVal e As System.EventArgs)
If DirectCast(sender, ComboBox).SelectedIndex = 0 'Alive
DirectCast(DirectCast(sender, ComboBox).Tag, TextBox).enabled = True
Else
DirectCast(DirectCast(sender, ComboBox).Tag, TextBox).enabled = False
End If
End Sub
You will want to use the ComboBox_SelectedIndexChanged() event to capture that the combo box item has been changed. At that point, you will need to check to see which combo box item has been selected and make a decision as to whether the TextBox should be enabled or not. Here is an example. Note: This example assumes that "Alive" is the first item in your combobox at the 0 index.
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
If ComboBox1.SelectedIndex = 0 Then 'Alive
TextBox1.Enabled = True
Else
TextBox1.Enabled = False
End If
End Sub
Dynamically generate the combobox and add handler.
Dim cmb as New ComboBox
AddHandler cmb.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
Me.Controls.Add(cmb)
I suppose that as you will have 10 comboboxes... in the same way you'll have 10 textboxes.
In that case... once you attach and handle the event as AndyPerfect and Joseph... in that method you will need code something to know which of the Textboxs you need to enable/disable.
First you need to know which combobox triggered the event: this is done using the "sender" parameter. ctype(sender, Combobox) to access the methods and properties of the ComboBox.
Once you know which combo, you need to activate/deactivate the correct textbox.
To accomplish this, you'll need to add a reference to the TextBox in the "TAG" property of the Combobox at the moment of creating it.
Dim txt as new TextBox
Dim cmb as new ComboBox
cmb.Tag = txt
Then... you simple use:
ctype(ctype(sender, Combobox).Tag, TextBox).Enable = true
Here is how I ended up writing it in the end. I appreciate all the help! Thank you!
If DirectCast(sender, ComboBox).SelectedIndex = 2 Then
DirectCast(Me.Controls.Item(DirectCast(sender, ComboBox).Tag), TextBox).Enabled = True
Else
DirectCast(Me.Controls.Item(DirectCast(sender, ComboBox).Tag), TextBox).Enabled = False
End If