Hi, as I am doing a small chat application in vb.net (windows form), I'm having problem for the design of my chatbox. I would like text messages to be selectable so user can copy(ctrl+v) it but not usernames which displays who sent the message. What form controls should I use in order to accomplish this and what properties to change from it? Thank you. Also keep in mind usernames and their messages must be correctly aligned.
catch the ctrl+c and parse the selected text for username and delete them
(maybe you can make the usernames images what will prevent standard text copy paste)
Well, here's a pretty cool idea, but I don't know enough about your requirements to know if it will work for you or not. I am thinking first of all that, if I were using a chat application, I would like to simply click on a message and have it be copied into the clipboard. After all, I can't think of anything else I'd need to click on a message for. So, it would be great if hovering over a message caused it to be highlighted, and clicking caused it to be copied.
If that works for your situation, then the question is how to achieve that. I am thinking that you could use a Label to show the username who sent the message, and a readonly textbox to show the message itself. Every time a new message is added to the chat, you insert a new label and a new textbox (by adding these controls to the controls collection of the page). Probably should wrap the label and textbox in a div, so you can highlight the entire div when they hover.
Now, how do you make clicking cause the text of the message to be copied to the clipboard? It will take some JavaScript. If you're using a JavaScript library, see what it offers. If not, here are some links. The Javascript code would need to get only the contents of the textbox, which it can do by looking for the right css class or the right id's. And then, copy that text to the clipboard as described in these links:
Javascript for copying to clipboard
A 2nd link about Javascript for copying to clipboard
A 3rd link about Javascript for copying to clipboard
A 4th link about Javascript for copying to clipboard
By the way, maybe you have the requirement that users should be able to select the text of many messages. In that case, perhaps clicking on a message "selects" it, and you can have multiples selected. Then, there's a button for "Copy All Selected Messages". The only thing this doesn't handle is selecting part of the text of a message. So it may or may not be right for your needs.
Have you thought about using a DataGridView control?
You could have separate columns for the usernames and the messages and only allow copying from the message column.
The DataGridView control is extremely powerful these days and would allow you to customize the appearance however you want (hiding gridlines to make it look more like a textbox for example).
There are tips and hints all over the place, but this page shows how to change gridline colours.
By selecting the two text boxes and validating the username and password for two different users and take the list boxes for seeing the messages, in code open a socket connection to chat between the users.