473,396 Members | 2,115 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 ?..
Apr 21 '09 #1
14 9390
tlhintoq
3,525 Expert 2GB
You need to save your values someplace... ini file, text file, config file, registry...
Apr 22 '09 #2
kurtzky
26
you need to save the details for every person, then retrieve them the next time a certain person logs in..
Apr 22 '09 #3
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
Expand|Select|Wrap|Line Numbers
  1. private static string getConfigFilePath()
  2.         {
  3.             return Assembly.GetExecutingAssembly().Location + ".config";
  4.         }
  5.         private static XmlDocument loadConfigDocument()
  6.         {
  7.             XmlDocument docx = null;
  8.             try
  9.             {
  10.                 docx = new XmlDocument();
  11.                 docx.Load(getConfigFilePath());
  12.                 return docx;
  13.             }
  14.             catch (System.IO.FileNotFoundException e)
  15.             {
  16.                 throw new Exception("No configuration file found.", e);
  17.             }
  18.         }
  19.         private void rem_CheckedChanged(object sender, EventArgs e)
  20.         {
  21.             if (rem.Checked == true)
  22.             {
  23.                 rem.CheckState = CheckState.Checked;
  24.                 System.Xml.XmlDocument docx = new System.Xml.XmlDocument();
  25.                 docx = loadConfigDocument();
  26.                 System.Xml.XmlNode node;
  27.                 node = docx.SelectSingleNode("//appsettings");
  28.                 try
  29.                 {
  30.                     string key = "rem.checked";
  31.                     string value = "true";
  32.                     XmlElement elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
  33.                     if (elem != null)
  34.                     {
  35.                         elem.SetAttribute("value", value);
  36.                     }
  37.                     else
  38.                     {
  39.                        elem = docx.CreateElement("add");
  40.                         elem.SetAttribute("key", key);
  41.                         elem.SetAttribute("value", value);
  42.                         node.AppendChild(elem);
  43.                     }
  44.                     docx.Save(getConfigFilePath());
  45.                 }
  46.                 catch (Exception e2)
  47.                 {
  48.                     MessageBox.Show(e2.Message);
  49.                 }
  50.  
any idea?..

Thank you..

Dinesh.
Apr 24 '09 #4
tlhintoq
3,525 Expert 2GB
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?
Apr 24 '09 #5
hello tlhintoq,

Expand|Select|Wrap|Line Numbers
  1. elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
  2.  
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.
Apr 25 '09 #6
tlhintoq
3,525 Expert 2GB
@sarabonn
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 ".SelectSingleNode" 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
Expand|Select|Wrap|Line Numbers
  1. elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
has to do with your checkbox.checked being either true or false.

i think because i will get null value in the initial state so it directly showing NullException.
checkbox.Checked 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.
Apr 25 '09 #7
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.checked" and in the value iam passing the value true..
Apr 25 '09 #8
tlhintoq
3,525 Expert 2GB
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.checked" 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?
Apr 26 '09 #9
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.
Apr 26 '09 #10
tlhintoq
3,525 Expert 2GB
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.checked property to either true or false?
Apr 26 '09 #11
hello tlhintoq,

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

Expand|Select|Wrap|Line Numbers
  1. elem = (XmlElement)node.SelectSingleNode(string.Format("//add[@key='{0}']", key));
  2.  
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.
Apr 26 '09 #12
tlhintoq
3,525 Expert 2GB
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
Expand|Select|Wrap|Line Numbers
  1. // My code may be off here since I don't do config files
  2. // but you should be able to catch the logic of it and clean it up
  3. if (   SelectSingleNode(string.Format("//add[@key='{0}']", key) != null)
  4. {
  5. }
  6. else
  7. {
  8. }
  9.  
Apr 26 '09 #13
tlhintoq
3,525 Expert 2GB
Like I said, I tend to still do old-fashioned registry.

Expand|Select|Wrap|Line Numbers
  1. RegistryKey Regkey = "HKEY_CURRENT_USER\\Software\\MyApplication";
  2. RegKey.SetValue("myCoolCheckbox", myCoolCheckbox.Checked);
Apr 26 '09 #14
Curtis Rutland
3,256 Expert 2GB
I think you are making this more complicated than it needs to be.
http://msdn.microsoft.com/en-us/libr...69(VS.80).aspx
Apr 27 '09 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Fabri | last post by:
How can I, on button click, to select ONLY the following 4 checkbox? I would like to do this without a for loop through form.lenght because this is only an example and I have to apply this script...
4
by: feanor | last post by:
I need to select children checkboxes when selecting the parent one. This is my function: function SelectChildrens(checkbox_name){ form = document.forms; Sname = checkbox_name.split("-"); for...
0
by: mike | last post by:
Hi there: I've read an excellent "how to"-article by Microsoft (no. 306227) - partly cited cited at the end of this email). I have implemented the code related to the part "How to Add a...
5
by: DotNetJunkies User | last post by:
1. i want to populate checkboxlist using javascript only at client side ....how can i do this..by populate word i mean that checkboxes should be checked or unchecked on some condition basis.......
4
by: KC | last post by:
What's the difference between 'Checked' & 'CheckState' for the checkbox? Am I wrong or do they do the same thing, which begs the question why? --- Ken
2
by: marcmc | last post by:
Hey All, I want to find whether my first item within my CheckListBox is in a checked or unchecked state. I tried the following but it did not work, Can anyone help. If...
10
by: rn5a | last post by:
All the rows in a DataGrid, including the Header, are accompanied with a CheckBox. I want that when the CheckBox in the Header is checked, then all the CheckBoxes should automatically get checked....
0
by: cyberdawg999 | last post by:
Greetings all in ASP land I have overcome one obstacle that took me 2 weeks to overcome and I did it!!!!! I am so elated!! thank you to all who invested their time and energy towards helping me...
4
by: sarabonn | last post by:
Hello everyone, Iam firing a event in the chechbox_checkedchanged. It works fine private void checkBox1_CheckedChanged(object sender, EventArgs e) { ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.