Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Yesterday I asked a question with the hopes of being pointed in the right direction.

Quote:
using listbox as a dialog box.":


Thank you to both Nishant Sivakumar and SAKryukov for your response in the matter.
Upon googling "Logging LIstbox" and "send string to Listbox" I still have problems finding the answers as to how I am to do this.
I am under the impression that I should be able to do this: this -> listbox1 -> add.
in the code. The results tell me I am missing some key information.

So,
Question 1: How do I do this?
Question 2: Nishant, does your book delve into this area?
Question 3: any other resources that are beyond the basic forms?

I hope I my questions are clear enough.
Posted
Updated 16-Nov-11 15:59pm
v2
Comments
Sergey Alexandrovich Kryukov 16-Nov-11 19:26pm    
You still don't tag you UI library. Please do it: WPF, Forms, what?
The answer is just one or two lines, but I don't want to write two or more variants of code...
--SA
Member 8401669 16-Nov-11 22:00pm    
Sorry about the tagging issue, was not aware of the ability to add multiple phrases.
Sergey Alexandrovich Kryukov 17-Nov-11 1:44am    
All right, thank you, now you can get the answer. Please see my solution.
Any your follow-up questions are welcome.
--SA
Steve Wellens 16-Nov-11 23:25pm    
Add the code you tried and the exact error message.
Member 8401669 17-Nov-11 0:17am    
hr = m_directoutput.GetDeviceType(hDevice, &typeguid);
if (FAILED(hr))
{
this -> listbox1 -> show(L "failed")
}

1 solution

Something like that:
C#
ListBox ListBoxLog = new ListBox();

//...

internal void Log(string message) {
    ListBoxLog.Items.Add(message);
    ListBoxLog.SelectedIndex = ListBoxLog.Items.Count - 1;
}


You may also want to make it thread-safe, more exactly, to make it possible to add message from non-UI thread. It's just a bit more complex:

C#
ListBox ListBoxLog = new ListBox();

//...

static void Log(ListBox listBox, string message) {
    listBox.Items.Add(message);
    listBox.SelectedIndex = listBox.Items.Count - 1;
}

internal void Log(string message) {
    if (ListBoxLog.InvokeRequred) //if called from any other thread...
        ListBoxLog.Invoke(new Action<ListBox, string>((listBox, msg)=>{
            Log(listBox, msg);
        }), ListBoxLog, message);
    else //if called from the same thread as the UI of ListBoxLog:
        Log(ListBoxLog, message);
}


Finally, you may want to store items of any type in a list box, not just strings. It could be very useful to store additional information in the items, such as type of message, topic, exception information, etc. On selection events of the list box, you could get this data from an item and show it in, say PropertyGrid in the style of master-detail view.

You can do it, but the problem will be: what will be presented as a text of view item? The answer is simple: whatever is returned by the method ToString. For this purpose, you just need to override object.ToString in the type of a list item to present appropriate part of data in the form of one short string.

—SA
 
Share this answer
 
v2
Comments
Abhinav S 17-Nov-11 1:48am    
5. Should help the OP.
Sergey Alexandrovich Kryukov 17-Nov-11 1:48am    
You are too quick! Thank you, Abhinav.
--SA
Member 8401669 17-Nov-11 3:39am    
Much thanks,
This should give me enough to keep me busy for a while. I will call it solved for now.
Now i am off to increase my knowledge!!!
Oh, Abhinav,
While I appreciate your positive attitude , SAKryukov's warning is well in the ball park. I'm a Newbie !!!!!
Sergey Alexandrovich Kryukov 17-Nov-11 11:21am    
Great; you are welcome.
Sorry, I cannot remember where I warned you (I could have).
--SA
RKnGl 17-Nov-11 3:53am    
Very lean and mean ... my 5.. I suggest SAKryukov writes an article on thread safe messaging regarding common UI elements .. The way this answer was explained promises very nice read (of course , just an idea :D )

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900