Solved

Getting data from a ListView into a TextEdit box

Posted on 2004-04-23
17
1,922 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
  • 10
  • 4
  • 3
17 Comments
 
LVL 13

Expert Comment

by:SteH
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 13

Expert Comment

by:SteH
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 9

Expert Comment

by:Cayce
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
I really need to stick with the TListView.
0
 

Author Comment

by:marhk51
Comment Utility
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
Comment Utility
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
Comment Utility
That doesn't seem to work, I just get an error about improper use of AnsiString ?
0
 

Author Comment

by:marhk51
Comment Utility
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
Comment Utility
It's OK, I found a way to make it work, I just used the StrToInt.

Cheers for your help.
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

772 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now