【问题标题】:Select a particular line in textbox?在文本框中选择特定行?
【发布时间】:2013-02-27 20:48:44
【问题描述】:

我有两个表单,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;
      }
  }
}

【问题讨论】:

  • 我不知道怎么做
  • 我用过visual studio 2010。
  • 是的,Windows 应用程序不是 Web 应用程序
  • int line = Form1.ab; // 此行包含文本框 form1 的长度
  • 我已经添加了两张图片更清晰的信息。运行我们的程序后的第一张图片(从上到下)讲述了记事本和第二张图片。我在文本框中写了一行,然后选择编辑选项,然后选择转到。当我们选择 GOTO 选项弹出新表单时,就像第二张图片一样。我输入行号并选择 GOTO 按钮。那是一个不正常的工作,请任何人帮助我....提前谢谢。

标签: c# winforms textbox


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    将此逻辑应用于您的代码,并根据需要重新编码。

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

    【讨论】:

      【解决方案3】:

      试试类似的东西;

      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;
      

      注意:您可以直接在另一个表单中访问另一个文本框,方法是转到 Form2 设计器,单击文本框并转到属性。在“属性”对话框中,查找名为 Modifiers 的属性并将值更改为 internalpublic。这将允许您像这样直接访问其他形式的文本框值;

      private void Form1_Load(object sender, EventArgs e)
      {
          Form2 form2Instance = new Form2();
          string sampleText = form2Instance.textBox1.Text;
      }
      

      如果您需要了解有关如何访问其他表单上的控件/详细信息的更多示例,请告诉我。

      【讨论】:

        【解决方案4】:

        您正在创建一个新表单1,其中文本框可能为空白,并在该空表单上调用 GetPass()。您需要一个已经打开的 form1 的实例,其中文本框可能有一个值。了解更多信息

        click here

        【讨论】:

          【解决方案5】:

          试试下面的代码

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

          【讨论】:

          • 虽然代码可能会回答问题,但最好在可能的情况下包含解释和相关参考。
          【解决方案6】:

          也许这样更好:

          {
              ...
              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();
              ...
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-01-19
            • 1970-01-01
            • 1970-01-01
            • 2014-03-03
            • 2020-04-03
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多