locked
Problem on Checking the value already exist in the ComboBox in VB.NET RRS feed

  • 問題

  • Hi

    I have a ComboBox on my VB.Net Application, which contain a lists of Options. I have another control E.g. Textbox1, that Allow user to enter a string. After they click on the button, it will check if the string that user entered is already exist on the ComboBox.

    The Items that on the ComboBox is retrieved from a SQL Query. I am sure it has contain a string "abc" in the combobox. Therefore I hard coded the code below to test this method

    E.g.
         If cb_Testing.Items.Contains("abc") Then
                MsgBox("Yes")
            Else
                MsgBox("no")
            End If

    I am not sure why it always return "no" when I run the code above.

    Could you give me some advice on how could check it the string is already in the ComboBox.

    Thanks in advance.

    Chi

    2008年5月9日 下午 01:41

解答

  • Hello ChiYau, 

     

    Contains() method checks the exact object which contained in this ComboBox. Because your ComboBox binds with the data in DataTable, the underling object in each item is DataRowView type. String typed value isn’t equal with DataRowView typed object. Thereby, contains method always retrun false.

     

    Please try the following method.

           

    Code Snippet

    private void button1_Click(object sender, EventArgs e)

            {

                string a=this.comboBox1.SelectedValue.ToString();  

                bool c = checkValue(a, this.comboBox1);

                MessageBox.Show(c.ToString());

               

            }

     

            public bool checkValue(string Value,ComboBox cb)

            {

                foreach (System.Data.DataRowView drv in cb.Items)

                {

                    if (Value.Equals(drv[cb.ValueMember]))

                        return true;

                }

                return false;

            }

     

     

    Hope this helps

    Wen Yuan

    2008年5月14日 下午 03:52

所有回覆

  • Which combobox control you are using? Also, is that the data from SQL are properly trimmed (remove whitespace)? I wrote a simple program and it works for me, below is the excerpt:

     

    Code Snippet
                //
                // comboBox1
                // 
                this.comboBox1.Items.AddRange(new object[] {
                "abc",
                "def",
                "ijk",
                "xyz"});

     

    Code Snippet

            private void button1_Click(object sender, EventArgs e)
            {
                if (comboBox1.Items.Contains(textBox1.Text))
                    MessageBox.Show("Yes!");
            }

    2008年5月12日 上午 01:42
  • Hi Jack

    Thanks for your information.

    I tried to use LTRIM and RTRIM on my SQL Query
    E.G.

    SQL Code
    ---------
    SELECT LTRIM(RTRIM(ValueCol)) AS ValueCol
    FROM MyTABLE


    VB Code
    ------
    ddl_AnalysisCode.DataSource=tbl '(the table is retrieve from Stored procedure from the SQL Above)

    ddl_ValueCol.DisplayMember="ValueCol"
    ddl_ValueCol.ValueMember="ValueCol"


    on the Button Click Event I have the code below

      MsgBox(ddl_ValueCol.SelectedValue)
      Dim a As String = ddl_ValueCol.SelectedValue
      MsgBox(ddl_ValueCol.Items.Contains(a))

    The first pop up display the selected value
    and the second pop up box always display false =(

    I am not sure what it like that.

    Could you give me some more suggestion please?
    2008年5月12日 上午 08:24
  • Hello ChiYau, 

     

    Contains() method checks the exact object which contained in this ComboBox. Because your ComboBox binds with the data in DataTable, the underling object in each item is DataRowView type. String typed value isn’t equal with DataRowView typed object. Thereby, contains method always retrun false.

     

    Please try the following method.

           

    Code Snippet

    private void button1_Click(object sender, EventArgs e)

            {

                string a=this.comboBox1.SelectedValue.ToString();  

                bool c = checkValue(a, this.comboBox1);

                MessageBox.Show(c.ToString());

               

            }

     

            public bool checkValue(string Value,ComboBox cb)

            {

                foreach (System.Data.DataRowView drv in cb.Items)

                {

                    if (Value.Equals(drv[cb.ValueMember]))

                        return true;

                }

                return false;

            }

     

     

    Hope this helps

    Wen Yuan

    2008年5月14日 下午 03:52
  • Hi Wen Yuan

    Thanks very much for your explaination, I understood now.

    Thanks a lot,

    2008年5月15日 上午 08:27