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

时间:2018-10-02 02:12:48

标签: c# user-interface fonts textbox label

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

麻烦的是应该从文本框中使用“巴黎”一词,但我不确定如何使它成为标签中字符串的一部分。在下面的代码中,我的计划是针对每个按钮为每种可能的文本样式组合(粗体/大体,粗体/小体,斜体/大体,斜体/小体)创建一些IF语句。但是我不确定其语法,也不确定如何将文本框中的文本作为字符串的一部分包含在内。对于如何使按钮显示适当的消息有任何帮助或指导,将不胜感激。

我应该注意,以下代码中的IF语句行在Visual Studio中已标记,但未提供任何有用的信息。

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)
        {

        }

    }
}


}

1 个答案:

答案 0 :(得分:1)

正如我在评论中提到的那样,您的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的字体大小,样式和粗细。

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