我正在處理靜態方法,並從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語句,除此之外,我很開放,爲改進代碼的任何建議。
謝謝!