将值从父表单传递到多个子froms. [英] pass value from parent form to multiple child froms.

查看:130
本文介绍了将值从父表单传递到多个子froms.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有父表单"login",我需要将用户名从登录表单传递给所有子表单.
我尝试使用构造函数,并且还使文本框公开了登录表单.

bt值从登录名传递到管理形式(登录的子形式)..
并转为bookmgmt形式(管理员的孩子),需要重复相同的步骤.


有什么方法可以将值从父级传递到所有子级n子级子级的形式?

任何帮助将不胜感激!

在此先感谢:)

i have parent form "login" i need to pass the username from login form to all it''s child forms.
i tried using constructors and also making making the textbox public of login form.

bt values passes from login to admin form(child form of login)..
and to pass into bookmgmt form (child of admin), same procress needed to be repeated.


Is there any way to pass values from parent to all it''s child n sub child''s form??

any help would be appreciated!

thanks in advance :)

推荐答案

创建一个静态类,并将要共享的信息存储为该类的属性.

编辑===============

静态类在程序启动时进行初始化,而无需编写外部对象的任何代码即可实现初始化.因此,考虑到以下类别:

Create a static class and store the info to be shared as properties of that class.

EDIT ===============

A static class is initialized at program startup without any code from extrnal objects having to be written to implement the initialization. So, given the floowing class:

public static class Globals
{
    public static string UserID { get; set; }

    Globals()
    {
        UserID = "";
    }
}


程序启动时,将调用Globals构造函数,并将UserID初始化为空字符串

要访问UserID,您可以执行以下操作:


When the program starts, the Globals constructor is called, and UserID is initialized to an empty string

To access UserID, you would do this:

if (string.IsNullOrEmpty(Globals.UserID))
{
    // user isn't logged in
}



简单,干净,可维护.此外,应用程序中的任何对象都可以使用静态Globals对象,这意味着您的所有表单都可以使用设置/获取Globals中的公共数据.
是的,您可以使用接口,但是这需要您更改所有现有表单,然后记住新表单需要继承该接口.我更喜欢使用静态类.



It''s easy, clean, and maintainable. Further, ANY object in the application can use the static Globals object, which means all of your forms can use set/get the public data in Globals.

Yeah, you could use an interface, but that would require you to change all of your existing forms, and then remember that new forms need to inherit the interface. I prefer to use a static class.


您必须创建一个Global类,就像这样
公共静态布尔ValidateId(字符串用户名,字符串密码)
{
试试
{
DataSet ds = new DataSet();
字符串SQLCommand =从UserDetails中选择user_is_system,其中User_Name =""+ UserName +"和User_Password =""+ EncryptDecrypt.Encode(Password)+"";
SqlDataAdapter Adapter =新的SqlDataAdapter(SQLCommand,Globals.ConnectionString);
Adapter.Fill(ds);
Adapter.SelectCommand.Connection.Close();
如果(ds.Tables [0] .Rows.Count> 0)
{
如果(Convert.ToBoolean(ds.Tables [0] .Rows [0] ["User_is_system"]))
isSystemUser = true;
其他
isSystemUser = false;
返回true;
}
}
catch(ex ex例外)
{
扔前;
}
返回false;
}
现在,您可以以任何形式从此类获取用户名.
-在form1加载事件中-
exp:lblStart.Text ="Welcome," + Globals.UserName;
You have to create a Global class, like this
public static bool ValidateId(string UserName, string Password)
{
try
{
DataSet ds = new DataSet();
string SQLCommand = "select user_is_system from UserDetails where User_Name = ''" + UserName + "'' and User_Password = ''" + EncryptDecrypt.Encode(Password) + "''";
SqlDataAdapter Adapter = new SqlDataAdapter(SQLCommand, Globals.ConnectionString);
Adapter.Fill(ds);
Adapter.SelectCommand.Connection.Close();
if (ds.Tables[0].Rows.Count > 0)
{
if (Convert.ToBoolean(ds.Tables[0].Rows[0]["User_is_system"]))
isSystemUser = true;
else
isSystemUser = false;
return true;
}
}
catch (Exception ex)
{
throw ex;
}
return false;
}
Now you can fetch your username from this class in any form.
--In form1 load event--
exp: lblStart.Text = " Welcome, " + Globals.UserName;


您已经获得了一些可行的解决方案,但这实际上取决于您应用程序的设置方式.子表格已经打开了吗?如果是这样,那么您可以让他们在需要时使用委托来获取信息.如果没有,他们应该在其构造函数或表单加载事件中获取信息.

如前所述,也许您应该重新考虑您的设计以及表单之间的交互.
You''ve already been given some workable solutions, but it really depends on how your app is set up. Are the child forms already open? If so then you let them use a delegate to get the info when they need it. If not, they should get the info in their constructor or form loading event.

As mentioned, perhaps you should rethink your design and interaction between the forms.


这篇关于将值从父表单传递到多个子froms.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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