Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Another simple ListBox question 3

Status
Not open for further replies.

stylonurus

Programmer
Jun 7, 2006
19
US
I really appreciate the response I got yesterday. Thanks so much. I've been working three months on a server-client program and being new to the Borland Build C++ environment I've had to be persistent in order to get my application to work.

I have just completed a chat protion of my project. When I receive a Chat string I merely insert it into the bottom of a listbox. The box has a black background, and either bright yellow or bright green text. After I insert the text, a solid blue background covers the latest line making the text hard to read. I'd rather not see it it at all. Is there anyway to remove it. Thanks
Rick
 
After I insert the text, a solid blue background covers the latest line making the text hard to read.

That's because that line is selected in the listbox. That's the purpose of a listbox, is to supply a list of items that a user can select from. You can change it by taking control of the OnDrawItem event and drawing the items in the listbox yourself. But before even talking about that, is there a good reason for using a listbox to display chat messages? I would think you would prefer the functionality and flexibility that you would have with a TRichEdit, or at the very least a TMemo component.

I really appreciate the response I got yesterday.
Yes, there are very helpful people here. If someone like 2ffat helps you out (and 2ffat spends a lot of time answering questions here), it's probably a good idea to give him a
star.gif
by clicking on the "Thank 2ffat for this valuable post!" link at the bottom of his post. He'll be much more likely to want to help next time [smile].
 
Good suggestion. I will try a TRichEdit object and see if that gives me more flexibility. I appreciate it.
Rick
 
I think you'll be much happier using a RichEdit, unless as I said before, there's some specific reason for using a ListBox that I don't know about. If that's the case, post again & I'll post some code showing how to take control of the OnDrawItem event to do what you need.

The RichEdit component will allow you to change colors, fonts etc. as well as bolding, italicizing, underlining and more if you ever want to add additional features to your chat client. It will also allow the user to copy/paste which could prove valuable.

If you do use a RichEdit for your messages, you'll still have the same scrolling problem you had with the ListBox -- it won't automatically scroll down as you add lines. You can do the following to scroll to the bottom each time:

Code:
  RichEdit1->Lines->Add(MyString);
  RichEdit1->SelStart = RichEdit1->Text.Length();
  RichEdit1->Perform(EM_SCROLLCARET,0,0);

Good luck!
 
itsgsd,
Again thanks so much. I was trying to figure this out for the last hour or so. So your post made my day. And it worked. So, it looks like the RichEdit box is going to work for me. I've added vertical scroll bar to it and have made it Read Only.

When I was using List Box I ran the following code (OnDrawItem) to update the viewable display lines with the appropriate text color (green for client, yellow for server)from a separate array which held the correct color for each line.

Code:
ListBox1->Canvas->Font->Color = (TColor) ChatInputLineCountArray[Index];
ListBox->Canvas->TextOut(Rect.Left+1, Rect.Top+1, ChatOutput->Items->Strings[Index].c_str());

Well a bit cumbersome to say the least but it worked. [blue] I take it that I can assign a color to each line in the RichEdit box as I add it. After I assign it I don't need to refresh it when it is scrolled. Is my assuption correct? [/blue]

Thanks

Rick
 
I take it that I can assign a color to each line in the RichEdit box as I add it. After I assign it I don't need to refresh it when it is scrolled. Is my assuption correct?
Yes, your assumption is correct, the following should work:
Code:
// First make sure the cursor is at the end of the text
RichEdit1->SelStart = RichEdit1->Text.Length();

// Now set the font color, you could also set any other font attributes here i.e. bold, underline, italics
RichEdit1->SelAttributes->Color = myColor;

// Finally add the line and scroll down, see the earlier post above.

You could (although not as simple as setting the font color) set things like subscript, superscript, background (highlight) color. If you need, I could also post code showing how that is done.

Hope that makes sense -- although you seem to be ahead of the average newbie who posts here, so I'm sure you'll get it . And hey, you know how to spell separate [smarty] (a rare quality) Kudos!
 
itsgsd,
Again your snippets of code worked perfectly. To say the least I owe you. This would of taken me hours of trial and error hacking away to find the solution. I appreciate your help very much.
The RichEdit box is now working perfectly. I think I can figure out how to change the font etc. with the information you provided.
I was wondering; Being a newbie to the the Borland C++ Builder 6 environment, are there any good reference books that explain objects like how/when to use RichEdit vs. ListBox etc.
My background is pretty much the straight C embedded world, but after I ported our code base from vxWorks to W2K a couple years ago, I have been working more in the Win32 API world. Now with this new client/server app I'm developing I'm into the another huge different arena. (I'm not complaining, I love new environments). I usually try to figure things out for myself but sometimes a helping hand can really help out. You sure supplied that. The fact that I spelled separate corrrectly has got to be pure luck. All the best to you and most likely I will be back here sometime
Rick
 
are there any good reference books that explain objects like how/when to use RichEdit vs. ListBox etc.

I'm not going to be of any great help here, I haven't really kept up with the books available out there. I can tell you what I have used, but others may have a better idea of good recommendations.

When initially starting with Builder, I ran through Charlie Calvert's C++ Builder Unleashed. It was simple and concise and is now freely available in electronic form on the web.

A bit later, I purchased the eBook "Borland C++ Builder Programming" from FuncionX Press ( which does an adequate job of going through many of the VCL components.

I also have "C++ Builder HOW-TO" by Miano, Cabanski and Howe which gives a good understanding of C++ Builder by first intdroducing problems and then develops applications to solve them. It was very informative and helpful and provided some "ah-hah" moments for me.

Sam's C++ Builder Developer's Guide also did a fair job and gave a more in-depth overview of Builder.

You may want to search for other posts here regarding books, I know there are plenty. Also, browse Amazon and look at user comments. Mainly, just get into builder and go through the component palette, see what's there and try to figure out what everything does. Then when you need to solve a problem, you'll be familiar with the components that are there & have a better idea of what fits.

Have fun!
 
I second C++ Builder HOW-TO. It may not be in print, though, so you may to really hunt to find it.

A couple of other resources: C++Builder Developer's Journal is an online mag that is great (IMHO). See . You can find me other there on their boards, too.

Also "C++Builder Commonly Asked Questions" ( is another good (and related) site. I think I pointed you to that once before.

James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
itsgsd , 2ffat,
Yes, I checked out the link 2ffat gave me for the bcbjounal site earlier today. I think I may subscribe to the publication and maybe order the CD. By the way, I appreciate all your suggestions. I've already ordered 5 books on C++ builder, but from your recommendations I've only got the SAM's publication so its great to hear some more suggestions.

Its been my experience that as soon as I get somewhat competent in something I'm off learning how to do something else.
I find the Builder 6 / C++ environment huge, extremely powerful, very very impressive and just downright awesome. Thanks again for all your help and have a great weekend
Sincerely,
Rick Eis
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top