gpt4 book ai didi

c# - 在文本框中选择特定行?

转载 作者:太空狗 更新时间:2023-10-29 19:59:56 25 4
gpt4 key购买 nike

from1 picture enter image description here我有两种形式,1 和 2。Form1 有一个文本框,form2 有一个文本框和按钮。我想转到指定的行,这意味着当我输入 form2 的文本框的值时,我的鼠标光标会转到 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;
}
}
}

最佳答案

TextBox.GetFirstCharIndexFromLine 方法查找一行的第一个字符的索引。所以你的选择从那里开始。然后找到该行的结尾,即 Environment.NewLine 或文本结尾。由于行号是由用户输入的,因此您应该使用 int.TryParse 来处理无效输入。

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);
}
}

关于c# - 在文本框中选择特定行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15122086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com