2017-09-04 91 views
0

我的Windows窗體應用程序的主窗體中有一個名爲'language'的變量。c#如何從另一個類中更改變量

在一個子表單我有我需要改變的「語言」 值未做主要形式的新實例組合框

namespace MMI_WFA 
{ 
    public partial class MainWindow : Form 
    { 
     int language; 
      ... 
      ... 
    } 
} 


namespace MMI_WFA 
{ 
    public partial class MENU : Form 
    { 
    ... 

     private void cbo_Language_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      function_to_Change_language_in_mainForm(); 
     } 
    } 
} 

所以我可以改變一個變量在主要的形式,與其他形式的事件?

編輯:我改變這個,但在this.window窗口有紅色下劃線。

public MENU(MainWindow window) // main constructor 
     { 
      this.window = window; 
      InitializeComponent(); 
      cbo_SerialPort.Items.AddRange(ports); 
      //cbo_SerialPort.SelectedIndex = 2;   

      cbo_Baudrate.Items.AddRange(baudrates); 
      //cbo_Baudrate.SelectedIndex = 1; 

      cbo_ParityBits.Items.AddRange(partitybits); 
      //cbo_ParityBits.SelectedIndex = 0; 

      hideExtraSettings(); 
      fillLanguageBox(); 
      indexComboboxes(); 
     } 
+0

看看[問],特別是[mcve]。你可能會得到更好的回答你的問題。 – dd4711

回答

0

好吧,如果你Menu構型中沒有主窗體的實例(你可以傳遞構造函數或屬性),你仍然可以使用這個OpenForms.OfType<MainWindow>得到它 - 「絕招」:

var main = Application.OpenForms.OfType<MainWindow>().First(); 
main.Language = 1; 

你需要使它成爲公共財產:

public partial class MainWindow : Form 
{ 
    public int Language { get; set; } 
} 
+0

這是訣竅,非常感謝你。這非常有用。可惜C#有這麼複雜的語法來做這樣一件簡單的事情 – bask185

+0

@ bask185這不是C#特有的,而是面向對象的。這使您可以參考您想要更改的內容。爲了*改變*一個對象,你應該*在它之前得到它,或者至少在它的某個地方得到它。 – HimBromBeere

1

您需要將您的MainWindow的引用傳遞給其他類以訪問其成員。要實現此目的,請在您的MENU-類中創建一個構造函數,該類需要MainWindow-類的實例。此外要更改成員應public,最好的屬性,而不是一個領域:

namespace MMI_WFA 
{ 
    public partial class MainWindow : Form 
    { 
     public int Language { get; set; } 
     ... 
     ... 
    } 

    public partial class MENU : Form 
    { 
     private readonly MainWindow window; 

     public MENU(MainWindow window) { this.window = window; } 

     private void cbo_Language_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      this.window.Language = // 
     } 
    } 
} 
+0

它不編譯。我用新的代碼編輯了OP。 – bask185

+0

在'MENU'上添加一個字段以存儲MainWindow的實例。 '私人只讀MainWindow窗口;' –

+0

@DanielMay Ooops,忘記了。謝謝你的提示。 – HimBromBeere

0

我準備了很微小的例子,你怎麼可以檢索子窗體的東西沒有通過主要形式,以孩子

運行的主要形式

void Main() 
{ 
    var main = new MainForm(); 
    main.ShowDialog(); 
} 

主要形式有1只按鈕 - 問子窗體的數量

class MainForm : Form 
{ 
    public MainForm() 
    { 
     var button = new Button(); 
     button.Click += button_pressed; 
     button.Text = "Ask number";  
     this.Controls.Add(button);  
    } 

    void button_pressed(object sender, EventArgs e) 
    { 
     var child = new SubForm(); 
     var result = child.ShowDialog(); 

     if (result == DialogResult.OK) 
     { 
      MessageBox.Show($"Entered number is {child.SelectedNumber}"); 
     } 
    } 
} 

子表單會要求用戶輸入號碼。如果用戶輸入號碼,那麼表單將被關閉,並顯示OK對話結果。對話結果將幫助我們瞭解用戶是選擇了某種東西還是僅僅關閉了表格。

class SubForm : Form 
{ 
    public int SelectedNumber { get; set;} 

    public SubForm() 
    { 
     var button = new Button();  
     var textBox = new TextBox(); 

     button.Click += (s, e) => { 
      int i; 
      if (int.TryParse(textBox.Text, out i)) 
      { 
       this.SelectedNumber = i; 
       this.DialogResult = DialogResult.OK; 
       this.Close(); 
      } 
      else 
      { 
       MessageBox.Show("pls, enter number", "Warning", 
        MessageBoxButtons.OK, MessageBoxIcon.Warning); 
      } 

     }; 

     textBox.SetBounds(0, 0, 100, 20); 
     button.SetBounds(100, 0, 30, 20); 

     button.Text = "OK";  
     this.Controls.Add(textBox); 
     this.Controls.Add(button); 
    } 
} 
相關問題