TabControl里面的FlowLayoutPanel

时间:2014-04-01 06:41:21

标签: c# tabcontrol flowlayoutpanel

我在每个标签内都有一个tabControl和一个flowLayoutPanel。当我将一个文件拖放到一个标签上时,它会创建一个按钮,文件图标被删除。但我可以选择创建更多选项卡,我希望能够将文件拖动到选定的选项卡..但问题是添加按钮时的flowLayoutPanel ..

到目前为止我的代码:

   public Process myProcess = new Process();
   FlowLayoutPanel fl_panel = new FlowLayoutPanel();

    string path_app;

    public Form1()
    {

        InitializeComponent();
     //add the flowLayoutPanel on the first tab
        fl_panel.Dock = DockStyle.Fill;
        fl_panel.BringToFront();
        tabPage1.Controls.Add(fl_panel);

        this.DragEnter += new DragEventHandler(Form1_DragEnter);
        this.DragDrop += new DragEventHandler(Form1_DragDrop);

    }

    void Form1_DragEnter(object sender, DragEventArgs e)
    {
        if (e.Data.GetDataPresent(DataFormats.FileDrop, false))
         e.Effect = DragDropEffects.All;
    }


    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);
                path_app = String.Format("{0}", s);
                button.Tag = path_app;
                string filename = path_app;
                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;

            }
    }
   private void button_Click(object sender, System.EventArgs e)
    {
        String path_app = ((sender as Button).Tag as String); 
        myProcess.StartInfo.FileName = path_app;
        myProcess.Start();

    }

   private void add_tab_btn_Click(object sender, EventArgs e)
    {

       //Create new tab with FLP inside
        string title = Convert.ToString(textBox1.Text);
        TabPage new_TabPage = new TabPage(title);
        fl_panel.Dock = DockStyle.Fill; 
        fl_panel.BringToFront();
        new_TabPage.Controls.Add(fl_panel);
        tabControl1.TabPages.Add(new_TabPage);
    }
}

如果我使用fl_panel.Controls.Add(button);它在第一个选项卡上添加了很好的按钮,但如果我创建一个新选项卡,我不知道如何使用tabControl.SelectedTab和fl_panel.Controls.Add(按钮)在所选选项卡上正确添加按钮

1 个答案:

答案 0 :(得分:3)

您必须为每个标签创建一个新的FlowLayoutPanel

FlowLayoutPanel fl_panel = new FlowLayoutPanel();
...
new_TabPage.Controls.Add(fl_panel);

然后您可以将TabPage的第一个元素投射到FlowLayoutPanel并从那里访问Controls

FlowLayoutPanel selectedFLP = (FlowLayoutPanel)tabControl.SelectedTab.Controls[0];
...