Solved

Yes, No, Don't ask again Dialog

Posted on 2009-05-17
20
996 Views
Last Modified: 2012-05-07
Hey guys!
I need to make a dialog that asks the user; Yes, No, and a checkbox saying Don't ask again
I need to store the checkbox result in the users profile..
Thanks,
max
0
Comment
Question by:mazzzzz
  • 10
  • 6
  • 4
20 Comments
 
LVL 2

Expert Comment

by:d1rtyw0rm
ID: 24405301
Personnaly i think that one of the best way to do this is to use the registry.

HKEY_CURRENT_USER
This key holds profile information for the user that is currently logged on. Each time a user logs on; the user's profile is copied from the HKEY_USERS key to the HKEY_CURRENT_USER key.

Hope that will help you.
strDontAskAgain = "";
            RegistryKey regKeyAppRoot = Registry.CurrentUser.CreateSubKey(strPath); 
 
            if (chkDontAskAgain.Checked == True)
                strDontAskAgain = "1";                
            else
                 strWindowsState = "0"; 
 
             regKeyAppRoot.SetValue("DontAskAgain", strDontAskAgain);
 
             return;

Open in new window

0
 
LVL 2

Expert Comment

by:d1rtyw0rm
ID: 24405306
And hm, to read it back ...
RegistryKey regKeyAppRoot = Registry.CurrentUser.CreateSubKey(strPath); 
 
strDontAskAgain = (string)regKeyAppRoot.GetValue("DontAskAgain"); 
 
return;

Open in new window

0
 
LVL 1

Author Comment

by:mazzzzz
ID: 24405311
The problem is that the program Im making is made to be portable, so I don't want to modify anything on the actual computer.
0
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
LVL 2

Expert Comment

by:d1rtyw0rm
ID: 24405341
Ok so i guess that you can log it into an xml file ?

You can get the currently logged user with :
System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();

And write it to an xml file like that ...
// New XML Element Created
XmlElement newcatalogentry = xmldoc.CreateElement("Configuration");
 
// First Element - Username - Created
XmlElement firstelement = xmldoc.CreateElement("Username");
 
// Value given for the first element
firstelement.InnerText = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();
 
// Append the newly created element as a child element
newcatalogentry.AppendChild(firstelement);
 
 
// Second Element - DontAskAgain - Created
XmlElement secondelement = xmldoc.CreateElement("DontAskAgain");
 
// Value given for the second element
secondelement.InnerText = chkDontAskAgain.Checked.ToString;
 
// Append the newly created element as a child element
newcatalogentry.AppendChild(secondelement);
 
// New XML element inserted into the document
xmldoc.DocumentElement.InsertAfter(newcatalogentry,
                                   xmldoc.DocumentElement.LastChild);
 
// An instance of FileStream class created
// The first parameter is the path to the XML file - Config.xml
 
FileStream fsxml = new FileStream(path,FileMode.Truncate,
                                  FileAccess.Write,
                                  FileShare.ReadWrite);
 
// XML Document Saved
xmldoc.Save(fsxml);

Open in new window

0
 
LVL 1

Author Comment

by:mazzzzz
ID: 24405367
Pardon me, but how exactly would that help?
0
 
LVL 11

Expert Comment

by:saragani
ID: 24405690
You can use the application settings...
In the project itself you have in the tree Properties and inside it you have Settings.
Right Click on Settings.Designer.Cs

Change the code so it will be:

        public static Settings Default {
            get {
                return defaultInstance;
            }
        }

        [global::System.Configuration.UserScopedSettingAttribute()]
        [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [global::System.Configuration.DefaultSettingValueAttribute("")]
        public bool DontAskAgain
        {
            get
            {
                return ((bool)(this["DontAskAgain"]));
            }
            set
            {
                this["DontAskAgain"] = value;
            }
        }



Now when the user changes the checkbox and click on then:


Properties.Settings.Default.DontAskAgain= Checkbox.Checked;
Properties.Settings.Default.Save();


And for checking the value of the settings, you just look at the value of  Properties.Settings.Default.DontAskAgain
0
 
LVL 11

Expert Comment

by:saragani
ID: 24406139
Well, maybe the settings isn't such a good idea since it doesn't seem like it is being saved in the program directory.
(I'm not sure where if is actually being saved... I'm not sure if its being saved in the registry or somewhere in the application data/ user profile etc...).

Maybe the best idea if your case is to save the "Dont Show Again" in an XML file, and read the value from that file whenever you need it.
0
 
LVL 2

Expert Comment

by:d1rtyw0rm
ID: 24406346
mazzzzz:Pardon me, but how exactly would that help?

That will help you to do what you want to do...i don't know what you don't understand so i will try to be more explicit.

When the user click Yes or No you save in an xml file the configuration, so the xml will look like this.

<Configuration>
 <Username>mazzzzz</Username>
 <DontAskAgain>1</DontAskAgain>
</Configuration>
<Configuration>
 <Username>d1rtyw0rm</Username>
 <DontAskAgain>1</DontAskAgain>
</Configuration>

The username is the windows user currently connected so it's similar to save it in the HKEY_CURRENT_USER.


When the user open your application back you read from the XML file to read FOR THIS USER is the DontAskAgain is 0 or 1.

Do you follow me ?

0
 
LVL 2

Expert Comment

by:d1rtyw0rm
ID: 24406430
And sargani recommendation seems to be good too, using the application setting, you can setup you application setting to be ('By Application' OR 'By User').  It's similar to my XML Fil suggestion cause the configuration file will be saved in an XML file too 'Application.Setting' in your project folder, but can be a bit easier to handle.

But i think that my solution is more flexible.

It's your choice.
0
 
LVL 1

Author Comment

by:mazzzzz
ID: 24406751
I don't think I was specific enough (sorry), I thought there was something like MessageBox that I could use, because of all the other programs that have the same functionality.
Is that not true?

I found one dll that would do it, but you could only save the check result in the registry, I wanted to save it into a variable, and later to file.  I have everything set up, except for making the dialog, and retrieving the result of the dialog.
0
 
LVL 11

Expert Comment

by:saragani
ID: 24407113
The message box that comes with .Net doesn't support that.
You will have to create a form and show it as Dialog, and you will have to read the configuration either from the settings or from an XML file.

There might be some 3rd party dlls that do that without saving the settings into registry. Frankly, you can create your own DLL for that matter, cause you know best what your needs are.

And if you were referring to that dll:
http://www.codeproject.com/KB/dialog/MessageBoxChk.aspx
then you can just edit the sources and recompile it so it will work with XML instead of registry.
0
 
LVL 1

Author Comment

by:mazzzzz
ID: 24407161
Yes, I was referring to that dll, but I have no clue how to edit the dlls, and I can't even find the dll to edit..

Um, I guess I'll just make the class from scratch, unless you guys can think of something else?
0
 
LVL 11

Expert Comment

by:saragani
ID: 24407285
What do you mean you can find the dll... there is a "Download demo project - 17.4 Kb" link at the top
You need to register code project in order to download files.
In that zip file you will find the sources of the dll
0
 
LVL 1

Author Comment

by:mazzzzz
ID: 24407345
I know that much, but there are two dll in the folder, neither seem to have the functions that the project requests..
0
 
LVL 1

Author Comment

by:mazzzzz
ID: 24407391
I worked out some of the code, and was wondering how I could pass something like "namespace.Properties.Settings.Default.path" to the dialog, aka what variable type would I use??
0
 
LVL 2

Expert Comment

by:d1rtyw0rm
ID: 24407659
By user
0
 
LVL 1

Author Comment

by:mazzzzz
ID: 24407817
I don't understand, could you write a small example??
0
 
LVL 1

Author Comment

by:mazzzzz
ID: 24437256
This is the basic code I got from your guy's code, thanks for the help!

Note:
Portable_App.Properties.Settings.Default is the namespace to make settings files through C#, they're xml files.

Save the below as a Form.
    public partial class Dialog: Form {
 
        string path_v = "";
        public string path
        {
            get { return path_v; }
            set { path_v = value; }
        }
        string text_var = "";
        public string dis_text
        {
            get { return text_var; }
            set { text_var = value; }
        }
        string title_v = "";
        public string title
        {
            get { return title_v; }
            set { title_v = value; }
        }
        string chkbox_title_v = "";
        public string chkbox_title
        {
            get { return chkbox_title_v; }
            set { chkbox_title_v = value; }
        }
    // everything else is what the Forms Designer generated for you
 
 
        public Dialog()
        {
 
            InitializeComponent();
        }
        public string Wrap(string me, uint maxLength)
        {
            string com = "";
            for (int i = 1; i < ((int)(me.Length / maxLength) + 1); i++)
            {
                if (me.Length / i > maxLength)
                {
                    string m;
                    if (maxLength * (i+1) > me.Length)
                    {
                        m = me.Substring((int)(i * maxLength), me.Length - (int)(i * maxLength));
                        return com + m + Environment.NewLine;
                    }
                    m = me.Substring((int)(i * maxLength), (int)maxLength);
                    char[] cutPossible = new char[] { ' ', ',', '.', '?', '!', ':', ';', '-', '\n', '\r', '\t' };
                    int cutIndex = m.LastIndexOfAny(cutPossible);
                    if (cutIndex > 0)
                    { com += m.Substring(0, cutIndex).Trim() + Environment.NewLine; }
                    else
                    { com += m + Environment.NewLine; }
                }
                else
                {
                    return com;
                }
            }
            return me;
        } 
        private void Dialog_Load(object sender, EventArgs e)
        {
            if (Portable_App.Properties.Settings.Default.eject_ask_again)
            {
                this.Hide();
                this.DialogResult = DialogResult.OK;
            }
 
            chkbox.Text = chkbox_title;
            this.Text = title;
            output.Text = dis_text;
 
            output.Text = Wrap(output.Text, 50);
            this.Size = new Size(output.Width + output.Location.X + 95, output.Height + output.Location.Y + 80);
            this.MinimumSize = this.Size;
            this.MaximumSize = this.Size;
        }
 
        private void chkbox_CheckedChanged(object sender, EventArgs e)
        {
            Portable_App.Properties.Settings.Default.eject_ask_again = chkbox.Checked;
        }
 
        private void Ok_Click(object sender, EventArgs e)
        {
            Portable_App.Properties.Settings.Default.Save();
        }
 
    }

Open in new window

0
 
LVL 1

Author Comment

by:mazzzzz
ID: 24437264
oh, and interface with the script with:
            Dialog di = new Dialog(); //create new dialog
            di.dis_text = "Are you sure you want to eject the drive?"; //the main text
            di.title = "Eject Drive"; //The dialog's title
            di.chkbox_title = "Do not ask again"; //the checkbox's Text value
 
            if (di.ShowDialog() == DialogResult.OK) //display the result
            {
                MessageBox.Show("OK");
            }
            else
            {
                MessageBox.Show("Cancel");
            }
            MessageBox.Show(Portable_App.Properties.Settings.Default.eject_ask_again.ToString()); //display the checkbox's state

Open in new window

0
 
LVL 1

Accepted Solution

by:
mazzzzz earned 0 total points
ID: 24437278
Crap, last but not least, the designer code..
        #region Windows Form Designer generated code
 
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.output = new System.Windows.Forms.Label();
            this.Ok = new System.Windows.Forms.Button();
            this.cancel = new System.Windows.Forms.Button();
            this.chkbox = new System.Windows.Forms.CheckBox();
            this.SuspendLayout();
            // 
            // output
            // 
            this.output.AutoSize = true;
            this.output.Font = new System.Drawing.Font("Times New Roman", 11.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.output.Location = new System.Drawing.Point(94, 23);
            this.output.Name = "output";
            this.output.Size = new System.Drawing.Size(76, 17);
            this.output.TabIndex = 0;
            this.output.Text = "To Be Text";
            // 
            // Ok
            // 
            this.Ok.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.Ok.DialogResult = System.Windows.Forms.DialogResult.OK;
            this.Ok.Location = new System.Drawing.Point(322, 131);
            this.Ok.Name = "Ok";
            this.Ok.Size = new System.Drawing.Size(75, 23);
            this.Ok.TabIndex = 1;
            this.Ok.Text = "Okay";
            this.Ok.UseVisualStyleBackColor = true;
            this.Ok.Click += new System.EventHandler(this.Ok_Click);
            // 
            // cancel
            // 
            this.cancel.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));
            this.cancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.cancel.Location = new System.Drawing.Point(241, 131);
            this.cancel.Name = "cancel";
            this.cancel.Size = new System.Drawing.Size(75, 23);
            this.cancel.TabIndex = 2;
            this.cancel.Text = "Cancel";
            this.cancel.UseVisualStyleBackColor = true;
            // 
            // chkbox
            // 
            this.chkbox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left)));
            this.chkbox.AutoSize = true;
            this.chkbox.Location = new System.Drawing.Point(33, 135);
            this.chkbox.Name = "chkbox";
            this.chkbox.Size = new System.Drawing.Size(80, 17);
            this.chkbox.TabIndex = 3;
            this.chkbox.Text = "checkBox1";
            this.chkbox.UseVisualStyleBackColor = true;
            this.chkbox.CheckedChanged += new System.EventHandler(this.chkbox_CheckedChanged);
            // 
            // Dialog
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(409, 166);
            this.Controls.Add(this.chkbox);
            this.Controls.Add(this.cancel);
            this.Controls.Add(this.Ok);
            this.Controls.Add(this.output);
            this.Name = "Dialog";
            this.Text = "Dialog";
            this.Load += new System.EventHandler(this.Dialog_Load);
            this.ResumeLayout(false);
            this.PerformLayout();
 
        }
 
        #endregion
 
        private System.Windows.Forms.Label output;
        private System.Windows.Forms.Button Ok;
        private System.Windows.Forms.Button cancel;
        private System.Windows.Forms.CheckBox chkbox;

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Simple Injector with Web Service 4 55
how to change the view engine in visual studio 2015 enterprises edition 7 108
Printing 1 60
Call Controller Action Method from ASPX 2 23
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question