-1

Salam/Hi, I have two winform, 1 named as "fudafrm" another is "loginfrm".I want to control tabs or close 3,5 and 8 tab which is located in fudafrm through loginfrm,when Id and password is correct.

fudafrm is this:

fudafrm

AND loginfrm is :

loginfrm

I tried thees code in loginfrm: (wrong codes)

if (txt_userid.Text == "user" && txt_password.Text == "user")
            {
                Form f1 = new fudafrm();
                TabControl.TabPageCollection("tabpage3").hide();
                TabControl.TabPageCollection("tabpage5").hide();
                TabControl.TabPageCollection("tabpage8").hide();
                this.Hide();
            }
            else
            {
                MessageBox.Show("Invalid User Name & password","Error");

            }

kindly help me for controlling tabs through other forms. I am using windows form C# visual studio 2010.

  • 1
    You need an instance of your form `fudafrm`. But don't create it and throw it away as in your local variable above. Use the real form variable that you use when you show it. Then you could use `f1.Controls.OfType()` to get it or provide a property in that form. – Tim Schmelter Nov 23 '13 at 00:01
  • Still don't get it,Need more detail or example @TimSchmelter – Muhammad Azeem Khan Nov 23 '13 at 00:09
  • Which form was displayed first? – Idle_Mind Nov 23 '13 at 00:15
  • by default fudafrm but i coding in fudafrm 'private void fudafrm_Load(object sender, EventArgs e) { this.Hide(); Form f2 = new loginfrm(); f2.ShowDialog(); }' – Muhammad Azeem Khan Nov 23 '13 at 00:22

1 Answers1

1

Change your login form so that it returns DialogResult.OK when login is successful:

public partial class loginfrm : Form
{

    public enum UserTypes
    {
        admin,
        salesman,
        accountant,
        stockmanager
    }

    private UserTypes _UserType;
    public UserTypes UserType
    {
        get { return _UserType; }
    }

    public loginfrm()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        if (txt_userid.Text == "user" && txt_password.Text == "user")
        {
            // ... set _UserType somehow ...
            this._UserType = UserTypes.salesman;
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
        else
        {
            MessageBox.Show("Invalid User Name & password", "Error");
        }
    }

}

Now, back in your main form, you can check that result and change the TabControl directly:

    private void fudafrm_Load(object sender, EventArgs e)
    {
        this.Hide(); // not necessary from the Load() event
        loginfrm f2 = new loginfrm();
        if (f2.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            switch(f2.UserType)
            {
                case loginfrm.UserTypes.admin:
                    // remove nothing
                    break;

                case loginfrm.UserTypes.salesman:
                    tabControl1.TabPages.Remove(tabPage1);
                    tabControl1.TabPages.Remove(tabPage3);
                    break;

                case loginfrm.UserTypes.accountant:
                    tabControl1.TabPages.Remove(tabPage1);
                    tabControl1.TabPages.Remove(tabPage2);
                    tabControl1.TabPages.Remove(tabPage5);
                    break;

                case loginfrm.UserTypes.stockmanager:
                    tabControl1.TabPages.Remove(tabPage1);
                    tabControl1.TabPages.Remove(tabPage4);
                    tabControl1.TabPages.Remove(tabPage7);
                    tabControl1.TabPages.Remove(tabPage8);
                    break;
            }

        }
        else
        {
            Application.Exit(); // ?
        }
    }

You can set DialogResult to Cancel to indicate a failed login.

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • can i throw my own dialog like user Dialog (because there is limited dialog,Unable for all user) user1 = admin (show all tab) user2 = salesman (remove tab 1,3) user3 = accountant (remove 1,2,5) user4 = stockmanager (1,4,7,8) @Idle_Mind – Muhammad Azeem Khan Nov 23 '13 at 01:32
  • You'd use the same appraoch, but also create an **enum** to represent admin, salesman, accountant, and stockmanager. Now create a Public Property in loginfrm to hold whatever the logged in user is. Finally, when "OK" is returned to ShowDialog() you can read the Public Property to determine which tabs to change. – Idle_Mind Nov 23 '13 at 06:03
  • good but little more trouble because i created a button logout.And coding in it Coding: this.close(); //for closing main form , loginfrm.showdialog(); when i am login again now its not working because of coding is inside private void fudafrm_load. @Idle_Mind – Muhammad Azeem Khan Nov 25 '13 at 23:38