Triggered ListBox event when items are added [C#/CS 2005]

Shaitan00

Junior Contributor
Joined
Aug 11, 2003
Messages
358
Location
Hell
I am using a ListBox (lbChat) as a chat box for the chat text of my chatting application - simply put when a user sends the server a message it is written (added) to the chat listbox as so:
Code:
lbChatBox.Items.Add(stext);
Where (stext) is a string of text that will be displayed, something like "Player1: Has Joined the Game" or "Player2: How was lunch?", etc.. you get the idea...
(quick note - I am using the LISTBOX because I thought it was the best control to use - if you have other suggestions that would be better then a listbox I am all ears...)

So, the way it works - Clients send text to the servers TCP Listener Threads which pick them up and then add them to the servers listbox (lbChat) as chat text using the method shown above.
The next step for me was now getting the SERVER to send the message to all the clients so that everyone sees the text - for this I was planning on using the ListBox Properties... (but running into trouble with that now)...

Now what I was planning on doing was - when Text is added to the Listbox (using the method shown above) I would use an event triggered but the fact that I just ADDED TEXT and use that event to send the ADDED TEXT to every client on my list...
This causes me two problems - a) I can't seem to find a LISTBOX EVENT fired everytime I add text and b) how would I get a copy of the text added to the listbox? I need something like "string sNewText = lbChat.LastLine" or something, I suppose I could use indexes or lenghts or something...

Anyways - without the EVENT this method won't work....
Any ideas, hints, and help would be greatly appreciated, thanks
 
If you wanted to find the text in the lstBox you would simply use the last index. You don't really need an event as you can just call the method to send the text directly after calling lbChatBox.Items.Add(stext), that way you can send stext and don't have to worry about retrieving the text from the lstbox.
 
Building on Cags' answer, a quick recommendation. If you are adding text to the ListBox from one place and only one place, I would do exactly as Cags said: code the logic in directly after the call to ListBox.Items.Add. If you might be adding text from multiple locations, do either one of the following:

(1)Define a method that will always be used to add text to the ListBox. This method will insert the item into the ListBox and do any network notification of the message.
(2)Subclass the ListBox to create a network message box. Hide, override, or shadow the Items property since it probably won't be necessary to access externally (the managing of items should be done internally) and create a public method to add a message which will also broadcast this message over the network to other network message boxes.

Either way you are creating a function that all text to displayed should be channeled through, allowing you to intercept messages instead of catching them with events.
 
Back
Top