This is my first post so be easy on me please.

Im using a INI parser to read and write to an ini file. When reading the ini file I have certain parameters set to 1 or 0, which need to be stored to a checkbox variable. Heres a peice of code for reading the ini file.

string whispernotify = source.Configs["Settings"].Get("whisper_notify");
            
            if (whispernotify == "1")
            {
                whisper_notify.Checked = true;
            }
            else
            {
                whisper_notify.Checked = false;
            }

Its working fine but the problem occurs when Im writing back to the ini file. Heres the write back sample:

source.Configs["Settings"].Set ("whisper_notify", whisper_notify.Checked);

In the ini file its coming up with a "True" or "False" not a 1 or 0 like I need. Ive spent several hours trying different things. Any help pointing me in the right direction would be awesome. Thanks.

Recommended Answers

All 8 Replies

I have the honor of welcoming you to Daniweb :). That said:

Your problem is that C# strong types its boolean values. A True in C# is not equal to 1, as a false is not equal to 0. Might I suggest you modify your code to specfically tell it to write a 0 or 1?

I would do it like this:

//Write this function first:
int boolasnum(bool bval){
    return (bval==True)? 1 : 0; //Are you famliar with this operator? What this does is test the condition before the ?, if it's true, the result is the value after the ?, otherwise, the result is the value after the :
}
//then on the write line, I would put this:
source.Configs["Settings"].Set ("whisper_notify", boolasnum(whisper_notify.Checked));

I hope this is what you're looking for. Good luck.

Writing an ini parcer is pretty straight forward.
I have attached a class library that I use, and included the source code for you to check out.

I have the honor of welcoming you to Daniweb :). That said:

Your problem is that C# strong types its boolean values. A True in C# is not equal to 1, as a false is not equal to 0. Might I suggest you modify your code to specfically tell it to write a 0 or 1?

I would do it like this:

//Write this function first:
int boolasnum(bool bval){
    return (bval==True)? 1 : 0; //Are you famliar with this operator? What this does is test the condition before the ?, if it's true, the result is the value after the ?, otherwise, the result is the value after the :
}
//then on the write line, I would put this:
source.Configs["Settings"].Set ("whisper_notify", boolasnum(whisper_notify.Checked));

I hope this is what you're looking for. Good luck.

Thanks for the help, heres a work around I did instead:

if (use_smtp.Checked == true)
            {
                source.Configs["Settings"].Set("use_smtp","1");
            }
            else
            {
                source.Configs["Settings"].Set("use_smtp","0");
            }

Hi,

I have a string(let's call it "Default"). How do I search for this string in a contextmenustrip child items? if this string exists in the contextmenustrip child items, then apply the check mark.

Thanks

If your context menu is a single level menu then use this:

foreach (ToolStripMenuItem item in contextMenuStrip1.Items)
            {
                if (item.Text.Equals("Default"))
                    item.Checked = true;
            }

If not, then build a recursive method that can dive into each of the item's items collection. If you don't know how to do that, then reply with a request for help, but please try to do it on your own before asking.

Hi,

This only check the parent items. To give you an idea what i want, here is what i have and want to search for:

Definition ---> Default,Joe,Mike
Save As
Save

I want to be able to search for the Default word under definition.

Hmmmm, you must not have read the last line of my earlier reply...

The code snippet below uses what we call a recursive method.
In this example, there is a button click event just to fire up the process of searching for the menu item with text of "Default". It does this by calling a method I have named SearchForMenuByName.
Recursive methods usually start off with a main loop that will call another method that is the actual recursive method. SearchForMenuByName is that main loop method. For each of the main menu items at the top level, it will call a method named SearchSubItems. SearchSubItems is the recursive method that will call itself to drill down through menu levels.

private void button1_Click(object sender, EventArgs e)
        {
            ToolStripMenuItem item = SearchForMenuByName("Default");
            if (item != null)
                item.Checked = true;
        }

        private ToolStripMenuItem SearchForMenuByName(string value)
        {
            ToolStripMenuItem result = null;
            foreach (ToolStripMenuItem item in contextMenuStrip1.Items)
            {
                result = SearchSubItems(item,value);
                if (result != null)
                    break;
            }
            return result;
        }

        private ToolStripMenuItem SearchSubItems(ToolStripMenuItem menu,string searchFor)
        {
            ToolStripMenuItem result = null;
            if (menu.Text.Equals(searchFor))
                return menu;
            else if (menu.HasDropDownItems)
            {
                foreach (ToolStripMenuItem subItem in menu.DropDownItems)
                {
                    result = SearchSubItems(subItem, searchFor);
                    if (result != null)
                        break;
                }
            }
            return result;
        }

You're awesome JerryShaw !!!! Thank you so much.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.