Winform/Program以及如何将数组中的类1、类2、类3、类4写入链接标签?

发布于 2024-08-30 14:18:22 字数 10010 浏览 4 评论 0原文

所以我的程序是这样工作的:使用winforms,用户输入ID号,使用一个数组,根据正确的ID号,将学生信息和课程表输出在消息框中!

我的问题是如何获取消息框/数组中的 4 个类并将它们写入表单 2 中的链接标签文本???

我的 Getschedule 类包含该数组,如下所示

: {

public class GetSchedule
{
    IDnumber[] IDnumbers = new IDnumber[3];

    public string GetDataFromNumber(string ID)
    {
        foreach (IDnumber IDCandidateMatch in IDnumbers)
        {

            if (IDCandidateMatch.ID == ID)
            {
                StringBuilder myData = new StringBuilder();
                myData.AppendLine(IDCandidateMatch.Name);
                myData.AppendLine(": ");
                myData.AppendLine(IDCandidateMatch.ID);
                myData.AppendLine(IDCandidateMatch.year);
                myData.AppendLine(IDCandidateMatch.class1);
                myData.AppendLine(IDCandidateMatch.class2);
                myData.AppendLine(IDCandidateMatch.class3);
                myData.AppendLine(IDCandidateMatch.class4);
                //return myData;
                return myData.ToString();
            }
        }
        return "";
    }

    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" };

    }
    public 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; }


       public static void ProcessNumber(IDnumber myNum)
            {
                StringBuilder myData = new StringBuilder();
                myData.AppendLine(myNum.Name);   
                myData.AppendLine(": ");
                myData.AppendLine(myNum.ID);
                myData.AppendLine(myNum.year);
                myData.AppendLine(myNum.class1);
                myData.AppendLine(myNum.class2);
                myData.AppendLine(myNum.class3);
                myData.AppendLine(myNum.class4);  
                MessageBox.Show(myData.ToString());
            }




    }

}

}

我的表单 2 将包含链接标签,如下所示:

public class YOURCLASSSCHEDULE : System.Windows.Forms.Form { 公共System.Windows.Forms.LinkLabel linkLabel1; 公共System.Windows.Forms.LinkLabel linkLabel2; 公共System.Windows.Forms.LinkLabel linkLabel3; 公共System.Windows.Forms.LinkLabel linkLabel4; 私有按钮button1;

    /// Required designer variable.

    public System.ComponentModel.Container components = null;

    public YOURCLASSSCHEDULE()
    {

        //
        InitializeComponent();

        // TODO: Add any constructor code after InitializeComponent call

    }

    /// Clean up any resources being used.
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (components != null)
            {
                components.Dispose();
            }
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(YOURCLASSSCHEDULE));
        this.linkLabel1 = new System.Windows.Forms.LinkLabel();
        this.linkLabel2 = new System.Windows.Forms.LinkLabel();
        this.linkLabel3 = new System.Windows.Forms.LinkLabel();
        this.linkLabel4 = new System.Windows.Forms.LinkLabel();
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // linkLabel1
        // 
        this.linkLabel1.BackColor = System.Drawing.SystemColors.ActiveCaption;
        this.linkLabel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.linkLabel1.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.linkLabel1.LinkArea = new System.Windows.Forms.LinkArea(0, 7);
        this.linkLabel1.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
        this.linkLabel1.Location = new System.Drawing.Point(41, 123);
        this.linkLabel1.Name = "linkLabel1";
        this.linkLabel1.Size = new System.Drawing.Size(288, 32);
        this.linkLabel1.TabIndex = 1;
        this.linkLabel1.TabStop = true;
        this.linkLabel1.Text = "Class 1";
        this.linkLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
        // 
        // linkLabel2
        // 
        this.linkLabel2.BackColor = System.Drawing.SystemColors.ActiveCaption;
        this.linkLabel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.linkLabel2.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.linkLabel2.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
        this.linkLabel2.Location = new System.Drawing.Point(467, 123);
        this.linkLabel2.Name = "linkLabel2";
        this.linkLabel2.Size = new System.Drawing.Size(288, 32);
        this.linkLabel2.TabIndex = 2;
        this.linkLabel2.TabStop = true;
        this.linkLabel2.Text = "Class 2";
        this.linkLabel2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        this.linkLabel2.VisitedLinkColor = System.Drawing.Color.Navy;
        this.linkLabel2.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel2_LinkClicked);
        // 
        // linkLabel3
        // 
        this.linkLabel3.BackColor = System.Drawing.SystemColors.ActiveCaption;
        this.linkLabel3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.linkLabel3.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.linkLabel3.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
        this.linkLabel3.Location = new System.Drawing.Point(41, 311);
        this.linkLabel3.Name = "linkLabel3";
        this.linkLabel3.Size = new System.Drawing.Size(288, 32);
        this.linkLabel3.TabIndex = 3;
        this.linkLabel3.TabStop = true;
        this.linkLabel3.Text = "Class 3";
        this.linkLabel3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        this.linkLabel3.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel3_LinkClicked);
        // 
        // linkLabel4
        // 
        this.linkLabel4.BackColor = System.Drawing.SystemColors.ActiveCaption;
        this.linkLabel4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.linkLabel4.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.linkLabel4.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
        this.linkLabel4.Location = new System.Drawing.Point(467, 311);
        this.linkLabel4.Name = "linkLabel4";
        this.linkLabel4.Size = new System.Drawing.Size(288, 32);
        this.linkLabel4.TabIndex = 4;
        this.linkLabel4.TabStop = true;
        this.linkLabel4.Text = "Class 4";
        this.linkLabel4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        this.linkLabel4.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel4_LinkClicked);
        // 

        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
        this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
        this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        this.ClientSize = new System.Drawing.Size(790, 482);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.linkLabel4);
        this.Controls.Add(this.linkLabel3);
        this.Controls.Add(this.linkLabel2);
        this.Controls.Add(this.linkLabel1);
        this.Font = new System.Drawing.Font("OldDreadfulNo7 BT", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Name = "YOURCLASSSCHEDULE";
        this.Text = "Your Classes";
        this.Load += new System.EventHandler(this.Form2_Load);
        this.ResumeLayout(false);

    }
    #endregion

    public void Form2_Load(object sender, System.EventArgs e)
    {
       // if (text == "900456317")
       // {

        //}

    }

    public void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
    {

        System.Diagnostics.Process.Start("http://www.georgiasouthern.edu/map/");
    }

    private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {

    }

    private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {

    }

    private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form1 form1 = new Form1();
        form1.Show();
        this.Hide();
    }
}

}

So my program works like this: using winforms, user enters ID number, using an array, based on the right id number, that student information and class schedule outputs in a message box!

My question is how to take the 4 classes in the message box/array and write them to the linklabel text in form 2????

My Getschedule class contains the array and is listed below:

namespace Eagle_Eye_Class_Finder
{

public class GetSchedule
{
    IDnumber[] IDnumbers = new IDnumber[3];

    public string GetDataFromNumber(string ID)
    {
        foreach (IDnumber IDCandidateMatch in IDnumbers)
        {

            if (IDCandidateMatch.ID == ID)
            {
                StringBuilder myData = new StringBuilder();
                myData.AppendLine(IDCandidateMatch.Name);
                myData.AppendLine(": ");
                myData.AppendLine(IDCandidateMatch.ID);
                myData.AppendLine(IDCandidateMatch.year);
                myData.AppendLine(IDCandidateMatch.class1);
                myData.AppendLine(IDCandidateMatch.class2);
                myData.AppendLine(IDCandidateMatch.class3);
                myData.AppendLine(IDCandidateMatch.class4);
                //return myData;
                return myData.ToString();
            }
        }
        return "";
    }

    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" };

    }
    public 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; }


       public static void ProcessNumber(IDnumber myNum)
            {
                StringBuilder myData = new StringBuilder();
                myData.AppendLine(myNum.Name);   
                myData.AppendLine(": ");
                myData.AppendLine(myNum.ID);
                myData.AppendLine(myNum.year);
                myData.AppendLine(myNum.class1);
                myData.AppendLine(myNum.class2);
                myData.AppendLine(myNum.class3);
                myData.AppendLine(myNum.class4);  
                MessageBox.Show(myData.ToString());
            }




    }

}

}

My form 2 which will contain the linklabels is listed below:

public class YOURCLASSSCHEDULE : System.Windows.Forms.Form
{
public System.Windows.Forms.LinkLabel linkLabel1;
public System.Windows.Forms.LinkLabel linkLabel2;
public System.Windows.Forms.LinkLabel linkLabel3;
public System.Windows.Forms.LinkLabel linkLabel4;
private Button button1;

    /// Required designer variable.

    public System.ComponentModel.Container components = null;

    public YOURCLASSSCHEDULE()
    {

        //
        InitializeComponent();

        // TODO: Add any constructor code after InitializeComponent call

    }

    /// Clean up any resources being used.
    protected override void Dispose(bool disposing)
    {
        if (disposing)
        {
            if (components != null)
            {
                components.Dispose();
            }
        }
        base.Dispose(disposing);
    }

    #region Windows Form Designer generated code
    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {
        System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(YOURCLASSSCHEDULE));
        this.linkLabel1 = new System.Windows.Forms.LinkLabel();
        this.linkLabel2 = new System.Windows.Forms.LinkLabel();
        this.linkLabel3 = new System.Windows.Forms.LinkLabel();
        this.linkLabel4 = new System.Windows.Forms.LinkLabel();
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // linkLabel1
        // 
        this.linkLabel1.BackColor = System.Drawing.SystemColors.ActiveCaption;
        this.linkLabel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.linkLabel1.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.linkLabel1.LinkArea = new System.Windows.Forms.LinkArea(0, 7);
        this.linkLabel1.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
        this.linkLabel1.Location = new System.Drawing.Point(41, 123);
        this.linkLabel1.Name = "linkLabel1";
        this.linkLabel1.Size = new System.Drawing.Size(288, 32);
        this.linkLabel1.TabIndex = 1;
        this.linkLabel1.TabStop = true;
        this.linkLabel1.Text = "Class 1";
        this.linkLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        this.linkLabel1.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel1_LinkClicked);
        // 
        // linkLabel2
        // 
        this.linkLabel2.BackColor = System.Drawing.SystemColors.ActiveCaption;
        this.linkLabel2.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.linkLabel2.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.linkLabel2.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
        this.linkLabel2.Location = new System.Drawing.Point(467, 123);
        this.linkLabel2.Name = "linkLabel2";
        this.linkLabel2.Size = new System.Drawing.Size(288, 32);
        this.linkLabel2.TabIndex = 2;
        this.linkLabel2.TabStop = true;
        this.linkLabel2.Text = "Class 2";
        this.linkLabel2.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        this.linkLabel2.VisitedLinkColor = System.Drawing.Color.Navy;
        this.linkLabel2.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel2_LinkClicked);
        // 
        // linkLabel3
        // 
        this.linkLabel3.BackColor = System.Drawing.SystemColors.ActiveCaption;
        this.linkLabel3.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.linkLabel3.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.linkLabel3.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
        this.linkLabel3.Location = new System.Drawing.Point(41, 311);
        this.linkLabel3.Name = "linkLabel3";
        this.linkLabel3.Size = new System.Drawing.Size(288, 32);
        this.linkLabel3.TabIndex = 3;
        this.linkLabel3.TabStop = true;
        this.linkLabel3.Text = "Class 3";
        this.linkLabel3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        this.linkLabel3.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel3_LinkClicked);
        // 
        // linkLabel4
        // 
        this.linkLabel4.BackColor = System.Drawing.SystemColors.ActiveCaption;
        this.linkLabel4.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
        this.linkLabel4.Font = new System.Drawing.Font("Times New Roman", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.linkLabel4.LinkBehavior = System.Windows.Forms.LinkBehavior.HoverUnderline;
        this.linkLabel4.Location = new System.Drawing.Point(467, 311);
        this.linkLabel4.Name = "linkLabel4";
        this.linkLabel4.Size = new System.Drawing.Size(288, 32);
        this.linkLabel4.TabIndex = 4;
        this.linkLabel4.TabStop = true;
        this.linkLabel4.Text = "Class 4";
        this.linkLabel4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
        this.linkLabel4.LinkClicked += new System.Windows.Forms.LinkLabelLinkClickedEventHandler(this.linkLabel4_LinkClicked);
        // 

        // 
        this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
        this.BackgroundImage = ((System.Drawing.Image)(resources.GetObject("$this.BackgroundImage")));
        this.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
        this.ClientSize = new System.Drawing.Size(790, 482);
        this.Controls.Add(this.button1);
        this.Controls.Add(this.linkLabel4);
        this.Controls.Add(this.linkLabel3);
        this.Controls.Add(this.linkLabel2);
        this.Controls.Add(this.linkLabel1);
        this.Font = new System.Drawing.Font("OldDreadfulNo7 BT", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
        this.Name = "YOURCLASSSCHEDULE";
        this.Text = "Your Classes";
        this.Load += new System.EventHandler(this.Form2_Load);
        this.ResumeLayout(false);

    }
    #endregion

    public void Form2_Load(object sender, System.EventArgs e)
    {
       // if (text == "900456317")
       // {

        //}

    }

    public void linkLabel1_LinkClicked(object sender, System.Windows.Forms.LinkLabelLinkClickedEventArgs e)
    {

        System.Diagnostics.Process.Start("http://www.georgiasouthern.edu/map/");
    }

    private void linkLabel2_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {

    }

    private void linkLabel3_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {

    }

    private void linkLabel4_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        Form1 form1 = new Form1();
        form1.Show();
        this.Hide();
    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖不如嘢 2024-09-06 14:18:22

将 4 个类移至静态对象或业务层。使用业务层跨表单访问数据。

例如,使用它:

YourBusinessLayer.ScheduleManager manager = new ScheduleManager();

ScheduleManager.Add(new Schedule("Name", "ID", "Class", "Timings"); //Add schedules
List<Schedule> listOfAllSchedules= manager.GetSchedules(); //Get Schedules

manager.Schedule[0].ID
manager.Schedule[0].Name
manager.Schedule[0].Class
//....

然后,在任何表单中访问(编辑/更新)您的日程管理器。

顺便说一句,正如站点注释一样,类不应该(读:不能)是 GetSchedule;相反,一个类可能包含一个名为 GetSchedule() 的方法(读取:操作)。

Move your 4 classes to static objects or business layer. Use business layer to access data across your forms.

Use it like, for instance:

YourBusinessLayer.ScheduleManager manager = new ScheduleManager();

ScheduleManager.Add(new Schedule("Name", "ID", "Class", "Timings"); //Add schedules
List<Schedule> listOfAllSchedules= manager.GetSchedules(); //Get Schedules

manager.Schedule[0].ID
manager.Schedule[0].Name
manager.Schedule[0].Class
//....

Then, access(edit/update) the your schedule manager in your any of your forms.

Btw, just as a site note, a class should'nt(read: cannot) be a GetSchedule; rather a class may contain a method (read: action) called GetSchedule().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文