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()

Recommended Answers

All 25 Replies

I dont see any code of comboBox, only textbox

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

Are you wanting to limit the select count to 60 per each item in the combobox, or 60 total selections of items?

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.

While entering record in combo box make sure that it validates the count

Dim countvalue as Integer
countvalue=combobox1.Items.Count
if countvalue>60
Msgbox("Count cannot exceed 60")
else
'code to add the record in combobox
End If

Hope it helps u...

'Sorry I guess I did not understand the requirement....extremely sorry

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?

ahm.. mitja thre's an error in your codes.. : Index was outside the bounds of the array.

Where exactly?

in this part >>>

If comboBox_SelectedItems(index) <= 60

it says that : Index was outside the bounds of the array.

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

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?

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

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.

@beginnerdev.. can you post a code ? i dont know how to start .. from your idea..

@mitja same error. T.T

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.

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. :)

if i have ten sections in every year levels .. the indexes are 40 ?

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

so 40 indexes should i save in my database?

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.

my combobox is populated based on my database.. like this..

if 1st year have 10 sections then may Sectionscombobox populated with 10 items..

so isnt the same with 40 items in the combobox?

I already showed him the code how to create a counter for each index.

paoi00, change this lin of code:

    comboBox_SelectedItems = New Integer(comboBox1.Items.Count - 1) {}

to:

    comboBox_SelectedItems = New Integer(comboBox1.Items.Count) {}

It has to work now.

same error mitja T.T

There is for sure something wrong with indexing.
When this error occurs? From very beginning, or later?

the moment i used to select on the comboboxSections... T.T
the combobox that i used for count have no items in the beggining because it can be populated when the first combobox select one item..
eg.
ComboboxYrLvl choose 1st year in the items then ComboboxSections populated the available sections ..

the comboBoxSections is the one i want to count the number of times the item selected.

the error says : Object reference not set to an instance of an object.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.