473,327 Members | 2,055 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,327 software developers and data experts.

Copy String^ to CheckBox^

Hello,

I have written my code in Visual C++ using .NET Framework. I have several checkboxes on my GUI and I would like to give the user the option of saving the current state of the checkboxes. I took care of the save file, now the loading of the file is where I have my problem. The checkbox names are saved in the file, when the user loads this file back into the GUI, I read the checkbox names line by line and I am suppose to check whatever checkbox name it is.

Example:
checkbox_file.txt content:

checkBox1
checkBox5

When checkbox_file.txt is loaded back into the application,
"checkBox1" is saved as a String^

I am trying to save this string to a checkbox so that I may do this:
checkBox->checked = true;


thanks in advance.



Expand|Select|Wrap|Line Numbers
  1.  
  2. private: System::Void read_file(String ^strFileName)
  3. {
  4.     /* save the contents of the file line by line as elements in the lines array */
  5.     array<String^> ^lines = System::IO::File::ReadAllLines(strFileName);
  6.  
  7.     System::Windows::Forms::CheckBox^ check = (gcnew System::Windows::Forms::CheckBox());
  8.  
  9.     for(int i = 0; i < lines->Length; i++)
  10.     {
  11.         /* as long as the string is not an empty line */
  12.         if(lines[i]->CompareTo("") != 0)
  13.         {
  14.              check->Name = lines[i];
  15.              check->Checked = true;
  16.         }
  17.      }
  18. }
  19.  
  20.  
Jul 10 '09 #1
2 1754
Oh I forgot to metion,

I know I can do it with an if condition such as. The only problem with this approach is that I have many checkboxes, so I am trying to avoid this method.

thanks again

Expand|Select|Wrap|Line Numbers
  1.  
  2. if(lines[i]->CompareTo("checkBox1") == 0)
  3. {
  4.     checkBox1->checked = true;
  5. }
  6. else if(lines[i]->CompareTo("checkBox2") == 0)
  7. {
  8.     checkBox2->checked = true;
  9. }
  10.  
  11.  
Jul 10 '09 #2
I found the answer if anyone runs into this problem. Instead of 300 if else statements to compare the checkbox names to strings, i found this to be a better method although not as efficient as I would hope.

I saved colums of checkboxes to a checkbox array. Then I would check each checkbox name with the string I read from the file. If there is a match, then check the checkbox and return so that I can look for the next entry.

Expand|Select|Wrap|Line Numbers
  1.  
  2. /* THIS PORTION OF THE CODE GOES AFTER THE CONSTRUCTOR */
  3. public ref class Form1 : public System::Windows::Forms::Form
  4. {
  5. public:
  6.     Form1(void)
  7.     {
  8.         InitializeComponent();
  9.         //
  10.         //TODO: Add the constructor code here
  11.         //
  12.     }
  13.  
  14.     /* THIS PORTION OF THE CODE GOES AFTER THE CONSTRUCTOR - as you can see from above*/
  15.     array<System::Windows::Forms::CheckBox^>^ main_CB_array()
  16.     {   
  17.         /* store all the checkboxes into one main checkbox array, I'm showing 4 checkboxes for this example */
  18.         array<System::Windows::Forms::CheckBox^>^ main_CB = {checkBox_1, checkBox_2, checkBox_3, checkBox_4};
  19.         /* return the array with all the checkboxes from that column */
  20.         return main_CB;
  21.     }
  22.     /* RIGHT BEFORE THE PROTECTED */
  23. protected:
  24.     /// <summary>
  25.     /// Clean up any resources being used.
  26.     /// </summary>
  27.     ~Form1()
  28.     {
  29.         if (components)
  30.         {
  31.             delete components;
  32.         }
  33.     }
  34.  
  35.  
  36.  
  37. /* this portion is a regular function and it goes after the windows generated code */
  38. private: System::Boolean check_CheckBox(String ^checkBox_name)
  39. {            
  40.    array<System::Windows::Forms::CheckBox^>^ main_CB = Form1::main_CB_array();
  41.  
  42.    /* for each checkbox array element */
  43.    for(int i = 0; i < main_CB->Length; i++)
  44.    {
  45.         /* if the string name matches */
  46.         if(checkBox_name->CompareTo(main_CB[i]->Name) == 0)
  47.         {
  48.             /* check the checkbox and return */
  49.             main_CB[i]->Checked = true;
  50.             return true;
  51.         }                
  52.     }                
  53.     return false;
  54. }
  55.  
  56.  
  57.  
Jul 13 '09 #3

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

Similar topics

1
by: tabonni | last post by:
Hi all I want to create an ASP page, which can copy the real PDF files into the clipboard and then the user can paste it in Outlook message as attachments(it's like inserting attachments) My...
5
by: tabonni | last post by:
Hello All I am creating an ASP page. There are a list of filename and checkbox next to it. When user checked all the documents they want and click ADD TO CLIPBOARD button. All filepaths will be...
3
by: Lauren Quantrell | last post by:
This a a long convoluted string parsing question... I have a string in an Access 2K database table field that I use for noting if a user has checked a record. The string goes like this: xy So...
1
by: louise raisbeck | last post by:
This sounds complicated but bear with me. I have a datagrid. It is dynamic in that, i dont know how many columns i will have until the dataset is back so i have to build the datagrid dynamically. I...
3
by: Uma sakshi | last post by:
Hi I have one VB.NET application,in that application i have one datagrid control.The datagrid control contains somedata.I want to copy the data in a particular cell and paste it into my C#.NET...
10
by: dba123 | last post by:
Why am I getting this error for Budget? Error: An exception of type 'System.FormatException' occurred in mscorlib.dll but was not handled in user code Additional information: String was not...
5
by: Blazej | last post by:
First of all I want to say Hi to every one. So.. Hi :) I'm new in Access, so please be patient. I've got this problem: I have a subform, where user can see results from one table called A. There...
6
by: Andi Wolf | last post by:
Hello, is there any simple posibility to have a control on 2 diferent tabs of a tabcontrol without synchronizing? I want to have e.g. a checkbox with the same label and functionality on two...
8
by: ALTAFAD | last post by:
How to copy selected (s) textfield, which those selected through checkbox I like to copy in clipboard. Access textfield data when front of the textfield checkbox is checked if not checked it...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.