Wait till winform loads before performing next action? - winforms

Winform application contains one form with a reportViewer control. When the form is initialized the report is generated but when I try to programmatically run a PrintDialog on the reportviewer I get a 'Operations is not valid due to the current state of the object' error message.
When I comment out the PrintDialog line the report form shows fine. I think the problem is a lag as it generates the form/report. Is there a way to wait until the form loads before launching the PrintDialog?
Code excerpt:
this.reportViewer1.RefreshReport();
this.reportViewer1.PrintDialog();
UPDATE
Solution is (as suggested):
private void form_load(...)
{
createReport;
this.reportViewer1.RefreshReport();
}
private void reportViewer1_RenderingComplete(...)
{
this.reportViewer1.PrintDialog();
}

This article suggestions you can't/shouldn't call PrintDialog until the RenderingComplete event fires:
http://msdn.microsoft.com/en-us/library/microsoft.reporting.winforms.reportviewer.renderingcomplete(v=vs.80).aspx
http://social.msdn.microsoft.com/Forums/en-US/vsreportcontrols/thread/a8993f3e-7787-4e0a-b32f-fcfbf8df8001/

Related

WPF MessageBox in App.xaml.cs stops MainWindow initialisation so app never appears but is (apparently) running correctly

There's no error message and no indication why it is not displaying the window. The app initialises App.xaml.cs: App() {} and I can step through the App.xaml file. It gets the startup uri and then... silence. No output in the Output window and no unhandled exception and no window, I can't find where to put a breakpoint to debug as it isn't hitting the start of MainWindow.xaml.cs.
Really confused.
This was working 20m ago.
In that time all I did was add Windows.Office.Interop.Outlook reference. I removed the reference and rebuilt but still the same. Would that cause this problem? Has anyone seen this before? Google isn't helping!
EDIT :
App.xaml.cs:
public App()
{
using (var dbContext = new DBEntities())
{
if (!db.Exists())
{
try
{
db.Database.Create();
MessageBox.Show("Database created"); // this is the problem!!
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
I've added App.xaml.cs, I found that the problem was using a MessageBox to give info (this is still in development!). I'd been meaning to get rid of it and eventually did and my problem went away. This meant I could find relevent Goolge results:
MSDN query and answer for exactly my problem
I will be adding an 'loading window' in between app load and main window load eventually in which I will be able to feedback information using Bindings etc.
Fixed error by removing the MessageBox.Show(..) call. The selected answer from the MSDN URL given in the question states:
"I performed a test based on your description, the applicationi stop at the method : USER32!GetMessageW+0x33, calling USER32!NtUserGetMessage"
I assume this is what was occurring in my case, although I didn't test it.
What happens if you create a new window and set that as the StartupUri?
You also might want to create a new project and make sure that the namespaces referenced in the App.xaml in your existing app haven't somehow been inadvertently edited.

How to check if a page hosted in a Web Browser Control is already rendered?

Hy People!. I'm here again asking for your help. I have a web browser control in a Wpf App. Into the event Load of the mainwindows I set up the control's source:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
webBrowser1.Source = new Uri(ConnectionString);
webBrowser1.Navigate(ConnectionString);
}
I'm redirected to the following url: https://login.live.com/login.srf?wa=wsignin1.0&wtrealm=urn%3acrm%3adynamics.com&wctx=rm%3d1%26id%3de513a320-df72-4de8-bce8-b1f918dc4eff%26ru%3dhttps%253a%252f%252fwebfortis38.crm.dynamics.com%252fdefault.aspx&wct=2011-09-06T14%3a49%3a38Z
At this point I must Sign in with my Windows Live Id. To do that I look for the Input controls in order to fill them up with my username and my Pass, and then the button submit in order to calls the event Click():
HTMLDocument mdoc = (HTMLDocument)webBrowser1.Document;
IHTMLElement usern = mdoc.getElementById("i0116");
IHTMLElement dom = mdoc.getElementById("i0118");
IHTMLElement btl = mdoc.getElementById("idSIButton9");
if (usern != null && dom != null && btl != null)
{
// pass authentication
usern.setAttribute("value", UserName);
dom.setAttribute("value", password);
btl.click();
IsRendered=true;
}
HERE IS THE PROBLEM!. If the page is not already rendered, the procedure getElementById returns Null!!.
Is there any way to know when the page is completely rendered?
Thanks in advance!
Start a new thread from DocumentCompleted,
from the new thread's entry point,
sleep for 500ms, (or however long is safe to allow complete JavaScript initialization)
invoke to the webbrowser's owning thread,
now work with the computed document
You can use Navigated event of webBrowser1.
Hy, a could solve the problem. What I've done is to create a new thread. Into this new thread I've inserted my code into a DO-WHILE loop and with a thread.abort() into the IF loop. Thanks for the help

Windows forms problem

when I create an application with a form X I use: X->Show(); The application terminates instantly. So I use the X->ShowDialog(); method. Now the UI stops to execute anything after that line. Message boxes will only be shown after I closed the form X, updates and textbox changes won't result in anything...??? How to get rid of this problem? I only want to show a form and change some content of it by user interactions and the user should close it(not the program)...shouldn't it be the easiest thing all over the world when I'm programming Windows programs for Windows with Windows forms? LOL!
int main(array<System::String ^> ^args)
{
Application::EnableVisualStyles();
Application::SetCompatibleTextRenderingDefault(false);
Form1^ X = gcnew Form1();
X->ShowDialog();
MessageBox::Show("test", "Warning", MessageBoxButtons::OK);
// message box not shown, only after closing the form...
return 0;
}
Not sure about c++-cli right now but in C# the main form is started and shown in this way:
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
so I can be wrong here, but where is your Application.Run ?
What you're trying to do is illogical. You can either ShowDialog() which keeps your program running until the form is closed, or you can keep going through the program and exit immediately. Where do you expect your program to pause? And when do you expect it to close?
The simplest 'solution' to get both on the screen is to reverse the order to:
MessageBox::Show("test", "Warning", MessageBoxButtons::OK);
X->ShowDialog();
then you'll get both on screen. Otherwise run the MessageBox from within the form (in the constructor, OnLoad, wherever).

Control.IsAccessible

I need to check if a c# WinForm Window (FORM Class) has been initialized and waiting for user events. But I could not find out how to manage that.
Therefore I had the idea to set the Control.IsAccessible Flag of the Form to true, within the OnLoad Event of the Windows Form.
My question is now, what is the Control.IsAccessible Flag origin intended for? Or is there an other solution to check if the Winform is initialized.
Thanks for your help
I do not know what IsAccessible is intended for but for the check you are doing you want Created
if(myForm.Created)
{
//Do stuff
}
I had a whole bunch of problems with it, here is one of my old question on SO that helped me out a lot with it.
Control.IsAccessible just means the control is visible to accessibility applications.
You can check myForm.Created to see if the window exists.
You can also register an event handler for the Application.Idle event, which occurs when the application has finished initializing and is ready to begin processing windows messages.
Here is a common usage:
public int Main(string[] args)
{
Application.Idle += WaitUntilInitialized;
}
private void WaitUntilInitialized(object source, EventArgs e)
{
// Avoid processing this method twice
Application.Idle -= WaitUntilInitialized;
// At this point, the UI is visible and waiting for user input.
// Begin work here.
}

Silverlight 3: No change but now I am getting "Page Not Found"

Had a page that was working fine. Only change I made was to add a datagrid to the page (which also added the xmlns) and all of the sudden I'm getting Page Not Found. Checked the UriMappings. Tried the default nav link. No joy.
Ideas?
UPDATE: The answer was that I had a mock class that was not initializing a collection. See Byrant's answer for a way to save yourself some time.
To see what the issue is you need to make one change to your MainPage.xaml.cs:
// If an error occurs during navigation, show an error window
private void ContentFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
Exception ex = e.Exception;
while (ex.InnerException != null)
{
ex = ex.InnerException;
}
e.Handled = true;
ChildWindow errorWin = new ErrorWindow(ex);
errorWin.Show();
}
Once you've made that change when you start the application you should see the exception instead of the page where the exception occurred.
Firebug is always a good friend to see which kind of requests are called ...
Often the devServer is not closed properly , have a look at the taskbar

Resources