【问题标题】:C# Displaying text (including text from a textbox) when a button is pressedC# 按下按钮时显示文本(包括文本框中的文本)
【发布时间】:2018-10-02 02:12:47
【问题描述】:

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

问题是 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)
        {

        }

    }
}


}

【问题讨论】:

  • 首先,您的 if 语句没有编译,因为您将 largeBold 声明为 private 并且方法内的变量不允许使用访问修饰符。
  • 谢谢。 if 语句仍然有错误,因为它仍然没有编译
  • 因为你在("The capital of France is {0}", capitalText)之前忘记了String.Format
  • 啊,是的,再次感谢您。虽然它说变量 capitalText 在当前上下文中不存在。我应该在其他地方声明吗?

标签: c# user-interface fonts textbox label


【解决方案1】:

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

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

【讨论】:

  • 我会试试这个
  • 这不允许更改字体样式。无论在组合框中选择什么,它都将是常规和粗体。
  • 这可能是因为styleComboBox.SelectedText 实际上不是您在组合框中选择的内容,并且没有执行任何 if 语句。也许你正在寻找styleComboBox.Text
  • 我发现了导致问题的错误。非常感谢您的帮助,并感谢您提供有关命名约定的提示。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-20
  • 2023-03-17
  • 2016-11-26
  • 1970-01-01
  • 2016-03-06
  • 2016-03-24
相关资源
最近更新 更多