1.Lablelable添加图片,解决图片和字体重叠?
Text属性添加足够空格即可,显示效果如下所示:
2.根据窗体名称获取窗体并显示到指定panel?
Label item = sender as Label;
if (item == null) return;
Assembly assembly = Assembly.GetExecutingAssembly();
var path = "Namespace." + item.Name;
Form form = assembly.CreateInstance(path) as Form;
if (form == null) return;
this.panelContent.Controls.Clear();
form.TopLevel = false;
form.FormBorderStyle = FormBorderStyle.None;
form.Dock = DockStyle.Fill;
form.Parent = this.panelContent;
this.panelContent.Controls.Add(form);
form.Show();
注意:item.Name为获取到的窗体名称,如:LoginForm.
3.panel添加控件并为控件添加事件?
public class MenuItemNodes
{
public string Value { get; set; }
public string Name { get; set; }
} private void InitNavigation(List<MenuItemNodes> items)
{
if (items == null) return; this.panleNavigation.Controls.Clear();
foreach (MenuItemNodes item in items)
{
Add(new Label(), item, this.panleNavigation);
}
} private void Add(Label item, MenuItemNodes node, Panel panel)
{
item.Name = node.Name;
item.Text = node.Value;
item.Size = new Size(, );
item.TextAlign = ContentAlignment.MiddleLeft;
item.ForeColor = Color.White;
item.Font = new Font("微软雅黑", 12f, FontStyle.Bold);
//34, 95, 129
item.BackColor = System.Drawing.Color.FromArgb(, , );
item.BorderStyle = BorderStyle.FixedSingle; if (panel.Controls.Count == ) item.Location = new Point();
else
{
int y = ;
int x = ;
if (panel.Controls.Count % > )
{
y = panel.Controls[panel.Controls.Count - ].Location.Y;
x = panel.Controls[panel.Controls.Count - ].Location.X + item.Width;
}
else
{
y = panel.Controls[panel.Controls.Count - ].Location.Y + item.Height;
x = panel.Controls[panel.Controls.Count - ].Location.X;
} item.Location = new Point(x, y);
}
item.MouseClick -= item_MouseClick;
item.MouseClick += new MouseEventHandler(item_MouseClick); panel.Controls.Add(item);
} void item_MouseClick(object sender, MouseEventArgs e)
{
}