Click here to Skip to main content
15,891,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello. I have a little trivial problem in my application. I have a dialog (d1) that inherits from Form. And in Main, if I call

d1.ShowDialog()


,it works fine. But calling

d1.Show()


opens a bad form with pale labels and textboxes, and when I try to drag that window form it says "Not Responding."
What's going on? I think I need "Show" and not "ShowDialog," because the user should be dealing with several simultaneous forms.

Thanks.

Here's the code for the CS file. It's basically a simple form that contains two textboxes, one for username and password. Nothing fancy at all... It still freezes with Show.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using System.Threading;
using System.Security.Cryptography.X509Certificates;

namespace MyConnection
{
    public partial class LoginDlg : Form
    {
        #region Constructors
        public LoginDlg()
        {
            InitializeComponent();
        }
        #endregion

        #region Private Fields
		String username;
		String password;
        #endregion

        #region Public Interface
		/// 
		/// Used for getting the username and password in a GUI environment.
		/// Directly modifies the username and password when the user clicks the OK button.
		/// Returns whether the user proceeds with the login process (clicks OK) or not (clicks Cancel).
		/// 
		public Boolean getUsernameAndPassword(ref String user, ref String pass)
		{
			if (ShowDialog() == DialogResult.OK)
			{
				user = username;
				pass = password;

				return true;
			}

			DialogResult = DialogResult.Cancel;
			return false;
		}
		#endregion

		#region Event Handlers
		private void OkBTN_Click(object sender, EventArgs e)
        {
            try
            {
				//String username = (String)UserNameCB.SelectedItem;
				username = (String)UserNameCB.SelectedItem;

				if (String.IsNullOrEmpty(username))
					username = UserNameCB.Text;

				//String password = PasswordTB.Text;
				password = PasswordTB.Text;

				DialogResult = DialogResult.OK;
            }
            catch (Exception exception)
            {
                GuiUtils.HandleException(this.Text, MethodBase.GetCurrentMethod(), exception);
            }
		#endregion
		}
    }
}
Posted
Updated 26-Oct-10 12:51pm
v2
Comments
William Winner 26-Oct-10 14:47pm    
if you provide a bit more code, we could possibly help you understand better what is going on. Like, what happens in the Form_Load event of d1 and what happens after d1.Show() is hit.
Software_Guy 26-Oct-10 16:23pm    
Thank you guys. I've managed to delete everything in Form_Load and start over, and so far it doesn't freeze.
Software_Guy 26-Oct-10 16:40pm    
Actually, it doesn't work. Apparently I had ShowDialog and was happy that it was working. Sorry.

[moved the code into the OP]
Software_Guy 26-Oct-10 16:43pm    
Note: It's a console application, but it only opens a couple forms.
Software_Guy 26-Oct-10 16:58pm    
I even made a short test program to figure out how "Show" works, but it's still not working, even with nothing in it.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;

namespace Test1
{
static class Program
{
///
/// The main entry point for the application.
///

[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//Application.Run(new Form1());

Form1 x1 = new Form1();
x1.Show(); // not painted for the next ten seconds
//x1.ShowDialog();

Thread.Sleep(10000); // after ten seconds the program closes


}
}
}

After you call Show(), you are doing something that blocks the UI thread, which means this child form will remain seemingly frozen too.

[Edit/Update]
----------------
Okay, it's obvious that this will not work. You call Show and then control goes to the next line where you call Thread.Sleep which is executed on the main thread (the same thread that the dialog is created and shown in). All UI needs some kind of message pump which is provided by the normal WinForms framework, but since you use a console app and do not use the Application class, you will either need to implement a message pump or use ShowDialog (which implements an internal message pump).
 
Share this answer
 
v3
Comments
Software_Guy 26-Oct-10 16:23pm    
Thank you guys. I've managed to delete everything in Form_Load and start over, and so far it doesn't freeze.
Software_Guy 26-Oct-10 16:43pm    
Actually, it still doesn't work for me. I have no idea what's killing it. I posted the code, and if you'd be willing to take a quick look at it I'd appreciate it.

Note: It's a console application, but it only opens a couple forms.
Software_Guy 26-Oct-10 18:46pm    
Thanks for the advice, Nishant. I'm really inexperienced when it comes to multi-threading programming. I should not use ShowDialog, so the "Message Pump" seems to be my only option at this point. Would you happen to have an article or sample code handy that can help me?
Nish Nishant 26-Oct-10 18:51pm    
Why don't you just create a regular WinForms app? Then you won't have to go through all these hoops.

Unlike with native applications, merely implementing a message pump may not guarantee that your app will work correctly because WinForms need lots of extra plumbing that is normally done by the Application class.

You can keep your multiple modeless dialogs design with a standard WinForms app too.
Software_Guy 26-Oct-10 19:36pm    
I can see how modeless dialogs work with the WinForms app, but won't that mean my black Command Prompt screen will go away, and all my Console.WriteLine commands will be useless?

If that's the only way, I could create a read-only textbox and dump all the print statements in it instead of printing to the console. But if there's a way to keep my command console, I'd like to use it.

Once again, thank you very much for your concern. I appreciate your help.
Alright he's an alternate way to do this. You create your modeless forms from your console app, and then call Application.Run. Here's a minimal code snippet that shows how this can be done.

C#
namespace Test
{
    class Form1 : Form
    {
        public Form1()
        {
            Text = "This is form1";
        }
    }

    class Form2 : Form
    {
        public Form2()
        {
            Text = "This is form2";
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            new Form1().Show();
            new Form2().Show();
            Application.Run();
        }

    }
}
 
Share this answer
 
Comments
Software_Guy 26-Oct-10 19:37pm    
Yep. Works flawlessly for Win apps. Thank you!
Nish Nishant 26-Oct-10 19:38pm    
You are welcome.

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