Link to home
Start Free TrialLog in
Avatar of marhk51
marhk51

asked on

Getting data from a ListView into a TextEdit box

Hi,

I am pretty new to C++ so you'll have to bear with me.

I am using Borland C++ Builder 6 on a Windows platform.

I am using the ListView box to display my data. I noticed that when you click on a row inside this box it gets selected, is there anyway when I dbl click on a certain row to get one of the colums data to be diplayed in a TextEdit box on the same form.

Each row has 3 colums, Index, Userid, username. I need to the userid of the row selected to be displayed in the TextEdit box called useridEdit.

Thanks in advance.
Avatar of Member_2_1001466
Member_2_1001466

Have BCB not at hand so I give the answer from my head. There might be some faults but feel free to ask if there are. Can check later for correctness.

Create a function that handles double clicks on the listview. In it get the selected row and use Rows[1] to get the AnsiString of userid. Set this to the editbox next.

This function has then be selected from object viewer -> events tab in the line for double click event.
Avatar of marhk51

ASKER

Ok, I know how to do most of that but how do write the code to get the selected rows ? Thats the bit I was stuck with.

Regards.
Avatar of marhk51

ASKER

Oh, How do I launch a website from my code, the website address should be coded in, it is not passed as a string.
Found some source files here: try
Listbox->ItemIndex
to get the currently selected row.

I did something similar but switch to a TStringGrid for its flexibility in addressing cells. Either single cells, rows or columns are easily accessible. And it is easy to resize columns using headers without writing a single line of code.
Avatar of marhk51

ASKER

Sorry, I must not of explained myself properly, the box is not a dropt down list box, it is one of those large boxes which can have multiple columns, each one of the colums has a clickable header, you know which box I mean now.

Your code above works alright to return the index of that row, but I need to return a specif column.
I have tried : useridEdit->Text = UserListView->Column[1]; but that just throws an error.

Thanks.
Avatar of Cayce
Hello Mahrk,

It's actually pretty easy to do:

Go to the designer and create and event handler for the ListView OnSelectItem event.

That will create a function just like this:

void __fastcall TForm1::ListView1OnSelectItem(TObject* Sender, TLisItem* Item, bool Selected) {
}

Now on this event handler all you need is to change the text on the edit box:

void __fastcall TForm1::ListView1OnSelectItem(TObject* Sender, TLisItem* Item, bool Selected) {
  Edit1->Text = Item->Caption;
}
Can't reply with more background until wednesday. But perhaps than I can give better help.

What I used where listboxes which show a single string in each line. Columns I had to do myself with formatting the string. But I went to string grids exactly to be able to use the control to store all info I needed and not have a seperate class to define the columns seperatly.
TListView reflects a bit the CListCtrl of MFC which handles report views in a very complicated way in my opinion. TStringGrid allow direct access to each cell; row- and columnwise. They can be more seen as a spreadsheet like ctrl. But without Builder at hand I can't tell you how to do what you need exactly.
General structure of the windows class behind is that you can access things rowwise. From the selected row you get some class (TListItem?) in which you need to find the correct place for the column you are looking at.
Avatar of marhk51

ASKER

Cayce,

Ok, so far, this is what I have got now:

void __fastcall TMainForm::UserListViewSelectItem(TObject *Sender, TListItem *Item, bool Selected)
{
        useridEdit->Text = UserListView->Column[1]->Caption;
}

But all this does is just copies the column header name (which is 'User ID') in the text edit box, I need it to copy the data from the row I have selected. I assume I need to replace caption with something else ?
that's because you're doing exactly that with the code.

void __fastcall TMainForm::UserListViewSelectItem(TObject *Sender, TListItem *Item, bool Selected)
{
        useridEdit->Text = Item->Caption;
}
Avatar of marhk51

ASKER

Ok, changed the code, that now returns the value from my first column which is the index number, I need it to return the value of the second column.
Sorry, but I had to post on haste on the previous one.

Basically what you need is to copy the data from a ROW (?) into the editbox. Well, the thing is that you may not be using the right component.

TListView is good for displaying items with some properties, such as files or folders. But basicalyl if all you want is to use the spreadsheet like (Report) view of the TListView, you'd be better using TStringGrid.

When you address the Column property of a TListView you-re actually addressing the DEFINITION of the Report View Column Description, not the data itself. The data of the columns on a TListView is saved on the SubItems you create for each item. However TListItems have a Data property (void*) which you can use to store any data related to a ListView.

If you need examples on this I could post some code. But that would have to wait until monday when I go back to office (out of town right now). I've work a lot with TListViews and I think I can be of use. However it looks to me that you would be better using TStringGrids (unlessm you want the abilitie to change the view from report to list, icons, etc).
Avatar of marhk51

ASKER

I really need to stick with the TListView.
Avatar of marhk51

ASKER

Thanks for that but I worked it out:

useridEdit->Text = Item->SubItems->Strings[0];

seems to work fine, just one question; their are 3 columns, so how come '0' selects the second one and '1' selects the third one ?

Anyway, I'll award the points if you can help with this easy question:

I always need my userID's returned in a 4 digit format i.e. 0000, 0001, 0010 etc. The problem is that the hardware we are interfacing with only returns the userid without the leading 0's, so my question is, how do I add them back on again.

i.e. If a user exists with a id of 0001 or 0010, the hardware will return that to our software as 1 or 10, we need to change it so the end user see's 0001 or 0010, bear in mind that a userid can only be a maximum of 4 digits.
ASKER CERTIFIED SOLUTION
Avatar of Cayce
Cayce
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of marhk51

ASKER

That doesn't seem to work, I just get an error about improper use of AnsiString ?
Avatar of marhk51

ASKER

Sorry that seems to work fine, it was my fault.

One problem though, how do I pass the seleted userid to your function as I get an error saying cannot conver 'AnsiString' to 'int'. This is the code I am using to pass to your function:

useridEdit->Text = convertUserID(Item->SubItems->Strings[0]);

This is the function to which it calls:

AnsiString convertUserID(int userID) {
  char buffer[10];
  sprintf(buffer, "%04d", userID);
  return AnsiString(buffer);
}

I removed some lines of code as all the function needs to do is format the userid, it doesn't have to throw exceptions etc.
Avatar of marhk51

ASKER

It's OK, I found a way to make it work, I just used the StrToInt.

Cheers for your help.