[英]How to show the attributes of an object when clicking a button in Windows forms?
I want to show the attributes of an object when I click on a button in Windows Forms using c# and I don't know how to do it. 我想在使用c#单击Windows窗体中的按钮时显示对象的属性,但我不知道该怎么做。 Part of my code looks like this until now.
到目前为止,我的部分代码看起来像这样。 I'm a beginner in c#.
我是C#的初学者。
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; }
//}
And now, the form code is the following(note that i designed it already): 现在,表单代码如下(请注意,我已经设计了它):
public partial class ListaPizza : Form
{
public ListaPizza()
{
InitializeComponent();
}
private void Margherita_Click(object sender, EventArgs e)
{
string[] ingrMargh = new string[2] { "Sos rosii", "Mozzarella" };
Pizza Margherita = new Pizza("Margherita", 2, ingrMargh);
//Show(Margherita);
//here i want the object created above to be shown in a messagebox when i click the button in the form but i don't know how
}
}
Thank you! 谢谢!
MessageBox.Show(); MessageBox.Show();
has to be a string value so any parts of your object that is not a string will need the .ToString() method 必须为字符串值,因此对象的任何非字符串部分都需要.ToString()方法
Or if you want to customize the view you could pass the object to a new form and create a whole nice layout for it with the designer. 或者,如果您想自定义视图,则可以将对象传递给新表单,并与设计者一起为其创建一个完整的布局。
As for the getter - you should return an array of strings, not just a string: 至于getter-您应该返回一个字符串数组,而不仅仅是一个字符串:
public string[] PizzaIngredients
{
get
{
ingrediente;
}
set
{
ingrediente = value;
}
}
As for the showing - just use MessageBox.Show(): 至于显示-只需使用MessageBox.Show():
MessageBox.Show(string.Format("{0} {1}", Margherita.Name, Margherita.nrIngrediente));
You can set the getter and setter in below way. 您可以通过以下方式设置getter和setter。
public string PizzaIngredients
{
get
{
return String.Join(",",nringrediente);
}
set
{
nringrediente = value.Split(',');
}
}
And Show(Margherita); 和秀(玛格丽特); can be implemented like in Pizza Class :
可以像比萨饼类中那样实现:
public string GetDisplayMessageForPizza()
{
return "My Pizza is " + nume + ". It contains " + nringrediente + " ingredients : " + PizzaIngredients;
}
And in form ListaPizza : 并以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());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.