none
parcourir les items(contenant des nombres) d'un combobox RRS feed

  • Question

  • Bonsoir 

    je dispose d'un combobox contenant des chiffres.

    je desire ajouter des nombres  si ce nombres ne se trouve pas encore dans le combobox.

    mon code qui ne marche pas :

     
    a as integer
    
    m as interger 
    
    combobox.items=m
    
       for( a=0 to len(combobox1.items) step 1)
    
      if ( a<>m)
    
      combobox1.items.add(m)
    

    Tuesday, April 24, 2018 3:19 PM

Answers

  • Hi

    Here is some code that might help. You can use or discard MessageBoxes as they are just for this example.

    BTW: this is an English language forum.

    ' Form1 with ComboBox1,
    ' TextBox1 and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        ' some initial data
        ComboBox1.Items.AddRange({1, 2, 3, 4, 5, 6})
    
      End Sub
      Function GetInteger(s As String) As Integer
        Dim v As Integer = 0
        If Integer.TryParse(s, v) Then Return v
        Return -999 ' suitable unused value
      End Function
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim n As Integer = GetInteger(TextBox1.Text)
        If n < -998 Then
          ' invalid input
          ' do not clear TextBox
          MessageBox.Show(TextBox1.Text & " is Invalid input")
    
          ' clear and focus TextBox
          TextBox1.Text = Nothing
          TextBox1.Select()
          Exit Sub
        End If
        If Not ComboBox1.Items.Contains(n) Then
          ' CB doesn't contain input
          ComboBox1.Items.Add(n)
    
          ' clear and focus TextBox
          TextBox1.Text = Nothing
          TextBox1.Select()
        Else
          MessageBox.Show("Already IsNot List")
        End If
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, April 24, 2018 4:03 PM
    • Marked as answer by jacob mahone Tuesday, April 24, 2018 5:49 PM
    Tuesday, April 24, 2018 4:03 PM

All replies

  • Hi

    Here is some code that might help. You can use or discard MessageBoxes as they are just for this example.

    BTW: this is an English language forum.

    ' Form1 with ComboBox1,
    ' TextBox1 and Button1
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        ' some initial data
        ComboBox1.Items.AddRange({1, 2, 3, 4, 5, 6})
    
      End Sub
      Function GetInteger(s As String) As Integer
        Dim v As Integer = 0
        If Integer.TryParse(s, v) Then Return v
        Return -999 ' suitable unused value
      End Function
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim n As Integer = GetInteger(TextBox1.Text)
        If n < -998 Then
          ' invalid input
          ' do not clear TextBox
          MessageBox.Show(TextBox1.Text & " is Invalid input")
    
          ' clear and focus TextBox
          TextBox1.Text = Nothing
          TextBox1.Select()
          Exit Sub
        End If
        If Not ComboBox1.Items.Contains(n) Then
          ' CB doesn't contain input
          ComboBox1.Items.Add(n)
    
          ' clear and focus TextBox
          TextBox1.Text = Nothing
          TextBox1.Select()
        Else
          MessageBox.Show("Already IsNot List")
        End If
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, April 24, 2018 4:03 PM
    • Marked as answer by jacob mahone Tuesday, April 24, 2018 5:49 PM
    Tuesday, April 24, 2018 4:03 PM
  • Why don't you then not simply use first combobox findstringexact

    This is the findstringexact 

    https://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.findstringexact(v=vs.110).aspx

    Be aware this is an English language forum. Visited by many persons around the world for whom that is not their first language.

    Therefore try it as well 


    Success
    Cor

    Tuesday, April 24, 2018 4:03 PM
  • thank you it works
    Tuesday, April 24, 2018 6:10 PM
  • but i have another question 

    how can i remove each selected element in my combobox

    for example if element A is chosen in combobox , this element is immediatly removed.

    Tuesday, April 24, 2018 6:13 PM
  • but i have another question 

    how can i remove each selected element in my combobox

    for example if element A is chosen in combobox , this element is immediatly removed.

    Hi

    Assuming that the element 'A' is as per the OP, a number rather than a String.

    Here is the code I posted modified to include a Button2 which will remove an item matched from the same TextBox, or, message that it is not in the list.

    ' Form1 with ComboBox1,
    ' TextBox1, Button1 and Button2
    Option Strict On
    Option Explicit On
    Public Class Form1
      Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        ' some initial data
        ComboBox1.Items.AddRange({1, 2, 3, 4, 5, 6})
    
      End Sub
      Function GetInteger(s As String) As Integer
        Dim v As Integer = 0
        If Integer.TryParse(s, v) Then Return v
        Return -999 ' suitable unused value
      End Function
      Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click    ' add item
    
        Dim n As Integer = GetInteger(TextBox1.Text)
        If n < -998 Then
          ' invalid input
          ' do not clear TextBox
          MessageBox.Show(TextBox1.Text & " is Invalid input")
    
          ' clear and focus TextBox
          TextBox1.Text = Nothing
          TextBox1.Select()
          Exit Sub
        End If
        If Not ComboBox1.Items.Contains(n) Then
          ' CB doesn't contain input
          ComboBox1.Items.Add(n)
    
          ' clear and focus TextBox
          TextBox1.Text = Nothing
          TextBox1.Select()
        Else
          MessageBox.Show("Already IsNot List")
        End If
      End Sub
    
      Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click    ' remove item
    
        Dim n As Integer = GetInteger(TextBox1.Text)
    
        If ComboBox1.Items.Contains(n) Then
          ComboBox1.Items.Remove(n)
          ' clear and focus TextBox
          TextBox1.Text = Nothing
          TextBox1.Select()
          MessageBox.Show("Item removed")
        Else
          ' CB doesn't contain input
          MessageBox.Show("Not in the List")
        End If
      End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Tuesday, April 24, 2018 7:55 PM
    Tuesday, April 24, 2018 7:24 PM