C# 将 formname.designer.cs 中生成的 dataTable 设置为静态

发布于 2024-11-26 13:47:49 字数 691 浏览 4 评论 0原文

我想知道是否可以修改 formname.designer.cs 并将从设计模式生成的变量设置为私有静态:

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

我在这里阅读 C# Set Checkbox to Static 这不是一个好方法。 也许我可以用其他方式做到这一点。这就是我想要做的:

我有一个包含更多表单的表单,在面板中打开。一种表单包含一些带有数据库值的组合框。问题是,当我从带有文本框的另一个表单向数据库添加更多值时,需要再次填充组合框。我认为如果我在添加一些值后立即更新组合框,这可能会很容易。 (组合框和文本框 - 在数据库中添加由组合框显示的值 - 具有不同的形式)。

您还有其他这样做的想法吗?我还尝试在单击组合框时再次填充组合框,但因为我有更多组合框,所以当我快速从一个组合框单击到另一个组合框时,我会遇到一些致命错误。

编辑:作为最后一种方法:我可以添加一个按钮并在按下按钮时填充组合框,但我想自动执行此操作

(winforms而不是Web表单)

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 could add a button and fill the combobox when the button is pressed, but I want to do it automatically

(winforms not web forms)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无风消散 2024-12-03 13:47:49

一种方法是在添加值时在 FormA 上触发事件。
Form B 可以订阅事件并更新列表。
唯一棘手的一点是 FormB 需要引用 FormA 才能连接到事件。

像这样的东西...

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
 }

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
 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文