English 中文(简体)
A. 从方法中获取价值
原标题:Getting Values From a Method In A Form Into Another Form
  • 时间:2011-11-17 01:23:23
  •  标签:
  • c#
  • winforms

i) 使用另一种形式的形式存在问题

http://www.hchr.org。

i coded like this: (i entered 123 in textbox1)

public class Form1 : Form
{
    private string user_code;

  public string UserCode
    {
        get { return user_code; }
    }

    public bool LoginUser()
    {
   user_code = null;


    if(textBox1.Text=="123"){
          user_code="usercode";
             }
         * 
         */
    }
}

表格2:

          Form1 form1 = new Form1();
          form1.LoginUser();

        MessageBox.Show(form1.UserName);

now output is an empty string,i checked with breakpoint and i saw that when i call loginuser in form2 the value in textbox1 become empty and if condition become false what do you think about problem??

最佳回答
问题回答

页: 1

当你履行具有重大参数的职能时,你必须把这一参数作为<>>>out/strong>。

objfrm1.loginuser(out U, out v, out w, out a,out b, out c);

但它将更好地利用公共财产,而不是排除参数。

例如:

public class Form1 : Form
    {
        private string user_pass;
        private string user_name;
        private bool insert_ability;
        private bool update_ability;
        private bool delete_ability;

        public string UserPass
        {
            get { return user_pass; }
        }

        public string UserName
        {
            get { return user_name; }
        }

        public bool InsertAbility
        {
            get { return insert_ability; }
        }

        public bool UpdateAbility
        {
            get { return update_ability; }            
        }

        public bool DeleteAbility
        {
            get { return delete_ability; }           
        }

        public bool LoginUser()
        {
            /*
              Your code here
              user_pass = "userpass";
              user_name = "username";
              insert_ability = true;

              update_ability = false;
              delete_ability = false;
             * 
             */
        }
    }

使用:

Form1 form1 = new Form1();
            form1.LoginUser();

            MessageBox.Show(form1.UserName);

P.S. 和P.S.一样,请永远不使用诸如u、v、w、a、b、c等变名。 这种做法是错误的。

根据你的代码,我假定在某人进入用户名和密码时采用这种方法。 在不了解更多数据读取器或文本箱子的情况下,我想到的是,在第一个文本箱中,这一呼吁会失败,从而退回空洞。 你们是否试图确定文本框,或者在把方法称作形式1之前,如何更好地将案文与你从表2中确定的财产联系起来?

EDIT: Added Code based on your update to show what I mean. I have marked the changes to your code.

public class Form1 : Form
{
    private string user_code;

  public string UserCode
    {
        get { return user_code; }
    }

    private string _testData;                //THIS IS NEEDED
    public string TestData                   //THIS IS NEEDED
    {
        set { _testData = value;}        //THIS IS NEEDED
    }

    public bool LoginUser()
    {
        user_code = null;
        if(textBox1.Text=="123" || TestData=="123")   //THIS IS NEEDED
        {
            user_code="usercode";
        }
    }
}

表格2:

Form1 form1 = new Form1();
form1.TestData = "123";  //THIS IS NEEDED
form1.LoginUser();

MessageBox.Show(form1.UserName);

After editing your code, it does not compile again.
Are you sure you posted your real code?

问题在于:

if(textBox1.Text==123)

<代码>TextBox.Text is a string property,123 is int.

您不能直接比较<条码>、<条码>和<条码>。

因此,你想要做些什么?

Do you want open the Form1 from Form2, type username and password on textboxes in Form1 and pass it into Form2, Right?

EDIT: Your solution

class FormLogin : Form
    {
        public string UserName
        {
            get { return textBoxUserName.Text; }
            set { textBoxUserName.Text = value; }
        }

        public string Password
        {
            get { return textBoxPassword.Text; }
            set { textBoxPassword.Text = value; }
        }

        public FormLogin()
        {
            this.Closing += new System.ComponentModel.CancelEventHandler(FormLogin_Closing);
        }

        void FormLogin_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            if (this.DialogResult == DialogResult.Cancel) return;

            if (!LoginUser(UserName, Password))
            {
                MessageBox.Show("Username or password is incorrect");
                e.Cancel = true;
            }

        }

        bool LoginUser(string userName, string password)
        {
            // check login and password in database for example

            return true;
        }
    }

    class Form3 :Form
    {
        void LoginButtonClick()
        {
            FormLogin formLogin = new FormLogin();
            formLogin.UserName = "LastUserName";
            //uncomment if you want hide main form
            //this.Hide(); 
            if (formLogin.ShowDialog() == DialogResult.OK)
            {
                MessageBox.Show(string.Format("Congratulation! You are logged as {0}", formLogin.UserName));
            }

            //this.Show();
        }
    }

Don t forget add two buttons on FormLogin: OK with DialogResult property OK and Cancel with DialogResult Cancel.





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签