2

I have a multiline Textbox that uses threadsafe method to call it. After every new text, the caret ( cursor ) goes to first line position and for multiline texts. I can't read the lasts lines.

I try to use:

 textBox.CaretIndex = _textBox.Text.Length;

but it is not for threadsafe.

This is my code:

void test()
{
Thread demoThread =
               new Thread(new ThreadStart(this.ThreadProcSafe));
            demoThread.Start();
}
private void ThreadProcSafe()
{
    ThreadHelperClass.SetText(this, textBox2, "text: ");
}
public static class ThreadHelperClass{
    delegate void SetTextCallback(Form f, Control ctrl, string text);
    /// <summary>
    /// Set text property of various controls
    /// </summary>
    /// <param name="form">The calling form</param>
    /// <param name="ctrl"></param>
    /// <param name="text"></param>
    public static void SetText(Form form, Control ctrl, string text){
        // InvokeRequired required compares the thread ID of the 
        // calling thread to the thread ID of the creating thread. 
        // If these threads are different, it returns true. 
        if (ctrl.InvokeRequired){
            SetTextCallback d = new SetTextCallback(SetText);
            form.Invoke(d, new object[] { form, ctrl, text });
        } else {
            ctrl.Text += text;
            ctrl.Text += Environment.NewLine;
        }
    }
}

ThreadHelperClass.SetText(this, richTextBox1, "output>>" + e.Data);

I want to go at end of the Textbox ( for see the last lines text ) without clicking or moving the mouse. After a new text, caret go to end of the line.

i want to use Setting cursor at the end of any text of a textbox at ThreadHelperClass but it gives me error

want to use

txtBox.Focus();
txtBox.SelectionStart = txtBox.Text.Length;
OR
txtBox.Focus();
txtBox.CaretIndex = txtBox.Text.Length;
OR
txtBox.Focus();
txtBox.Select(txtBox.Text.Length, 0);

this is the error that I get when i using above codes in ThreadHelperClass Severity Code Description Project File Line Suppression State Error CS0120 An object reference is required for the non-static field, method, or property 'Form1.textBox2' Download C:\Users\work\source\repos\Download\Download\Form1.cs 108 Active

and when I use it outside of this function my application get crashed for multi thread

Ragnaros
  • 33
  • 1
  • 7
  • What is "`threadsafe` method"? Do you just mean `.Invoke(...)`? – Enigmativity Nov 25 '18 at 21:41
  • I want to use https://stackoverflow.com/questions/20423211/setting-cursor-at-the-end-of-any-text-of-a-textbox inside ThreadHelperClass – Ragnaros Nov 25 '18 at 21:50
  • when using outside of ThreadHelperClass it gives this crash : Cross-thread operation not valid: Control 'rtxtConsole' accessed from a thread other than the thread it was created on. – Ragnaros Nov 25 '18 at 22:13
  • Is this winforms or WPF? You need to provide us with a [mcve]. – Enigmativity Nov 25 '18 at 22:17

2 Answers2

0

Can you try calling textBox.CaretIndex = _textBox.Text.Length; from the GUI thread like this:

Application.Current.Dispatcher.Invoke(
    () =>
    {
         textBox.CaretIndex = _textBox.Text.Length;
    });
Moffen
  • 1,817
  • 1
  • 14
  • 34
0

Problem Solved By using Cross-thread operation not valid: Control 'textBox1' accessed from a thread other than the thread it was created on

delegate void SetTextCallback(string text);

private void SetText(string text)
{
  // InvokeRequired required compares the thread ID of the
  // calling thread to the thread ID of the creating thread.
  // If these threads are different, it returns true.
  if (this.textBox1.InvokeRequired)
  { 
    SetTextCallback d = new SetTextCallback(SetText);
    this.Invoke(d, new object[] { text });
  }
  else
  {
    this.textBox1.Text = text;
  }
}

in this way I can use textbox and all sub commands like Focus, Select in Cross-thread operation without crash.

txtBox.Focus();
txtBox.Select(txtBox.Text.Length, 0);

but in my Question Code there was a

Control ctrl

that didn't have Select, Focus like to know the answer for old way too

Anyway Thanks

Ragnaros
  • 33
  • 1
  • 7