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