【问题标题】:Add label under button in a flowLayoutPanel在 flowLayoutPanel 中的按钮下添加标签
【发布时间】:2014-04-01 20:13:32
【问题描述】:

当我将文件拖放到表单上时,我需要在 flowLayoutPanel 中动态创建带有标签的按钮。但是,由于 FLP 自行排列控件,如何将标签位置设置为按钮下方..?

我尝试过的:

 void Form1_DragDrop(object sender, DragEventArgs e)
    {
        string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];

        foreach (string s in fileList)
            {
                Button button = new Button();

                button.Click += new EventHandler(this.button_Click);
                fl_panel.Controls.Add(button);

                Icon icon = System.Drawing.Icon.ExtractAssociatedIcon(filename);
                Bitmap bmp = icon.ToBitmap();
                button.BackgroundImage = bmp;
                button.Width = 60;
                button.Height = 75;
                button.FlatStyle = FlatStyle.Flat;
                button.BackgroundImageLayout = ImageLayout.Stretch;            

                int space = 5;
                int Yy = button.Location.Y;
                int Xx = button.Location.X;
            Label label = new Label();
            label.Location = new Point(Yy + space, Xx);
            //label.Margin.Top = button.Margin.Bottom;

            fl_panel.Controls.Add(label);
            }
    }

【问题讨论】:

    标签: c# button label flowlayoutpanel


    【解决方案1】:

    我知道的最好的想法是实现一个自定义控件,其中包含正确排列的按钮和标签。然后将自定义控件添加到 FlowLayoutPanel。

    public class CustomControl:Control
    {
        private Button _button;
        private Label _label;
        public CustomControl(Button button, Label label)
        {
            _button = button;
            _label = label;
            Height = button.Height + label.Height;
            Width = Math.Max(button.Width, label.Width);
            Controls.Add(_button);
            _button.Location = new Point(0,0);
            Controls.Add(_label);
            _label.Location = new Point(0, button.Height);
        }
    }
    

    然后你可以像这样添加它:

    for (int i = 0; i < 10; i++)
    {
        CustomControl c = new CustomControl(new Button {Text = "Button!"}, new Label {Text = "Label!"});
        fl_panel.Controls.Add(c);
    }
    

    编辑: 如果你想监听按钮事件,试试这个:

    for (int i = 0; i < 10; i++)
    {
        var button = new Button {Text = "Button " + i};
        CustomControl c = new CustomControl(button, new Label {Text = "Label!"});
        button.Click += buttonClicked;
        fl_panel.Controls.Add(c);
    }
    ...
    private void buttonClicked(object sender, EventArgs e)
    {
        MessageBox.Show(((Button) sender).Text);
    }
    

    【讨论】:

    • 控件现在添加得很好,但点击事件没有触发......在我使用 button.tag 之前,在点击事件中,我将标签添加到字符串“String path_app = ((发送者为 CustomControl). 标记为字符串);"我使用了“button.Click += new EventHandler(button_Click);”定义事件..可能是什么问题?
    • 发送者将是一个按钮,而不是一个自定义控件。要么让 CustomControl 监听点击,然后通过你自己的事件使用适当的标签传递它,要么将点击投射到一个按钮(作为按钮的发送者)并使用它。
    • 即使将发送者作为 Button 也是一样的。我所拥有的是:“CustomControl button = new CustomControl(new Button { Tag = path_app, BackgroundImage = bmp }, new Label { Text = file_name } );" fl_panel.Controls.Add(按钮); button.Click += new EventHandler(button_Click);并在事件中单击: String path_app = ((sender as Button).Tag as String);
    • 我编辑了答案中的代码,以便为您提供更清晰的示例。
    猜你喜欢
    • 1970-01-01
    • 2011-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多