Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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.

Posted
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.

-Sean
Posted
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.
Posted
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.

-Sean
Posted
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.
Posted
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.

-Sean
Posted
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

-Sean
Posted
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.
Posted
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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...