Checkbox checkedstate c#.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sarabonn
    New Member
    • Nov 2008
    • 69

    Checkbox checkedstate c#.net

    Hello everyone,

    In my windows form application iam having a login form with 2 textboxes for username and password and checkbox (to keep the user logged in). When the user login for the first time if he check the checkbox then it should remains checked(iam having a option to uncheck in a menu item) even when the applcation is closed and then reopen it. How can code to remain the checkedstate of the checkbox always checked ?..
  • tlhintoq
    Recognized Expert Specialist
    • Mar 2008
    • 3532

    #2
    You need to save your values someplace... ini file, text file, config file, registry...

    Comment

    • kurtzky
      New Member
      • Nov 2008
      • 26

      #3
      you need to save the details for every person, then retrieve them the next time a certain person logs in..

      Comment

      • sarabonn
        New Member
        • Nov 2008
        • 69

        #4
        hello,

        I have tried to save the checkbox info to the config file. Iam getting a error "object reference is not set to an object". Below is my code
        Code:
        private static string getConfigFilePath()
                {
                    return Assembly.GetExecutingAssembly().Location + ".config";
                }
                private static XmlDocument loadConfigDocument()
                {
                    XmlDocument docx = null;
                    try
                    {
                        docx = new XmlDocument();
                        docx.Load(getConfigFilePath());
                        return docx;
                    }
                    catch (System.IO.FileNotFoundException e)
                    {
                        throw new Exception("No configuration file found.", e);
                    }
                }
                private void rem_CheckedChanged(object sender, EventArgs e)
                {
                    if (rem.Checked == true)
                    {
                        rem.CheckState = CheckState.Checked;
                        System.Xml.XmlDocument docx = new System.Xml.XmlDocument();
                        docx = loadConfigDocument();
                        System.Xml.XmlNode node;
                        node = docx.SelectSingleNode("//appsettings");
                        try
                        {
                            string key = "rem.checked";
                            string value = "true";
                            XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
                            if (elem != null)
                            {
                                elem.SetAttribute("value", value);
                            }
                            else
                            {
                               elem = docx.CreateElement("add");
                                elem.SetAttribute("key", key);
                                elem.SetAttribute("value", value);
                                node.AppendChild(elem);
                            }
                            docx.Save(getConfigFilePath());
                        }
                        catch (Exception e2)
                        {
                            MessageBox.Show(e2.Message);
                        }
        any idea?..

        Thank you..

        Dinesh.

        Comment

        • tlhintoq
          Recognized Expert Specialist
          • Mar 2008
          • 3532

          #5
          You could make us guess but... On what line does VS break with the null object error?

          While that line is highlighted in VS, you can hover your mouse over each object/variable and the tooltip will give you its value. Which one is null?

          Comment

          • sarabonn
            New Member
            • Nov 2008
            • 69

            #6
            hello tlhintoq,

            Code:
            elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
            After this its directly goes to the catch section. i think because i will get null value in the initial state so it directly showing NullException.

            Comment

            • tlhintoq
              Recognized Expert Specialist
              • Mar 2008
              • 3532

              #7
              Originally posted by sarabonn
              hello tlhintoq,

              Code:
              elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
              After this its directly goes to the catch section. i think because i will get null value in the initial state so it directly showing NullException.
              Your original question was how can you keep the checked state persistent between running instanances of your application.
              How can code to remain the checkedstate of the checkbox always checked ?..
              And two folks here told you to save the state of the checkbox when you close, and reload it when you open.

              I'm going to GUESS that your 'node' object doesn't yet exist when you run this line. Put a breakpoint on this line. When your code stops here, hover over each word (XmlElement) ... node ... and so on... and I'll bet that node turns out to be null.

              Either that or the return of your ".SelectSingleN ode" method is null (it didn't find a match) thus you don't have an object to work with.
              I have no idea what any of this
              Code:
              elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
              has to do with your checkbox.checke d being either true or false.

              i think because i will get null value in the initial state so it directly showing NullException.
              checkbox.Checke d only has two possible answers: true or false. Null is not an option. It is either checked or it isn't.

              If you are getting a null object it isn't the state of a checkbox. It might be the very existence of a checkbox. If your checkbox doesn't exist, then it can't have a state.

              Comment

              • sarabonn
                New Member
                • Nov 2008
                • 69

                #8
                Actually i want to save the state of the checkbox. I think you mistaken that error line. That has nothing to do with checkbox. It will just check the config file whether Add key is there or not if it is there then it will add the value or else it add the key and value. In the key iam giving my "rem.checke d" and in the value iam passing the value true..

                Comment

                • tlhintoq
                  Recognized Expert Specialist
                  • Mar 2008
                  • 3532

                  #9
                  I think you mistaken that error line. That has nothing to do with checkbox.
                  Then don't confuse the issue or those trying to help you with it by throwing in random bits and pieces that aren't related.

                  Actually i want to save the state of the checkbox. I think you mistaken that error line. That has nothing to do with checkbox. It will just check the config file whether Add key is there or not if it is there then it will add the value or else it add the key and value. In the key iam giving my "rem.checke d" and in the value iam passing the value true..
                  There is no question or issue in this latest message. Are you still having an issue directly related to your original post of how do you make the checkbox state persistent?

                  Comment

                  • sarabonn
                    New Member
                    • Nov 2008
                    • 69

                    #10
                    hello tlhintoq,

                    What iam trying to do in my application is

                    1). I have a Login groupbox and checkbox "Keep me Logged in". So when the user click this frist time then the state of the checkbox should be set to true even when i close and reopen my application.

                    2).I have a menu when the user click then the state of the checkbox should be set to false i mean remove key also from the config file..

                    This is what iam trying to do..

                    Thank you.

                    Comment

                    • tlhintoq
                      Recognized Expert Specialist
                      • Mar 2008
                      • 3532

                      #11
                      I understand your desire. I understood it the first time you posted.
                      Two users said you need to save the boolean state in some way of your choosing. That can be a config file, XML, text file, registry setting, database record... its all up to you, your preferences and your needs.

                      I personally still save these things in the registry, but encrypt all the values so a user can't just copy/paste/edit the values to gain new permissions.

                      So let me ask you again... What problem in your code are you having with respect to setting the checkbox.checke d property to either true or false?

                      Comment

                      • sarabonn
                        New Member
                        • Nov 2008
                        • 69

                        #12
                        hello tlhintoq,

                        I want to save the checkbox.checke d to true. Iam getting the error after this following line.

                        Code:
                        elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
                        Iam checking whether the config file has the Add key if else iam adding the key and value to the config file. First time since the value of the elem will be null the loop moves to the catch section. This is the problem i have or can you please suggest me whether my approach is right and try someother ?..

                        Thank you very much.

                        Dinesh.

                        Comment

                        • tlhintoq
                          Recognized Expert Specialist
                          • Mar 2008
                          • 3532

                          #13
                          I don't know a thing about XML config files. Never used them. Someone else will have help with that.

                          Considering your current problem isn't about the checkbox, but rather about config files you might want to start a new thread for that.

                          But I can give you some advice that you may or may not be able to apply to this situation. Never assume anything. Never assume something exists, or that the user wouldn't think to enter in a different type of data, or that your network is up *now* just because it was when the application started... etc.

                          First time since the value of the elem will be null the loop moves to the catch section.
                          If I am understanding this right... Just to see if the value is null before trying to do something with it. ie: Wrap the action inside an If { } construct
                          Code:
                          // My code may be off here since I don't do config files
                          // but you should be able to catch the logic of it and clean it up
                          if (   SelectSingleNode(string.Format("//add[@key='{0}']", key) != null)
                          {
                          }
                          else
                          {
                          }

                          Comment

                          • tlhintoq
                            Recognized Expert Specialist
                            • Mar 2008
                            • 3532

                            #14
                            Like I said, I tend to still do old-fashioned registry.

                            Code:
                            RegistryKey Regkey = "HKEY_CURRENT_USER\\Software\\MyApplication";
                            RegKey.SetValue("myCoolCheckbox", myCoolCheckbox.Checked);

                            Comment

                            • Curtis Rutland
                              Recognized Expert Specialist
                              • Apr 2008
                              • 3264

                              #15
                              I think you are making this more complicated than it needs to be.

                              Comment

                              Working...