开发者

C# set dataTable generated in formname.designer.cs as static

开发者 https://www.devze.com 2023-03-23 03:44 出处:网络
I want to know if it\'s ok to modify the formname.designer.cs and to set a variable that is generated from design mode as private as static:

I want to know if it's ok to modify the formname.designer.cs and to set a variable that is generated from design mode as private as static:

 private dtableAdapters.llist nameTable;// this to become static
 public static dtableAdapters.llist nameTable;//like this

I read here C# Set Checkbox to Static that is not a good method. Maybe I can do this in other way. Here is what I want to do:

I have a Form that contain more forms, opened in a panel. One form contains some comboboxes with values from the database. The problem is that when I add more values to the database from another form with a textbox, the combobox needs to be filled again. I thought that it could be easy if I update the combobox immediatlly after i add some values. (combobox and the textbox -that add values in the database which are shown by combobox- are in different forms).

Do you have an other ideea of doing this? I have tried also to fill the combobox again when it's clicked but because I have more comboboxes I get some fatal errors when I click fast from one to another.

edit: as a last method: I cou开发者_高级运维ld add a button and fill the combobox when the button is pressed, but I want to do it automatically

(winforms not web forms)


One approach is to fire an event on FormA when a value is added.
Form B can subscribe to the event and update the list. The only tricky bit is that FormB needs a reference to FormA to hook up to the event.

Something like this...

FormA

public delegate void DataAddedEventHandler(object sender, EventArgs e);
    public partial class FormA : Form
    {
    public event DataAddedEventHandler DataAdded;
    private void AddButton_Click(object sender, EventArgs e)
    {
        //do The database stuff...

        //fire the event
        OnDataAdded();
    }

    private void OnDataAdded()
    {
        if (DataAdded != null)
        {
            DataAdded(this, new EventArgs());
        }
    }

FormB

 public void HookupListener(FormA dataform)
 {
      //hook up the event to the handler
      dataform.DataAdded += new DataAddedEventHandler(dataform_DataAdded);
 }

 void dataform_DataAdded(object sender, EventArgs e)
 {
       //refresh the combo box
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消