2013-02-27 2 views
4

from1 pictureenter 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; 
     } 
    } 
} 
+0

난 비주얼 스튜디오 2010 –

+0

을하는 DoThis 아무 생각이 어떻게 웹 응용 프로그램이 아닌 응용 프로그램 –

+0

그래 창을 사용했습니다 –

답변

5

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

이 논리를 코드에 적용하고 필요한대로 다시 코드화하십시오.

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

다음과 같이 시도하십시오.

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의 디자이너로가는 텍스트 상자를 클릭하고 속성으로 이동하여 다른 형태로 직접 다른 텍스트 상자에 액세스 할 수 있습니다. Properties 대화 상자에서 Modifiers라는 속성을 찾아 값을 internal 또는 public으로 변경합니다. 이렇게하면 다른 양식의 텍스트 상자 값에 직접 액세스 할 수 있습니다.

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

다른 양식의 컨트롤/세부 정보에 액세스하는 방법에 대한 추가 샘플을 알고 싶으면 알려주십시오.

+0

ok 보스 나중에 시도 할 것입니다 .. 감사합니다 4 도움. –

1

텍스트 상자가 비어있을 가능성이있는 새 form1을 만들고 빈 양식에서 GetPass()를 호출합니다. 텍스트 상자에 값이있을 수있는 이미 열린 form1의 인스턴스가 필요합니다. 자세한 내용은

click here

1

는 아래의 코드

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

} 
+0

코드는 질문에 대답 할 수 있지만 가능한 경우 설명과 관련 참조를 포함하는 것이 좋습니다. –

0

을 시도 어쩌면이 더 나은 :

{ 
    ... 
    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(); 
    ... 
} 
관련 문제