2014-08-27 58 views
0

这是我第一次在这里问。在我的程序中有一些错误,如果我点击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; 
    } 
} 
+0

很难说明发布的代码。你在哪里订阅这些活动?您是否尝试调试该程序以查看发生了什么? – 2014-08-27 18:45:38

+0

请注意,在__和___关闭时触发'CheckedChanged'事件。另外:你不想让组合?该FontStyle可以通过or'ing他们(|)和CheckBoxes将是选择的控件.. – TaW 2014-08-27 18:47:40

+0

是我试着调试它,但我不知道为什么标签不会再次加下划线(点击两次rbUnderline后)。 @TaW是的,我知道关于|事情。我只需要每个单选按钮一个字体样式;) – xzbxxzxzmn 2014-08-27 18:48:06

回答

0

它可能是一个竞争条件,因为radioButton1_CheckedChanged将被调用两次。这就是如果你的三个单选按钮都使用CheckChanged事件的相同事件处理程序。事件处理程序将针对点击按钮调用一次,针对未选中按钮的按钮则调用一次。

尝试使用3独立的if语句来代替:

if (rbBold.Checked == true) 
{ 
    label1.Font = new Font(label1.Font, FontStyle.Bold); 
} 
if (rbItalic.Checked == true) 
{ 
    label1.Font = new Font(label1.Font, FontStyle.Italic); 
} 
if (rbUnderline.Checked == true) 
{ 
    label1.Font = new Font(label1.Font, FontStyle.Underline); 
} 

编辑:在回答你的更新,你还需要进行以下更改:

this.rbUnderline.CheckedChanged += new System.EventHandler(this.radioButton3_CheckedChanged); 

到:

this.rbUnderline.CheckedChanged += new System.EventHandler(this.radioButton1_CheckedChanged); 
+0

man that wor糟透了!非常感谢。我是新来的Windows窗体应用程序和C#。我只在互联网上学习。感谢所有帮助过我的人! – xzbxxzxzmn 2014-08-27 19:19:00

+0

你也应该添加相同的东西rbItalic – jbriggs 2014-08-27 19:21:04

+0

如果这不起作用,然后添加一个检查,以确保选中的选项也是发件人。如果(rbBold.Checked == true && sender == rbBold) – jbriggs 2014-08-27 19:23:20

0

你应该单选按钮组一起,把他们像面板或组框的容器,和你的问题应该是不见了。