2015-07-13 31 views
0

我正在處理靜態方法,並從WinForm返回一個值,它會在單擊按鈕時產生新表單,並且在點擊提交或取消按鈕時它會拋回它的價值。C#通過靜態方法引用當前表單

問題是,我無法引用我的窗體上的組合框控件來填充我的sqlreader的結果。

我已閱讀,我使用的包裝,看起來類似於

public ComboBox comboHolder { get return this.foo } 

但是我似乎無法引用它要麼建議。任何建議來解決這個問題?

的完整代碼

 public ComboBox comboboxWrapper 
    { 
     get { return this.comboUsernames; } 
    } 
    public static string SelectProfile() 
    { 

     Form selectProfile = new Select_Profile(); 
     selectProfile.ShowDialog(); 

     SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Users.mdf;Integrated Security=True;Connect Timeout=30"); 
     connection.Open(); 
     SqlCommand command = new SqlCommand("SelectAllUsers", connection); 
     SqlDataReader usersReader = command.ExecuteReader(); 

     List<string> accountNames = new List<string>(); 

     while (usersReader.Read()) 
     { 
      accountNames.Add((string)usersReader["Username"]); 
     } 

     //populate the combo box 

     foreach (string s in accountNames) 
     { 
      //I'd like to call comboboxWrapper here. 

     } 
     //set the combo box to have a default item 
     // combo.SelectedIndex = 0; 
    } 

此外,這是一項正在進行的工作,我知道我應該有一些嘗試,漁獲物和一個finally語句,除此之外,我很開放,爲改進代碼的任何建議。

謝謝!

回答

1

我會建議只是不要使方法靜態。但如果你真的需要爲某些原因,你可以傳遞給窗體的引用到您的靜態方法,如:

SelectProfile(Form myForm) 

然後,你將能夠使用這樣的方法中:

foreach (string s in accountNames) 
{ 
    // e.g myForm.comboboxWrapper 
} 
0

您的靜態方法需要類的對象,你必須通過那裏comboboxwrapper從外部

定義

public static string SelectProfile(ClassobjectofCoboboxWrapper obj) 
{ 
    obj.comboboxWrapper; 
} 

調用此方法的類的對象

SelectProfile(new ClassobjectofCoboboxWrapper()) 

注意: 由於靜態方法與對象的instace沒有關係,它與class有關。因此,要引用靜態方法中不是靜態的元素,您需要創建引用類的對象,或者需要傳遞想要引用的類的對象。

0

這是你的表單實例:

Form selectProfile = new Select_Profile(); 

所以你會打電話comboboxWrapper該實例:

selectProfile.comboboxWrapper 

雖然首先你需要改變它的類型,因爲Form不有一個名爲comboboxWrapper的成員。聲明它是這樣來代替:

Select_Profile selectProfile = new Select_Profile(); 

或簡單地:

var selectProfile = new Select_Profile(); 

即使comboboxWrapper構件在static方法之外定義,它的內部形式實例。一個static成員沒有默認的特定實例的概念,需要提供一個。或者,在這種情況下,內部創建一個。

0

首先,分解您的解決方案:就是不補習班數據庫和用戶界面爲單一的方法。接下來想想你的方法是什麼應該返回作爲String

public static IEnumerable<String> AccountNames() { 
    //TODO: Move it into Settings/Config... 
    String connectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Users.mdf;Integrated Security=True;Connect Timeout=30"; 

    // Dispose (via using) all Disposable... 
    using (SqlConnection connection = new SqlConnection(connectionString)) { 
    connection.Open(); 

    // Dispose: prevent resource leakage... 
    using (SqlCommand command = new SqlCommand("SelectAllUsers", connection)) { 
     using (SqlDataReader usersReader = command.ExecuteReader()) { 
     while (usersReader.Read()) 
      yield return (string)usersReader["Username"]; 
     } 
    } 
    } 
} 

// returns selected profile 
// or null if no profile was seelcted 
public static string SelectProfile() { 
    // var: You need Select_Profile, not just a Form, right? 
    // again (using): don't forget to clear up the resources 
    using (var selectProfile = new Select_Profile()) { 
    // Providing that comboboxWrapper is public (bad practice) 
    // or SelectProfile() is implemented within Select_Profile class (good one) 
    selectProfile.comboboxWrapper.Items.AddRange(AccountNames()); 

    if (selectProfile.comboboxWrapper.Items.Count > 0) 
     selectProfile.comboboxWrapper.SelectedIndex = 0; 

    if (selectProfile.ShowDialog() == System.Windows.Forms.DialogResult.OK) { 
     if (selectProfile.comboboxWrapper.SelectedIndex < 0) 
     return null; // No item to select 
     else 
     selectProfile.comboboxWrapper.SelectedItem.ToString(); 
    } 
    else 
     return null; // Just closed 
    } 
} 
相關問題