How to select a newly added ListBox entry? - logic

I have a listbox (ListBox1) and a button (Button1). When I click on the button, a new item is added to the listbox. If I click on the newly added item, it is highlighted. If I add another item to the listbox, the previously selected item is still highlighted. I want it so that the item that I add gets highlighted (selected) automatically.
How do I do this?
Thanks in advance.

Assign the ItemIndex property of the list box. For example...
ListBox1.ItemIndex := ListBox1.Items.Count-1;
The -1 is because lists are 0 based.
Alternatively, you can obtain the index of the new item directly when you call Add(), so...
var
I: Integer;
begin
I:= ListBox1.Items.Add('Some Value');
ListBox1.ItemIndex := I;
end;
Or even more simple:
ListBox1.ItemIndex := ListBox1.Items.Add('Some Value');

Related

Delphi form menus

So I have two menu's. When I click a button on menu 1, it creates menu 2, then menu 1 hides itself. After that I want to go back to menu 1 and hide menu 2.
How do I keep a reference to menu 1 in menu 2? Then if I want to go back to menu 2 I don't want to create a new instance, I want to use the already created one. How do I do that?
I know this may be easy but I find Delphi extremley confusing and can't seem to find a way out of it.
Thanks!
If you mean Main menu when you say "form menu" then you can have only one actual TMainMenu component on a form.
Dynamically creating a new TMainMenu is doable, but involves message handling to postpone the deletion of the exisiting TMainMenu to then be able to create a new one. The reason is that you can not delete a menu in the OnClick handler of one of the menu's items.
Let me suggest an easier way to achieve something similar by hiding / showing branches of a single TMainMenu as follows:
Drop a TMainMenu on the form and write menu items as usual. In my example I created two branches, one named MenuA and the other MenuB. Under these I added menu items, of which the first one (you are free to choose which one you use) activates the other menu branch and hides it's own branch. And visa versa for the other one.
Here's the menu part of the form in text view:
object MainMenu1: TMainMenu
Left = 112
Top = 48
object MenuA: TMenuItem
Caption = 'MenuA'
object Item11: TMenuItem
Caption = 'MenuB'
OnClick = Item11Click
end
object Item12: TMenuItem
Caption = 'Item12'
end
end
object MenuB: TMenuItem
Caption = 'MenuB'
object Item21: TMenuItem
Caption = 'MenuA'
OnClick = Item21Click
end
object Item22: TMenuItem
Caption = 'Item22'
end
object Item23: TMenuItem
Caption = 'Item23'
end
end
end
And here is the code for the menu clicks. Note that I make the second menu branch hidden in the forms OnCreate even. Instead, you can of course set the Visible property to False at design time, in the Object Inspector.
procedure TForm9.FormCreate(Sender: TObject);
begin
MenuB.Visible := False;
end;
procedure TForm9.Item11Click(Sender: TObject);
begin
MenuB.Visible := True;
MenuA.Visible := False;
end;
procedure TForm9.Item21Click(Sender: TObject);
begin
MenuB.Visible := False;
MenuA.Visible := True;
end;

Custom styled Firemonkey list box item not highlighted on click on android and iOS

I'm building multiplatform( iOS, Android, OSX, Windows APP) in Firemonkey. One of the things I'm trying to do is create a custom listbox item( with more data elements) that will work on all these platforms:
will give you ability to select item(s), display properly.
According to research I did, probably the best way for this is to create custom style for list box item and define data elements there. That's what I did.
I'm creating items from client dataset in this procedure:
procedure TMasterDetailForm.LoadAvailable;
var i: Integer;
Item: TListBoxItem;
begin
lstAvailable.Clear;
//Add Header
lstAvailable.BeginUpdate;
Item := TListBoxItem.Create( lstAvailable );
Item.Parent := lstAvailable;
Item.Height := 70;
//Item.OnApplyStyleLookup := ListItemApplyStyleLookupHandler;
Item.StyleLookup := AvailableListHeaderStyle;
//Add Details
cdsAvailable.First;
for I := 1 to cdsAvailable.RecordCount do
begin
Item := TListBoxItem.Create( lstAvailable );
Item.Parent := lstAvailable;
Item.Height := 50;
//Item.Selectable := True;
//Item.OnApplyStyleLookup := ListItemApplyStyleLookupHandler;
Item.StyleLookup := AvailableListItemStyle;
//Item.StyleLookup := 'ListboxItem1Style1';
Item.StylesData[ txtWoNum ] := cdsAvailable.FieldByName( 'work package' ).AsString;
Item.StylesData[ txtAircraft ] := cdsAvailable.FieldByName('aircraft').AsString;
Item.StylesData[ txtTaskDescription ] := cdsAvailable.FieldByName('task').AsString;
cdsAvailable.Next;
end;
lstAvailable.EndUpdate;
end;
Everything gets styled properly on all platforms, except that tapping(clicking) on ListBoxItem on Android or iOS, doesn't highlight the ListBoxItem. If I unissign style then selecting items also works.I can't figure out how to fix this.
Btw, onclick event on ListBox seems to work properly( itemindex changes).
Any input will be greatly appreciated.
Edit( 12/12/2014) : I tried simplifying the example by adding items manually in the ListBox editor and discarding this code here, and I found out that animation for selecting the listbox item changes. So, I customized the listbox item and only changed TextColor to blue. In runtime on Android when you select the listbox item it just changes the color of the text to black instead of painting the whole row. Any ideas how to have listbox behave in similar way like when there is no style attached to it?
Sorry my english is bad.
I have a solution (tested in XE7):
Open a form
Change the IDE Style to "iOS"
Select the TListBox an open a context menu and select "Edit Default
Style": the StyleBook2 is created.
Add a TRectangle component in the style "listboxstyle/background"
with the name "selection". This is the magic!
Now, Firemonkey found the 'selection' component and work fine!
If you already have StyleBook2 component before these steps, you may need to delete it, be careful!

Firemonkey style in Listbox - retrieving data

I'm trying to get information from a Tlistbox in Firemonkey XE5 but it has an associated style where each item in the listbox includes an image, a memo and some buttons.
When clicking on the button inside the listbox style, I can get information from that item.
I want to get information from a the memo box in the listbox separately. Previously, I would have got the text from item 1 by using the following code:
NewString:=ListBox1.items[1];
However, now each item in the listbox has more than one piece of information.
I can add a new Listbox item using the code as follows:
var Item: TListBoxItem;
begin
Item := TListBoxItem.Create(nil);
Item.Parent := ListBox1;
Item.StyleLookup := 'PlaylistItem';
Item.StylesData['Memo1']:='test text';
But, how do I read just the memo box of a particular item
Thanks
Aman
Update.
The solution is
Tempstr:=ListBox1.ItemByIndex(1).StylesData['Memo1'].AsString;
I'm now trying to work out how to get an image out as there isn't a AsImage or AsBitmap suffix.
I would advise subclassing TListBoxItem, then adding properties and methods to get/set the data from the style objects using FindStyleResource,
class TMemoListBoxItem = class(TListBoxItem)
protected
function GetMemoText: String;
procedure SetMemoText(const Text: String);
published
property MemoText: String read GetMemoText write SetMemoText;
end;
function TMemoListBoxItem.GetMemoText: String;
var O: TFMXObject;
begin
O := FindStyleResource('Memo1');
if O is TMemo then
Result := TMemo(O).Text
else
Result := '';
end;
procedure TMemoListBoxItem.SetMemoText(const Text: String);
var O: TFMXObject;
begin
O := FindStyleResource('Memo1');
if O is TMemo then
TMemo(O).Text := Text;
end;
And continue likewise for your other data.

jqGrid inline delete: selected row "selrow" is incorrect

I have a in-line delete button, I want to append more data to the delete message pop-up like this:
"Delete selected row with code = 7 ?"
I'm using the following in the delOptions:
beforeShowForm: function ($form) {
var sel_id = $("#list").jqGrid('getGridParam', 'selrow');
$("td.delmsg", $form[0]).html("Delete record with <b>code=" + $("#list").jqGrid('getCell', sel_id, 'cd') + "</b>?");}
The problem is If I click on the delete button without first clicking on any part of the row, the selrow is either null or it gets the previously selected row not the currently selected!
How do I make the row selected when clicking on the trash bin icon?
Any help is appreciated
I suppose that you use the example which I posted in the old answer. It was written in case of usage of the Delete button (the part of form editing) from navigator bar.
There are one hidden row in the Delete dialog which could help you. Try this one
beforeShowForm: function ($form) {
// get comma separated list of ids of rows which will be delete
// in case of multiselect:true grid or just id of the row.
// In the code below we suppose that single row selection are used
var idOfDeletedRow = $("#DelData>td:nth-child(1)").text();
$form.find("td.delmsg").eq(0)
.html("Delete record with <b>code=" +
$(this).jqGrid('getCell', idOfDeletedRow, 'cd') + "</b>?");
// REMARK: in old versions of jqGrid you can't use $(this) and
// will have to use something like $("#list")
}

How to export the contents of a combo box to the datatable?

I want to export the contents of a combo box to the local data table so that I can parametrize the test using that value.
You need to get each item from the ComboBox. For example where Window Name and ComboBox Name are the names of your window and ComboBox respectively
comboBox = Window("Window Name").WinComboBox("ComboBox Name");
count = comboBox.GetItemsCount
For i = 0 to count-1
item = comboBox.GetItem(i)
' put the item in the DataTable...
Next
How you output each item, the ' put the item in the DataTable.. line, will depend on your set up.

Resources