Click to See Complete Forum and Search --> : List box tribulations!
aebrooks
December 22nd, 2004, 10:33 AM
I have a total of three List boxes in use on one dialog box. The issue is that when i select a user from one of the list boxes using the code below program locks out normall send message functions that i have used.. At the moment my only theory is based upon the hList creation on line 7...
Can anyone offer any ideas?
Thanks
Alan
case IDC_LIST1:
// CHECKING FOR NOTIFICATIONS FROM THE LIST BOX
switch(HIWORD(wParam))
{
case LBN_SELCHANGE: //WHEN THE SELECTION CHANGES DO:
{
HWND hList = GetDlgItem(hwnd, IDC_LIST1);
int count = SendMessage(hList, LB_GETCURSEL , 0, 0);
char nameBuf[16] = "SKCOLLOB";
//ASSIGN THE CURRENT SELECTION TO NAMEBUF
SendMessage(hList, LB_GETTEXT , (WPARAM)count, (LPARAM)nameBuf);
for(int i = 0; i <=16; i++)
{
nameBufpass[i] = nameBuf[i];
}
GlobalFree(nameBuf);
}
break;
}
break;
NoHero
December 22nd, 2004, 10:42 AM
At first: If you are catching the WM_COMMAND message you do net to get the HWND of the control which caused the event by using GetDlgItem(). The lParam parameter gives you the HWND of the control that fired the event. Read about it at MSDN (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winui/winui/windowsuserinterface/resources/menus/menureference/menumessages/wm_command.asp).
Second: You must not free a buffer with GlobalFree you created locally without using the GlobalAlloc() function.
Third: What's exactly the problem with your code? Does it freeze your application? Does it crash? Sorry I do not understand your properly.
for(int i = 0; i <=16; i++)
{
nameBufpass[i] = nameBuf[i];
}
This also not necessary, because you can simply use the 'lstrcpy()' function to copy the data of a string to another string.
aebrooks
December 22nd, 2004, 01:22 PM
Thanks for the tips :-)
I am making a chat client to work with a custom chat server.. We have the basics working (i.e. the clients communicate and will send and recieve messages). We are now trying to impliment the private message function.. For this the server sends a list of room members to the client and the client displays that list in a list box. The idea is that you type the message you wish to send privately, select the user to send it to and click on the PM button.. The PM function works well.. If you send a PM message manually I.e. you send a normall message formatted to look like a PM message, everything works well along with the normall send and recieve functions untill you select a user from the list box.. at which point the send and recieve functions cease. The program does not crash but it will not function properly!
This is the code for the PM send Button:
case IDC_PM_SEND:
{
int len1 = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT));
if(len1 > 0)
{
char* buf2;
buf2 = (char*)GlobalAlloc(GPTR, len1 + 1);
GetDlgItemText(hwnd, IDC_TEXT, buf2, len1 + 1);
funcs.PMsend(buf2, nameBufpass, connect1.Socket1);
SendDlgItemMessage(hwnd, IDC_TEXT, WM_SETTEXT, 0, (LPARAM)empty);
}
}
break;
And this is the code for the send button:
case IDC_SEND:
{
int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT));
if(len > 0)
{
char* buf;
buf = (char*)GlobalAlloc(GPTR, len + 1);
GetDlgItemText(hwnd, IDC_TEXT, buf, len + 1);
funcs.SendMess(buf,socketVal);
SendDlgItemMessage(hwnd, IDC_TEXT, WM_SETTEXT, 0, (LPARAM)empty);
}
}
break;
They each call a seperate function that sends the required message to the server...
Thanks for your help
Alan
NoHero
December 22nd, 2004, 01:36 PM
Using GlobalAlloc() means that you create a memory object to the newly allocated memory. To have access to this memory you need 'GlobalLock()'. This function returns a void* which can be casted to any type. In this case 'char*'. But why arn't you using 'malloc()'? Or If you use a C++ compiler 'new' to allocate the buffer?
And what do you mean with 'does not function properly'? Can you go a little bit more into details?
aebrooks
December 22nd, 2004, 02:31 PM
Ok.. Defining not functioning correctly: After the user has been selected in the list box, A standard message can not be sent at all. Effectivly the program will not run the send functions in the code.. This is all that is abnormall.
Regarding your last message, I'm am still learning (obviously) so I have no idea what you mean about GlobalLock() or malloc() but i will look them up.. I have been using friends' knowlegde and some tutorials to put this program together, I understand 95% of it but this is an area than needs some work!
Would it help if i sent you the exes?
NoHero
December 22nd, 2004, 03:09 PM
Yes post the entire project here and I do everything to help you.
(The executable alone does not really help though :rolleyes:)
aebrooks
December 22nd, 2004, 03:13 PM
Would it be ok to e-mail it to you? I'd rather not post the whole thing if i can help it..
If you dont want to post your e-mail then e-mail me at aebrooks@hotmail.com and i'll reply to you :-)
NoHero
December 22nd, 2004, 03:17 PM
Would it be ok to e-mail it to you? I'd rather not post the whole thing if i can help it..
If you dont want to post your e-mail then e-mail me at aebrooks@hotmail.com and i'll reply to you :-)
Check your emails :cool: ;)
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.