如何在C#中从数据库值创建动态按钮 [英] How to Create Dynamic buttons in C# from Database Values

查看:280
本文介绍了如何在C#中从数据库值创建动态按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发食物应用程序,这取决于我的数据库值,我必须得到动态按钮和事件处理程序..如果有人知道只是帮助我!!!

I m developing food application in that depends upon my database value's i have to get the dynamic button and also event handler..if any one knows just help me!!!

推荐答案

您可以在Windows窗体中创建所需数量的按钮。

假设您已经在数据库中查询了这些值,并且已经在DataTable称dtButtonsToCreate为一列,称为'fruits'。

要根据需要创建尽可能多的按钮,请执行以下操作:

You can create as many buttons as you need in Windows Forms.
Suppose that you already queried those values in your database and that you have them in a DataTable called dtButtonsToCreate with a single column, say 'fruits'.
To create as many buttons as needed do:
for (int i = 0; i < dtButtonsToCreate.Count; i++)
{
      Button b = new Button();
      b.Click  += new EventHandler(OnButtonClick);
      this.Controls.Add(b);
//you will also need to add some code to accurately size and place the button. You can
//do that based on the amount of controls there are with dtButtonsToCreate.Count
} 





您需要提前为事件处理程序提供方法,如上所示。

此前需要在您的代码中(无法动态创建):



you need to have a method in advance for the event handler as seen above.
THIS NEEDS TO BE IN YOUR CODE IN ADVANCE (can't be created dynamically):

private void OnButtonClick(object sender, EvenArgs e)
{
     //your code for the event.
}









等等!

如果您需要不同的按钮,你必须要他们做不同的事情。

这样每个按钮都会触发完全相同的动作...



让我们解决这个问题!

创建一个名为ExtendedButton的新类,并从按钮类继承:







BUT WAIT!
If you need different buttons, you must need them to do different things.
In this way every button would trigger the exact same action...

Let's solve that!
Create a new class called ExtendedButton and inherit from the button class:

using System.Windows.Forms

namespace blah
{
    class ExtendedButton : System.Windows.Forms.Button //this inherits from button
    {

        //constructor
        public ExtendedButton()
        {  /*you dont need to do anything here*/  } 


        //add public property
        private string myval;
        public string _myval
        {
          get{ return myval; }
          set{ myval = value; }
        }
    }
}







现在做您在开始时所做的相同代码但有变化:




Now do the same code you did at the beginning but with variation:

for (int i = 0; i < dtButtonsToCreate.Count; i++)
{
      ExtendedButton b = new ExtendedButton(); //with ExtendedButton this time
      b._myval = dtButtonsToCreate.Rows[i][0].ToString();//this asigns the fruit name to the extended button
      b.Click  += new EventHandler(OnButtonClick);
      this.Controls.Add(b);
//you will also need to add some code to accurately size and place the button. You can
//do that based on the amount of controls there are with dtButtonsToCreate.Count
} 





之后,你可以获得按钮的值来执行你需要的任何东西。事件处理程序:



There after, you can get the value of the button to perform anything you need in the event handler:

private void OnButtonClick(object sender, EvenArgs e)
{
     string TheFruit = ((ExtendedButton)sender)._myval;
     //your code for the event.
}







希望这会有所帮助!



如果你需要这个在ASP.Net中,你几乎可以做同样的事情但坚持这个链接中的例子:

http://msdn.microsoft.com/en-us/library/ms743596.aspx [ ^ ]


这篇关于如何在C#中从数据库值创建动态按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆