Select a specific line in the text box?

from1 pictureenter image description here I have two forms: 1 and 2. Form1 has one text field, and form2 has a text field and a button. I want to go to the specified line, which means that when I enter the value of the text field form2, my mouse pointer moves to the text field form1.

private void button1_Click(object sender, EventArgs e) { int line = Form1.ab; for (int i = 1; i < line; i++) { if (i == Convert.ToInt16( textBox1.Text)) { // fr.textbox1 is a textbox form1 and // textbox1.text is a textbox of the form1 fr.textBox1.SelectionStart = int.Parse( textBox1.Text) ; fr.textBox1.ScrollToCaret(); break; } } } 
+4
source share
6 answers

The TextBox.GetFirstCharIndexFromLine method finds the index of the first character of a string. So your choice begins there. Then find the end of this line, which is equal to Environment.NewLine or the end of the text. Since the line number is entered by the user, you must use int.TryParse to handle invalid input.

 private void button1_Click(object sender, EventArgs e) { int lineNumber; if (!int.TryParse(textBox2.Text, out lineNumber) || lineNumber < 0) { textBox1.Select(0, 0); return; } int position = textBox1.GetFirstCharIndexFromLine(lineNumber); if (position < 0) { // lineNumber is too big textBox1.Select(textBox1.Text.Length, 0); } else { int lineEnd = textBox1.Text.IndexOf(Environment.NewLine, position); if (lineEnd < 0) { lineEnd = textBox1.Text.Length; } textBox1.Select(position, lineEnd - position); } } 
+7
source

Apply this logic to your code and recode it as needed.

 private void button1_Click(object sender, EventArgs e) { if (textBox_Form1.Text.Contains(textBox_Form2.Text)) { textBox_Form1.Focus(); textBox_Form1.SelectionStart = textBox_Form1.Text.IndexOf(textBox_Form2.Text); textBox_Form1.SelectionLength = textBox_Form2.Text.Length; } } 
+1
source

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.

+1
source

You create NEW form1 where the text field is likely to be empty and will call GetPass () in that empty form. You need an instance of an already open form1, where the text field can matter. for more information

Press here

+1
source

Try the code below

 var sdr = (System.Windows.Controls.TextBox) sender; if (!string.IsNullOrEmpty(sdr.Text)) { var start = sdr.Text.LastIndexOf(Environment.NewLine, sdr.CaretIndex); var lineIdx = sdr.GetLineIndexFromCharacterIndex(sdr.CaretIndex); var lineLength = sdr.GetLineLength(lineIdx); sdr.SelectionStart = start + 1; sdr.SelectionLength = lineLength; sdr.SelectedText.Substring(0, sdr.SelectedText.IndexOf(Environment.NewLine) + 1); Clipboard.SetText(sdr.SelectedText); } 
+1
source

maybe this is better:

 { ... string SelectedText = fr.textBox1.Lines[line]; int SelectedTextPos = fr.textBox1.Text.IndexOf(SelectedText); int SelectedTextLen = SelectedText.Lenght; fr.textBox1.Select(SelectedTextPos, SelectedTextLen); fr.textBox1.ScrollToCaret(); ... } 
0
source

All Articles