9 Replies - 5392 Views - Last Post: 20 April 2012 - 09:07 PM Rate Topic: -----

#1 StickHerHappy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-January 12

Can't compare ComboBox Item with String

Posted 10 March 2012 - 08:25 AM

I have a Combobox and created it and add some Items in runtime.. but when I compare it to a string, It doesn't compare , I used this following syntax:(combobox1.Text = "Clear"), (combobox1.SelectedText = "Clear") and
(combobox1.SelectedItem = "Clear", and still nothing works.. can you help me with this problem
Is This A Good Question/Topic? 0
  • +

Replies To: Can't compare ComboBox Item with String

#2 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • Joined: 02-June 10

Re: Can't compare ComboBox Item with String

Posted 10 March 2012 - 08:55 AM

Please post your actual code, because this doesn't help us.
Copy it from Visual Studio, paste it in the post, select it, then click the code button in the toolbar to wrap code tags around it
:code:

It isn't even a comparrison, it is an assignment
Single = is an assignment operation
Double == is a comparrison.

if (combobox1.SelectedText == "clear")
{
   // Do this
}

This post has been edited by tlhIn`toq: 10 March 2012 - 08:55 AM

Was This Post Helpful? 0
  • +
  • -

#3 StickHerHappy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-January 12

Re: Can't compare ComboBox Item with String

Posted 10 March 2012 - 09:04 AM

actually sir I tried that to but I am getting Error: "Possible unintented reference comparison; to get value comparison, cast the left hand side type to 'string'"

This post has been edited by tlhIn`toq: 10 March 2012 - 09:55 AM

Was This Post Helpful? 0
  • +
  • -

#4 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • Joined: 02-June 10

Re: Can't compare ComboBox Item with String

Posted 10 March 2012 - 09:55 AM

Judging by that error you tried to compare a string to your combobox object itself, and not the text it was displaying.

Show us your actually code if you want help.

Or don't show us. That's your choice.
Was This Post Helpful? 0
  • +
  • -

#5 StickHerHappy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-January 12

Re: Can't compare ComboBox Item with String

Posted 10 March 2012 - 10:10 AM

here is my full code sir

string Input_Rec = "SELECT ID,Stud_Name,Pre_Q1,Pre_Q2,Pre_Q3,Pre_Q4,Pre_Proj,Pre_Major,Pre_Total,Mid_Q1,Mid_Q2,Mid_Q3,Mid_Q4,Mid_Proj,Mid_Major,Mid_Total,Fin_Q1,Fin_Q2,Fin_Q3,Fin_Q4,Fin_Proj,Fin_Major,Fin_Total FROM Student WHERE ID=" + textBox1.Text;
            
connect.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Directory\sample.accdb;Persist Security Info=False;";
            connect.Open();
            OleDbCommand command = new OleDbCommand(Input_Rec, connect);
            OleDbDataReader reader = command.ExecuteReader();

if (reader.Read())
            {
                textBox1.Enabled = false;
                Controls.Remove(button1);
                Label Name = new Label();
                Name.Text = "Name";
                Name.Width = 38;
                Name.Height = 13;
                Name.Location = new Point(27, 58);
                this.Controls.Add(Name);
                TextBox text = new TextBox();
                text.Name = "textbox2";
                text.Width = 121;
                text.Height = 20;
                text.Location = new Point(109, 55);
                this.Controls.Add(text);
                text.Text = reader[1].ToString();
                text.Enabled = false;
                Label Quarter = new Label();
                Quarter.Text = "Quarter";
                Quarter.Width = 45;
                Quarter.Height = 13;
                Quarter.Location = new Point(27, 87);
                Controls.Add(Quarter);
                ComboBox cbq = new ComboBox();
                cbq.Location = new Point(109, 87);
                Controls.Add(cbq);

if (reader[2] != DBNull.Value && reader[3] != DBNull.Value && reader[4] != DBNull.Value && reader[5] != DBNull.Value && reader[6] != DBNull.Value && reader[7] != DBNull.Value && reader[8] != DBNull.Value)
                        cbq.Items.Add("Prelims");
                
if (reader[9] != DBNull.Value && reader[10] != DBNull.Value && reader[11] != DBNull.Value && reader[12] != DBNull.Value && reader[13] != DBNull.Value && reader[14] != DBNull.Value && reader[15] != DBNull.Value)
                        cbq.Items.Add("Midterms");

if (reader[16] != DBNull.Value && reader[17] != DBNull.Value && reader[18] != DBNull.Value && reader[19] != DBNull.Value && reader[20] != DBNull.Value && reader[21] != DBNull.Value && reader[22] != DBNull.Value)
                        cbq.Items.Add("Finals");



Was This Post Helpful? 0
  • +
  • -

#6 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • Joined: 02-June 10

Re: Can't compare ComboBox Item with String

Posted 10 March 2012 - 10:30 AM

So where are you actually trying to make your comparison to the text of combobox1 ? That is not in this code that I can see.
Was This Post Helpful? 0
  • +
  • -

#7 StickHerHappy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 15-January 12

Re: Can't compare ComboBox Item with String

Posted 10 March 2012 - 10:36 AM

ohh sir sorry I forgot to include, after the last if statement:
      if(cbq.SelectedText == ....)



and my combobox name is cbq that I want to compare , I used combobox1 only for common use
Was This Post Helpful? 0
  • +
  • -

#8 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5436
  • View blog
  • Posts: 11,653
  • Joined: 02-June 10

Re: Can't compare ComboBox Item with String

Posted 10 March 2012 - 11:13 AM

Well... This obviously isn't the actual code in your program because you can't be compiling with "....."

I'm done. I'm unsubscribing from this thread. I have other things to do besides interrogate someone for information when they are asking for help. The harder you make it on everyone else the less they are inclined to keep beating their head against a wall.

Here are some tutorials on how to debug.

See FAQ # 2. (Click the SHOW button below)






rookie What this shows us is that you aren't familiar with breakpoints and how to debug your own code. Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute. Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2


FAQ (Frequently Asked Questions - Updated Feb 2012
Spoiler



Was This Post Helpful? 1
  • +
  • -

#9 harry4u  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 20-February 10

Re: Can't compare ComboBox Item with String

Posted 20 April 2012 - 08:43 PM

someone has removed my post.
Did i post in a wrong section or something ??
Was This Post Helpful? 0
  • +
  • -

#10 superkb10  Icon User is offline

  • D.I.C Regular

Reputation: 29
  • View blog
  • Posts: 298
  • Joined: 27-November 11

Re: Can't compare ComboBox Item with String

Posted 20 April 2012 - 09:07 PM

There's an extremely easy way of doing this. Just read the error message, cast Combobox.SelectedText or Combobox.SelectedItem to as string using (string) or you can just use the ToString() method.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1