C# 在Windows窗体中单击按钮时如何显示对象的属性?
我想在Windows窗体中使用c单击按钮时显示对象的属性,但我不知道如何执行此操作。到目前为止,我的部分代码看起来像这样。我是c语言的初学者C# 在Windows窗体中单击按钮时如何显示对象的属性?,c#,class,windows-forms-designer,C#,Class,Windows Forms Designer,我想在Windows窗体中使用c单击按钮时显示对象的属性,但我不知道如何执行此操作。到目前为止,我的部分代码看起来像这样。我是c语言的初学者 class Pizza: ICloneable { private string nume; private int nrIngrediente; private string[] ingrediente; public Pizza() { n
class Pizza: ICloneable
{
private string nume;
private int nrIngrediente;
private string[] ingrediente;
public Pizza()
{
nume = "Margherita";
nrIngrediente = 2;
ingrediente = new string[nrIngrediente];
for (int i = 0; i < nrIngrediente; i++)
{
ingrediente[0] = "Sos rosii";
ingrediente[1] = "Mozzarella";
}
}
public Pizza(string den, int nri, string[] ing)
{
nume = den;
nrIngrediente = nri;
ingrediente = new string[nrIngrediente];
for (int i = 0; i < nrIngrediente; i++)
ingrediente[i] = ing[i];
}
public Pizza(Pizza p)
{
nume = p.nume;
nrIngrediente = p.nrIngrediente;
ingrediente = new string[nrIngrediente];
for (int i = 0; i < nrIngrediente; i++)
ingrediente[i] = p.ingrediente[i];
}
public string PizzaName
{
get { return nume; }
set { nume = value; }
}
public int PizzaNrIng
{
get { return nrIngrediente; }
set { nrIngrediente = value; }
//also, i don't know how to write the getter and setter for this one
public string PizzaIngredients
//{
// get
// {
// for(int i=0;i<nrIngrediente;i++)
// return ingrediente[i];
// }
// set { ingrediente = value; }
//}
谢谢大家! MessageBox.Show
必须是字符串值,因此对象的任何非字符串部分都需要.ToString方法
或者,如果您想自定义视图,您可以将对象传递到一个新窗体,并使用设计器为其创建一个完整的漂亮布局 至于getter,您应该返回字符串数组,而不仅仅是字符串:
public string[] PizzaIngredients
{
get
{
ingrediente;
}
set
{
ingrediente = value;
}
}
至于显示,只需使用MessageBox。显示:
MessageBox.Show(string.Format("{0} {1}", Margherita.Name, Margherita.nrIngrediente));
您可以用下面的方式设置getter和setter
public string PizzaIngredients
{
get
{
return String.Join(",",nringrediente);
}
set
{
nringrediente = value.Split(',');
}
}
和玛格丽塔;可以像在Pizza类中一样实现:
public string GetDisplayMessageForPizza()
{
return "My Pizza is " + nume + ". It contains " + nringrediente + " ingredients : " + PizzaIngredients;
}
以ListaPizza的形式:
private void Margherita_Click(object sender, EventArgs e)
{
string[] ingrMargh = new string[2] { "Sos rosii", "Mozzarella" };
Pizza Margherita = new Pizza("Margherita", 2, ingrMargh);
MessageBox.Show(Margherita.GetDisplayMessageForPizza());
}
我不确定我是否完全理解您想要什么,但是您可以重写ToString方法以准确显示您想要什么,然后您只需要执行MessageBox.ShowMarguerita.ToString;顺便说一句,将字符串与+连接是一种不好的做法,因为字符串是不可变的-每次添加都会创建一个新的字符串实例。最好使用string.Format或stringbuilder我知道这是一种不好的做法。我刚刚发布了提问者期望的答案。下次我会小心的。在GetDisplayMessageForPizza中,编码器可以以所需格式返回任何要显示的字符串。它工作得很好,但在向我显示配料时,是这样说的:它包含两种配料:System.string[]。用C++来阅读它们是比较容易的,哈哈哈,Vanditaoh,我的坏消息,我写了一些坏的东西。很抱歉给您带来不便,非常感谢您的帮助。祝你今天愉快@祝你好运@阿纳斯。如果您使用我的代码解决了问题,请将此标记为答案。
private void Margherita_Click(object sender, EventArgs e)
{
string[] ingrMargh = new string[2] { "Sos rosii", "Mozzarella" };
Pizza Margherita = new Pizza("Margherita", 2, ingrMargh);
MessageBox.Show(Margherita.GetDisplayMessageForPizza());
}