WPF form should resize to show its content - c#

When WPF window appear first time, its content seem frozen. To refresh content I need to resize form, then it will be fixed. Or I hit the TAB then find a listbox -it's not visible- and click it and viola! Form updates its content again.
What do you think? Weird huh? Thanks in advance!
Edit:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Show();
while (!AppMain.needClose)
{
System.Windows.Forms.Application.DoEvents();
DoThings();
}
}

Resizing the window would force the internals to invalidate and re-paint. You could try invalidating the form when it's loaded to force it to do the same:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
this.Show();
this.Invalidate();
while (!AppMain.needClose)
{
System.Windows.Forms.Application.DoEvents();
DoThings();
}
}
Unless you're doing some sort of custom message pumping though, the standard forms message pump should do that while loop for you. You might well find that because you're intercepting the window loaded event you're stopping initialisation from completing.
Calling DoEvents is a bad code smell in my experience. If you need to do something periodically, it's better to trigger it from a timer of some sort.

When your window is loaded it's already shown, why are you calling this.show() again??

Related

C# User Control Leave Event

I am very new with the C# UserControl. I have problems with the event Leave. This is my situation: I would like to go from usercontrolA to userControlB. Before going to userControlB, usercontrolA_Leave event is called.
private void usercontrolA_Leave(object sender, EventArgs e)
{
MessageBox.Show("you are leaving.....");
}
After MessageBox is shown, the program will not proceed to my userControlB. HOWEVER when there is no MessageBox in the code, the program can proceed to userControlB.
private void usercontrolA_Leave(object sender, EventArgs e){//anything but MessageBox}
In my case, I need MessageBox.
I need MessageBox(or other thing) for me to decide whether staying or leaving.. .
msdn Control.Leave Event
I heard about setting set focusor lost focus. Is that possible to use this?
I hope you guys could understand what I have written. Thank you in advance.. :)
You will not be able to preserve mouse movements once a MessageBox is created. This is a modal box that comes up on top of the existing window and takes the focus away from the current form and interrupts the mouse.
Consider an option that does not interrupt the mouse, such as writing out to a textbox. Create a TextBox will the multi-line and scrollbar options enabled. Then write to it.
private void usercontrolA_Leave(object sender, EventArgs e)
{
textBox1.Append("you are leaving A...\r\n");
}
private void usercontrolB_Enter(object sender, EventArgs e)
{
textBox1.Append("you are entering B...\r\n");
}

Why is my form going to background on DragDrop?

At the end of a drag&drop operation, I'm showing a form using ShowDialog.
Problem: When the form is closed, my main form is pushed behind any other application windows.
Code:
private void ctrl_DragDrop(object sender, DragEventArgs e) {
// ...
if (e.Effect == DragDropEffects.Move) {
string name = e.Data.GetData(DataFormats.Text).ToString();
viewHelperForm.ShowDialog(view.TopLevelControl);
// ...
}
Question: What can I do that the main form stays on top?
Your ShowDialog() call is blocking the DragDrop event. That is very, very bad, it gums up the drag source and make it go catatonic and unresponsive to Windows messages. This has all kinds of side-effects, like your window going catatonic as well or not getting reactivated since the D+D operation isn't completed yet.
Avoid this by only displaying the dialog after the D+D operation is completed. Elegantly done by taking advantage of the Winforms plumbing that allows posting a message to the message queue and get it processed later. Like this:
private void ctl_DragDrop(object sender, DragEventArgs e) {
//...
this.BeginInvoke(new Action(() => {
viewHelperForm.ShowDialog(view.TopLevelControl);
}));
}

Why does System.Windows.MessageBox lag a WPF window?

Consider the following code:
private void Window_Loaded(object sender, RoutedEventArgs e)
{
MessageBox.Show("MyMessage");
}
If I am trying to display a message box after a WPF window has been loaded, when I run the application, the WPF window is displayed with a transparent background (only the non-client area is visible) and it takes 3-5 seconds until the message box appears. The WPF window returns to normal only after the message box has been closed.
Is this normal? Does anyone else experience this?
EDIT: I have added a screenshot of how the window looks like:
The MessageBox is getting shown at the Normal DispatcherPriority, which occurs before things like DataBind, Render, and Loaded, so the code that initializes your Window's objects is not getting run until after you dismiss the MessageBox
You can fix this by simply showing the MessageBox at a later DispatcherPriority, such as Background
private void Window_Loaded(object sender, RoutedEventArgs e)
{
InitializeComponent();
this.Dispatcher.BeginInvoke(DispatcherPriority.Background,
new Action(delegate() { MessageBox.Show("MyMessage"); }));
}
Try this overload of Show method, or any other overload, accepting Window instance as parameter.

message sent when a window is shown

I am creating a window object globally and display it only when it is necessary, most of the time window will be in invisible mode. I have some work to do whenever the window is visible. can any one please tell me which message should i override to put the code which is supposed to execute when a window.show method is called?
IsVisibleChanged should do what you want.
private void Window_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
{
if ((bool)e.NewValue == true)
{
//Do what you need here
}
}
Look at the Window class documentation and also the Window lifetime cycle. Now out of this, we can conclude that you (probably) need the IsVisibleChanged event.

Minimize form to system tray

I want to hide my form while keeping my application running in background.
I've used notifyIcon and it remains always visible.
I've used "this.Hide();" to hide my form but unfortunately my application gets close (no exception).
I am also using threading and this form is on second thread.
Please tell me how can I solve it.
I am also using threading and this form is on second thread.
My crystal ball says that you've used ShowDialog() to show the form. Yes, calling Hide() on a modal dialog will close it. Necessarily so, a modal dialog normally disables all of the windows in the application. If you hide it then there's no way for the user to get back to the program, there are no windows left to activate. That this form runs on another thread otherwise doesn't factor into the behavior.
You'll need to call Application.Run(new SomeForm()) to avoid this. Now it isn't modal and you can hide it without trouble. But really, do avoid showing forms on non-UI threads. There's no reason for it, your main thread is already quite capable.
add the following event handlers for form resize and notify icon click event
private void Form_Resize(object sender, EventArgs e)
{
if (WindowState == FormWindowState.Minimized)
{
this.Hide();
}
}
private void notifyIcon_Click(object sender, EventArgs e)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
but this is not close you application

Categories