Solved

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

Posted on 2008-10-23
14
186 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
  • 6
  • 5
  • 3
14 Comments
 
LVL 85

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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
LVL 85

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 85

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 85

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 85

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

713 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