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