Solved

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

Posted on 2008-10-23
14
185 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:
Tiggerito 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 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:Tiggerito
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:Tiggerito
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
converto datetime always changes format 9 35
how to check to see if datatable has headers or not 2 27
Hey!!!! 1 20
Get number of Files in Directory and Sub Directories 2 40
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

776 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