2017-07-22 15 views
0

私は私の子フォーム上の子フォームを使用して親フォームの変数値を変更する方法は?

int count; 
private void timer1_Tick(object sender, EventArgs e) 
    { 
     counter.Text = "TOTAL: "+Convert.ToString(count); 
     time.Text = DateTime.Now.ToString("h:mm:ss tt"); 
     date.Text = DateTime.Now.ToString("MMMM d, yyyy"); 
    } 

を持っている私の親フォーム上で、私は

int counter; 
public void view() 
    { 
      //codes//codes//codes// 
     counter = pview.Rows.Count; 
    } 

を持っている私は、カウント=カウンタを設定したい... 私は

count = childform.counter; 

を試みたが、それは動作しませんでした..

私を助けてください。助けます。 -Lina Inverse

+0

が、私はそれを考え出しました!最後に! 私はちょうど int countを変更します。 〜 public static int count; と私の子供の書式で parent.count = counter; –

答えて

0

親フォームが子フォームの変数にアクセスするようにするには、親フォームに子フォームへの参照を格納する必要があります。また、あなたの子フォームは、外部からのアクセスを許可する公開変数またはメソッドを提供する必要があります。

ChildFormが表示されますのは、あなたがMainFormを実行していると仮定しましょう:

public partial class MainForm 
{ 

    private ChildForm _childForm; 

    public void ShowChildForm() 
    { 
     _childForm = new ChildForm(); 
     _childForm.Show(); 
    } 

    public void ReadRowCount() 
    { 
     if (_childForm != null) 
     { 
      // Reading variable from child form 
      int rowCount = _childForm.RowCount; 
     } 
    } 
} 

public partial class ChildForm 
{ 
    public int RowCount { get; private set; } 
} 
関連する問題