How to change "Enabled" Value of form2 when clicking in form1?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Fuzz13
    New Member
    • Jun 2010
    • 110

    How to change "Enabled" Value of form2 when clicking in form1?

    I have my main form, form1, and my secondary form, form2. When form2 is first launched it has two values that are disabled, however at a later time I want users to be able to enable those values and use them but I can't figure out how to either send a value to the form2 for a test or access the features of form2 in form1.cs (which seems preferable.)

    I was thinking something like this:
    [code]
    public void StartForm2(int passwordType)
    {


    gbxForm.Enabled = false;//this is meant to be disabled

    PasswordForm form2 = new PasswordForm(cb oxName.Text);//passes the users name to the new form

    //shows form2
    form2.Show();
    }

    private void lnklblChangePas sword_LinkClick ed(object sender, LinkLabelLinkCl ickedEventArgs e)
    {

    //ideal, but obviously doesn't work
    form2.txtbxOldP assword.Enabled = true;

    form2.show();
    }

    I know the above code doesn't work but it should give an idea of what I'm trying to achieve.
  • bentleyw
    New Member
    • Feb 2008
    • 32

    #2
    One way would be to overload the constructor to accept your first form as a parameter. Then you could access all of its properties and methods.

    Another way would be to create a static class with references to your forms. Then you could get at them through that.

    Comment

    • Fuzz13
      New Member
      • Jun 2010
      • 110

      #3
      I am having a difficult time trying to understand how to overload the constructor so I can access form 1 within form2.cs

      Could you possibly give an example so I have an idea where I'm going with it?

      Comment

      • bentleyw
        New Member
        • Feb 2008
        • 32

        #4
        Sure, no problem. Here would be the basic PasswordForm class definition:
        Code:
            public partial class PasswordForm : Form
            {
                StartForm2 otherFormReference = null;
        
                public PasswordForm()
                {
                    InitializeComponent();
                }
        
                public PasswordForm(string username, StartForm2 otherForm)
                {
                    otherFormReference = otherForm;
                }
            }
        The first PasswordForm() constructor is the default, and the second is an overload taking your other form as a parameter. Then you set that to a local variable so that you can access the other form's properties and methods.

        Comment

        • Fuzz13
          New Member
          • Jun 2010
          • 110

          #5
          So PasswordForm is in this situation my Form2, so when I click a button in Form1 it needs to Form2.Show(), but also within Form2 it needs to set
          Code:
          lblOldPassword.Enabled = true;
          I am not understanding how this overloading the constructor is allowing me to do that especially where the code is used within the PasswordForm (my Form2). Wouldn't I need to instantiate an object of Form2 in order to access the values of it?

          Comment

          • Fuzz13
            New Member
            • Jun 2010
            • 110

            #6
            Ok I got it all figured out. Here is what I did, very much thanks to you bentley as I just used your suggestion but did it a little different then the example.

            I use the same Form (passwordForm) for 2 different events (setting initial password, and changing password). When it is called to set the initial password i use this code in form1
            Code:
             public void StartForm2()//has value passed depending on which method calls this method
                    {
                        
            
                        gbxForm.Enabled = false;
                        //this is a one time set the password event
                        //this saves the name chosen in a field we can use it again
            
                        //passes the name chosen to form 2
            
                        //creates form 2 as an object so it ca be passed too
                        form2 = new PasswordForm(cboxName.Text);
            
                        //shows form2
                        form2.Show();
                    }
            this triggers this constructor in form2 (passwordForm)
            Code:
            public PasswordForm(string username)
                    {
                        InitializeComponent();
                        lblUserName.Text = username;
                    }
            Then when the user wants to change their password, they click accordingly in form1 and this code is used
            Code:
             public void lnklblChangePassword_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
                    {
                        const int ENABLE_PASSWORD_CHANGE = 1;//variable to select propper constructor
                        form2 = new PasswordForm(cboxName.Text, ENABLE_PASSWORD_CHANGE);
                        form2.Show();
                        
                    }
            And because it now sends a user name and the constant it triggers this constructor
            Code:
              public PasswordForm(string username,int enable)
                    {
                        InitializeComponent();
                        lblUserName.Text = username;
                        lblOldPassword.Enabled = true;
                        txtbxOldPassword.Enabled = true;
                    }
            This allowed me to choose how the form loads. Thank you so much for your help with this. I hope this can help someone else in the future.

            Comment

            • bentleyw
              New Member
              • Feb 2008
              • 32

              #7
              Oh, I see. Sorry about that, I misunderstood what you were trying to do. I thought you wanted to access a method from the other form and needed a reference to it. I'm glad you were able to solve it anyway.

              Comment

              Working...