将一个对象从一个表单传递给另一个.Winforms

时间:2014-04-16 11:17:50

标签: c# winforms object parameter-passing

我有form1,form1中有一个类。

  public partial class Form1 : Form
  { 
  public class Events
    {
        public string Time { get; set; }
        public int PlayerId { get; set; }
        public int TeamId { get; set; }
        public int Event { get; set; }
        public string DisplayPbp { get; set; }
        public int X { get; set; }
        public int Y { get; set; }
        public List<int> Change { get; set; }
    }
    public List<Events> events = new List<Events>();   

我还有第二个form2,我希望能够将第二个表单中的对象Event添加到第一个form1。这是第二个form2代码

      public partial class Form2 : Form
      {
      Form1 form1 = new Form1();
        List<int> Change = new List<int>();                
            for (int i = 0; i < Playinglist.Count(); i++)
            {
                Change.Add(Playinglist[i].Key);
            }     
      form1.events.Add(new BasketballApp.Form1.Events()
            {
                Time = string.Format("{0}:{1}", 10, 00),
                PlayerId = 0,
                TeamId = game.HomeTeamId,
                Event = 22,
                DisplayPbp = "Change",
                X = 0,
                Y = 0,
                Change = Change                    
            });

这不是将新对象传递给form1.Any建议吗?

1 个答案:

答案 0 :(得分:0)

根据错误,它使用错误的Events类:System.ComponentModel.Component.Events

您应该在其前面加上您的命名空间(或使用Events的其他名称)

在Form1中:

public List<YourNamespace.Events> events = new List<YourNamespace.Events>();

在Form2中:

form1.events.Add(new YourNamespace.Events()
{
    Time = string.Format("{0}:{1}", 10, 00),
    PlayerId = 0,
    TeamId = game.HomeTeamId,
    Event = 22,
    DisplayPbp = "Change",
    X = 0,
    Y = 0,
    Change = Change                    
});