开发者

How to display string on title bar on MDIparent form from login form?

I have a login form that have username and password. Then if i successfully login i want to display the entered username on login form to MDIparent title开发者_运维知识库 bar/Status bar. Example: MDIparent [ user name ]


You can just set the text property of the parent form from the child form.

this.MdiParent.Text = "MDIparent [" + username + "]";


you can create a class for user detail:

static class UserLogOn
{

    private static string _userName = "";
    private static string _fullName = "";

    internal static string FullName
    {
        get { return _fullName; }
        set { _fullName = value; }
    }

    internal static string UserName
    {
        get { return _userName; }
        set { _userName = value; }
    }
}

and then:

public partial class LoginForm : Form
{
    private string username = "";

    public LoginForm()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //Do login processing and assuming success call the following;
        UserLogOn.UserName = textBox1.Text;
        username = textBox1.Text;
        ((Form1)this.Owner).Username = UserLogOn.UserName;
        this.DialogResult = DialogResult.OK;
    }
}


Here's some rough example code. It assumes that your login dialog is modal and calls the login logic code (I have not fleshed it out). The login dialog does not care whether the parent is an MDI form or not. It is two WinForms - this is the code section of them - I have only placed a button on the login form as I am only showing you how to pass back the data.

public partial class Form1 : Form
{
    private string _username;
    public Form1()
    {
        InitializeComponent();
        this.OpenLoginForm();
    }

    public string Username 
    { 
        get
        { 
            return this._username;
        } 
        set
        { this._username=value; 
            this.Text=string.Format("Title string [{0}]", this._username);
        }
    }

    private void OpenLoginForm()
    {
        LoginForm frm = new LoginForm();
        frm.ShowDialog(this);

    }
}

public partial class LoginForm : Form
{
    private string username = "testnname";
    public LoginForm()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //Do login processing and assuming success call the following;
        ((Form1)this.Owner).Username = this.username;
        this.DialogResult = DialogResult.OK;
    }
}

Or you could have a property for Username on your login form and then use it as:

        LoginForm frm = new LoginForm();
        if (frm.ShowDialog() == DialogResult.OK)
        {
            this.Text = "Title text " + frm.Username;
        }

Where

public partial class LoginForm : Form
{
    public string Username { get; set; }

    public LoginForm()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        //the login process has been successful
        this.Username = "some user";
        this.DialogResult = DialogResult.OK;
    }
}


After log in, get the reference to the MDIparent(if you add some code with your question then how to do this may be specified as well). Then assign the entered name to the Test property of the parent form like this -

ParentReference.Text = usernameTextBox.Text;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜