Solved

Correct way to call a modal Form as a replacement to a message box

Posted on 2008-10-23
14
187 Views
Last Modified: 2013-12-17
Greetings:

I have an application that uses a message box like this:

                MessageBox.Show("The session has been closed :\r\n    - By " + body + "\r\n    - At " + DateTime.Now + "\r\n\r\n Click OK to continue", "Session remotely closed",
                                MessageBoxButtons.OK, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button1);


If the name of my replacement Form is SessionClosed.cs, how should this be called as a *modal* dialog to replace the current method:


 private void CloseSession(string body, bool sql)
        {


            if (!s_.Closed)
            {
             MessageBox.Show ( ..)  // old method

            SessionClosed  // method here

}

I need to be sure how to create the object instance and make it modal.

Thanks

0
Comment
Question by:John500
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
14 Comments
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22791245
Something like:
private void CloseSession(string body, bool sql)
{
    if (!s_.Closed)
    {
        SessionClosed sc = new SessionClosed();
        // setup "sc" somehow...
        // sc.xxx = yy;
        sc.ShowDialog(); // show it "modal"
    }
}

Open in new window

0
 

Author Comment

by:John500
ID: 22795843
Thanks for the help!

I placed a label on the form to display the message:

"The session has been closed :\r\n    - By " + body + "\r\n    - At " + DateTime.Now + "\r\n\r\n Click OK to continue"

However, I can't make reference to the label from the method we have been referring to:

private void CloseSession(string body, bool slq)
{
     if (!s_.Closed)
    {
        SessionClosed sc = new SessionClosed();
        // setup "sc" somehow...
        sc.label1.text = "The session has been closed :\r\n    - By " + body + "\r\n    - At " + DateTime.Now + "\r\n\r\n Click OK to continue"
       ...
}

The only setup I need to handle is adding text to the label and then closing the Form after the user selects 'OK'

So then, if you consider that the Form is located in the following namespace:

using CCControls.SessionStatus;

How would I assign text to the label.  Also, I guess I need a Load_Form() and Close_Form() event.  Can you help me with that?

Thanks!
0
 
LVL 23

Accepted Solution

by:
Tony McCreath earned 450 total points
ID: 22795948
Probably your label1 is private or protected. Good procatice is to create a property in your SessionClosed Form to access the labels text. e.g.

public string Message
{
  set {
    sc.label1.text = value;
  }
}

Then in your dialog using code...

sc.Message = "The session has been closed :\r\n    - By " + body + "\r\n    - At " + DateTime.Now + "\r\n\r\n Click OK to continue"
sc.ShowDialog();

I'd also recomend you name your dialog so its more obvious. e.g. SessionClosedDialog, SessionClosedMessageBox, SessionClosedForm...
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22796655
For your "OK" button, just close the dialog:

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
0
 

Author Comment

by:John500
ID: 22796915
... still isn't able to be referenced.  The namespace CCControls is from a project that when built is a dll.  This dll is called by the same executable that currently launches a MessageBox to inform the user.  I tried adding the Form object like this:

CCControls     // dll project name
          Status                              //  sub folder
                 Status.cs                  //   Form
                 WStatus.cs               //   Form
                 SessionClosed.cs     //    *my additional Form*


For the code in the two Forms above mine - it look like this:


namespace CCControls.Status
{
    public partial class WStatus : Form
    {
     ...
     ...
}

namespace CCControls.Status
{
    public partial class WStatus : CCControl
    {

     ...
     ...
}

My new addition looks like this:

namespace CCControls.Status
{
    public partial class SessionClosed : Form
    {
        public string Message
        {
            set
            {
                label1.Text = value;
            }
        }
        public SessionClosed()
        {
            InitializeComponent();
        }
    }
}

I was thinking I could simply make my Form part of the executable's project and forget about the dll but I figured it would help me to understand the relationship if  I could create my own.  

I also didn't know if the Form would present a problem if I launched it outside CCControls.

Thanks again.
0
 
LVL 23

Expert Comment

by:Tony McCreath
ID: 22797128
That looks fine to me. If you can create a SessionClosed object then you should be able to access its Message property.

I presume it compiles and there is the generated partial class that defines the forms layout and controls such as label1.
0
 

Author Comment

by:John500
ID: 22797699
It compiles as long as I don't include the label.  You can see by the picture below the label just doesn't appear as an option.

Also, if I force the statement like this:

sc.label1.message = "The session has been closed... ";

I get the following error:

Error 'SessionStatus.SessionClosed' does not contain a definition for 'label1'      C:\Projects\SessionStatus\MainInit.cs      133      20      SessionStatus

Where SessionStatus is the local executable/project that calls the dll objects.  For instance, this is how the SesstionStatus Program.cs looks in order to launch the dll thread:

namespace SessionStatus
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static int Main(string[] args)
        {            
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                MainInit minit = new MainInit();                                       // LOCAL PROJECT .cs FILE

                Application.Run(CCControls.Status.WStatus.Instance);   // CALL TO DLL HERE
                return minit.ReleaseSteps();
            }
            return 0;
        }      
    }
}

What do you think?

session-close.JPG
0
 
LVL 23

Expert Comment

by:Tony McCreath
ID: 22797889
Could you show the code that defines label1?  

As I said earlier, this is probably a private field so you can't access or see it outside the class.

Creating the Message property would provide you the way to access its text.

You've also quote the code I gave incorrectly. After creating the Message property you do this...

sc.Message = "The session has been closed...";

not

sc.label1.message = "The session has been closed... ";



0
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 50 total points
ID: 22798071
If you want to access Label1 DIRECTLY via "sc" then you have to change the Modifiers() Property for it to Public.  Select the Label and then

Tiggerito has given you the "proper" way of doing it by creating a Public Property instead of accessing the Label directly.
Modifiers.jpg
0
 

Author Comment

by:John500
ID: 22799275
My bad - I kept thinking 'label'.  This worked:

sc.Message = "The session has been closed...";

However, the dialog/form is not modal.  I am able to select other windows when this launches.

Can you help me out there?

Thanks!!
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22799320
Other windows in the ENTIRE Operating System...Yes.  This Modal to just the Application.
0
 

Author Comment

by:John500
ID: 22799477
I need it to be modal for the entire operating system.

Thanks
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 22799537
We'll good luck with that...there is a reason we don't have an easy way to make system modals!
0
 

Author Closing Comment

by:John500
ID: 31509448
Thanks both of you.  The modal dialog is definitely possible - my current application does it with a different window.  I just haven't figured out how it does it............
0

Featured Post

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

710 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