在另一个事件中使用在 Form_Load 上创建的控件

发布于 2024-11-05 08:18:41 字数 782 浏览 0 评论 0原文

我在 Form_Load 上创建了两个控件,一个按钮和一个组合框。我还有一个按钮事件,但该事件应该能够看到新创建的组合框。 当我尝试按名称调用组合框时,它说它在此上下文中不存在。

private void Form1_Load(object sender, EventArgs e)
{
    Button przycisk = new Button(); 
    przycisk.Name = "przycisk";
    przycisk.Dock = DockStyle.Bottom;
    przycisk.Text = "Wybierz";

    ComboBox kombo = new ComboBox(); 
    kombo.Name = "kombo";
    kombo.Dock = DockStyle.Bottom;
    kombo.Items.Add("Przycisk");   
    kombo.Items.Add("Etykeita");
    kombo.Items.Add("Pole tekstowe");

    Controls.Add(kombo);  
    Controls.Add(przycisk);

    przycisk.Click += new EventHandler(przycisk_Click); 
}

private void przycisk_Click(object sender, EventArgs e)
{
    kombo.Items.Add("Panel");  //just an example 
}

有办法让这项工作发挥作用吗?

I have two controls created on Form_Load, a button and a combobox. I also have an event for the button, but the event should be able to see the newly created combobox.
When I try to call the combobox by it's name it says that it doesn't exist in this context.

private void Form1_Load(object sender, EventArgs e)
{
    Button przycisk = new Button(); 
    przycisk.Name = "przycisk";
    przycisk.Dock = DockStyle.Bottom;
    przycisk.Text = "Wybierz";

    ComboBox kombo = new ComboBox(); 
    kombo.Name = "kombo";
    kombo.Dock = DockStyle.Bottom;
    kombo.Items.Add("Przycisk");   
    kombo.Items.Add("Etykeita");
    kombo.Items.Add("Pole tekstowe");

    Controls.Add(kombo);  
    Controls.Add(przycisk);

    przycisk.Click += new EventHandler(przycisk_Click); 
}

private void przycisk_Click(object sender, EventArgs e)
{
    kombo.Items.Add("Panel");  //just an example 
}

Is there a way to make this work?

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

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

发布评论

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

评论(2

陪你搞怪i 2024-11-12 08:18:41

只有在带有 runat="server" 的标记中使用的控件才是页面上的类变量。它们实际上是在设计器文件中定义的。

您需要做的是在类中添加类似以下内容的内容,其中您有一个类变量,然后在页面加载函数中分配 kombo。然后,它将存在于您的点击事件处理程序中。

 // kombo is now scoped for use throughout this class
 ComboBox kombo = null;

 private void Form1_Load(object sender, EventArgs e)
    {
        Button przycisk = new Button(); 
        przycisk.Name = "przycisk";
        przycisk.Dock = DockStyle.Bottom;
        przycisk.Text = "Wybierz";

        // Assign to our kombo instance
        kombo = new ComboBox(); 
        kombo.Name = "kombo";
        kombo.Dock = DockStyle.Bottom;
        kombo.Items.Add("Przycisk");   
        kombo.Items.Add("Etykeita");
        kombo.Items.Add("Pole tekstowe");

        Controls.Add(kombo);  
        Controls.Add(przycisk);

        przycisk.Click += new EventHandler(przycisk_Click); 

    }

    private void przycisk_Click(object sender, EventArgs e)
    {
        // Using the kombo we created in form load, which is still referenced
        // in the class
        kombo.Items.Add("Panel");  //just an example 
    }

Only controls which are in used in markup with runat="server" will be class variables on your page. They are actually defined in the designer file.

What you'll want to do is in the class add something like the following where you have a class variable, then assign kombo in your page-load function. Then, it will exist in your click event handler.

 // kombo is now scoped for use throughout this class
 ComboBox kombo = null;

 private void Form1_Load(object sender, EventArgs e)
    {
        Button przycisk = new Button(); 
        przycisk.Name = "przycisk";
        przycisk.Dock = DockStyle.Bottom;
        przycisk.Text = "Wybierz";

        // Assign to our kombo instance
        kombo = new ComboBox(); 
        kombo.Name = "kombo";
        kombo.Dock = DockStyle.Bottom;
        kombo.Items.Add("Przycisk");   
        kombo.Items.Add("Etykeita");
        kombo.Items.Add("Pole tekstowe");

        Controls.Add(kombo);  
        Controls.Add(przycisk);

        przycisk.Click += new EventHandler(przycisk_Click); 

    }

    private void przycisk_Click(object sender, EventArgs e)
    {
        // Using the kombo we created in form load, which is still referenced
        // in the class
        kombo.Items.Add("Panel");  //just an example 
    }
半步萧音过轻尘 2024-11-12 08:18:41

您必须首先使用 FindControl() 方法来查找对象。

private void przycisk_Click(object sender, EventArgs e)
{
   ComboBox kombo = (ComboBox)FindControl("kombo");
   kombo.Items.Add("Panel");
}

You will have to use the FindControl() method to find the object first.

private void przycisk_Click(object sender, EventArgs e)
{
   ComboBox kombo = (ComboBox)FindControl("kombo");
   kombo.Items.Add("Panel");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文