RampagePS Posted January 3, 2005 Posted January 3, 2005 Hey, I was wondering if any of you know how to fix a error I get in my chat client I made. The error is that when you hit ctrl + enter into the box that you type your text in, and you hit enter it just sends the username and a bunch of blank text where no text has been entered. I was wondering if there was a way to fix this problem to where the client will not send all the blank text, just make it to where it will not send it at all unless there is text in the box. Here is what the code looks like private void rtbTalk_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if(e.KeyChar == 13) { if(m_sock == null || !m_sock.Connected) { MessageBox.Show( this, "Must be connected to send a message" ); return; } try { string Message; Message = Username + ":" + " " + rtbTalk.Text; Byte [] byteDateLine = Encoding.ASCII.GetBytes( Message.ToCharArray() ); m_sock.Send( byteDateLine, byteDateLine.Length, 0 ); rtbTalk.Text = ""; } catch(Exception ex) { MessageBox.Show( this, ex.Message, "Send message failed!" ); } } } Any help would be greatly appreciated. Quote
Administrators PlausiblyDamp Posted January 3, 2005 Administrators Posted January 3, 2005 You could try using the .Trim method on rtbTalk.Text to remove whitespace charaters and then see if the string contains any valid data, if not then do not send anything. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
RampagePS Posted January 3, 2005 Author Posted January 3, 2005 Thanks man, I hate to ask but could you tell me where the .Trim has to go. Not sure if it needs to be in a if statement or where. Quote
coldfusion244 Posted January 3, 2005 Posted January 3, 2005 Hey, I was wondering if any of you know how to fix a error I get in my chat client I made. The error is that when you hit ctrl + enter into the box that you type your text in, and you hit enter it just sends the username and a bunch of blank text where no text has been entered. I was wondering if there was a way to fix this problem to where the client will not send all the blank text, just make it to where it will not send it at all unless there is text in the box. Here is what the code looks like private void rtbTalk_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e) { if(e.KeyChar == 13) { if(m_sock == null || !m_sock.Connected) { MessageBox.Show( this, "Must be connected to send a message" ); return; } try { string Message; Message = Username + ":" + " " + rtbTalk.Text; //now that Message is set, trim off the whitespace Message.Trim(); Byte [] byteDateLine = Encoding.ASCII.GetBytes( Message.ToCharArray() ); m_sock.Send( byteDateLine, byteDateLine.Length, 0 ); rtbTalk.Text = ""; } catch(Exception ex) { MessageBox.Show( this, ex.Message, "Send message failed!" ); } } } Any help would be greatly appreciated. You want to trim the string before you encode it into ASCII, and after you set it's value, as to remove all the whitespace. Quote -Sean
RampagePS Posted January 3, 2005 Author Posted January 3, 2005 I tried what you said but it still does the whitespaces. I think I might have phrased it wrong, the rtbTalk box is the box you put in all your text that you want to send to all the connected clients. The box has a vertical scrollbar and when you hold down ctrl and hit enter it makes a new line in the rtbTalk box and then the scroll bar starts to scrolldown. I was wondering if there was a way to make it to where if they did do that and if there was no text in rtbTalk that it would not send and if there was text in rtbTalk it would just send the text not all the new lines. Quote
coldfusion244 Posted January 3, 2005 Posted January 3, 2005 I tried what you said but it still does the whitespaces. I think I might have phrased it wrong' date=' the rtbTalk box is the box you put in all your text that you want to send to all the connected clients. The box has a vertical scrollbar and when you hold down ctrl and hit enter it makes a new line in the rtbTalk box and then the scroll bar starts to scrolldown. I was wondering if there was a way to make it to where if they did do that and if there was no text in rtbTalk that it would not send and if there was text in rtbTalk it would just send the text not all the new lines.[/quote'] Any chance you could attach the project to the forum? It would make debugging it easier. Quote -Sean
RampagePS Posted January 4, 2005 Author Posted January 4, 2005 (edited) Sure here it isHeLL Klan Chat Client.zip Edited January 4, 2005 by PlausiblyDamp Quote
coldfusion244 Posted January 4, 2005 Posted January 4, 2005 Sure here it is I changed the code around a little. Replace with this file and build the application. It works fine for me now.FormMain.zip Quote -Sean
RampagePS Posted January 4, 2005 Author Posted January 4, 2005 Hey I replaced the file but it still did the it, if you want to I will send you the ip to connect to the server to see if that helps you any. Here is my aim name if you want to try that AtomicForce11. I will put in my profile to. Quote
coldfusion244 Posted January 4, 2005 Posted January 4, 2005 Hey I replaced the file but it still did the it' date=' if you want to I will send you the ip to connect to the server to see if that helps you any. Here is my aim name if you want to try that AtomicForce11. I will put in my profile to.[/quote'] Yes, i'll compile what I have and try it out, just e-mail me the server ip. Quote -Sean
coldfusion244 Posted January 4, 2005 Posted January 4, 2005 Yes' date=' i'll compile what I have and try it out, just e-mail me the server ip.[/quote'] AH! I am such an idiot! The reason it is not going to trim anything is because of this right here: [CS] string Message; Message = Username + ":" + " " + rtbTalk.Text + "\n"; //Here is the reason Message.Trim(); [/CS] You are adding a newline to the end of every message (so it will be appended after all of the whitespace). What I did was Trim it first, and then add the newline constant. It's attached below :D ;)FormMain.zip Quote -Sean
RampagePS Posted January 4, 2005 Author Posted January 4, 2005 Hey thanks man thats exactly what I needed sry I wasn't here had to do some computer work to do at my friends house. Come back on the server if you want and we can chat alittle bit, I might have another question for you but I think I know how to fix it. Quote
RampagePS Posted January 5, 2005 Author Posted January 5, 2005 Yeah the question I had for you I fixed had to add another .Trim() after Message = Username.Text + ":" + " " + rtbTalk.Text.Trim(), that makes to where if they hold ctrl + enter and then put the text in it trims all the whitespaces. Thanks for your help I really appreciate it. :) :D Quote
coldfusion244 Posted January 5, 2005 Posted January 5, 2005 No problem, that's what this community is here for :D Quote -Sean
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.