如何将值从表单传递到另一个表单并在程序中的其他位置使用。 [英] How to pass value from form to another form and use else where in the program.

查看:87
本文介绍了如何将值从表单传递到另一个表单并在程序中的其他位置使用。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

登录类



  private   void  loginButton_Click( object  sender,EventArgs e)
{
username = userNameTextBox.Text;
password = passwordTextBox.Text;
role = roleSelectionComboBox.Text;
if (username == || password == || role ==
{
MessageBox.Show( 请填写所有);
}
else
{
dbcon.Connection();
query = 登录;
com = new SqlCommand(query,dbcon.con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue( @ useName,username);
com.Parameters.AddWithValue( @ pass,密码);
int userCount =( Int32 )com.ExecuteScalar();
if (userCount == 1
{
.Hide();
Dashboard adminDashboard = new Dashboard();
个人资料profileData = new 个人资料();
profileData.ProfileInfo(role);
adminDashboard.Show();
}
else
{
MessageBox.Show( count为0);
}
}
}





仪表板





  public   partial   class 信息中心:表格
{
public 字符串 uname;
public 信息中心()
{
登录LG = 登录();
Profile pro = new Profile();
uname = pro.ProfileInfo(LG.role);
InitializeComponent();
}



private void Dashboard_Load( object sender,EventArgs e)
{
label1.Text = uname;
// MessageBox.Show(profileInfo.UserName);
}
}





Profile.cs



  public   class 个人资料
{
public string UserName { get ; set ;}
// public string ImageUrl {get;组; }
public string ProfileInfo( string userName)
{
this .UserName = userName;
return UserName;
// this.ImageUrl = imageUrl;
}
}







这是我的问题



Step1:我将值传递给profile.cs

配置文件profileData =  new  Profile(); 
profileData.ProfileInfo(role);





步骤2:在仪表板中我想显示已登录用户的名称所有与该dashbaord相关的表单我是否正在尝试使用profileInfo函数来执行此操作。

step3:因此,当我实例化没有传递值的类配置文件时,我想获取详细信息。 />


提前致谢。

如果任何人提供任何有用的链接欢迎。

解决方案

< blockquote>这是关于表单协作的流行问题。最强大的解决方案是在表单类中实现适当的接口,并传递接口引用而不是引用Form的整个实例。有关更多详细信息,请参阅我以前的解决方案:如何以两种形式复制列表框之间的所有项目 [ ^ ]。



另请参阅此处的其他解决方案讨论。如果应用程序足够简单,解决方案就像在一个表单中声明一些 internal 属性并将对一个表单的实例的引用传递给另一个表单的实例一样简单形成。对于更复杂的项目,这种违反严格封装的样式和松散耦合可能会增加代码的意外复杂性并引发错误,因此封装良好的解决方案将是优惠。



另请参阅:

http://en.wikipedia.org/wiki/Accidental_complexity [ ^ ],

http://en.wikipedia.org/wiki/Loose_coupling [ ^ ]。



-SA


Login Class

private void loginButton_Click(object sender, EventArgs e)
        {
            username = userNameTextBox.Text;
            password = passwordTextBox.Text;
            role = roleSelectionComboBox.Text;
            if(username == "" || password == "" || role =="")
            {
                MessageBox.Show("Please fill all");
            }
            else
            {
                dbcon.Connection();
                query = "Login";
                com = new SqlCommand(query,dbcon.con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@useName", username);
                com.Parameters.AddWithValue("@pass", password);
                int userCount = (Int32)com.ExecuteScalar();
                if(userCount == 1)
                {
                    this.Hide();
                    Dashboard adminDashboard = new Dashboard();
                    Profile profileData = new Profile();
                    profileData.ProfileInfo(role);
                    adminDashboard.Show();
                }
                else 
                {
                    MessageBox.Show("count is 0");
                }
            }
        }



Dashboard


public partial class Dashboard : Form
    {
        public string uname;
        public Dashboard()
        {
            Login LG = new Login();
            Profile pro = new Profile();
            uname = pro.ProfileInfo(LG.role);
            InitializeComponent();
        }

        

        private void Dashboard_Load(object sender, EventArgs e)
        {
            label1.Text = uname;
            //MessageBox.Show(profileInfo.UserName);
        }
    }



Profile.cs

public class Profile
    {
        public string UserName{get; set;}
      //  public string ImageUrl { get; set; }
        public string ProfileInfo(string userName)
        {
            this.UserName = userName;
            return UserName;            
            //this.ImageUrl = imageUrl;
        }    
    }




Here comes my problem

Step1 : I am passing the value to profile.cs

Profile profileData = new Profile();
        profileData.ProfileInfo(role);



Step2 : In Dashboard i want to display the name of the logged user as well all form related to that dashbaord do i am trying to use profileInfo function to do that.
step3: As a result when i am instantiating the class profile with out passing value i want to get details.

Thanks in advance.
If any one providing any useful link welcome.

解决方案

This is the popular question about form collaboration. The most robust solution is implementation of an appropriate interface in form class and passing the interface reference instead of reference to a "whole instance" of a Form. Please see my past solution for more detail: How to copy all the items between listboxes in two forms[^].

Please also see other solutions in this discussion. If the application is simple enough, the solution could be as simple as declaring of some internal property in one form and passing a reference to the instance of one form to the instance of another form. For more complex projects, such violation of strictly encapsulated style and loose coupling could add up the the accidental complexity of the code and invite mistakes, so the well-encapsulated solution would be preferable.

Please see also:
http://en.wikipedia.org/wiki/Accidental_complexity[^],
http://en.wikipedia.org/wiki/Loose_coupling[^].

—SA


这篇关于如何将值从表单传递到另一个表单并在程序中的其他位置使用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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