7

from1枚 ここに画像の説明を入力1 と 2 の 2 つのフォームがあります。Form1 には 1 つのテキスト ボックスがあり、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;
      }
  }
}
4

6 に答える 6

11

この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);
    }
}
于 2013-02-27T21:16:24.263 に答える
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;
                }
            }
于 2013-02-27T21:10:53.260 に答える
1

テキストボックスが空白になる可能性が高い新しい form1 を作成し、その空のフォームで GetPass() を呼び出しています。テキストボックスに値がある可能性がある、既に開いている form1 のインスタンスが必要です。詳細については

ここをクリック

于 2013-02-28T11:29:29.030 に答える
1

次のようなものを試してください;

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 というプロパティを探し、値をinternalまたはに変更しますpublic。これにより、他のフォームのテキスト ボックスの値に直接アクセスできるようになります。

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

他のフォームのコントロール/詳細にアクセスする方法についてさらにサンプルを知る必要がある場合は、お知らせください。

于 2013-02-27T21:24:12.830 に答える