Winforms 单击按钮后调用.cs文件/程序

Winforms 单击按钮后调用.cs文件/程序,winforms,c#-2.0,Winforms,C# 2.0,被调用的类是一个单独的.CS文件,都在同一个项目中,如下所述 //compares text to the id number below } 这应该做到: using System; using System.IO; using System.Data; using System.Text; using System.Drawing; using System.Data.OleDb; using System.Collections; using

被调用的类是一个单独的.CS文件,都在同一个项目中,如下所述

        //compares text to the id number below



   }
这应该做到:

 using System;
 using System.IO;
 using System.Data;
 using System.Text;
 using System.Drawing;
 using System.Data.OleDb;
 using System.Collections;
 using System.ComponentModel;
 using System.Windows.Forms;
 using System.Drawing.Printing;
 using System.Collections.Generic;

 namespace Eagle_Eye_Class_Finder
 {
     class GetSchedule
     {
          class IDnumber
         {

             public string Name { get; set; }
            public string ID { get; set; }
            public string year { get; set; }
            public string class1 { get; set; }
            public string class2 { get; set; }
            public string class3 { get; set; }
            public string class4 { get; set; }

        }


            //
            // Displays the Students Class Schedule.
            //
            Console.WriteLine("--- Students Class Schedule ---");
            foreach (IDnumber IDnumber in IDnumbers)
            {                          
                Console.Write(IDnumber.Name);
                Console.Write(": ");
                Console.WriteLine(IDnumber.ID);
                Console.WriteLine(IDnumber.year);
                Console.WriteLine(IDnumber.class1);
                Console.WriteLine(IDnumber.class2);
                Console.WriteLine(IDnumber.class3);
                Console.WriteLine(IDnumber.class4);


                //get { return this.label1.Text; }
                //set { this.label1.Text = class1; }

                //get { return this.label2.Text; }
                //set { this.label2.Text = class2; }


                //get { return this.label3.Text; }
                //set { this.label3.Text = class3; }

                //get { return this.label4.Text; }
                //set { this.label1.Text = class4; }
            }

            // Clear first two elements in IDnumbers array.

            Array.Clear(IDnumbers, 0, Math.Min(2, IDnumbers.Length));
        }

    }
}

GetSchedule
的定义修改为
Public Class
Public Static Main
,然后David的代码就可以工作了

类定义的类的默认访问级别是
内部
(C#)/
朋友
(VB)。

显而易见的答案是

Eagle_Eye_Class_Finder.GetSchedule.Main()
但是,在我看来,你们希望做一些有价值的事情 900456317

为了实现这一点,您必须声明一个额外的函数,该函数将该值作为参数接收,然后对其执行一些有用的操作。可能是这样的:

Eagle_Eye_Class_Finder.GetSchedule.Main()
然后你可以这样称呼它:

static void ProcessNumber(IDNumber myNum)
{
    StringBuilder myData = new StringBuilder();
    myData.AppendLine(IDnumber.Name);   
    myData.AppendLine(": ");   
    myData.AppendLine(IDnumber.ID);   
    myData.AppendLine(IDnumber.year);   
    myData.AppendLine(IDnumber.class1);   
    myData.AppendLine(IDnumber.class2);   
    myData.AppendLine(IDnumber.class3);   
    myData.AppendLine(IDnumber.class4);  
    MessageBox.Show(myData);
}
如果你能详细说明你希望实现的目标,可能会更容易

编辑

您需要做的是将IDnumbers数组的概念从函数调用本身移到类成员中。例如,考虑添加以下代码:

   if (text == "900456317")    
   {    
        Eagle_Eye_Class_Finder.GetSchedule.ProcessNumber(new IDnumber() { Name = "Joshua Banks",ID = "900456317", year = "Senior", class1 = "TEET 4090", class2 = "TEET 3020", class3 = "TEET 3090", class4 = "TEET 4290" });     

   } 
这将导致每次创建类的新实例时都运行该代码。然后,你可以有一个函数

IDnumber[] IDnumbers = new IDnumber[3];   

public GetSchedule()
{
        IDnumbers[0] = new IDnumber() { Name = "Joshua Banks",ID = "900456317", year = "Senior", class1 = "TEET 4090", class2 = "TEET 3020", class3 = "TEET 3090", class4 = "TEET 4290" };    
        IDnumbers[1] = new IDnumber() { Name = "Sean Ward", ID = "900456318", year = "Junior", class1 = "ENGNR 4090", class2 = "ENGNR 3020", class3 = "ENGNR 3090", class4 = "ENGNR 4290" };    
        IDnumbers[2] = new IDnumber() { Name = "Terrell Johnson",ID = "900456319",year = "Sophomore", class1 = "BUS 4090", class2 = "BUS 3020", class3 = "BUS 3090", class4 = "BUS 4290" };    

}
然后您的方法调用从Form1更改为:

public string GetDataFromNumber(string ID)
{
    foreach (IDnumber idCandidateMatch in IDnumbers)         
    { 
        if (IDCandidateMatch.ID == ID)
        {
            StringBuilder myData = new StringBuilder();
            myData.AppendLine(IDnumber.Name);   
            myData.AppendLine(": ");   
            myData.AppendLine(IDnumber.ID);   
            myData.AppendLine(IDnumber.year);   
            myData.AppendLine(IDnumber.class1);   
            myData.AppendLine(IDnumber.class2);   
            myData.AppendLine(IDnumber.class3);   
            myData.AppendLine(IDnumber.class4);  
            return myData;
        }
    }
    return "";
}
}

这里的想法是将您的程序分解为一系列较小的“部分”,每个“部分”负责完成一件事,并将其做好。在本例中,类“GetSchedule”表示程序的一部分,给定ID号,该部分可以检索该用户的描述。上面读取的代码行

public void button2_Click(object sender, System.EventArgs e)             
{             


   string text = textBox1.Text;             
   Mainform = this;             

   this.Hide();             

   GetSchedule myScheduleFinder = new GetSchedule();
   string result = myScheduleFinder.GetDataFromNumber(text); 
   if (!string.IsNullOrEmpty(result))             
    {             
        MessageBox.Show(result);
   }             
   else             
   {             
       MessageBox.Show("Enter A Valid ID Number!");             
   }             
基本上说,“我想要一个GetSchedule类的新副本,我想用‘myScheduleFinder’这个名字来跟踪它。”。每当你在C#中看到“new”这个词时,就会发生这种情况。当单词new后跟类名和括号时,它调用了所谓的“构造函数”。构造函数基本上是每次创建类时调用的特殊函数;在您的情况下,它是我们在方法中放置的代码

   GetSchedule myScheduleFinder = new GetSchedule();

现在,假设我们有一个GetSchedule类的副本,由构造函数正确初始化,我们可以调用该类上的ProcessNumber函数,传递我们正在搜索的数字。“if”语句基本上确保在所有可能的记录中,我们只使用具有相同ID的记录。然后我们获取该记录,将其转换为字符串,并返回它。然后我们将它显示在一个漂亮的小消息框中,尽管此时您显然可以使用它做任何您想做的事情

我有一个windows窗体应用程序,它在窗体1上接收该id号作为输入,然后根据该id号输出用户信息和类计划…最终我想将类写入窗体2!上面提到的方法不起作用,我假设getschedule出了问题,所以我尝试了你的方法,但出现了错误,说非静态字段方法或属性需要对象引用。有意义的是,public getschedule()不需要返回类型吗?public getschedule()不需要返回类型,因为它是一个构造函数。本质上,由于函数的名称与类的名称相同,编译器假定它的返回值将是类的类型,但是我现在得到一个错误,说方法必须有一个返回类型将它们更改为…..公共类GetSchedule和公共类IDnumber…..仍然说它不可访问很糟糕-也将主要更改为publicYou's welcome!对回答者(如果他们的答案有帮助的话)进行一些加分/正确答案标记会很好地表示感谢。阅读页面顶部的常见问题:)-每个答案上都有一些向上/向下箭头-这就是你如何向上/向下投票的方式-有一个“勾选”标志将该答案标记为正确答案
public GetSchedule()