2014-09-24 2 views
1

두 개의 창으로 된 응용 프로그램이 있습니다. 하나의 창은 텍스트 상자를 포함하고 다른 창은 키보드입니다. 내 문제는 지금 키보드 창을 클릭 할 때 오류가 발생한다는 것입니다. 다른 창에서 클릭 한 텍스트 상자에는 입력 할 수 없습니다. 여기 내 코드가있다.다른 창을 사용하여 창에 텍스트 상자 채우기

창에서 텍스트 상자 하나를 누르면이 마우스 이벤트가 트리거됩니다.

private void TextBoxPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
{ 
    textBox = sender as TextBox; 
    textBox.Focus();  
} 

내 텍스트 상자가 프로그래밍 방식으로 내 창에 추가되었으므로 보낸 사람을 TextBox로 사용했습니다. 나는 그것을 텍스트 박스의 이름을 얻기 위해 사용했다.

내 키보드 창에서 단추를 클릭하면 코드가 작성됩니다.

하자 사용 버튼을 1 :

private void button_numeric_1_Click(object sender, RoutedEventArgs e) 
{ 
    Screen1 screen1 = new Screen1(); 
    screen1.textBox.Text += "1"; 
} 

화면 1 내 텍스트 상자를 포함하는 창입니다.

작성한 키보드를 사용하여 텍스트 상자에 텍스트를 입력하는 방법. 나는 C#을 사용하고있다. 누구든지 나를 도와주세요.

답변

1

new Screen1(); 대신 화면의 실제 인스턴스를 사용해야 할 수도 있지만 생성자를 통해 키보드로 전달할 수 있습니다.

위의 예

class Screen 
{ 
    Keyboard keyboard; 

    public Screen() 
    { 
     //pass the screen instance to the keyboard 
     keyboard = new Keyboard(this); 
    } 

    //... 

    private void TextBoxPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 
     textBox = sender as TextBox; 
     textBox.Focus(); 
     //open keyboard etc 
     keyboard.Show(); 
    } 
} 

class Keyboard 
{ 
    private Screen screenInstance; 

    public Keyboard(Screen instance) 
    { 
     //store the instance in a member variable 
     screenInstance = instance; 
    } 

    //... 

    private void button_numeric_1_Click(object sender, RoutedEventArgs e) 
    { 
     //use the stored screen instance 
     screenInstance.textBox.Text += "1"; 
    } 

    public void Show() 
    { 
     //display logic etc 
    } 
} 

는, 당신은 필요에 따라 코드를 통합/조정 수있는 몇 가지 가정을 기반으로 한 예입니다.

당신은 당신이

예를

class Screen 
{ 
    Keyboard keyboard = new Keyboard(); 

    private void TextBoxPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e) 
    { 

     textBox = sender as TextBox; 
     textBox.Focus(); 
     //open keyboard etc 
     keyboard.Show(textBox); 
    } 
} 

class Keyboard 
{ 
    private TextBox textBoxInstance; 

    private void button_numeric_1_Click(object sender, RoutedEventArgs e) 
    { 
     //use the stored TextBox instance 
     textBoxInstance.Text += "1"; 
    } 

    public void Show(TextBox instance) 
    { 
     textBoxInstance = instance; 
     //display logic etc 
    } 
} 
를 사용하는 여러 텍스트 상자가있는 경우 텍스트 상자의 인스턴스를 전달하기 위해 동일한를 조정할 수 있습니다
관련 문제