按下按钮后,文本变为粗体

时间:2010-09-09 18:42:01

标签: c#-4.0

我在C#中有一个带有一些按钮的表单。我希望在按下按钮时,将文本设为粗体。


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Net;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // string str = @"C:\windows\system32\notepad.exe";
            // string str = @"C:\windows\system32\winamp.exe";
            string str = @"C:\priority\bin.95\WINMENU.exe";
            Process process = new Process();
            process.StartInfo.FileName = str;
            process.Start(); 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            // string str = @"C:\windows\system32\notepad.exe";
            // string str = @"C:\windows\system32\winamp.exe";
            string str = @"C:\Program Files\UltraVNC\vncviewer.exe";
            Process process = new Process();
            process.StartInfo.FileName = str;
            process.Start(); 
        }

        private void button3_Click(object sender, EventArgs e)
        {
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            // string str = @"C:\windows\system32\notepad.exe";
            // string str = @"C:\windows\system32\winamp.exe";
            string str = @"C:\Program Files\UltraVNC\vncviewer.exe";
            Process process = new Process();
            process.StartInfo.FileName = str;
            process.Start(); 
        }

        private void button4_Click(object sender, EventArgs e)
        {
            // string str = @"C:\windows\system32\notepad.exe";
            // string str = @"C:\windows\system32\winamp.exe";
            string str = @"C:\Windows\system32\mstsc.exe";
            Process process = new Process();
            process.StartInfo.FileName = str;
            process.Start();
        }

        private void button5_Click(object sender, EventArgs e)
        {
           // string str = @"C:\windows\system32\notepad.exe";
            // string str = @"C:\windows\system32\winamp.exe";
            string str = @"C:\Program Files\Microsoft Office\Office14\winword.exe";
            Process process = new Process();
            process.StartInfo.FileName = str;
            process.Start();

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {


        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }
    }
}

我现在该怎么办?

3 个答案:

答案 0 :(得分:3)

使用按钮的Click事件,如下所示:

private void button_Click(object sender, EventArgs e)
    {
        Button b = ((Button)sender);
        b.Font = new Font(b.Font, FontStyle.Bold);
    }

答案 1 :(得分:1)

@hyprsleepy - 我认为您的答案无效,因为Button.Font.Bold属性是只读的,因此您只能读取该值以查看当前设置的内容。

@asher - 你有这些活动的地方:

private void button5_Click(object sender, EventArgs e)
{
   // string str = @"C:\windows\system32\notepad.exe";
    // string str = @"C:\windows\system32\winamp.exe";
    string str = @"C:\Program Files\Microsoft Office\Office14\winword.exe";
    Process process = new Process();
    process.StartInfo.FileName = str;
    process.Start();

}

使用类似的语法:

private void button5_Click(object sender, EventArgs e)
{
   // string str = @"C:\windows\system32\notepad.exe";
    // string str = @"C:\windows\system32\winamp.exe";
    string str = @"C:\Program Files\Microsoft Office\Office14\winword.exe";
    Process process = new Process();
    process.StartInfo.FileName = str;
    process.Start();

    Button b = ((Button)sender);
    b.Font = new Font(b.Font, FontStyle.Bold);

}

答案 2 :(得分:0)

以下是代码:

private void button1_Click(object sender, EventArgs e)
    {
        button1.Font = new Font(this.Font, FontStyle.Bold);
    }