I’ve searched for several hours on how I would manage to do this, but none of the solutions I’ve found have worked…
I have a form with several ComboBoxes, and I’d like to grab the selected values and use it in another class’s method. The values will be used as variables in a filter to query a database. When assigning the values though, I got the error “An object reference is required for the non-static field, method, or property”. I tried creating a new instance of the form in the other class, but wouldn’t the new instance not have the ComboBox values I wanted in the first place?
I’m not quite sure on how to do this. I’ve tried a bunch of ways but they’ve all failed.=/ I’m new to programming, so I would appreciate any help I can get!
public static void LoadMainTable(ref DataTable mainGridTable)
{
//Loads entires into a data table
//FormMonitor FormMonitor = new FormMonitor();
string bankBox = FormMonitor.ComboBox1.SelectedItem;
string theDate = FormMonitor.DateTimePicker.Value.ToString("yyyy-MM-dd");
//Grabbed in the order they will be displayed
cmd.CommandText = String.Format("W.I.P.");
//Add variables for filters after figuring out how to do it
reader = cmd.ExecuteReader();
//reads data into dmainGridTable
while (reader.Read())
{
mainGridTable.Rows.Add(reader["column1"], reader["column2"], reader["column3"], reader["column4"].ToString(),
reader["column5"].ToString(), reader["column6"]);
//FormMonitor.file_ProgressUpdated(); //non-static error again, fix
connect.Close();
}
}
You can use the value of any control IF you use the REFERENCE of the instance of the form. You are using the class name.
Exemple: Right now, my winform project is using an MDI as main form. I sometimes need the selected value of the MonthCalendar control of one on my form. What I did, is a kind of accessor in this form, and when I want to get these value, I use MdiParent to find my main form (an MdiContainer) and i found the reference to the form containing the MonthCalendar. And I call my accessor. (It’s more clean to use an accessor then getting the control value directly)
Of course, if your program is done differently, you might need to find another logic. But it’s a base.