Click here to Skip to main content
15,888,113 members
Home / Discussions / C#
   

C#

 
AnswerRe: How can my "secondary form" still be enabled when a modal MessageBox is shown from the "main form"? Pin
Mc_Topaz16-Jan-19 1:16
Mc_Topaz16-Jan-19 1:16 
GeneralRe: How can my "secondary form" still be enabled when a modal MessageBox is shown from the "main form"? Pin
arnold_w16-Jan-19 1:31
arnold_w16-Jan-19 1:31 
AnswerRe: How can my "secondary form" still be enabled when a modal MessageBox is shown from the "main form"? Pin
OriginalGriff16-Jan-19 2:49
mveOriginalGriff16-Jan-19 2:49 
GeneralRe: How can my "secondary form" still be enabled when a modal MessageBox is shown from the "main form"? Pin
Pete O'Hanlon16-Jan-19 2:50
mvePete O'Hanlon16-Jan-19 2:50 
AnswerRe: How can my "secondary form" still be enabled when a modal MessageBox is shown from the "main form"? Pin
Luc Pattyn16-Jan-19 3:15
sitebuilderLuc Pattyn16-Jan-19 3:15 
GeneralRe: How can my "secondary form" still be enabled when a modal MessageBox is shown from the "main form"? Pin
BillWoodruff16-Jan-19 21:43
professionalBillWoodruff16-Jan-19 21:43 
GeneralRe: How can my "secondary form" still be enabled when a modal MessageBox is shown from the "main form"? Pin
Luc Pattyn22-Jan-19 7:29
sitebuilderLuc Pattyn22-Jan-19 7:29 
AnswerRe: How can my "secondary form" still be enabled when a modal MessageBox is shown from the "main form"? Pin
BillWoodruff16-Jan-19 22:42
professionalBillWoodruff16-Jan-19 22:42 
imho, the main issue here is your overall strategy for error handling: in general, I think errors should be handled modally, stopping execution. imho, you should never "swallow" errors (handle errors without throwing) by just using a try/catch to "skip" over them.

However. for specific situations, like file and file-stream business, you may want to notify the user without stopping program execution, and not throw an error modally.

If I wanted to achieve what you describe, I would:

1. in the second form put a public Action method (delegate) that can be defined the main form:
using System;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class SecondaryForm : Form
    {
        public SecondaryForm()
        {
            InitializeComponent();
        }

        public Action<string, string, DateTime> SendErrorMessage;

        // create an error
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                throw new ArgumentException("some error");

            }
            catch (Exception ex)
            {
                SendErrorMessage?.Invoke(ex.Message, ex.Source, DateTime.Now);
            }
        }
    }
}
2. in the MainForm:
using System;
using System.ComponentModel;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private SecondaryForm secondaryForm = new SecondaryForm();
        private ErrorReportForm errForm = new ErrorReportForm();

        private void Form1_Load(object sender, EventArgs e)
        {
            errForm.Owner = this;
            secondaryForm.SendErrorMessage = SendErrorMessage;
            errForm.Closing += errFormOnClosing;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            secondaryForm?.Show();
        }

        private void SendErrorMessage(string err, string src, DateTime dtime)
        {
            this.BringToFront();
            errForm.ShowErrForm(err, src, dtime);
        }

        private void errFormOnClosing(object sender, CancelEventArgs e)
        {
            secondaryForm.BringToFront();
        }
    }
}
3. something like this in an error report form with four textboxes:
using System;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class ErrorReportForm : Form
    {
        public ErrorReportForm()
        {
            InitializeComponent();
        }

        public void ShowErrForm(string err = "", string src = "", DateTime dt = default(DateTime))
        {
            tbxErr.Text = err;
            tbxSrc.Text = src;

            tbxDate.Text = dt.ToLongDateString();
            tbxTime.Text = dt.ToLongTimeString();

            this.Show();
        }
    }
}
Of course, you want to consider what to do when/if the secondary form is closed.
«Where is the Life we have lost in living? Where is the wisdom we have lost in knowledge? Where is the knowledge we have lost in information?» T. S. Elliot

GeneralRe: How can my "secondary form" still be enabled when a modal MessageBox is shown from the "main form"? Pin
arnold_w23-Jan-19 21:33
arnold_w23-Jan-19 21:33 
QuestionMEF Exception Pin
Kevin Marois15-Jan-19 7:45
professionalKevin Marois15-Jan-19 7:45 
AnswerRe: MEF Exception Pin
Nathan Minier16-Jan-19 1:30
professionalNathan Minier16-Jan-19 1:30 
QuestionAccess Db in DataGridView Pin
User 1367511413-Jan-19 7:27
User 1367511413-Jan-19 7:27 
AnswerRe: Access Db in DataGridView Pin
Dave Kreskowiak13-Jan-19 12:03
mveDave Kreskowiak13-Jan-19 12:03 
GeneralRe: Access Db in DataGridView Pin
User 1367511413-Jan-19 22:24
User 1367511413-Jan-19 22:24 
GeneralRe: Access Db in DataGridView Pin
Dave Kreskowiak14-Jan-19 2:46
mveDave Kreskowiak14-Jan-19 2:46 
AnswerRe: Access Db in DataGridView Pin
OriginalGriff13-Jan-19 22:15
mveOriginalGriff13-Jan-19 22:15 
QuestionFind Subnet Mask Pin
Richard Andrew x6412-Jan-19 8:50
professionalRichard Andrew x6412-Jan-19 8:50 
AnswerRe: Find Subnet Mask Pin
Luc Pattyn12-Jan-19 16:35
sitebuilderLuc Pattyn12-Jan-19 16:35 
GeneralRe: Find Subnet Mask Pin
Richard Andrew x6413-Jan-19 4:22
professionalRichard Andrew x6413-Jan-19 4:22 
Questionimport data Pin
Member 1358870012-Jan-19 3:15
Member 1358870012-Jan-19 3:15 
AnswerRe: import data Pin
Richard MacCutchan12-Jan-19 3:37
mveRichard MacCutchan12-Jan-19 3:37 
AnswerRe: import data Pin
Victor Nijegorodov12-Jan-19 5:01
Victor Nijegorodov12-Jan-19 5:01 
AnswerRe: import data Pin
OriginalGriff12-Jan-19 5:30
mveOriginalGriff12-Jan-19 5:30 
AnswerRe: import data Pin
mtoha13-Jan-19 17:44
professionalmtoha13-Jan-19 17:44 
QuestionSmtpclient "One of the streams has already been used and can't be reset to the origin." Pin
jkirkerx10-Jan-19 11:49
professionaljkirkerx10-Jan-19 11:49 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.