C# 在文本框中选择特定行?
我有两张表格,1和2。Form1有一个文本框,form2有一个文本框和按钮。我想转到一个指定的行,这意味着当我输入form2的textbox的值时,我的鼠标光标就会转到form1的textboxC# 在文本框中选择特定行?,c#,winforms,textbox,C#,Winforms,Textbox,我有两张表格,1和2。Form1有一个文本框,form2有一个文本框和按钮。我想转到一个指定的行,这意味着当我输入form2的textbox的值时,我的鼠标光标就会转到form1的textbox private void button1_Click(object sender, EventArgs e) { int line = Form1.ab; for (int i = 1; i < line; i++) { if (i == Convert.ToInt16(
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;
}
}
}
private void按钮1\u单击(对象发送者,事件参数e)
{
int line=Form1.ab;
对于(int i=1;i
将此逻辑应用于代码,并根据需要重新编码
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;
}
}
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);
}
}
private void按钮1\u单击(对象发送者,事件参数e)
{
整数行号;
如果(!int.TryParse(textBox2.Text,输出行号)| |行号<0)
{
textBox1.选择(0,0);
返回;
}
int position=textBox1.GetFirstCharIndexFromLine(行号);
如果(位置<0)
{
//行号太大了
textBox1.选择(textBox1.Text.Length,0);
}
其他的
{
int lineEnd=textBox1.Text.IndexOf(Environment.NewLine,position);
if(lineEnd<0)
{
lineEnd=textBox1.Text.Length;
}
textBox1.选择(位置,lineEnd-位置);
}
}
试试类似的方法
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;
如果您需要了解有关如何访问其他表单控件/详细信息的更多示例,请告诉我。您正在创建一个新表单1,其中文本框可能为空,并在该空表单上调用GetPass()。您需要一个已打开form1的实例,其中文本框可能有一个值。更多信息 尝试下面的代码
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);
}
也许这样更好:
{
...
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();
...
}
我不知道我是如何使用visual studio 2010的。是的windows应用程序不是web应用程序Int line=Form1.ab;//这一行包含一个文本框form1i的长度,我添加了两个图片更清晰的信息。第一张图片(从上到下)讲述记事本,第二张图片在运行我们的程序后。我在文本框中写了一行,选择编辑选项,然后选择一个转到。当我们选择“转到”选项时,弹出新窗体,如第二张图片。我输入行号并选择“转到”按钮。这是一个不正常的工作,请任何人帮我。。。。提前感谢。虽然代码可能会回答问题,但在可能的情况下,最好包含解释和相关参考。
{
...
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();
...
}