TopMost property not working - c#

I have a form that when I check a box (Duplicate #), a form pops up (CableID_DuplicateView), and I want it to remain ontop until the checkbox is unchecked. However this isn't happening.
I have set the TopMost property to true both dynamically and statically;
this.TopMost = true;
this.TopMost = Checkbox.Checked;
But if I click on anything on the previous form, it gets shoved to the back.
How can I keep it ontop?
Note: I want the other form to still be accessible beneath the top form. And yes, this is an Mdi application, does that make a difference on the TopMost property?

This worked for my similar problem:
try
yourTopForm.TopLevel=true;
yourTopForm.TopMost=true;
yourTopForm.Show(this);
The overloaded Show(this) funcition to show the form is the important part.

It is impossible to make the windows stay on top in MDI system. You can try to activate the windows, so they pop on the top, but this makes only more harm to other actions. I wouldn't recommend trying that. There is just no working way to make some window TopMost in MDI configuration.

Pass your parent form into the Show method of your Top Most form.

Related

Dialog box doing more than just hiding itself on close

According to the MSDN document, the close operation on a form shown with ShowDialog() should only cause the form to be hidden. Subsequent calls to ShowDialog() will unhide the form.
This doesn't seem to be case, exactly. I have a form with a tree view on it. The check states are preserved between calls to ShowDialog() but any node expansion the user has done is reset back to its default state. Also, the Load event is being executed every time as well. So it seems to be doing more than just "hiding" the form. Anyone have any idea what's up?
Thanks
I've experienced this issue myself. For some reason, calling Form.Hide or setting visible = false on a modal form will call Form.Close in at least some cases. To work around it I set the opacity to zero. You can also use Form.Show instead.
It is somewhat intuitive if you imagine the behavior of a modal dialogue. It blocks the parent window. So if you hide it then there would be no active window for the user to interact with. FWIW, I think the behavior should have been that the parent becomes active again. That's just not always the case.

How do I get a user control to be closed when I close a specific form?

When I open from the main screen of the application a child form I also display a user control that should be displayed until the child form is closed or the user closes it. If I set the child form to be usercontrol's parent, the user control is not displayed (so the parent of the user control is the desktop). I used SetWindowPos with HWND_TOPMOST and I get the correct behaviour. now when I close the child form I want the usercontrol to close as well. Do you know what I shoud do to get this?
thanks,
When you Show() the child you can also subscribe to it's Closed or Closing event and use that to properly close the UC.
var f = new ChildForm();
// show userctl
f.Closed += MainForm_ChildClosed_Handler;
void MainForm_ChildClosed_Handler(object s, EventArgs e)
{
// close/hide userctl
}
If you keep a reference towards your usercontrol, you should be able to Close (or even Dispose) it from the Closingevent of his parent.
I don't know what you are doing, a UserControl is meant to be a child control, it should be embedded in a Form. It is technically possible to turn a UC into a top level window (like Form), you'd have to set the TopLevel property to True. That however isn't very productive, the window doesn't have the chrome to make it friendly to use. It is missing the title bar, no easy way for the user to move it around, minimize it, close it. And no easy way to solve focus problems, it can disappear behind another window with no easy way for the user to bring it back to the front. Making it top-most is a hack of sorts to get at least the focusing problem solved, but at rather a deer cost.
Just use the UC as it was intended to be used: put it inside a form. That form should probably be the child form you open. It could also be a third form, say a tool window. Use the Show(owner) overload to keep its Z-order in check and cannot do the disappearing act. That also causes it to automatically close when the owner closes.

Showing a form via Form.Show is not given focus?

I two forms, Form1, and a UserControl which hosts Form2. Within that UserControl on Form1 I call Form2.Show();. I have also tried Form2.Show(this);. Either way, the focus is not given to the form. I have to click once within the new form to give it focus, and then I can click items within that form.
I figured that control is passing back to my main control/form, and thus the focus is getting lost. So I am waiting until Form2 is closed via:
while (form2.Visible == true)
{
System.Threading.Thread.Sleep(100);
Application.DoEvents();
}
This seems to work. However after I close the form, now the reverse holds true. Form1 is not given focus (even if I call this.Focus()) until I click once within the main form window.
Any ideas how to handle this properly. I want to show a child form (modeless) and immediatley be able to click on it, and when that form is closed, immediately be able to take action back on the parent form.
You should probably use .ShowDialog(), this can also be extended to give your response on if the user performed Form2's operation properly or aborted early.
This does make the form locked in focus up front and will halt your code execution on the first form till that form is closed.
use this.Activate(); in place of this.Focus();
Not sure I follow completely, but from your UC try opening Form2 like this:
form2.Show(Parent);
This should specify the UC's Parent form as form2's owner.
This came from the fact that I was overriding WndProc in order to display the form. When I received the CBN_DROPDOWN message, I would display the form. I fixed this by instead Invoke'ing the method that shows the form and it fixed it.
case CBN_DROPDOWN:
Invoke(new MethodInvoker(Show_DropDown));
return;

c# Add HelpButton to a MDI child form at run time

i'd like the following code to work (where this. is a MDI child form)
this.HelpButton = true;
this.HelpButtonClicked += HandleHelpButtonClicked;
this.Refresh();
this code is being called after the mdi child form is displayed.
I wonder if i need to find some way of redrawing the title bar?
David
From the MSDN docs:
The value of the HelpButton property is ignored if the Maximize or Minimize buttons are shown.

C# - How to deal with 2 "TopMost" Forms?

I have a parent form that is set to be TopMost and then I have another form that opens when a button is clicked. This child form is also set to be TopMost. The first issue I had was that when I opened the child form, the application would basically freeze because you couldn't access anything. I decided that instead of using ShowDialog() to open the child form, I would use Show(this). This did fix the initial problem but now I have a new one. The childforms start postition is set to be CenterParent and when I use Show(this), it doesn't work. Is there any way I can make the childform open while both it and the parent form are set to topmost while having the childforms start position set to CenterParent? Thank you.
I've find something useful to share with you, guys. Instead following code
form2.TopMost = true;
use this code in main form:
form2.Owner = this;
If you use Form.TopMost property, the form will overlap all other non-topmost forms, but also those from other applications. Instead of this, set the Form.Owner property to the parent form – the one which should be under the form (e.g. the main form).
G00d luck :)
You could try clearing the TopMost property of the parent form for the duration that the child form is visible.
This would solve the problem of which form should be top most, as there will only ever be one.
Hmm. I've created To forms. Then i set TopMost = true on both. The i add button to first and wrote
new Form2().ShowDialog();
And all just fine. Form2 active and clickable. Form1 not since ShowDialog was called
And second variant works fine. Form2 opened in center of the screen.
May be i misunderstood something?

Categories