所以我正在尝试基于flowlayoutpanel创建一个自定义控件。这个面板基本上创建了在打开或关闭时链接到不同表单的按钮 - 您可以将其视为基于FlowLayoutPanel的自定义CrumbBar - 我没有使用crumbBar作为基础,因为您无法创建自定义按钮碎屑的形状。
但是,我的问题是我需要按钮才能更好地组合在一起。 (见下图)
在左侧,您可以看到按钮靠得很近并且排列成“适合”在一起。在右边,你可以看到我希望摆脱一些多余的间距。
我玩过控件的填充/边距,但我还没有找到任何工作。目前,对于所有控件,我的Margin和Padding设置为0,0,0,0 - 主窗体,自定义控件,按钮控件,与此自定义控件相关的任何内容都具有属性设置,以及因为它硬编码到代码中,见下文
using System;
using System.Drawing;
using System.Windows.Forms;
namespace PremierMerchant
{
public partial class CcCrumbBarFlow : FlowLayoutPanel
{
public CcCrumbBarFlow()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs pe)
{
base.OnPaint(pe);
this.Margin = new Padding(0);
this.Padding = new Padding(0);
}
public void AddButton(Form form)
{
//Form Main is the active form
var mainForm = Form.ActiveForm as FormMain;
if (mainForm != null)
{
Button button;
//if it's the first button, create a new Flat Arrow button
if (mainForm.crumbBarMainFlow.Controls.Count == 0)
{
button = new CC_ButtonArrowShapedFlat();
}
//...otherwise, add a new regular arrow one
else
{
button = new CC_ButtonArrowShaped();
}
//Set some properties
button.Text = form.Name.ToString();
button.Name = form.Name.ToString();
button.Margin = new Padding(0);
button.Height = 32;
button.Width = 100;
button.Click += (s, e) =>
{
RemoveButton(form);
};
//add the button
mainForm.crumbBarMainFlow.Controls.Add(button);
}
}
public void RemoveButton(Form form)
{
//set mainform as the active button
var mainForm = Form.ActiveForm as FormMain;
//for all controls, starting from the latest added one
for (int i = mainForm.crumbBarMainFlow.Controls.Count - 1; i >= 0; i--)
{
//if you find the control you're looking for, stop, and break
if(mainForm.crumbBarMainFlow.Controls[i].Name == form.Name)
{
break;
}
else
{
//for every currently open form, starting from the back of the list
Form f;
for(int j = mainForm.openForms.Count - 1; j >= 0; j --)
{
//set the for you're looking at as the most recently opened form
f = mainForm.openForms[j];
//if this form matches a button on the crumbbar
if(f.Name == mainForm.crumbBarMainFlow.Controls[j].Name)
{
//remove the form, close the form, remove the control, and break
mainForm.openForms.Remove(f);
f.Close();
mainForm.crumbBarMainFlow.Controls.Remove(mainForm.crumbBarMainFlow.Controls[j]);
break;
}
}
}
}
}
}
}
有人有任何提示吗?
编辑:有人提到使用Resize事件,但我的表单从未调整过大小。一旦它被铺设,它就被修复了。