Solved

Getting data from a ListView into a TextEdit box

Posted on 2004-04-23
17
1,960 Views
Last Modified: 2013-11-17
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.
0
Comment
Question by:marhk51
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 4
  • 3
17 Comments
 
LVL 13

Expert Comment

by:SteH
ID: 10897708
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.
0
 

Author Comment

by:marhk51
ID: 10897736
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.
0
 

Author Comment

by:marhk51
ID: 10897762
Oh, How do I launch a website from my code, the website address should be coded in, it is not passed as a string.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 13

Expert Comment

by:SteH
ID: 10899634
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.
0
 

Author Comment

by:marhk51
ID: 10899808
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.
0
 
LVL 9

Expert Comment

by:Cayce
ID: 10900673
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;
}
0
 
LVL 13

Expert Comment

by:SteH
ID: 10900816
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.
0
 

Author Comment

by:marhk51
ID: 10901888
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 ?
0
 
LVL 9

Expert Comment

by:Cayce
ID: 10901937
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;
}
0
 

Author Comment

by:marhk51
ID: 10901968
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.
0
 
LVL 9

Expert Comment

by:Cayce
ID: 10902482
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).
0
 

Author Comment

by:marhk51
ID: 10904158
I really need to stick with the TListView.
0
 

Author Comment

by:marhk51
ID: 10904338
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.
0
 
LVL 9

Accepted Solution

by:
Cayce earned 50 total points
ID: 10904868
The first one is the CAPTION property.

Item->Caption is the first one.

Hmm. I would think of (C++Builder Code):

AnsiString UserID(int userID) {
  if(userID <0 || userID > 9999)
    throw Exception("Invalid user ID.");
  char buffer[10];
  sprintf(buffer, "%04d", userID);
  return AnsiString(buffer);
}
0
 

Author Comment

by:marhk51
ID: 10906747
That doesn't seem to work, I just get an error about improper use of AnsiString ?
0
 

Author Comment

by:marhk51
ID: 10906831
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.
0
 

Author Comment

by:marhk51
ID: 10906844
It's OK, I found a way to make it work, I just used the StrToInt.

Cheers for your help.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

696 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question