我目前正在使用 C# 中的填充。我在多行文本框中显示结果。问题是这条线string result1 = string.Format(format, berries + " ");
给了我错误Index (zero based) must be greater than or equal to zero and less than the size of the argument list
。我不知道如何解决这个问题。如何在中间填充均匀的情况下显示结果?
代码
namespace farm
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public abstract class Plants
{
protected string the_name;
protected double num_stock;
protected double price_peritem;
protected double total_item_value;
public Plants(string new_name, int new_stock, double new_price)
{
the_name = new_name;
num_stock = new_stock;
price_peritem = new_price;
}
public override string ToString()
{
return "";
}
public virtual double Get_Value()
{
double s = 0;
return s;
}
}
public class Berries : Plants
{
string variety;
string months;
public Berries(string new_name, int new_stock, double new_price, string new_variety, string new_months)
: base(new_name, new_stock, new_price)
{
variety = new_variety;
months = new_months;
total_item_value = num_stock * price_peritem;
//total_value += total_item_value;
}
public override string ToString()
{
string s = "Berries" + " " + num_stock + " " + the_name + " " + price_peritem;
return s;
}
public override double Get_Value()
{
total_item_value = num_stock * price_peritem;
return total_item_value;
}
}
public void Report()
{
const string format = "{0,-25} {1,-25} {2,-25} {3,-25} {4,-25}";
Berries berries1 = new Berries("BlueBerries", 12, 5, "AAA Early", "July");
string result1 = string.Format(format, berries1 + " ");
textBox1.AppendText(result1 + Environment.NewLine);
Berries berries2 = new Berries("Strawberry", 12, 5, "FrostStar", "December");
string result = string.Format(format, berries2 + " ");
textBox1.AppendText(result + Environment.NewLine);
}
private void button1_Click(object sender, EventArgs e)
{
Report();
}
}
}