Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 窗体上的按钮不工作或显示任何文本_C#_Winforms_Button - Fatal编程技术网

C# 窗体上的按钮不工作或显示任何文本

C# 窗体上的按钮不工作或显示任何文本,c#,winforms,button,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; thi

我正在尝试为我的表单创建自定义标题栏。因此,我有:

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;
意思是这样的:

但是,由于某些原因,当我运行应用程序时,它们显示如下(没有文本):

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

private void Close_Click(object sender, EventArgs e)
{
    this.Close();
}
但是,当我单击“关闭”时,什么也没有发生

有人能指出我的代码有什么问题吗。或者我必须添加什么来修复它

编辑:
原来,当我把按钮颜色改成红色时,十字会显示出来,但它是黑色的,不是白色的

你可以试着替换你喜欢的

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


首先,您需要更改Close和Hide的设计名称

关闭
隐藏
更改为其他名称。。否则,命名空间将与窗体的隐藏和窗体的关闭冲突。还要记住,如果没有
FormBorderStyle
,您将无法像往常一样移动表单

试试下面,这至少对我有用

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

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();
}

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

Designer.cs代码

namespace WindowsFormsApplication1
{ 部分类别表格3 { /// ///必需的设计器变量。 /// 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;
}

单击“关闭按钮”时,我已经完成了关闭表单的代码。它也起作用。

我遇到了同样的问题,我的文本框在我的一个项目中没有显示文本,而在所有其他项目中都起作用。我发现,不知怎的,我的按钮字体颜色更改为与按钮默认颜色相同的颜色,因此文本就在那里,我说不只是不可见。

为什么不使用图像按钮而不是按钮。不需要文本。在该按钮上使用背景图像,您的问题就解决了。我知道,我可以使用图像。我只是好奇为什么该代码不起作用。@patel,这并不能解释为什么我单击“关闭”时什么也没有发生。您是否设置了中断点int并测试当你点击按钮时是否点击。此外,KcDOD关于名称的评论是正确的,更改名称并清理项目,然后重新构建。很难猜测,听起来像是你一直在破解系统颜色和系统颜色。ButtonFace不再是默认的战舰灰色,而是现在为黑色。只需关闭亮度设置就更容易了您的单击事件处理程序无法工作,因为您用“关闭”按钮隐藏了Form.Close()方法。请注意错误列表窗口中出现的警告。只需使用示例表单尝试提供的代码。然后将其应用于解决方案。请尝试
    /// <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();
    }