【问题标题】:Call a Get Method from Another Form, C#从另一个窗体调用 Get 方法,C#
【发布时间】:2022-01-23 12:03:21
【问题描述】:

对不起,如果我的问题很愚蠢,我是初学者。我有两种形式:

  • Form1:显示信息表
  • Form2:显示要填写信息的表单

我需要使用get方法将Form2中的信息获取到Form1(如果有更好的方法请提出)。

我的问题是当我在 Form1 中键入这些 get 方法时,它们无法识别。

表格1

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        //---------------------------------Initial Stuff------------------------------------
        Form2 form2 = null;

        //----------------------------------Constructor-------------------------------------
        public Form1()
        {
            InitializeComponent();
        }

        private void nouveau_Click(object sender, EventArgs e)
        {
            if (form2 == null)
            {
                form2 = new Form2();
                form2.Show();
            }
        }

        //---------------------------------ListView of Information------------------------------
         ListViewItem lvi = new ListViewItem(getClient());
            lvi.SubItems.Add(societe.Text);
            lvi.SubItems.Add(datedebut.Text);
            lvi.SubItems.Add(type.Text);
            lvi.SubItems.Add(etat.Text);

            
    }
}

表格2:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form2 : 
    {
        
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            
            client.Text="";
            societe.Text = "";
            datedebut.Text = "";
            type.Text = "";
            etat.Text = "";
            
            
        }
        //----------------------------Return Functions for table----------------------
        
        public String getClient()
        {
            return client.Text;
        }
        public String getSociete()
        {
            return societe.Text;
        }
        public String DateDebut()
        {
            return datedebut.Text;
        }
        public String getType()
        {
            return type.Text;
        }
        public String getEtat()
        {
            return etat.Text;
        }
        
    }
}


所以我更新了我的代码并尝试了另一种方式来做事

现在我有 4 个 .cs 文件:Principal、FillInfo、Folder、Program

程序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Principal());
        }
    }
}

文件夹:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WindowsFormsApplication1
{
    class Folder
    {
        //-----------------------------------------CONSTRUCTOR--------------------------
        public Folder()
        {
            this.Customer = "";
            this.Company = "";
            this.StartDate = "";
            this.TechUsed = "";
            this.Status = "";
        }

        //-----------------------------------------GETTERS AND SETTERS-------------------
        public string Customer { get; set; }
        public string Company { get; set; }
        public string StartDate { get; set; }
        public string TechUsed { get; set; }
        public string Status { get; set; }

        
    }
}

校长:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Principal : Form
    {
        
        
        //-----------------------------------INITIAL VARIABLES--------------------------------------------------

        FillInfo fillinfo = null;
        public Folder f;
        
        
        

        //-----------------------------------INITIAL METHODS----------------------------------------------------

        public Principal()
        {
            InitializeComponent();
        }
                
        //-----------------------------------ELEMENTS METHODS--------------------------------------------------
        
        // NEW BUTTON
        private void pNew_Click(object sender, EventArgs e)
        {
            f= new Folder();
            
            if (fillinfo == null)
            {
                fillinfo = new FillInfo();
                fillinfo.Show();
            }
        }
        
        //---------------------------------------PROCESSING-----------------------------------------------------

         ListViewItem fillInfoListView = new ListViewItem(f.getCustomer());
         

    }
}

填充信息:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class FillInfo : Form
    {
        //-----------------------------------INITIAL VARIABLES--------------------------------------------------
        
        //-----------------------------------INITIAL METHODS----------------------------------------------------
        
        public FillInfo()
        {
            InitializeComponent();
        }

        //-----------------------------------ELEMENTS METHODS--------------------------------------------------
        
        private void fOkButton_Click(object sender, EventArgs e)
        {
            


            f.setCustomer = fCustomerTextField.Text;
            f.setCompany = fCompanyTextField.Text;
            f.setStartDate = FStartDateDatePicker.Text;
            f.setTechUsed = fTechUsedDropList.Text;
            f.setStatus = fStatusDropList.Text;

            fCustomerTextField.Text = "";
            fCompanyTextField.Text = "";
            FStartDateDatePicker.Text = "";
            fTechUsedDropList.Text = "";
            fStatusDropList.Text = "";
            
        }
     
    }
}

【问题讨论】:

  • 为什么要使用单独的方法来公开数据而不是传递单个模型对象?
  • 为您的表单命名Form1Form2 是不具描述性且无用的表单名称 - 并为您的项目取一个合适的名称而不是 WindowsFormsApplication1。此外,您还应该在引用实例成员时使用this.,以便阅读您的代码的人可以立即分辨出哪些标识符是表单成员、静态变量和本地变量/参数。
  • 哦,是的,这样更好,我试试,谢谢
  • C# 有属性。当您实际上打算使用属性时,请不要使用方法。属性不是字段的 getter 和 setter 的快捷方式,它是对象 API 表面的一部分。字段只是实现细节。所有库都需要属性:序列化程序处理属性,Windows 窗体上的数据绑定或 WPF 处理属性(这是您在这里需要的),验证处理属性。
  • 谢谢@PanagiotisKanavos,我正在重写代码以使用属性而不是 getter 和 setter,谢谢,这很有帮助

标签: c# class methods


【解决方案1】:

假设 Form2 出现并询问用户信息,然后在用户完成输入后消失,它可能看起来像:

    private void nouveau_Click(object sender, EventArgs e)
    {
        var form2 = new Form2();
        form2.ShowDialog();   //SHOW DIALOG

        ListViewItem lvi = new ListViewItem(getClient());
        lvi.SubItems.Add(form2.Societe);   //the property you are busy writing
        lvi.SubItems.Add(form2.DateDebut); //the property you are busy writing
        lvi.SubItems.Add(form2.Type);      //the property you are busy writing. Try and think of a more hepful name than Type
        lvi.SubItems.Add(form2.Etat);      //the property you are busy writing

        //do you need to add that lvi to something?
    }

    

从类级变量中删除 Form2

【讨论】:

  • 非常感谢,我会将lvi添加到ListView1,这是设计器Form2中的表自动创建为一个类,我需要使用设计器,我如何使用设计器而不创建Form2 作为一个类?对不起,如果我很慢
  • 回到 Dai 关于命名事物的评论 - 当您在表单上放置一个控件时重命名它(listView1 -> SocietyListView)。如果 Microsoft 采用“按其类型调用所有内容,并根据添加到代码中的时间递增数字”的做法,那么 C# 编程将是一个非常乏味的体验:我们不会有 DateTime.Now, DateTime.UtcNow, DateTime.Year, DateTime.Month 等等,我们会有 @987654323 @ 并且没有代码是有意义的
  • 是的,我会这样做,我只是在测试功能,所以我没有费心重命名它们,我现在将它们重命名以便更清晰
  • 如何在不将 Form2 创建为类的情况下使用设计器? - 呃.. 当你使用你正在开发的设计器时(我故意避免使用“创建”这个词) Form2 作为一个类 - 你正在绘制东西,Visual Studio 正忙于在 FormX.Designer.cs 中编写数百行代码。当你想使用那个类做一些有用的事情时,你需要创建一个new它的实例,显示它,让用户点击它。所问问题的确切答案是“你不能使用设计器,也不能不创建类”——创建类是设计器的工作。我怀疑你的意思是别的
  • 将 Form2 从类级变量中移除,这就是我的意思,你不能使用设计器,也不能创建类,设计师将其创建为一个类,如果从类杠杆变量中删除,我如何删除,非常感谢您的帮助
猜你喜欢
  • 2011-08-10
  • 2012-09-10
  • 2018-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多