这是我第一次在这里问。在我的程序中有一些错误,如果我点击rbUnderline,label1将被加下划线,但如果我点击另一个rb让我们说rbBold(obv。该标签将变为粗体),然后单击rbUnderline标签不会被再次加下划线。Windows窗体应用程序单选按钮
这里是我的代码:
public Form1()
{
InitializeComponent();
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
if (rbBold.Checked == true)
{
label1.Font = new Font(label1.Font, FontStyle.Bold);
}
else if (rbItalic.Checked == true)
{
label1.Font = new Font(label1.Font, FontStyle.Italic);
}
else if (rbUnderline.Checked == true)
{
label1.Font = new Font(label1.Font, FontStyle.Underline);
}
}
Designed.cs代码
namespace WindowsFormsApplication1
{
partial class Form1
{
/// <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.label1 = new System.Windows.Forms.Label();
this.rbBold = new System.Windows.Forms.RadioButton();
this.rbItalic = new System.Windows.Forms.RadioButton();
this.rbUnderline = new System.Windows.Forms.RadioButton();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.label1.Location = new System.Drawing.Point(58, 65);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(158, 25);
this.label1.TabIndex = 0;
this.label1.Text = "SAMPLE TEXT";
//
// rbBold
//
this.rbBold.AutoSize = true;
this.rbBold.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.rbBold.Location = new System.Drawing.Point(15, 20);
this.rbBold.Name = "rbBold";
this.rbBold.Size = new System.Drawing.Size(66, 28);
this.rbBold.TabIndex = 1;
this.rbBold.TabStop = true;
this.rbBold.Text = "Bold";
this.rbBold.UseVisualStyleBackColor = true;
this.rbBold.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged);
//
// rbItalic
//
this.rbItalic.AutoSize = true;
this.rbItalic.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.rbItalic.Location = new System.Drawing.Point(87, 20);
this.rbItalic.Name = "rbItalic";
this.rbItalic.Size = new System.Drawing.Size(64, 28);
this.rbItalic.TabIndex = 2;
this.rbItalic.TabStop = true;
this.rbItalic.Text = "Italic";
this.rbItalic.UseVisualStyleBackColor = true;
//
// rbUnderline
//
this.rbUnderline.AutoSize = true;
this.rbUnderline.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.rbUnderline.Location = new System.Drawing.Point(157, 20);
this.rbUnderline.Name = "rbUnderline";
this.rbUnderline.Size = new System.Drawing.Size(110, 28);
this.rbUnderline.TabIndex = 3;
this.rbUnderline.TabStop = true;
this.rbUnderline.Text = "Underline";
this.rbUnderline.UseVisualStyleBackColor = true;
this.rbUnderline.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(270, 104);
this.Controls.Add(this.rbUnderline);
this.Controls.Add(this.rbItalic);
this.Controls.Add(this.rbBold);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.RadioButton rbBold;
private System.Windows.Forms.RadioButton rbItalic;
private System.Windows.Forms.RadioButton rbUnderline;
}
}
很难说明发布的代码。你在哪里订阅这些活动?您是否尝试调试该程序以查看发生了什么? – 2014-08-27 18:45:38
请注意,在__和___关闭时触发'CheckedChanged'事件。另外:你不想让组合?该FontStyle可以通过or'ing他们(|)和CheckBoxes将是选择的控件.. – TaW 2014-08-27 18:47:40
是我试着调试它,但我不知道为什么标签不会再次加下划线(点击两次rbUnderline后)。 @TaW是的,我知道关于|事情。我只需要每个单选按钮一个字体样式;) – xzbxxzxzmn 2014-08-27 18:48:06