Click here to Skip to main content
15,881,812 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a custom dialog, and I want to close it after a few seconds.
I am showing this dialog when the user doesn't enter anything in 2 textboxes I have.
I don't know how to exit the dialog after 1 second.
Please help.

What I have tried:

I tried pressing enter programatically, but it only executes the validation and makes the dialog appear once again after I have closed it.
I also tried this inside the class that I am using to create the custom dialog:
dialog.Close();
dialog.Dispose();

inside the class the has the code for the custom dialog, and
DialogResult.OK;

But this only hangs my application.
Can someone please help me?
Thanks.
Posted
Updated 10-Dec-17 11:40am

1 solution

The dialog has to close itself. Put a timer on the dialog form and set it for whatever your timeout is. In the dialog code, handle the Timer Tick event and close the dialog from there.
 
Share this answer
 
Comments
Member 10850253 10-Dec-17 17:29pm    
But How?
Here is my dialog class code:
public partial class BetterDialog : Form
    {
        /// <summary>
        /// Create a special dialog in the style of Windows XP or Vista. A dialog has a custom icon, an optional large
        /// title in the form, body text, window text, and one or two custom-labeled buttons.
        /// </summary>
        /// <param name="titleString">This string will be displayed in the system window frame.</param>
        /// <param name="bigString">This is the first string to appear in the dialog. It will be most prominent.</param>
        /// <param name="smallString">This string appears either under the big string, or is null, which means it is
        /// not displayed at all.</param>
        /// <param name="leftButton">This is the left button, typically the "accept" button--label it with an
        /// action verb (or "OK").</param>
        /// <param name="rightButton">The right button--typically "Cancel", but could be "No".</param>
        /// <param name="iconSet">An image to be displayed on the left side of the dialog. Should be 32 x 32 pixels.</param>
        static public DialogResult ShowDialog(string title, string largeHeading, string smallExplanation,string leftButton, string rightButton, Image iconSet)
        {
            //title, largeHeading,smallExplanation, leftButton, rightButton, iconSet
            using (BetterDialog dialog = new BetterDialog(title, largeHeading, smallExplanation, leftButton,rightButton, iconSet))
            {
                DialogResult result = dialog.ShowDialog();                
                return result;                               
            }
        }

        /// <summary>
        /// The private constructor. This is only called by the static method ShowDialog.
        /// </summary>
        private BetterDialog(string title, string largeHeading, string smallExplanation,string leftButton, string rightButton, Image iconSet)
        {
            this.Font = SystemFonts.MessageBoxFont;
            this.ForeColor = SystemColors.WindowText;

            InitializeComponent();

            // set our width and height to these values (redundant, but who cares?)
            this.Width = 350;
            this.Height = 150;

            using (Graphics graphics = this.CreateGraphics())
            {
                SizeF smallSize;
                SizeF bigSize;

                if (string.IsNullOrEmpty(smallExplanation) == false)
                {
                    if (SystemFonts.MessageBoxFont.FontFamily.Name == "Segoe UI")
                    {
                        // use the special, windows-vista font style if we are running vista (using Segoe UI).
                        label1.ForeColor = Color.FromArgb(0, 51, 153); // [ColorTranslator.FromHtml("#003399")]
                        label1.Font = new Font("Segoe UI", 12.0f, FontStyle.Regular, GraphicsUnit.Point);

                        // bigger for vista/aero
                        this.Width += 50;

                        label1.Width += 50;
                        label2.Width += 50;

                        smallSize = graphics.MeasureString(smallExplanation, this.Font, this.label2.Width);
                        bigSize = graphics.MeasureString(largeHeading, label1.Font, this.label1.Width);

                        this.Height = (int)smallSize.Height + 158;

                        // add in a little margin on the top as well
                        pictureBox1.Margin = new Padding(pictureBox1.Margin.Left, pictureBox1.Margin.Top + 6,
                            pictureBox1.Margin.Right, pictureBox1.Margin.Bottom);
                    }
                    else
                    {
                        // might want to special case the old, MS Sans Serif font.
                        // use the regular
Dave Kreskowiak 10-Dec-17 17:43pm    
    Close();
Member 10850253 10-Dec-17 17:49pm    
I used this, and nothing happens:
using (BetterDialog dialog = new BetterDialog(title, largeHeading, smallExplanation, leftButton,rightButton, iconSet))
            {
                DialogResult result = dialog.ShowDialog();
                Thread.Sleep(1000);
                dialog.Close();
                return result;                               
            }
Dave Kreskowiak 10-Dec-17 18:04pm    
Go back and re-read what I wrote. The timer code goes in the DIALOG code, not your form code that instantiates the dialog.
Member 10850253 10-Dec-17 18:11pm    
I did as you suggested, but nothing happens.
<pre>private void timer1_Tick(object sender, EventArgs e)
        {
            Close();
        }

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900