9 Replies - 584 Views - Last Post: 04 October 2012 - 01:16 PM Rate Topic: -----

#1 Fatcap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-October 12

New line, reloads the textbox?

Posted 04 October 2012 - 06:25 AM

Hello!

First of all, I'm verry new to this whole C# programming language and OOP.

My problem:
I'm trying to make a chat-window like msn/icq/irc etc.. The textbox with the text that's been posted just keeps updating and refreshing when a new message is posted. Is there some way to pusch the message and not update the whole box. It's a problem for me because i want to keep a line selected and when a new line is posted the selection dessapears.

I hope you understand my problem.

/Fatcap

Is This A Good Question/Topic? 0
  • +

Replies To: New line, reloads the textbox?

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3652
  • View blog
  • Posts: 11,421
  • Joined: 05-May 12

Re: New line, reloads the textbox?

Posted 04 October 2012 - 07:07 AM

Can you show us the code for how you are updating the text within the textbox? Are you replacing the Text property? Are you appending to the Text property?
Was This Post Helpful? 0
  • +
  • -

#3 Fatcap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-October 12

Re: New line, reloads the textbox?

Posted 04 October 2012 - 07:13 AM

View PostSkydiver, on 04 October 2012 - 07:07 AM, said:

Can you show us the code for how you are updating the text within the textbox? Are you replacing the Text property? Are you appending to the Text property?



I was replacing the text, i don't know any other way really.
Was This Post Helpful? 0
  • +
  • -

#4 h4nnib4l  Icon User is offline

  • The Noid
  • member icon

Reputation: 1182
  • View blog
  • Posts: 1,677
  • Joined: 24-August 11

Re: New line, reloads the textbox?

Posted 04 October 2012 - 08:14 AM

Use += instead of = when you want to add to the existing value of something. In this case, you'll want to append a newline character to the end of posts, so the new ones will appear on a new line.

Disclaimer: Without your code, it's hard to be specific, and I'm having to make some assumptions.
Was This Post Helpful? 0
  • +
  • -

#5 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: New line, reloads the textbox?

Posted 04 October 2012 - 08:42 AM

AppendText is probably what you are looking for. Use that method instead of updating the Text property.
textBox1.AppendText(NewMessage);

Was This Post Helpful? 0
  • +
  • -

#6 Fatcap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-October 12

Re: New line, reloads the textbox?

Posted 04 October 2012 - 09:08 AM

Alright, here is my code, I just made some project to show you all what i ment.


        public Form1()
        {
            InitializeComponent();

            timer1.Interval = 1000;
            timer1.Start();

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            string Seconds = DateTime.Now.Second.ToString();
            textBox1.AppendText(Seconds);
        }




If i select the text in textBox1 in my application, the selected part dissapears when the new tick apply.. I want the text to just fill upp in the textbox and my selection is still there.

Here is an image to show you.
Posted Image
This is the selection, but when the textbox uppdates the selection is unselected.. I don't want the textbox to refresh. Is there some way to make that happen? Maybe some other technique? Rich textbox? Labels?

And as I told you before, I'm really new into this C# language.. I have massive knowledge in Classic ASP and SQL tho.

/Fatcap
Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1606
  • View blog
  • Posts: 5,163
  • Joined: 25-September 09

Re: New line, reloads the textbox?

Posted 04 October 2012 - 09:19 AM

It's going to refresh from the appendtext but you could store the .Selectionstart and .SelectionLength properties to an integer prior to the appendtext and then set them back to those values after leaving you with the same text selected.
Was This Post Helpful? 1
  • +
  • -

#8 Robin19  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 272
  • View blog
  • Posts: 552
  • Joined: 07-July 10

Re: New line, reloads the textbox?

Posted 04 October 2012 - 12:06 PM

Following on CharlieMay's advice, use a method on the form to do all of the work. Then simply call the method from anywhere in your code. That way you never have to repeat code if you update the text box from different places in the code. Also, get into the habit of renaming everything when you create it so things are easier to read/comprehend.
public partial class CharForm : Form
{
   // Notice that the method and all variable names are
   // clear and concise?  Now other coders (including
   // your future self) can make sense of what
   // is going on
   private void AddTextToChat(string TextToAdd)
   {
      // record what is currently selected
      int selectionstart = txtChat.Selectionstart;
      int selectionLength = txtChat.SelectionLength;
      // append text
      txtChat.AppendText(TextToAdd);
      // set what is selected
      txtChat.Selectionstart = selectioStart;
      txtChat.SelectionLength = selectionLength;
   }

   private void TestTimer_Tick(object sender, EventArgs e)
   {
      string now = DateTime.Now.Second.ToString();
      AddTextToChat(now);
   }
}

Was This Post Helpful? 1
  • +
  • -

#9 Fatcap  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 04-October 12

Re: New line, reloads the textbox?

Posted 04 October 2012 - 12:18 PM

View PostRobin19, on 04 October 2012 - 12:06 PM, said:

Following on CharlieMay's advice, use a method on the form to do all of the work. Then simply call the method from anywhere in your code. That way you never have to repeat code if you update the text box from different places in the code. Also, get into the habit of renaming everything when you create it so things are easier to read/comprehend.
public partial class CharForm : Form
{
   // Notice that the method and all variable names are
   // clear and concise?  Now other coders (including
   // your future self) can make sense of what
   // is going on
   private void AddTextToChat(string TextToAdd)
   {
      // record what is currently selected
      int selectionstart = txtChat.Selectionstart;
      int selectionLength = txtChat.SelectionLength;
      // append text
      txtChat.AppendText(TextToAdd);
      // set what is selected
      txtChat.Selectionstart = selectioStart;
      txtChat.SelectionLength = selectionLength;
   }

   private void TestTimer_Tick(object sender, EventArgs e)
   {
      string now = DateTime.Now.Second.ToString();
      AddTextToChat(now);
   }
}


Thank you very much, that helped me alot! I'm still lerning about the language and this was really helpfull. Do you have any tutorials you recommend? I was looking up socket-programming but it was verry confusing.

Thanks again.

/Fatcap
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is offline

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5633
  • View blog
  • Posts: 12,086
  • Joined: 02-June 10

Re: New line, reloads the textbox?

Posted 04 October 2012 - 01:16 PM

I'd just like to point out we have a couple very good "How to build a chat system" tutorials in the C# tutorial section. Rather than try to fix so much, and try to architect something when you have no experience you might learn a great deal more by looking at something architected by an expert. Dissecting the work of the more experienced will give you a great deal of insight into more than just code - but underlying concepts and principals.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1