C# Windows 创建控件时出现故障
C# Windows Forms glitch while creating a control
我以某种方式注意到,当我在我的应用程序中创建控件时,它首先显示为奇怪的矩形,然后它“缩小”为正确的形式。
First thing you'll see after creation
Correctly created object
我的问题是,为什么我首先看到左上角的矩形,然后突然变成正确的形状。就像一瞬间的事情(我不得不使用 Thread.Sleep 因为不可能截屏),但我的眼睛仍然可以看到它,当它发生时我真的被触发了。
这是我创建控件的代码:
var label = new Label
{
AutoSize = true,
TextAlign = ContentAlignment.MiddleLeft,
Font = new Font("Courier New", 9F, FontStyle.Regular,
GraphicsUnit.Point, 238),
Text = keyword,
Margin = new Padding(0, 6, 25, 3),
Padding = new Padding(0, 3, 0, 0)
};
var button = new Button
{
BackgroundImage = Image.FromFile("../../../Images/cross_200x200.png"),
BackgroundImageLayout = ImageLayout.Stretch,
Dock = DockStyle.Right,
Width = 21,
Height = 21,
FlatStyle = FlatStyle.Flat,
};
button.FlatAppearance.BorderSize = 0;
var pan = new Panel
{
AutoSize = true,
Padding = new Padding(0, 0, 1, 1),
BackColor = Color.PowderBlue,
BorderStyle = BorderStyle.FixedSingle,
Tag = keyword
};
button.Click += delegate
{
_keywords.Remove(pan.Controls.OfType<Label>().First().Text);
pan.Dispose();
StatusLabel.Text = $@"Removed {keyword}";
};
pan.Controls.Add(label);
pan.Controls.Add(button);
FlowLayoutPanelMain.Controls.Add(pan);
每次在 FlowLayoutPanel 控件中添加一个“关键字”时,首先它是左上角的一个矩形,然后紧接着就可以了。
在我一个朋友的帮助下,我们发现这是由于 Windows Forms 旧技术(它可能不会发生在 .NET Core 的 WPF 中)并且无法在运行时创建控件.所以,为此提供的解决方案似乎只是 .Hide()
控件, .Add()
到 FlowLayoutPanel 然后简单地 .Show()
它回来,现在我的眼睛会很满意。
...
pan.Hide();
FlowLayoutPanelMain.Controls.Add(pan);
pan.Show();
...
尝试删除 AutoSize 属性 并将 new Size()
分配给 panel
AutoSize = false,
为我工作
我以某种方式注意到,当我在我的应用程序中创建控件时,它首先显示为奇怪的矩形,然后它“缩小”为正确的形式。
First thing you'll see after creation
Correctly created object
我的问题是,为什么我首先看到左上角的矩形,然后突然变成正确的形状。就像一瞬间的事情(我不得不使用 Thread.Sleep 因为不可能截屏),但我的眼睛仍然可以看到它,当它发生时我真的被触发了。
这是我创建控件的代码:
var label = new Label
{
AutoSize = true,
TextAlign = ContentAlignment.MiddleLeft,
Font = new Font("Courier New", 9F, FontStyle.Regular,
GraphicsUnit.Point, 238),
Text = keyword,
Margin = new Padding(0, 6, 25, 3),
Padding = new Padding(0, 3, 0, 0)
};
var button = new Button
{
BackgroundImage = Image.FromFile("../../../Images/cross_200x200.png"),
BackgroundImageLayout = ImageLayout.Stretch,
Dock = DockStyle.Right,
Width = 21,
Height = 21,
FlatStyle = FlatStyle.Flat,
};
button.FlatAppearance.BorderSize = 0;
var pan = new Panel
{
AutoSize = true,
Padding = new Padding(0, 0, 1, 1),
BackColor = Color.PowderBlue,
BorderStyle = BorderStyle.FixedSingle,
Tag = keyword
};
button.Click += delegate
{
_keywords.Remove(pan.Controls.OfType<Label>().First().Text);
pan.Dispose();
StatusLabel.Text = $@"Removed {keyword}";
};
pan.Controls.Add(label);
pan.Controls.Add(button);
FlowLayoutPanelMain.Controls.Add(pan);
每次在 FlowLayoutPanel 控件中添加一个“关键字”时,首先它是左上角的一个矩形,然后紧接着就可以了。
在我一个朋友的帮助下,我们发现这是由于 Windows Forms 旧技术(它可能不会发生在 .NET Core 的 WPF 中)并且无法在运行时创建控件.所以,为此提供的解决方案似乎只是 .Hide()
控件, .Add()
到 FlowLayoutPanel 然后简单地 .Show()
它回来,现在我的眼睛会很满意。
...
pan.Hide();
FlowLayoutPanelMain.Controls.Add(pan);
pan.Show();
...
尝试删除 AutoSize 属性 并将 new Size()
分配给 panel
AutoSize = false,
为我工作