我正在尝试在从另一个类调用的列表视图控件中添加项目。 我有一个带有列表视图控件和一些按钮的表单。当我单击其中一个按钮时,会出现一个带有按钮的用户控件 (addButtons.cs)。
///making the buttons on the usercontrol
internal class AddButton
{
ublic void New(UserControl user, String text, int Location_X, int Location_Y, String stringPath, string price)
{
System.Windows.Forms.Button button = new System.Windows.Forms.Button
{
Text = text,
............
Image = System.Drawing.Image.FromFile(stringPath)
};
//when the button is clicked
button.Click += BTNclick;
user.Controls.Add(button);
user.CreateControl();
// button.Click += new System.EventHandler(OnClick);
}
public void BTNclick(object sender, EventArgs e)
{
System.Windows.Forms.Button clickedbtn = sender as System.Windows.Forms.Button;
//frmHoofd is my Form
frmHoofd frmHoofd = new frmHoofd();
string mail= clickedbtn.Text;
string price= clickedbtn.Name;
string count= "1";
string[] array = { mail, count, price};
frmHoofd.UpdatingListView(array);
}
列表视图控件所在的主窗体。
namespace KassaSystem1._1
{
// public delegate void EventHandler (string gerecht, string prijs);
public partial class frmHoofd : Form
{
private ContrlVoorgerechten u1;
delegate void MyDelegate(string[] array);
public frmHoofd()
{
InitializeComponent();
}
//updating the listview control calling from the class addButton.cs
public void UpdatingListView(string[] array)
{
if (this.lstBesteld.InvokeRequired)
{
this.lstBesteld.Invoke(new MyDelegate(UpdatingListView), new object[] { array });
}
else
{
ListViewItem lvi = new ListViewItem(array[0]);
lvi.SubItems.Add(array[0]);
lvi.SubItems.Add(array[1]);
lvi.SubItems.Add(array[2]);
//i see the string text appears correctly in the messagebox
MessageBox.Show("mail: " +array[0] + " count: " +array[1]+ " price: " + array[2]);
// lstBesteld.BeginUpdate();
lstBesteld.Items.Add(lvi);
// lstBesteld.EndUpdate();
}
}
//calling the usercontrol
private void btVoorgerechten_Click(object sender, EventArgs e)
{
u1 = new ContrlVoorgerechten() ;
//u1.Dock = DockStyle.Fill;
u1.Width = 750;
u1.Height = 477;
u1.Location = new Point(430, 165);
this.Controls.Add(u1);
我没有任何错误,但我看到列表视图控件中没有出现任何内容。 我做错了什么?
如果您使用其他表单或用户控件在主表单(frmHoofd)的列表视图中添加项目,则可以创建引用。在您的用户控件中添加一个构造函数:
internal class AddButtons
{
frmHoofd h;
public AddButtons(frmHoofd h)
{
InitializeComponent();
this.h = h;
}
public void BTNclick(object sender, EventArgs e)
{
string mail= clickedbtn.Text;
string price= clickedbtn.Name;
string count= "1";
h.AddItem(mail, count, price);
}
}
然后添加你的frmHoofd:
public void AddItem(string mail, int count, decimal price)
{
ListViewItem lvi = new ListViewItem(array[0]);
lvi.SubItems.Add(mail);
lvi.SubItems.Add(count);
lvi.SubItems.Add(price);
lstBesteld.Items.Add(lvi);
}
顺便说一句,最好为控件使用更简单的名称。如Buttons,使用btnAdd。这样您就可以更轻松地进行控制。同样在您的用户控件中,BTNclick ..只需使用 btnAdd_Clicked(object sender, EventArgs e)...
最后一件事..使用正确的变量。对于计数,使用整数,对于价格,使用小数。您可以使用命名空间
System.Globalization.CultureInfo.
设置货币格式
CultureInfo nfi = new CultureInfo("nl-NL", false);
nfi.NumberFormat.CurrencyDecimalDigits = 2;
nfi.NumberFormat.CurrencyDecimalSeparator = ".";
nfi.NumberFormat.CurrencyGroupSeparator = "";
nfi.NumberFormat.NumberDecimalDigits = 2;
nfi.NumberFormat.NumberDecimalSeparator = ".";
nfi.NumberFormat.NumberGroupSeparator = "";
decimal price = Convert.ToDecimal(price, nfi);
//And show the price as string
string priceDisplay = price.ToString("N", nfi); //Use N for number or C for currency. C will add the currency sign.