Hi, this is my first post here so I am not quite sure how to enter codes properly, but I will do my best to explain my problem anyway...

Suppose I have a new form (Form1), and I insert a combo box (ComboBox1), and the items for ComboBox1 are 1, 2, 3, and 4 (in that order).
Here is a sample of my code that I want examined and fixed:

[B]procedure[/B] TForm1.ComboBox1Change(Sender: TObject);
[B]begin[/B]
 [B]if[/B] ComboBox1.Text<>'4' [B]then[/B]
  [B]begin[/B]
   [B]if[/B] MessageDlg('Did you mean "4"?',mtConfirmation,[mbYes,mbNo],0)=mrYes [B]then[/B]
    [B]begin[/B]
     ComboBox1.Text := '4'
    [B]end[/B]
  [B]end[/B]
[B]end[/B];

To me, this code reads in English "If I don't select "4" from the combo box selection, I will receive a popup asking me if I would like to select "4", and if I click yes, then the item selected in the combo box will be "4". However, this is not what happens, in fact, nothing happens at all, the message box goes away (like it should), but the item in the combo box remains the same as what was selected.

I even tried replacing ComboBox1.Text := '4' with ComboBox1.Text := ComboBox1.Items[4] which should select the fourth item in TStrings, with the fourth item of course being "4", but again nothing happens. And if you were wondering, nothing happens if I replace ComboBox1.Text := ComboBox1.Items[4] with ComboBox1.Text := ComboBox1.Items.Strings[4] either.

I read somewhere that the line Form1.SetFocus; should fix this problem, but it doesn't, and I even tried ComboBox1.SetFocus; to no avail.

This is of course not a replica of what I am trying to achieve, however this is a simple demonstration that could help resolve the only issue that I am having with the functionality of my program.

Any help with this issue would be very much appreciated

Thanks
- Adam

first thing to do is put a break point on the if combobox1.text <> 4 line and see what combobox1.text value really is and confirm its running the code

Thank you for your prompt response Liz. I tried adding break points in two different positions individually and all of them seemed to halt the program. I tried the [B]if[/B] ComboBox1.Text <> '4' line like you suggested, and I also tried the ComboBox1.Text := '4' line within the [B]if[/B] MessageDlg statement.

Now for something even weirder!!! I added two labels, and before the ComboBox1.Text := '4' line, I added the line Label1.Caption := ComboBox1.Text and after the ComboBox1.Text := '4' line I added the line Label2.Caption := ComboBox1.Text . Now what happens when the prompt comes up and I click "Yes" is that Label 1 now displays the selected value from Combo Box 1, and Label 2 displays '4', but the value inside Combo Box 1 is still not changing! The labels are changing as I expected them to, which is a good thing (as it shows that my code is correct and that it is in fact running properly), but it doesn't explain why the value of the combo box isn't changing.

This is really weird and is very annoying, as this is the only bug in my program. If the script is running, why is the value of ComboBox1.Text not changing???!!!!

Sure there are no spaces? which was the point of the break point? Why not

the value in combox must be changing if the code youve said (eg the labels) is true. Hence Ive asked about the spaces

Yes I am positive that there are no spaces, as when I select "4" from the combo box, the dialog doesn't appear, which means that I am selecting '4' and not '4 ' or ' 4'. Have you tested this code? Does it work for you?

Thanks
- Adam

Well a short bit of debugging would show you part of the problem.

Try just changing the index of the selected item if its not the right one.

I added some code that basically told the program to change all entries in Combo Box 1 to '4', and then I ran the program, and after clicking "Yes", the combo box is blank. I then added a line telling the program to select the first item in the item index, but this also had no effect. After clicking "Yes" from the dialog, the combo box is just blank, but if I click on the drop-down arrow, I have four items to choose from and they are all "4", so the entire script is working except changing the value of ComboBox1.Text to "4"...

As I said change the selected item..

OMG this is soooo stupid!!!! I thought it was silly that you were asking me to change the selected item because that's what I've been trying to do the whole time and it's not working. Then I realised...the ItemIndex for the value '4' is not 4, it is 3, because the count starts at 0 and not 1. So I put a line of code ComboBox1.ItemIndex := 3 and voila it worked!!!
Thankyou for your patience Liz, I was a stupid customer!!!

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.