表单上的按钮不工作或显示任何文本

时间:2015-03-14 04:42:05

标签: c# winforms button

我正在尝试为表单创建自定义标题栏。出于这个原因,我有:

this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

我已经定位了两个自定义按钮,用于最小化和关闭:

// Close
// 
this.Close.BackColor = System.Drawing.Color.Black;
this.Close.FlatAppearance.BorderColor = System.Drawing.Color.White;
this.Close.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.Close.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Close.ForeColor = System.Drawing.SystemColors.ButtonFace;
this.Close.Location = new System.Drawing.Point(376, 0);
this.Close.Name = "Close";
this.Close.Size = new System.Drawing.Size(27, 23);
this.Close.TabIndex = 1;
this.Close.Text = "X";
this.Close.UseVisualStyleBackColor = false;
this.Close.Click += new System.EventHandler(this.Close_Click);
// 
// Hide
// 
this.Hide.BackColor = System.Drawing.SystemColors.ControlText;
this.Hide.FlatAppearance.BorderColor = System.Drawing.Color.White;
this.Hide.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
this.Hide.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.Hide.ForeColor = System.Drawing.SystemColors.ButtonFace;
this.Hide.Location = new System.Drawing.Point(346, 0);
this.Hide.Name = "Hide";
this.Hide.Size = new System.Drawing.Size(30, 23);
this.Hide.TabIndex = 0;
this.Hide.Text = "---";
this.Hide.UseVisualStyleBackColor = true;

这意味着这样:

enter image description here

但是,由于某种原因,当我运行应用程序时,它们看起来像这样(没有文本):

What the APP draws

此外,我对其进行了编码,以便在点击Close按钮后运行此代码:

private void Close_Click(object sender, EventArgs e)
{
    this.Close();
}

然而,当我点击关闭时没有任何事情发生。

任何人都可以指出我的代码有什么问题。或者我必须添加什么来修复它。

编辑:

原来,当我将按钮颜色更改为红色时十字架显示,但它是黑色,而不是白色!

4 个答案:

答案 0 :(得分:0)

你可以尝试替换你喜欢的

this.Close.ForeColor = System.Drawing.SystemColors.ButtonFace;

lblExample.ForeColor = System.Drawing.Color.White;

答案 1 :(得分:0)

首先,您需要更改关闭和隐藏的设计名称。

CloseHide更改为其他名称。否则,与表单的隐藏和表单关闭会发生namespace次冲突。还要记住,如果没有FormBorderStyle,您将无法像往常一样移动表单。

尝试以下,这至少对我有用。

注意:更改是我已将控件放在我希望他们看到的位置而不是使用Drawing

public Mainform()
{

 InitializeComponent();

 this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;

 // Close
 // 
 this.CloseBut.BackColor = System.Drawing.Color.Black;
 this.CloseBut.FlatAppearance.BorderColor = System.Drawing.Color.White;
 this.CloseBut.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
 this.CloseBut.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
 this.CloseBut.ForeColor = System.Drawing.SystemColors.ButtonFace;
 //this.CloseBut.Location = new System.Drawing.Point(376, 0);
 this.CloseBut.Name = "Close";
 this.CloseBut.Size = new System.Drawing.Size(27, 23);
 this.CloseBut.TabIndex = 1;
 this.CloseBut.Text = "X";
 this.CloseBut.UseVisualStyleBackColor = false;
 this.CloseBut.Click += new System.EventHandler(this.Close_Click);
 // 
 // Hide
 // 
 this.HideBut.BackColor = System.Drawing.SystemColors.ControlText;
 this.HideBut.FlatAppearance.BorderColor = System.Drawing.Color.White;
 this.HideBut.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
 this.HideBut.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
 this.HideBut.ForeColor = System.Drawing.SystemColors.ButtonFace;
 //this.HideBut.Location = new System.Drawing.Point(346, 0);
 this.HideBut.Name = "Hide";
 this.HideBut.Size = new System.Drawing.Size(30, 23);
 this.HideBut.TabIndex = 0;
 this.HideBut.Text = "---";
 this.HideBut.UseVisualStyleBackColor = true;
 this.HideBut.Click += new EventHandler(this.hide_click);

}

private void hide_click(object sender, EventArgs e)
{
    this.WindowState=FormWindowState.Minimized;
}

private void Close_Click(object sender, EventArgs e)
{
    this.Close();
}

答案 2 :(得分:0)

它工作得很好。我试过了。我正在附加所有文件请更改命名空间并试一试。

Designer.cs代码

namespace WindowsFormsApplication1

{     部分类Form3     {         ///         ///所需的设计变量。         ///         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.Close = new System.Windows.Forms.Button();
        this.Hide = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // Close
        // 
        this.Close.BackColor = System.Drawing.Color.Black;
        this.Close.FlatAppearance.BorderColor = System.Drawing.Color.White;
        this.Close.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.Close.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Close.ForeColor = System.Drawing.SystemColors.ButtonFace;
        this.Close.Location = new System.Drawing.Point(260, 0);
        this.Close.Name = "Close";
        this.Close.Size = new System.Drawing.Size(27, 23);
        this.Close.TabIndex = 1;
        this.Close.Text = "X";
        this.Close.UseVisualStyleBackColor = false;
        this.Close.Click += new System.EventHandler(this.Close_Click);
        // 
        // Hide
        // 
        this.Hide.BackColor = System.Drawing.SystemColors.ControlText;
        this.Hide.FlatAppearance.BorderColor = System.Drawing.Color.White;
        this.Hide.FlatStyle = System.Windows.Forms.FlatStyle.Flat;
        this.Hide.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Hide.ForeColor = System.Drawing.SystemColors.ButtonFace;
        this.Hide.Location = new System.Drawing.Point(230, 0);
        this.Hide.Name = "Hide";
        this.Hide.Size = new System.Drawing.Size(30, 23);
        this.Hide.TabIndex = 0;
        this.Hide.Text = "---";
        this.Hide.UseVisualStyleBackColor = true;
        // 
        // Form3
        // 
        this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
        this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
        this.ClientSize = new System.Drawing.Size(284, 262);
        this.Controls.Add(this.Hide);
        this.Controls.Add(this.Close);
        this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
        this.Name = "Form3";
        this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
        this.Text = "Form3";
        this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.Button Close;
    private System.Windows.Forms.Button Hide;
}

}

关闭按钮单击

 private void Close_Click(object sender, EventArgs e)
    {
        this.Close();
    }

关闭按钮单击我已完成关闭表单的代码。它也正常工作。enter image description here

答案 3 :(得分:0)

我遇到了同样的问题,即我的文本框在所有其他项目中均不能正常显示时,在其中一个项目中未显示文本。我发现按钮的字体颜色以某种方式变为与按钮的默认颜色相同的颜色,因此文本在那里,只是看不见。