|
MessageBox is a Modal form - you know that because you called it that when you wrote the title.
And a modal form prevents anything else going on in the thread until the form has been dismissed, usually by pressing the OK or Cancel buttons.
Because Form1 and SecondaryForm are both running on the same thread (the UI thread, as they have to be) the use of a Modal form in either of them will automatically prevent the other from doing anything.
You can't "get round" that, you will have to write your own MessageBox form which is not modal, and display your messages in that.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
MessageBox is a modal dialog. This means that you cannot interact with the running application until you close it.
This space for rent
|
|
|
|
|
Hi,
As others have said, when a modal window is shown, a new message pump is launched and the previous one is temporarily disabled, so you can't interact with any other open window in your program for as long as the modal window is open.
However, you may drastically improve on the situation with a buried mainform by only using the MessageBox.Show() overloads that have an explicit owner as their first parameter (example[^]), where this would be the suggested value. Doing so the calling Form becomes the owner of the modal dialog (it isn't by default!) resulting in a more solidary behavior of caller and callee.
modified 16-Jan-19 9:47am.
|
|
|
|
|
Hi, Luc,
It's a great suggestion, however, I think the OP is already doing that:Quote: If there is some kind of error, then the main form will show a modal error MessageBox, which has the main form as its parent. Of course, there;s some critical information we don't have, like: where the error occurs.
«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
|
|
|
|
|
Hi Bill,
I think you're right.
And anyways ownership doesn't really solve the problem when other applications add windows to the desktop...
|
|
|
|
|
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;
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
|
|
|
|
|
|
I have a large app that has been working for a while. I'm adding a new service class to it that is loaded as a MEF plugin. The service class loads some plugins that are NOT MEF plugins. I'm using relfection for those assemblies.
I'm getting this runtime exception on startup:
Cannot cast the underlying exported value of type
'Microsoft.Practices.Prism.MefExtensions.Regions.Behaviors.MefDelayedRegionCreationBehavior (ContractName="Microsoft.Practices.Prism.Regions.Behaviors.DelayedRegionCreationBehavior")'
to type
'Microsoft.Practices.Prism.Regions.Behaviors.DelayedRegionCreationBehavior'.
I can step through all of my code and everything runs, but after my class is contructed I get the error. If I comment out the part of my class that loads my assemblies, everything works fine. My assemblies don't error at all, and I can't see how externally loaded assemblies could cause some MEF problem.
I don't know MEF that well. I've Googled this a few different ways with no luck. Anyone know what this means?
If it's not broken, fix it until it is.
Everything makes sense in someone's mind.
Ya can't fix stupid.
|
|
|
|
|
I don't know a blessed thing about Prism, but it looks like the error that I get when trying to import an object that does not construct properly.
Are you trying to import a Prism region that might have a configuration issue, or might be influenced by an environmental factor?
"Never attribute to malice that which can be explained by stupidity."
- Hanlon's Razor
|
|
|
|
|
Hello,
Once I try to show up Access DB (.Accdb) in DataGridView, resolution of the form changes.
what is solution for this issue ? Thanks.
modified 13-Feb-19 21:02pm.
|
|
|
|
|
This isn't a good question. Just using an Access database in your app isn't going to "change the resolution" of anything.
We have no idea how your code is written, what the exact code at the point of failure looks like, or anything else about your code so it's impossible to tell you anything useful.
You're going to have to describe exactly what happens. Does the size of the entire form change? Is it just a control on the form, like the DataGridView? You're also going to have to use the debugger narrow down the exact method in which this "change" happens. Is it when the data is retrieved from the database? Is it when the DGV is bound to the new data? When?
|
|
|
|
|
Thanks for advice. Codes are below :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace datagrid
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.kolonTableAdapter.Fill(this.stokDataSet.kolon);
}
}
}
I also see .accdb and .dataset.xsd file are added to project. Yes entire form size changes once I start the app. No error returns from debugger. please let me know if more to clarify.
modified 13-Feb-19 21:02pm.
|
|
|
|
|
OK, so you're using the designers to do database work. That's not a good idea, despite what Microsoft says. The designers hide a lot of the code and workings, basically teaching you nothing about how to do this stuff.
What I can say is none of that code is going to change the size of either controls or forms, so the problem is somewhere else. What properties did you change on the Form and the DGV. The properties that have been changed from default will be shown in bold text in the Properties window when you click on the Form or the DGV in the designer.
|
|
|
|
|
Dave is exactly right: "show up Access" in a DataGridView will not change the resolution of a form: nothing can change the resolution of a form independently of the system on which it is running!
What you probably mean is that something changes size; maybe your form gets bigger or smaller, we don't know. But we have no idea what you did to do that: we can't see your screen, access your HDD, or read your mind; we only get exactly what you type to work with.
Have a read here: Why Won't You Answer My Question?[^] and try asking again - we'd like to help, but you have to help us to help you!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I'm googled out and I haven't yet found out how to get the subnet mask for any connected network in the .NET framework.
Does anyone know? Or can point me in the right direction?
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
Hi Richard,
I haven't done this, however maybe this[^] may help.
I found this link and others googling for C# internet submask ; I always start my search with C# when looking for an API.
I also looked into WMI , the only class I found holding submask info is Win32_IP4RouteTable , but it looks all but simple to use that.
|
|
|
|
|
Thank you Luc!
The difficult we do right away...
...the impossible takes slightly longer.
|
|
|
|
|
i want from my application to import data from text file or csv file or excle file by clik one button how do this by c# code
|
|
|
|
|
|
Member 13588700 wrote: i want from my application to import data from text file or csv file or excle file by clik one button how do this by c# code
Well, it always, and it still is a very good idea to first search MSDN for the problem before posting a question to the Forum.
|
|
|
|
|
For CSV, I use this: A Fast CSV Reader[^] which works really well.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hello, I have good resources and keyword here:
Articles how to start
Let me know what is destination of the data? Is it Ms SQL Server, or what?
As far as I know You can use SSIS to import data from excel, csv, etc.
And I have some experience create it from scratch and the system goes well so far.
Regards
Toha
|
|
|
|
|
In my Winform App, I'm sending an email with an attachment and I get the above error. Kind of stumped on this and I know it' attachment related.
So I get this on the first run after starting the program.
I generate a PDF, write it to AppData folder, then program the Smtp Client, run the code below.
I tried ..
Comment out the attachment and it works fine.
Tried not using "using"
Tried fileStream.Position = 0
Tried fileStream.FlushAsync = 0
Searched for a quick fix but never really found anything.
I'm using CeTe Dynamic PDF to generate the PDF with Document.Draw
if (message.FilePath != null )
{
using (var fileStream = File.OpenRead(message.FilePath))
{
var attach = new Attachment(fileStream, message.FilePath);
mailMessage.Attachments.Add(attach);
await fileStream.FlushAsync();
}
}
using (var smtpClient = SendEngineAsync.Create_SMTPClient(smtpC))
{
var result = await SendEngineAsync.SendEmailAsync(smtpClient, mailMessage, 5);
SendEngineAsync.ProcessSendResult(resend, result);
}
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|
Well, since the attachment will always be there, I changed the code to this.
It works, but I need to test it multiple sends to see if it happens again down the road.
using (var fileStream = File.OpenRead(message.FilePath))
{
var document = new Attachment(fileStream, message.FilePath);
mailMessage.Attachments.Add(document);
using (var smtpClient = SendEngineAsync.Create_SMTPClient(smtpC))
{
var result = await SendEngineAsync.SendEmailAsync(smtpClient, mailMessage, 5);
SendEngineAsync.ProcessSendResult(resend, result);
}
}
If it ain't broke don't fix it
Discover my world at jkirkerx.com
|
|
|
|
|