2013-02-15 4 views
1

Windows 폼에서 프롬프트를 사용할 때 어떻게 텍스트 상자를 자동으로 선택하겠습니까? 아래 코드는 내가 시도한 것을 보여 주지만 여전히 텍스트 상자가 아니라 버튼에 초점을 맞추고 있습니다. 도움과 도움을 미리 감사드립니다.Windows C# 양식 : 프롬프트에 초점을 텍스트 상자

  Form prompt = new Form(); 
      prompt.Width = 500; 
      prompt.Height = 200; 
      prompt.Text = caption; 
      Label textLabel = new Label() { Left = 50, Top = 20, Text = text }; 
      TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 }; 
      Button confirmation = new Button() { Text = "Ok", Left = 50, Width = 100, Top = 90 }; 
      confirmation.Click += (sender, e) => { prompt.Close(); }; 
      textBox.Select(); 
      textBox.Focus(); 
      prompt.Controls.Add(confirmation); 
      prompt.Controls.Add(textLabel); 
      prompt.Controls.Add(textBox); 
      prompt.ShowDialog(); 
      return textBox.Text; 

답변

4

폼이 표시 될 때까지 텍스트 상자에 집중할 때까지 기다려야합니다. 양식이 처음으로 표시되기 전에 able에 초점을 맞추지 않아야합니다. 양식이 처음 표시된 후 Shown 이벤트를 사용하여 일부 코드를 실행할 수 있습니다.

string text = "Text"; 
string caption = "caption"; 
Form prompt = new Form(); 
prompt.Width = 500; 
prompt.Height = 200; 
prompt.Text = caption; 
Label textLabel = new Label() { Left = 50, Top = 20, Text = text }; 
TextBox textBox = new TextBox() { Left = 50, Top = 50, Width = 400 }; 
Button confirmation = new Button() { Text = "Ok", Left = 50, Width = 100, Top = 90 }; 
confirmation.Click += (s, e) => { prompt.Close(); }; 
prompt.Controls.Add(confirmation); 
prompt.Controls.Add(textLabel); 
prompt.Controls.Add(textBox); 
prompt.Shown += (s, e) => textBox.Focus(); 
prompt.ShowDialog(); 
return textBox.Text; 
+0

작동하지 마십시오! 감사! 곧 당신의 대답을 받아 들일 것입니다. – AustinT

+0

@AustinTruong 맞아요, 편집을보십시오 (이번에 테스트했습니다). – Servy

관련 문제