C#从主窗体到控制页面获取标签值 [英] C# Get Value of label from Main Form to Control Page

查看:201
本文介绍了C#从主窗体到控制页面获取标签值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主表单,我有选项卡控件和标签页,内部标签页我使用了用户控件。在Main表单中我有datetime.Text(Label),我想调用/获取它的值到标签页(这是用户控件)告诉我如何使datetime.Text全局,这样我就可以获得值它的。谢谢,抱歉我的英文不好



这里是图片,请复制并粘贴链接到浏览器查看图片 tinypic.com/ view.php?pic = 2w20yg4& s = 9

Hi, i have one Main form, and i have tab control and tab pages, Inside tab pages i used User Control. In Main form I have datetime.Text(Label), and i wanted to call/get value of it to tab page (which is the user control) tell me how to make the datetime.Text global, so that i can get the value of it. Thanks , sorry for my bad english

here's the pic, Please copy and paste the link to browser to view the image tinypic.com/view.php?pic=2w20yg4&s=9

推荐答案

声明一个公共类来保存公共生命周期所需的公共信息。应用程序,它是存储和访问公共信息的便捷方式。创建一个名为 CommonInfo 的类,如下所示,并创建一个名为 DateTimeInfo 的字符串属性来保存DateTime信息。

Hi, declare a common class to hold common information which are needed in the lifetime of an application, it is the convenient way to store and access common information. Create a Class named CommonInfo like below, and create a string property named DateTimeInfo to hold DateTime information.
public static class CommonInfo
{
  private static string _DateTimeInfo = string.Empty;

  public static string DateTimeInfo
  {
    get { return _DateTimeInfo; }
    set { _DateTimeInfo = Value; }
  }
}



现在,您可以从项目内的任何位置存储和访问DateTimeInfo的信息。加载主窗体时,将DateTime信息分配给 CommonInfo.DateTimeInfo 属性。在Main表单的Load事件中分配这样的DateTime信息,


Now you can store and access information to DateTimeInfo from anywhere inside your project. Assign the DateTime information to CommonInfo.DateTimeInfo property when you load the main form. In the Load event of Main form assign DateTime information like this,

CommonInfo.DateTimeInfo = your expression;



现在,您可以从标签页中获取此信息。为此,只需使用 CommonInfo.DateTimeInfo


不要。

Globals不仅在C#中几乎没有必要,它们会破坏OOP设计的所有优点。



在这种情况下,这意味着您不能在多个地方或主表单之外重用用户控件或当前的应用程序。



而是向控件添加一个属性或方法,让容器直接设置DateTime值。

见这里: 在两种表格之间传递信息,第1部分:父母对孩子 [< a href =http://www.codeproject.com/Tips/548052/Transferring-information-between-two-forms-Parttarget =_ blanktitle =New Window> ^ ] - 这是基于表单,但完全相同的代码也适用于控件。您的表单是父级,您的控件是子级。
Don't.
Globals are not only pretty much unnecessary in C#, they break all the advantages of OOPs design.

In this case, it would mean that you can't "reuse" the usercontrol in multiple places, or outside the main form or current application.

Instead, add a property or method to the control which lets the container set the DateTime value directly.
See here: Transferring information between two forms, Part 1: Parent to Child[^] - it's forms based but teh exact same code works for controls as well. Your form is the "Parent", your control is the "Child".


这篇关于C#从主窗体到控制页面获取标签值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆