C# 按下按钮时显示文本(包括文本框中的文本)

C# Displaying text (including text from a textbox) when a button is pressed

下面的程序是一个作业的不完整解决方案,该作业创建一个显示,为用户提供一些文本显示方式的多个选项。有一个可以设置为 "Bold" 或 "Italic" 的组合框、用于小号或大号字体的单选按钮,以及一个用户可以在其中输入首都城市名称的文本字段。共有三个按钮,分别标记为法国、英国和墨西哥。按下按钮时,将显示根据用户选择的选项格式化的文本。例如,假设用户在文本框中输入 Paris,从组合框中选择粗体,然后选择大号单选按钮。当按下法国按钮时,文本应该显示在一个标签中,上面写着“法国的首都是巴黎”。

问题是 Paris 这个词应该从文本框中取出,我不确定如何让它成为标签中字符串的一部分。在下面的代码中,我的计划是,对于每个按钮,为每种可能的文本样式组合创建一些 IF 语句(Bold/Large、Bold/Small、Italic/Large、Italic/Small) .但我不确定它的语法,也不确定如何将文本框中的文本作为字符串的一部分。非常感谢任何有关如何让按钮显示适当消息的帮助或指导。

我应该注意到,Visual studio 中标记了下面代码中的 IF 语句行,但未提供有用的信息。

namespace HW_Ch9_20
{
public partial class Form1 : Form
{
    private Button france = new Button();
    private Button england = new Button();
    private Button mexico = new Button();

    private RadioButton large = new RadioButton();
    private RadioButton small = new RadioButton();

    private ComboBox style = new ComboBox();

    private TextBox capital = new TextBox();

    private Label styleLable = new Label();
    private Label sizeLable = new Label();
    private Label enterCapital = new Label();
    private Label display = new Label();



    public Form1()
    {
        france.Text = "France";
        england.Text = "England";
        mexico.Text = "Mexico";
        large.Text = "Large";
        small.Text = "Small";
        //style.Text = "Select a style";
        styleLable.Text = "Style";
        sizeLable.Text = "Size";
        enterCapital.Text = "Enter capital";
        capital.Text = "";
        display.Text = "";


        Size = new Size(800, 400);
        display.Size = new Size(250, 200);

        france.Location = new Point(250, 30);
        england.Location = new Point(330, 30);
        mexico.Location = new Point(410, 30);
        large.Location = new Point(350, 250);
        small.Location = new Point(350, 275);
        style.Location = new Point(80, 68);
        styleLable.Location = new Point(40, 70);
        capital.Location = new Point(560, 150);
        sizeLable.Location = new Point(310, 265);
        enterCapital.Location = new Point(580, 130);
        display.Location = new Point(240, 80);

        style.Items.Add("Bold");
        style.Items.Add("Italic");

        Controls.Add(france);
        Controls.Add(england);
        Controls.Add(mexico);
        Controls.Add(large);
        Controls.Add(small);
        Controls.Add(style);
        Controls.Add(capital);
        Controls.Add(styleLable);
        Controls.Add(sizeLable);
        Controls.Add(enterCapital);
        Controls.Add(display);


        france.Click += new EventHandler(france_Click);
        england.Click += new EventHandler(england_Click);
        mexico.Click += new EventHandler(mexico_Click);

        string capitalText = capital.Text;

        void france_Click(Object sender, EventArgs e)
        {
            if(large.Checked && style.SelectedText == "Bold")
                private Font largeBold = new Font(("The capital of France is {0}", capitalText), 24, FontStyle.Bold);

        }

        void england_Click(Object sender, EventArgs e)
        {

        }

        void mexico_Click(Object sender, EventArgs e)
        {

        }

    }
}


}

正如我在评论中提到的,您的 if 语句未编译,因为您将 largeBold 声明为 private 并且方法内部的变量不允许使用访问修饰符。

我会这样做:

void france_Click(Object sender, EventArgs e)
{
    int fontSize = 24;
    FontStyle fontStyle = FontStyle.Regular | FontStyle.Bold;

    // Set the font size
    if (largeRadioButton.Checked) // Large font size
    {
        fontSize = 24;
    }
    else if (smallRadioButton.Checked) // Small font size
    {
        fontSize = 16;
    }

    // Set the font style and font weight
    if (styleComboBox.SelectedText == "Bold") // Bold font
    {
        fontStyle = FontStyle.Bold;
    }
    else if (styleComboBox.SelectedText == "Italic") // Italic font
    {
        fontStyle = FontStyle.Italic;
    }

    // Apply the font style.
    displayLabel.Font = new Font("Arial", fontSize, fontStyle);
    // Set the text.
    displayLabel.Text = String.Format("The capital of France is {0}", capitalTextBox.Text);

}

在标签上显示文本之前,您只需通过控件设置并设置 displayLabel 的字体大小、样式和粗细。

请注意,我在变量上添加了一个后缀,以指示它们是哪种控件以提高可读性。然而,这只是我个人的喜好。