Solved

Yes, No, Don't ask again Dialog

Posted on 2009-05-17
20
991 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
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

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

810 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