C# 以编程方式创建的表单为空
我正在尝试创建一个弹出窗口,通知用户操作成功。它应该在几秒钟内可见并自行消失。为此,我创建了一个从表单继承的表单,并将ShowWithoutActivation属性设置为true,还创建了一个静态类来控制其构造和操纵其不透明度 问题是,创建新表单时,它的大小和初始不透明度正确,但完全为空。作为唯一的子控件,不显示停靠(填充)标签。我在设计器中设置的背景/前景颜色属性似乎也被忽略,而支持默认值。窗体会按预期淡入并关闭 表格本身:C# 以编程方式创建的表单为空,c#,visual-studio,winforms,C#,Visual Studio,Winforms,我正在尝试创建一个弹出窗口,通知用户操作成功。它应该在几秒钟内可见并自行消失。为此,我创建了一个从表单继承的表单,并将ShowWithoutActivation属性设置为true,还创建了一个静态类来控制其构造和操纵其不透明度 问题是,创建新表单时,它的大小和初始不透明度正确,但完全为空。作为唯一的子控件,不显示停靠(填充)标签。我在设计器中设置的背景/前景颜色属性似乎也被忽略,而支持默认值。窗体会按预期淡入并关闭 表格本身: public partial class TempForm : Fo
public partial class TempForm : Form
{
protected override bool ShowWithoutActivation
{
get { return true; }
}
public TempForm(string message)
{
InitializeComponent();
messageLabel.Text = message;
}
private TempForm() { }
}
创建它的静态类:
public static class FadeAwayNotifier
{
public static void DisplayFadeNotification(Point parentLocation, string message)
{
Task t = Task.Factory.StartNew(() =>
{
int fadeDelay = 2000;
int animationLength = 1000;
int threadRestLength = 20;
Form popUp = buildForm(parentLocation, message);
popUp.Show();
int opacityIncrements = (animationLength / threadRestLength);
double opacityDecrementAmount = popUp.Opacity / opacityIncrements;
Thread.Sleep(fadeDelay);
for (int i = 0; i < opacityIncrements; i++)
{
Thread.Sleep(threadRestLength);
popUp.Opacity -= opacityDecrementAmount;
}
popUp.Close();
});
}
private static Form buildForm(Point startLocation, string text)
{
TempForm returnForm = new TempForm(text);
returnForm.Location = startLocation;
returnForm.messageLabel.Text = text;
returnForm.BackColor = Color.Black;
returnForm.ForeColor = Color.White;
return returnForm;
}
}
我到处找了找,但没有看到任何与情况有关的东西。不过,如果我遗漏了什么,我很高兴被纠正
为什么创建的表单是完全空白的?您的表单是空白的,因为您试图在
Task.Factory.StartNew()中显示它
Task.Factory.StartNew()
在其中异步运行代码,这是form.Show()方法由于某种原因出现的问题
解决方法是使用Task t=Task.Factory.StartNew(…
使用Task t=new Tast(…
),然后在创建任务后,使用t.RunSynchronously()
运行它。这样它就会工作
我如何显示临时表单:
创建临时表单创建新的空白winform(通过vs解决方案资源管理器)
添加如下代码:
public partial class TempForm : Form
{
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
double seconds = 3;
public TempForm(int secs, string text)
{
InitializeComponent();
//Custom property to dock it to down right to the screen
Rectangle workingArea = Screen.GetWorkingArea(this);
this.Location = new Point(workingArea.Right - Size.Width, workingArea.Bottom - Size.Height);
t = new System.Windows.Forms.Timer();
this.seconds = (secs != 0) ? secs : this.seconds;
richTextBox1.Text = text;
}
private void TempForm_Load(object sender, EventArgs e)
{
t.Interval = (int)(seconds * 1000);
t.Tick += new EventHandler(CloseForm);
t.Start();
}
private void CloseForm(object sender, EventArgs e)
{
this.Close();
this.Dispose();
}
public static void Show(int seconds, string text)
{
TempForm tf = new TempForm(seconds, text);
tf.Show();
}
}
要调用它,只需使用TempForm.Show(10,“SomeText”);
还可以使它看起来更好(不像标准表单),因此它看起来如下所示:
public partial class TempForm : Form
{
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
double seconds = 3;
public TempForm(int secs, string text)
{
InitializeComponent();
//Custom property to dock it to down right to the screen
Rectangle workingArea = Screen.GetWorkingArea(this);
this.Location = new Point(workingArea.Right - Size.Width, workingArea.Bottom - Size.Height);
t = new System.Windows.Forms.Timer();
this.seconds = (secs != 0) ? secs : this.seconds;
richTextBox1.Text = text;
}
private void TempForm_Load(object sender, EventArgs e)
{
t.Interval = (int)(seconds * 1000);
t.Tick += new EventHandler(CloseForm);
t.Start();
}
private void CloseForm(object sender, EventArgs e)
{
this.Close();
this.Dispose();
}
public static void Show(int seconds, string text)
{
TempForm tf = new TempForm(seconds, text);
tf.Show();
}
}
您不显示放置控件的代码在表单上,你真的不应该在任务中创建表单。我建议使用UWP@pm100。将标签放在表单上的代码是通过设计器完成的。设计器代码仍然有用吗?@Danial a.White你能解释一下为什么不应该在任务中创建表单吗?我尝试异步执行此操作,以免中断用户的工作。虽然此代码运行良好,可以根据我的目的进行修改,但我仍然想知道我的原始解决方案显示空白表单的原因。您能将您的行放在调用DisplayFadeNotification
method的位置吗?@NealDarley找到了发生此问题的原因和解释。我将在5分钟内编辑答案很不幸,将任务更改为同步运行并没有解决此问题。我将继续修补它。@NealDarley您确定吗,因为我复制/粘贴了您的全部代码,出现了相同的错误,并执行了此操作,结果成功了
public partial class TempForm : Form
{
System.Windows.Forms.Timer t = new System.Windows.Forms.Timer();
double seconds = 3;
public TempForm(int secs, string text)
{
InitializeComponent();
//Custom property to dock it to down right to the screen
Rectangle workingArea = Screen.GetWorkingArea(this);
this.Location = new Point(workingArea.Right - Size.Width, workingArea.Bottom - Size.Height);
t = new System.Windows.Forms.Timer();
this.seconds = (secs != 0) ? secs : this.seconds;
richTextBox1.Text = text;
}
private void TempForm_Load(object sender, EventArgs e)
{
t.Interval = (int)(seconds * 1000);
t.Tick += new EventHandler(CloseForm);
t.Start();
}
private void CloseForm(object sender, EventArgs e)
{
this.Close();
this.Dispose();
}
public static void Show(int seconds, string text)
{
TempForm tf = new TempForm(seconds, text);
tf.Show();
}
}