set limits to a combobox.selecteditem
hi! can you help me..?
i want to limit how many times can user select the every items inside the combobox to 60. how could i do that? pls help me. :)
here's my code in my combobox.. i dont know how to start the codes in limiting.. that' why i dont have codes for that.
strsql = "select * from Schedulings where Sections = '" & ComboBox1.Text & "'"
Dim acscmd As New OleDb.OleDbCommand
acscmd.CommandText = strsql
acscmd.Connection = asconn
acsdr = acscmd.ExecuteReader()
TextBox9.Clear()
TextBox5.Clear()
ListView1.Items.Clear()
acsdr.Read()
While (acsdr.Read())
TextBox9.Text = (acsdr("SectionNames"))
TextBox5.Text = (acsdr("Adviser"))
showmyrecords()
End While
acscmd.Dispose()
acsdr.Close()
paoi00
Junior Poster in Training
72 posts since Mar 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
I dont see any code of comboBox, only textbox
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 13
I see, didnt completely read your post. Sorry.
You can do it this way:
Public Sub New()
InitializeComponent()
'i will show you an example, so this is how I populate comboBox:
comboBox1.Items.AddRange(New String() {"a", "b", "c"})
'you populate comboBox as you like
'now lets create an array, which will count each item in comboBox selected:
'now we have set as many items as comboBox has in array, and are set to 0 (initial values)
comboBox_SelectedItems = New Integer(comboBox1.Items.Count - 1) {}
End Sub
Private Sub comboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
If comboBox1.SelectedIndex > -1 Then
Dim index As Integer = comboBox1.SelectedIndex
If comboBox_SelectedItems(index) <= 60 Then
'rest of you code in here ... (your upper code you pasted)
comboBox_SelectedItems(index) += 1
Else
MessageBox.Show("Item " & comboBox1.SelectedItem.ToString() & " was only allowed to be selected 60 times. Limit is now exceded.")
End If
End If
End Sub
Hope it helps,
bye
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 13
Are you wanting to limit the select count to 60 per each item in the combobox, or 60 total selections of items?
Begginnerdev
Practically a Posting Shark
861 posts since Apr 2010
Reputation Points: 184
Solved Threads: 141
Skill Endorsements: 8
Please note that Mitja Bonca's way of performing the check assumes that the selection limit is per user/session. If the user restarts your program then the number of times each entry is selected will be reset to 0. Also 60 is the limit per user, so for 10 users they will be able to select each item 600 times.
Can you be a bit more specific as to the requirement? Is this supposed to be persistant throught the sessions? Then you need to save the number of selections to your db.
adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11
thank you for your replies.. :)
i'm trying your codes and update you .
ahm.. mitja thre's an error in your codes.. : Index was outside the bounds of the array.
@begginerdev -- yes i want to limit 60 per selection..
@adam k -- i want to limit the number of times that the user can choose the sections inside the combobox that depends on the number of records in the database .. i dontknow how should i do that.. would you like to help me?
paoi00
Junior Poster in Training
72 posts since Mar 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
ahm.. mitja thre's an error in your codes.. : Index was outside the bounds of the array.
Where exactly?
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 13
in this part >>>
If comboBox_SelectedItems(index) <= 60
it says that : Index was outside the bounds of the array.
paoi00
Junior Poster in Training
72 posts since Mar 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
comboBox_SelectedItems is class variable. Instanitate it before form load put in on top ov all method, just bellow class (form) name:
Dim comboBox_SelectedItems As Integer
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 13
already declared.
and then there's another error :
expression is not an array or a method , and cannot have an argument list <
Value of type "1-dimensional array of Integer" cannot be converted to 'integer'
how should i solve that issue?
paoi00
Junior Poster in Training
72 posts since Mar 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Sorry, that variable is an integer array.
This should work:
Private comboBox_SelectedItems As Integer()
Public Sub [New]()
InitializeComponent()
'i will show you an example, so this is how I populate comboBox:
comboBox1.Items.AddRange(New String() {"a", "b", "c"})
'you populate comboBox as you like
'now lets create an array, which will count each item in comboBox selected:
'now we have set as many items as comboBox has in array, and are set to 0 (initial values)
comboBox_SelectedItems = New Integer(comboBox1.Items.Count - 1) {}
End Sub
Private Sub comboBox1_SelectedIndexChanged(sender As Object, e As EventArgs)
If comboBox1.SelectedIndex > -1 Then
Dim index As Integer = comboBox1.SelectedIndex
If comboBox_SelectedItems(index) <= 60 Then
'rest of you code in here ... (your upper code you pasted)
comboBox_SelectedItems(index) += 1
Else
MessageBox.Show("Item " + comboBox1.SelectedItem.ToString() + " was only allowed to be selected 60 times. Limit is now exceded.")
End If
End If
End Sub
Mitja Bonca
Posting Maven
2,561 posts since May 2009
Reputation Points: 642
Solved Threads: 486
Skill Endorsements: 13
If you are wanting to keep count even after the user closes and restarts the application, you will have to store the count in a database.
Another method would be to store it in the application settings, which is stored as an xml in the application directory.
Begginnerdev
Practically a Posting Shark
861 posts since Apr 2010
Reputation Points: 184
Solved Threads: 141
Skill Endorsements: 8
@beginnerdev.. can you post a code ? i dont know how to start .. from your idea..
@mitja same error. T.T
paoi00
Junior Poster in Training
72 posts since Mar 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
For the application settings, you can do something like this.
'Assign a counter for each item.'
'Example: index0Count'
' index1Count'
' index2Count'
' index3Count'
'All of these will be delcared in MyProject > Settings'
'Then, from the application, in the selected index change event, place this code.'
If myComboBox.SelectedIndex = 0 Then
If My.Settings.index0Count < 60 Then
My.Settings.index0Count + = 1
My.Settings.Save()
Else
MsgBox("You have reached your limit for this item.")
return
End If
ElseIf myComboBox.SelectedIndex = 1 Then
If My.Settings.index1Count < 60 Then
My.Settings.index1Count + = 1
My.Settings.Save()
Else
MsgBox("You have reached your limit for this item.")
return
End If
ElseIf myComboBox.SelectedIndex = 2 Then
If My.Settings.index2Count < 60 Then
My.Settings.index2Count + = 1
My.Settings.Save()
Else
MsgBox("You have reached your limit for this item.")
return
End If
ElseIf myComboBox.SelectedIndex = 3 Then
If My.Settings.index3Count < 60 Then
My.Settings.index3Count + = 1
My.Settings.Save()
Else
MsgBox("You have reached your limit for this item.")
return
End If
End If
'The code will repeat until you have accounted for all of your indexes.'
Now, for the database version.
The database version will require a table with the index and counter.
Example:
'Table: Counters'
'Column1: indexID'
'Primary Key, Integer'
'Column2: count'
'Integer, Not Null'
Then, add all of the counters for the indexes.
Next, update them from the form when used.
'You will have to create a connection to the database and fill the data table.'
'example SELECT statement'
'"SELECT * FROM Counters"'
'dt = data table'
If myComboBox.SelectedIndex = 0 Then
If dt.Rows(myComboBox.SelectedIndex).Item(1) < 60 Then
sqls = "UPDATE Counters SET count=" & (dt.Rows(myComboBox.SelectedIndex).Item(1) + 1) & "WHERE indexID=" & myComboBox.SelectedIndex
Else
MsgBox("You have reached your limit for this item.")
return
End If
When using the database method, it is VERY important that your indexes are matched with the data in the table.
Begginnerdev
Practically a Posting Shark
861 posts since Apr 2010
Reputation Points: 184
Solved Threads: 141
Skill Endorsements: 8
begginnerrdev : more explanation for indexes please.. what is the function of indexes? isn't depend in my items in combobox or what?
thank you for further explanation. :)
paoi00
Junior Poster in Training
72 posts since Mar 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
if i have ten sections in every year levels .. the indexes are 40 ?
paoi00
Junior Poster in Training
72 posts since Mar 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
An index is the integer reference to the selected item.
Arrays are 0 based, meaning the first item starts at 0, and the last item will always be a terminator.
Think of comboboxes as arrays.
An index of 0 gets the item at 0 in the array.
So if you have 5 items in the combo box your array will look like...
Index 0 - Item 1
Index 1 - Item 2
Index 2 - Item 3
Index 3 - Item 4
Index 4 - Item 5
Begginnerdev
Practically a Posting Shark
861 posts since Apr 2010
Reputation Points: 184
Solved Threads: 141
Skill Endorsements: 8
so 40 indexes should i save in my database?
paoi00
Junior Poster in Training
72 posts since Mar 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
You will need to create a counter for each index, so if there are 40 items in the combo box, you will need 40 counters.
Begginnerdev
Practically a Posting Shark
861 posts since Apr 2010
Reputation Points: 184
Solved Threads: 141
Skill Endorsements: 8