我正在尝试创建一个弹出窗口,以通知用户操作成功。它应该可以看到几秒钟,然后自行消失。为此,我创建了一个继承自Form的表单,并将ShowWithoutActivation属性设置为true,还创建了一个静态类来控制其构造和操纵其不透明度。
问题在于创建新表单时,它具有正确的大小和初始不透明度,但完全空白。唯一的子控件,没有显示停靠的(填充)标签。我在设计器中设置的Background / Foreground Color属性似乎也被默认值所忽略。窗体会按预期淡入淡出和关闭。
表格本身:
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;
}
}
并且,如果有帮助,请参见以下TempForm的设计器代码:
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.messageLabel = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// messageLabel
//
this.messageLabel.BackColor = System.Drawing.Color.Black;
this.messageLabel.Dock = System.Windows.Forms.DockStyle.Fill;
this.messageLabel.Font = new System.Drawing.Font("Cambria", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.messageLabel.ForeColor = System.Drawing.Color.White;
this.messageLabel.Location = new System.Drawing.Point(0, 0);
this.messageLabel.Name = "messageLabel";
this.messageLabel.Size = new System.Drawing.Size(254, 81);
this.messageLabel.TabIndex = 0;
this.messageLabel.Text = "Message";
this.messageLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// TempForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 19F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.Black;
this.ClientSize = new System.Drawing.Size(254, 81);
this.Controls.Add(this.messageLabel);
this.Font = new System.Drawing.Font("Cambria", 12F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.ForeColor = System.Drawing.Color.White;
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Margin = new System.Windows.Forms.Padding(4, 4, 4, 4);
this.Name = "TempForm";
this.Opacity = 0.8D;
this.StartPosition = System.Windows.Forms.FormStartPosition.Manual;
this.Text = "tempForm";
this.TopMost = true;
this.ResumeLayout(false);
}
#endregion
public System.Windows.Forms.Label messageLabel;
对于那些询问如何调用该方法的人:
private void btnUpdate_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
/* Input validation here */
if (cmbBox.SelectedValue != null)
{
updateMethod(a, b....out errorMessage);
if (String.IsNullOrEmpty(errorMessage))
FadeAwayNotifier.DisplayFadeNotification(this.Location, "Rule successfully updated");
}
else
MessageBox.Show("A selection must be made in order to update");
Cursor.Current = Cursors.Default;
}
我进行了搜索,但没有发现任何与情况有关的信息。不过,如果我错过了一些事情,我很乐意得到纠正。
为什么创建的表单完全空白?
答案 0 :(得分:1)
您的表单为空,因为您试图将其显示在Task.Factory.StartNew()
内。
Task.Factory.StartNew()
在其内部异步运行代码,由于某种原因,这是form.Show()
方法的问题。
解决方案是使用Task t = Task.Factory.StartNew(...
而不是Task t = new Tast(....
,然后在创建任务后使用t.RunSynchronously()
运行它。这样就可以了。
我如何显示临时表格是
创建临时表单(通过vs解决方案资源管理器)创建新的空白winform
添加这样的代码:
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");
还要使其看起来更好(不像标准形式),所以看起来像这样: