Solved

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

Posted on 2008-10-23
14
189 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

636 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