Member Avatar for mwaqas1990

My Application is not db based i am developing just for learning. i have 2 form first have just user name and password textfield and submit button and second form have one label. i have make condition with if-else when username is "waqas" and password is "a" then show form2 and hide form1.

i want that when form2 appear, the text of label on form2 should "Hello waqas".

i am trying from 1 hr. :(

Alright, I'll try to help you if you're trying to help yourself. Post me some code and let me know what you're trying and what you think isn't working. I won't just do it for you, but maybe we can work through it together.

Member Avatar for mwaqas1990

form1:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void xbtnReset_Click(object sender, EventArgs e)
        {
            xtxtUserName.Text = "";
            xtxtPass.Text = "";
        }

        private void xbtnLogin_Click(object sender, EventArgs e)
        {
            var objform2 = new Form2();
            if (xtxtUserName.Text == "a" && xtxtPass.Text == "a")
            {
                this.Hide();
                objform2.Show();
            }
            else 
            {
                MessageBox.Show("Your Password is incorrect");
            }
            
        }

form2:

public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void xbtnExit2_Click(object sender, EventArgs e)
        {
            Close();
        }
    }

Actually i m using visual studio and cant find where is label properties in code.

Hi,

Try this code:

public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void xbtnReset_Click(object sender, EventArgs e)
        {
            xtxtUserName.Text = "";
            xtxtPass.Text = "";
        }

        private void xbtnLogin_Click(object sender, EventArgs e)
        {
           // var objform2 = new Form2(); my advice is to create an instance of form2 only when the password is correct. 
            if (xtxtUserName.Text == "a" && xtxtPass.Text == "a")
            {
                var objform2 = new Form2(); 
                this.Hide();
                objform2.UserName = xtxtUserName.Text; //i created a new property for form2
                objform2.Show();
            }
            else 
            {
                MessageBox.Show("Your Password is incorrect");
            }
            
        }

form2:

public partial class Form2 : Form
    {
        private string FUserName;
        public string UserName 
        {
           get {return FUserName ;}
           set {FUserName = value ;}
       } 
        public Form2()
        {
            InitializeComponent();

        }

        private void Form2_Load(object sender, EventArgs e) 
       {
              label1.Text = "Hello " + UserName; //i assume that you have a label on your form2
       }

        private void xbtnExit2_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
Member Avatar for mwaqas1990

Thank You So Much! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.