动态地创建在C#Windows窗体控件 [英] Dynamically creating Controls in c# Windows Forms

查看:132
本文介绍了动态地创建在C#Windows窗体控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是建立在C#Windows窗体的书店应用
的过程中,我感到在这里做一个应用程序结果
1.Inserting书名,书图像数据库。结果
2.Now我需要检索的图像形式,但都应该像结果被动态地创建,如果我有两本书面板,图片框,按钮,应当设立教科书和价值观应还有表现..结果
如果我在DB有三个entereids意味着三个图片框,按钮和教科书应建立和值应在形式那里表现。结果
有什么的方式来解决这个问题了..搜索结果

I am just creating an application in C# Windows Forms for Book Store application The process i am doing here is
1.Inserting Book name, book image to Database.
2.Now i need to retrieve as in the image to forms but all should be dynamically created like
if i have two books this panel with picture box, button and that textbook should be created and values should be showed there..
If i have got three entereids in DB means the three picture box, button and that textbook should be created and values should be showed there in the form.
is there any way to solve this out..

需要根据动态创建这些控件的 N 的在数据库搜索结果
的行数。有关我的问题更加明晰请参阅图片
结果

Need to Create these controls Dynamically according to the "n" number of rows in Database

For more Clarity about my question please see the image

推荐答案

这很简单,实现你想要什么。你只需要创建一些用户控件每本书或面板被确定。我将介绍使用面板它。事情是这样的:

It's simple to achieve what you want. You just need to create some UserControl for each book or a Panel is OK. I'll introduce to using the Panel for it. Something like this:

public class BookPanel : Panel
{
    public string BookName
    {
        get { return text.Text; }
        set { text.Text = value; }
    }
    public Image BookCover
    {
        get { return pic.Image; }
        set { pic.Image = value; }
    }
    public event EventHandler BuyBook;
    public string BuyButtonText
    {
        get { return button.Text; }
        set { button.Text = value; }
    }        
    //inner child controls
    PictureBox pic = new PictureBox();
    TextBox text = new TextBox();
    Button button = new Button();
    public BookPanel()
    {
        pic.Parent = text.Parent = button.Parent = this;
        pic.Top = 5;
        text.Left = pic.Left = 5;
        button.Text = "Buy";
        button.Width = 50;
        button.Anchor = AnchorStyles.Right | AnchorStyles.Bottom;
        text.Anchor = AnchorStyles.Left | AnchorStyles.Bottom | AnchorStyles.Right;
        pic.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
        pic.SizeMode = PictureBoxSizeMode.StretchImage;
        button.Click += (s, e) =>
        {
            EventHandler handler = BuyBook;
            if (handler != null) handler(this, EventArgs.Empty);
        };
    }
    bool init;
    protected override void OnSizeChanged(EventArgs e)
    {            
        base.OnSizeChanged(e);
        if (!init)
        {
            text.Width = Width - button.Width - 12;
            button.Left = text.Right + 5;
            pic.Height = Height - 35;
            pic.Width = Width - 10;
            text.Top = pic.Bottom + 5;
            button.Top = text.Top - 2;
            init = true;
        }
    }
}

//Usage
bookPanel1.BookCover = yourImage;
//Try this to see how the Buy button works
bookPanel1.BuyBook += (s, e) => {
    MessageBox.Show(bookPanel1.BookName);
};

注意:上面的代码不完成,你没有定制的外观和感觉你的文本框,按钮和图片框的全部能力,但是你可以添加代码,这样做,该代码是一个小许多。我觉得对于一个简单的控制,这是不够的。你也应该注意使用码头属性,则保证金填充属性和其它的布局和容器控件像面板 FlowLayoutPanel的的TableLayoutPanel 来定制自己的控制。

NOTE: The code above is not complete, you don't have full ability to customize the look and feel of your TextBox, Button and PictureBox, however you can add code to do so, the code is a little much. I think for a simple control, it's enough. You should also notice the use of Dock property, the Margin and Padding properties and other layout and container controls like Panel, FlowLayoutPanel, TableLayoutPanel to customize your own control.

要使用它在 FlowLayoutPanel的,试试这个代码:

To use it in a FlowLayoutPanel, try this code:

flowLayoutPanel1.AutoScroll = true;
for (int i = 0; i < 100; i++) {
   new BookPanel {
                Parent = flowLayoutPanel1, 
                Width = 150, 
                Height = 200,
                BorderStyle = BorderStyle.FixedSingle,
                BookCover = yourImageList[i]
    }.BuyBook += buyBook;
}
private void buyBook(object sender, EventArgs e){
   BookPanel book = sender as BookPanel;
   //your code goes here ....
}

这篇关于动态地创建在C#Windows窗体控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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