CGDevTools Forum

Welcome to the Official CGDevTools Support Community Forums.

TIWCGJQComboBoxEx causes AV during lookup

by dfields » 17 Dec 2014 22:19

The TIWCGJQComboBoxEx component causes an access violation when calling the ItemsByValue method with a value that is not in the list.

I populate the items with 3 entries where the Caption and Value properties are set to the same value: "aaa", "bbb" and "ccc". I add an IWCGJQEdit on the form. Next, I drop a button on the form and set its OnChange event to a single line of code:

{code}
procedure TIWForm1.Button1JQButtonOptionsClick(Sender: TObject; AParams: TStringList);
begin
ComboBoxEx1.SelectedIndex := ComboBoxEx1.Items.ItemsByValue[IWCGJQEdit1.Text].Index;
end;
{code}

If the value in the Edit is set to any of the values in the list, everything works fine. If I enter any value that is not in the items, such as "123", the error is generated.

Is there a property that can be set to prevent this?
dfields
 
Posts: 130
Joined: 24 Jul 2013 20:14

by ScottWGast » 17 Dec 2014 22:52

It's my guess that the value of .Index is -1.

Scott
ScottWGast
 
Posts: 875
Joined: 23 May 2012 11:02

by Jorge Sousa » 17 Dec 2014 23:30

Hello Daniel

Can you please submit a test case please? t.i.a.

Is AV an Access Violation? or any other exception?
Best Regards
CGDevTools Develop / Support Team
Home Page: http://www.cgdevtools.com
Jorge Sousa
 
Posts: 4261
Joined: 17 May 2012 09:58

by dfields » 17 Dec 2014 23:35

Here is the test case I used. I did mean "access violation" by AV.
You do not have the required permissions to view the files attached to this post.
dfields
 
Posts: 130
Joined: 24 Jul 2013 20:14

by Jorge Sousa » 18 Dec 2014 10:51

Hi Daniel

What happens if there are no items for

ComboBoxEx1.Items.ItemsByValue[IWCGJQEdit1.Text]?

Do you imagine what is the return?
Best Regards
CGDevTools Develop / Support Team
Home Page: http://www.cgdevtools.com
Jorge Sousa
 
Posts: 4261
Joined: 17 May 2012 09:58

by ScottWGast » 18 Dec 2014 17:24

If IWCGJQEdit1.Text does not contain a valid ComboBoxEx1.Items.ItemsByValue "value", then I'm not surprised if you get an AV when you hit this line:

ComboBoxEx1.SelectedIndex := ComboBoxEx1.Items.ItemsByValue[IWCGJQEdit1.Text].Index;

because

ComboBoxEx1.Items.ItemsByValue[IWCGJQEdit1.Text].Index;

will fail.
ScottWGast
 
Posts: 875
Joined: 23 May 2012 11:02

by Jorge Sousa » 18 Dec 2014 19:59

Hi Scott

I'm not surprised if you get an AV when you hit this line


Exactly!

ComboBoxEx1.Items.ItemsByValue[IWCGJQEdit1.Text]

will return nil

if there are no items for value IWCGJQEdit1.Text
Best Regards
CGDevTools Develop / Support Team
Home Page: http://www.cgdevtools.com
Jorge Sousa
 
Posts: 4261
Joined: 17 May 2012 09:58

by dfields » 18 Dec 2014 20:51

What? Are you saying that the designed behavior is to generate the error when searching for a value that is not in the list? I expect that it would it to return -1 or nil, but the error is completely unexpected.

When I use a normal ComboBox I can use a similar call without getting the error. If there is not a match I get -1 without an error.

Code: Select all
ComboBox1.ItemIndex := ComboBox1.Items.IndexOf(IWCGJQEdit1.Text);



Based upon what you are telling me I would have to add extra code any place I want to use a ComboBoxEx.

Code: Select all
try
  ComboBoxEx1.SelectedIndex := ComboBoxEx1.Items.ItemsByValue[IWCGJQEdit1.Text].Index;
except
  ComboBoxEx1.SelectedIndex := -1;
end;


So you are saying it is not a bug, but designed behavior that the control should function this way?
dfields
 
Posts: 130
Joined: 24 Jul 2013 20:14

by ScottWGast » 18 Dec 2014 23:03

The error is not caused by the .ItemsByValue "search", per se. The AV comes from attempting to access .Index when the search fails.

ComboBox1.Items.IndexOf method automagically returns -1 if the string is not found.

ComboBoxEx1.Items.ItemsByValue[IWCGJQEdit1.Text] returns nil if the string is not found; if the string is found, then it returns an instance of TIWCGJQComboBoxExItem.

You will need to check to see if ComboBoxEx1.Items.ItemsByValue[IWCGJQEdit1.Text] is nil before attempting to check the .Index property.
If it returns nil, my suggestion would be to add the manually entered string to the items list and then set the .SelectedIndex:
Code: Select all
if (ComboBoxEx1.Items.ItemsByValue[IWCGJQEdit1.Text] = nil) then
    ComboBoxEx1.Items.AddOption(IWCGJQEdit1.Text, IWCGJQEdit1.Text));

ComboBoxEx1.SelectedIndex := ComboBoxEx1.Items.ItemsByValue[IWCGJQEdit1.Text].Index;


HTH!

Scott
ScottWGast
 
Posts: 875
Joined: 23 May 2012 11:02

by Jorge Sousa » 19 Dec 2014 01:16

Hi Daniel

property Items is a collection

Items.ItemsByValue returns the collection Item class TIWCGJQComboBoxExItem if found if not, returns nil

so you will have to do

var
Item: TIWCGJQComboBoxExItem;
begin
Item:= ComboBoxEx1.Items.ItemsByValue[IWCGJQEdit1.Text];
if Assigned(Item) then
ComboBoxEx1.SelectedIndex := Item.Index
else
ComboBoxEx1.SelectedIndex := -1;
end;

or just...

ComboBoxEx1.SelectByValue(IWCGJQEdit1.Text);

and we don't care how other controls work, because we know that we are doing right.
Best Regards
CGDevTools Develop / Support Team
Home Page: http://www.cgdevtools.com
Jorge Sousa
 
Posts: 4261
Joined: 17 May 2012 09:58

Next

Return to General

cron

Who is online

Users browsing this forum: No registered users and 77 guests

Contact Us.