Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 192
  • Last Modified:

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

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
John500
Asked:
John500
  • 6
  • 5
  • 3
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
John500Author Commented:
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
 
Tony McCreathTechnical SEO ConsultantCommented:
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Mike TomlinsonMiddle School Assistant TeacherCommented:
For your "OK" button, just close the dialog:

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
0
 
John500Author Commented:
... 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
 
Tony McCreathTechnical SEO ConsultantCommented:
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
 
John500Author Commented:
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
 
Tony McCreathTechnical SEO ConsultantCommented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
John500Author Commented:
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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Other windows in the ENTIRE Operating System...Yes.  This Modal to just the Application.
0
 
John500Author Commented:
I need it to be modal for the entire operating system.

Thanks
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
We'll good luck with that...there is a reason we don't have an easy way to make system modals!
0
 
John500Author Commented:
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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 6
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now