Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将对象从一个窗体传递到另一个窗体。Winforms_C#_Winforms_Object_Parameter Passing - Fatal编程技术网

C# 将对象从一个窗体传递到另一个窗体。Winforms

C# 将对象从一个窗体传递到另一个窗体。Winforms,c#,winforms,object,parameter-passing,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;

我有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>();   
公共部分类表单1:表单
{ 
公开课活动
{
公共字符串时间{get;set;}
public int PlayerId{get;set;}
public int TeamId{get;set;}
公共int事件{get;set;}
公共字符串DisplayPbp{get;set;}
公共整数X{get;set;}
公共整数Y{get;set;}
公共列表更改{get;set;}
}
公共列表事件=新列表();
我还有第二个form2,我希望能够将对象事件从第二个form添加到第一个form1

      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                    
            });
公共部分类表单2:表单
{
Form1 Form1=新Form1();
列表更改=新列表();
对于(int i=0;i

这并没有将新对象传递给form1。有什么建议吗?

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

您应该在它前面加上名称空间(或者为
事件使用不同的名称)

在表格1中:

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

您会得到什么错误?类事件是内部类(在表单1中定义)还是外部类?您已将public List Events=new List();放置在事件类之外,但您在表单2中将其用作games.Events.add(blah blah blah);移动到类内的行上方,然后重试类事件是(在表单1中定义).在Form1.events.Add(新事件()的行中出现错误:System.ComponentModel.Component.Events是一个“属性”,但像“类型”一样使用。我也会在类内的行上方移动,然后重试,但不会将对象传递给同一命名空间中的Events类?如果在Form1中声明,则不会。然后应使用YourNamespace.Form1.Events。
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                    
});