1.11M Members

set limits to a combobox.selecteditem

 
0
 

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

I dont see any code of comboBox, only textbox

 
0
 

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

 
0
 

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

 
0
 

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.

 
0
 

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

 
0
 

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?

 
0
 

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

Where exactly?

 
0
 

in this part >>>

If comboBox_SelectedItems(index) <= 60

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

 
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
 
0
 

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?

 
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
 
0
 

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.

 
0
 

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

@mitja same error. T.T

 
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.

 
0
 

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

 
0
 

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

 
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

 
0
 

so 40 indexes should i save in my database?

 
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.

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article