C# Windows Forms主窗体日期变量传递至用户控件订单类的实现方法
嘿,我来帮你搞定这个变量传递的事儿!你现在需要把Form1里的day、month、year三个日期变量传到用户控件Production_usercontrol的production_ordered类中,这里有两种简单且实用的方法,结合你的现有代码来一步步实现:
方法一:给用户控件添加公开属性(推荐,封装性更好)
这种方法通过给用户控件定义公开的日期属性,让主窗体可以直接赋值,用户控件内部就能轻松获取这些值。
步骤1:在用户控件中添加公开属性
打开Production_usercontrol.cs,在类的顶部添加三个公开的整数属性:
// Production_usercontrol.cs public int CurrentDay { get; set; } public int CurrentMonth { get; set; } public int CurrentYear { get; set; }
步骤2:主窗体中给用户控件赋值
在Form1的菜单栏按钮点击事件里,创建用户控件实例时,把当前的day、month、year赋值给控件的属性:
// Form1.cs 菜单栏按钮点击事件 private void MenuButton_Click(object sender, EventArgs e) { Production_usercontrol prodControl = new Production_usercontrol(); // 把主窗体的日期变量传给用户控件 prodControl.CurrentDay = day; prodControl.CurrentMonth = month; prodControl.CurrentYear = year; // 显示用户控件(根据你的实际布局调整,比如加到GroupBox里或者直接显示) this.Controls.Add(prodControl); prodControl.Show(); }
步骤3:同步更新日期(因为Timer一直在走)
由于你的timer1会持续更新日期,所以如果用户控件是持续显示的,需要在timer1_Tick事件里同步更新控件的属性:
// Form1.cs timer1_Tick方法 private void timer1_Tick(object sender, EventArgs e) { // 你原来的日期更新逻辑... // 找到已经显示的用户控件实例,同步更新日期 var prodControl = this.Controls.OfType<Production_usercontrol>().FirstOrDefault(); if (prodControl != null) { prodControl.CurrentDay = day; prodControl.CurrentMonth = month; prodControl.CurrentYear = year; } }
步骤4:在用户控件中使用日期变量
现在在button1_Click_1方法里,就可以用这些属性来构造production_time了,还能简化你原来的日期拼接逻辑:
// Production_usercontrol.cs button1_Click_1方法 public void button1_Click_1(object sender, EventArgs e) { // 用控件的属性获取日期,自动补零的格式化比你原来的if判断简洁多了 string a = $"{CurrentDay:D2}.{CurrentMonth:D2}.{CurrentYear}"; // 后续如果要改成整数格式,比如YYYYMMDD: // int dateInt = CurrentYear * 10000 + CurrentMonth * 100 + CurrentDay; // 你原来的其余代码 bool ol; int b; ol = int.TryParse(Parallel_quantity.Text, out b ); bool rec; int c; rec = int.TryParse(Linear_quantity.Text, out c ) ; string d = checkedItemText; id++; production_ordered prod= new production_ordered(); prod.Ordered_prd(a, b, c, d, id); // ... }
方法二:直接引用主窗体实例(简单但封装性稍弱)
如果不想加属性,也可以让用户控件直接访问主窗体的日期变量,但需要把主窗体的变量改成公开的。
步骤1:修改主窗体变量的访问修饰符
在Form1.cs里,把day、month、year改成public:
// Form1.cs public int day; public int month; public int year;
步骤2:用户控件中添加主窗体引用属性
在Production_usercontrol.cs里添加一个主窗体的引用属性:
// Production_usercontrol.cs public Form1 MainForm { get; set; }
步骤3:主窗体传递自身实例
在菜单栏按钮点击事件里,把Form1的实例传给用户控件:
// Form1.cs 菜单栏按钮点击事件 private void MenuButton_Click(object sender, EventArgs e) { Production_usercontrol prodControl = new Production_usercontrol(); prodControl.MainForm = this; // 传递主窗体实例 this.Controls.Add(prodControl); prodControl.Show(); }
步骤4:在用户控件中获取日期
在button1_Click_1方法里直接从主窗体获取变量:
// Production_usercontrol.cs button1_Click_1方法 public void button1_Click_1(object sender, EventArgs e) { int currentDay = MainForm.day; int currentMonth = MainForm.month; int currentYear = MainForm.year; string a = $"{currentDay:D2}.{currentMonth:D2}.{currentYear}"; // 你原来的其余代码... }
额外小建议
你原来的日期拼接逻辑写了很多if判断来补零,其实用C#的字符串格式化就能轻松搞定:{day:D2}会自动把一位数的day补成两位(比如1变成01),代码会简洁很多哦!
内容的提问来源于stack exchange,提问作者Jakub Szurlej

