单击按钮文本粗体

时间:2013-03-09 19:05:40

标签: c# winforms

我做了一个测验游戏,我希望点击时按钮上的文字变为粗体。此代码有效:

button7.Font = new Font(button7.Font.Name, button7.Font.Size, FontStyle.Bold);

我遇到的问题是当我点击“下一步”按钮转到下一个问题时,即使没有点击答案,文字仍然是粗体。我该如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

只需在"下一步"按钮单击

button7.Font = new Font(button7.Font.Name, button7.Font.Size, FontStyle.Regular);

答案 1 :(得分:1)

单击“下一步”时,您需要取消加粗所有内容。下面的代码应该有所帮助(它还包括一个可能更清晰的粗体实现)。

// usage
foreach(var button in GetAnswerButtons())
{
    button.Click += OnClickToBold;
    button.Click += OnClickSetPropertyBasedOnCorrectness;
}

nextButton.Click += NextClick;


// implementations    

private void OnClickToBold(object sender, EventArgs e)
{
   var button = sender as Button;

   if (button == null) return;

   button.Font = new Font(button.Font.Name, button.Font.Size, FontStyle.Bold);
}

private void OnClickSetPropertyBasedOnCorrectness(object sender, EventArgs e)
{
   var button = sender as Button;

   if (button == null) return;

   button.WhateverProperty = IsCorrectAnswer(button) 
       ? valueWhenCorrect
       : valueWhenWrong;
}

private void NextClick(object sender, EventArgs e)
{
    foreach(var button in GetAnswerButtons())
    {
        button.Font = new Font(button.Font.Name, button.Font.Size, FontStyle.Regular);
        UnsetPropertyBasedOnCorrectness(button);
    }
}

private IEnumerable<Button> GetAnswerButtons() { ... }
private void UnsetPropertyBasedOnCorrectness(Button b) { ... }