2014-12-14 3 views
0

특수한 종류의 메시지 상자를 구성하는 클래스가 있습니다. 그 클래스에서 인수 중 하나는 내가 생성자에서 참조하는 변수입니다. 그러나 버튼을 클릭 할 때 생성자에서 인식 할 변수가 필요합니다.button_click 메서드 내에서 변수 값을 참조하는 방법

나는 메소드의 동작을 살펴보고 메소드가 변수의 값을 인식 할 수 있도록이를 이와 같이 참조해야한다는 것을 알게되었습니다. "buttonRight_click에 대한 어떠한 과부하은 System.EventHandler을 위임 일치하지 않습니다"

static void SetString1(ref string value) 
{ 
if (value == "cat") // Test parameter value 
{ 
    Console.WriteLine("Is cat"); 
} 
value = "dog"; // Assign parameter to new value 
} 

나는 같은 일을하고 싶어,하지만 난 변수 'variavelcaixa'를 참조하려고하면 버튼 클릭 방식으로, 그러나, 저를 줄 것이다. 이것이 의미하는 바는 무엇이며 어떻게 변수를 성공적으로 참조해야합니까?

private void buttonRight_Click(object sender, System.EventArgs e, ref int variavelcaixa) 
    { 
     if (checkBox1.Checked == true) 
     { variavelcaixa = 1; } 
     else { variavelcaixa = 0; } 
    } 

는 편집 : 다음 내가 특별한 메시지 상자 클래스에있는 코드는대로 : 생성자의

 public partial class BetterDialog : Form 
    { 
    public int variavelcaixa; 
static public DialogResult ShowDialog(string title, string largeHeading, string smallExplanation, 
     string leftButton, string rightButton, Image iconSet, ref int variavelcaixa) 
    { 
     using (BetterDialog dialog = new BetterDialog(title, largeHeading, smallExplanation, leftButton, 
      rightButton, iconSet, ref variavelcaixa)) 
     { 
      DialogResult result = dialog.ShowDialog(); 
      return result; 
     } 
    } 

    /// <summary> 
    /// The private constructor. This is only called by the static method ShowDialog. 
    /// </summary> 
    private BetterDialog(string title, string largeHeading, string smallExplanation, 
     string leftButton, string rightButton, Image iconSet, ref int variavelcaixa) 
    { 
     this.Font = SystemFonts.MessageBoxFont; 
     this.ForeColor = SystemColors.WindowText; 

     InitializeComponent(); 
    //A bunch of graphic design 
    } 

외부는에 버튼 클릭 방법

private void buttonRight_Click(object sender, System.EventArgs e) 
    { 
     if (checkBox1.Checked == true) 
     { variavelcaixa = 1; } 
     else { variavelcaixa = 0; } 
    } 

있다 메인 클래스 나는 단순히 메시지 박스 객체에 첨부 된 특정 변수와 함께 ref variavelcaixa를 추가한다.

MsgBoxCheck.MessageBox dlg = new MsgBoxCheck.MessageBox(); 
       string icone = "C:\\warning.png"; 
       DialogResult result = BetterDialog.ShowDialog("Alert", 
     "main message in message box", 
     "some secondary message", 
     "", "Ok", System.Drawing.Bitmap.FromFile(icone), ref Variables.checkbox53naomostrarnovamente); 
+0

'ref'로 전달한 경우에도 실제로 변수에 할당하지 않습니다. 매개 변수와 변수에 동일한 이름을 사용하면 자동으로 값이 할당되지 않습니다. –

+0

@ shree.pat18 그래서 어떻게 링크합니까? – ng80092b

+0

'ShowDialog'에서 변수에 지정하십시오! –

답변

1

버튼 클릭 이벤트 핸들러는 (object sender, System.EventArgs e)MSDN Reference의 서명이 있어야합니다. 그래서 세 번째 매개 변수를 처리기로 만들려고하면 오류가 발생합니다.

이 문제를 해결하는 한 가지 방법은 변수 variavelcaixa을 처리기 메서드에 액세스 가능하게 만드는 것입니다. 클래스의 전역 변수로 선언하여이 작업을 수행 할 수 있습니다. 그런 다음 생성자에서뿐만 아니라 이벤트 처리기 메서드에서도이 객체에 할당 할 수 있습니다. 일단 이렇게하면 ref int variavelcaixa을 이벤트 처리기 메서드에 전달하면 안됩니다.

기본적으로 showDialog을 호출하고 ref 매개 변수를 전달한 후에는 메소드 본문의 클래스 변수 variavelcaixa에 할당해야합니다. ref Variables.checkbox53naomostrarnovamente은 변수가 아닌 checkbox53naomostrarnovamente을 나타냅니다. 따라서,이에 코드를 변경 : 나는 혼란을 정리하기 위해 p_variavelcaixaref 매개 변수를 이름을 변경 한

static public DialogResult ShowDialog(string title, string largeHeading, string smallExplanation, string leftButton, string rightButton, Image iconSet, ref int p_variavelcaixa) 
{ 
    using (BetterDialog dialog = new BetterDialog(title, largeHeading, smallExplanation, leftButton, 
     rightButton, iconSet, ref p_variavelcaixa)) 
    { 
     variavelcaixa = p_variavelcaixa; 
     DialogResult result = dialog.ShowDialog(); 
     return result; 
    } 
} 

참고. 변수 매개 변수를 이해하기 위해 this demo을 살펴볼 수도 있습니다.

+0

그게 쉬운 방법이야. 나는 그것을 시도했지만, 심지어 코드가 버그없이 실행되었지만 변수 값을 인식하지 못한다. 그 변수를 다음 코드에서 추가 인수로 추가하려고합니다. http://www.dotnetperls.com/customized-dialog – ng80092b

+0

해당 링크에 많은 코드가 있습니다. 어느 것을 언급하고 있습니까? 또한 제안 된 방법으로 작성한 코드를 게시하여 문제가 무엇인지 확인할 수 있습니다. –

+0

에디션에 코드를 추가했고 messagebox 클래스에서 variavelcaixa가 공개되었습니다 :) – ng80092b

관련 문제