try something like:
int lineNumber = Form1.ab; // split the contents of the text box string text = textBox1.Text; string[] lines = text.Split(new string[] { Environment.NewLine }, StringSplitOptions.None); if (lineNumber < 0 || lineNumber > lines.Length) { MessageBox.Show("The line number is does not exist"); return; } // get the character pos int selStart = 0; for (int i = 0; i < (lineNumber - 1); i++) { selStart += lines[i].Length + Environment.NewLine.Length; } textBox1.Focus(); textBox1.SelectionStart = selStart; textBox1.SelectionLength = lines[lineNumber - 1].Length;
Note. You can access another text field directly in another form by going to the Form2 constructor, clicking on the text field and choosing Properties. In the Properties dialog box, find the Modifiers property and change the value to internal or public . This will allow you to access the text field in another form directly like this:
private void Form1_Load(object sender, EventArgs e) { Form2 form2Instance = new Form2(); string sampleText = form2Instance.textBox1.Text; }
If you need to find out more samples on how to access the controls / parts in other forms, let me know.
source share