-
Jun 9th, 2021, 07:40 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Which is Appropriate ComboBox1 Event to show a Msg that combobox1.item Does not Exist
Hello
Which is the most appropriate ComboBox1 Event to show a message that combobox1.item Does not Exists
ie user types wrong string/text in Combobox1 and Msg to POP up "Item Not there in the List"
a.) Typing few characters in Combobox1 and Msgbox Poping up OR
b.) Typing full string/Text (Wrong Entry itself) then Msgbox Poping up
preferable choice would be Option a.)
Code:
Private Sub ComboBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox1.SelectedIndexChanged
Textbox1.Text = dt.Rows(Combobox1.SelectedIndex).Item(1)
Textbox1.Update 'Updates the textbox
End Sub
Code:
If ComboBox1.Items.Contains(ComboBox1.Text) Then
Else
MsgBox "Item Not there in the list"
End if
Almost explored everything but somehow could not succeed
Do we have something like
Code:
If Not ComboBox1.Items.Contains(ComboBox1.Text) then
MsgBox "Item Not there in the list"
End if
or any different syntax
for whole coding you may refer my thread 892141 in this forum
Thanks
SamD
Thread 3: 892182
18
Last edited by SamDsouza; Jun 9th, 2021 at 07:46 AM.
-
Jun 9th, 2021, 08:29 AM
#2
Hyperactive Member
Re: Which is Appropriate ComboBox1 Event to show a Msg that combobox1.item Does not E
Personally, I would completely avoid popup windows when possible. Messageboxes are like spam when used too much.
A better method would be to disable the final button until the matching parameters are met. If you want to tell the user what is missing, you can do so with a label on top of the command in question.
Example:
Code:
Private Sub ComboBox1_TextChanged(sender As Object, e As EventArgs) Handles ComboBox1.TextChanged
UpDateComboSelection()
End Sub
Private Sub UpDateComboSelection()
'sets a value for boolean
Dim En As Boolean = False
' Ensure we have a proper string to search for.
If ComboBox1.Text <> "" Then
' Find the item in the list and store the index to the item.
Dim index As Integer = -1
Dim st As String = "Text was found"
'loop to find
For i = 0 To ComboBox1.Items.Count - 1
If ComboBox1.Text = ComboBox1.Items(i).ToString Then
index = i
Exit For
End If
Next
If index <> -1 Then
En = True
Else
En = False
st = "Text was NOT found"
End If
Button1.Enabled = En
'if you want to say something,
Label1.Text = st
End If
End Sub
-
Jun 9th, 2021, 09:20 AM
#3
Re: Which is Appropriate ComboBox1 Event to show a Msg that combobox1.item Does not E
I'd say that you are going about this in completely the wrong way. Autocomplete functionality is built into the ComboBox control so you should use it. You can set the list items to be the autocomplete source so the user will be able to see what items in the list match what they've typed and they can select one directly rather than type the whole thing, plus they will no that there are no matches when the autocomplete list is empty. You don't need any code at all and the UX is much improved.
-
Jun 10th, 2021, 01:04 AM
#4
Thread Starter
Addicted Member
Re: Which is Appropriate ComboBox1 Event to show a Msg that combobox1.item Does not E
Frabulator
Thanks for sending the Code. Tried yours but it nothing Triggered and executed. By the Way When You mention final Button and in your code mentions as Button1 I take as command button but FYI i've not incorporated any type of buttons on my user form. Is it because of that Button which needs to be incorporated. Also when coded i Put REM marks before the Button1.Enabled.
Rather than Creating new sub-routine and calling UpDateComboSelection() .It was directly coded inComboBox1 _TextChanged(sender As Object, e As EventArgs) Handles ComboBox1.TextChanged.
JM
Thanks for the suggestion and implemented
Autocomplete source = list items in the Combobox1 properties
plus they will Know that there are no matches when the autocomplete list is empty. You don't need any code at all and the UX is much improved.
What is the control to know if List item is wrong or item does not exists in the list
Just in case The user types Wrong string/text.
Observation when Selecting item or typing it correctly the other respective data is displayed in other textboxes
BUT when something wrong entered in combobox the Data displayed in other fields remains intact of the Correct items which infact should not have been the case. (Should have disappeared completely and immediately)
With your suggestion the Selection is clicked or Fully Type Correctly and Press Enter of Item existed other fields are displayed correctly.
You don't need any code at all and the UX is much improved.
Am i missing any other property of combobox for a slight better control
Thanks
SamD
Thread 3: 892182
19
Last edited by SamDsouza; Jun 10th, 2021 at 01:10 AM.
-
Jun 12th, 2021, 03:20 AM
#5
Thread Starter
Addicted Member
Re: Which is Appropriate ComboBox1 Event to show a Msg that combobox1.item Does not E
JM
Explored with more trials . You are right frankly speaking msgbox is not required niether its coding. Task gets completed with the property setting of combobox itself.
Code:
.AutoCompleteSource = AutoCompleteSource.ListItems
Indeed a practical suggestion.
Thanks
SamD
Thread 3: 892182
21
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|