【发布时间】: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