Creating popup without possibility to tab to a background element - c#

I am trying to create a popup but when it is open it is still possible to use the tab key to switch the focus to an element in the background (e.g. to a button and use space to press is). The only way I found until now is to check on every lostFocus event (which also fires for every element contained in the Border element) and check if the focus is now in a element inside the Border. If not I manually set the focus.
Is there a nicer way to keep the focus within the Border (or a Grid,...)
I'm working on a Windows 8 App.

Do you mean that using a Modal Dialog with Form.ShowDialog(Owner) still allows you to focus the parent components with Tab?
Can you give a sample of your code call?

Form2 form = new Form2(); //Make an instantiation of your Form
form.ShowDialog(); //ShowDialog()!!! NOT form.Show()!!! Or anything else :/

A few ideas:
Set Enabled to False on the background visual tree, though that might change the way things look if you still want to show them partly
Set IsHitTestVisible to False to disable pointer input
Use RenderTargetBitmap.Render() if targeting Windows 8.1 to render the content of the background to an image and simply replace all that visual tree with an image of it

Related

How to access PickerFlyout's app bar in a WP 8.1 XAML App

I'm writing a custom picker with the PickerFlyout class. By setting the ConfirmationButtonsVisible property to true, the flyout will show an application bar with an accept and a cancel button.
My problem is that the picker I am writing does not always have a valid value, and therefore I would like to disable the accept button when it does not make sense. Is there a way to do this in a Windows Phone 8.1 XAML app ("Store app")?
Other possible solutions:
An alternative solution would be to show my own app bar instead of the one given by ConfirmationButtonsVisible, which is possible by setting one in the Opening event. However, when this is done if the "overflow dots" of the application bar is clicked, the flyout will close. Apparently there is no way to prevent a flyout from closing.
If all else fails I will have to write a custom Popup, but I would rather not do this because the opening and closing animations used by PickerFlyout do not seem to be available as resources (internal to the class maybe?).
As long as there are items only in the PrimaryCommands section the flyout stays on screen when the overflow dots are tapped. Thus it is possible to temporarily replace the page's app bar with a new one for the duration of the flyout as long as no items in SecondaryCommands are needed. The accept button can be disabled in this new app bar.

How to prevent popup, new tab, new window and message boxes in webbrowser control?

Here is HTML code :
<script type='text/javascript'>//<![CDATA[
window.onload=function(){
alert('hi');
}//]]>
</script>
Not only alerts, popups, new tab, new window but anything cause focus change.
By other words I don't want WebBrowser control to get focused or change current focus.
Looking for a solution that doesn't hurt any weppage( I need javascript enabled).
In theory, you should implement IProtectFocus, but apparently there're issues with it:
http://social.msdn.microsoft.com/Forums/windows/en-US/211733a0-af84-4cc4-9851-2f19def7aedd/iprotectfocus-and-allowfocuschange-not-working-on-webbrowser?forum=winforms
Another option might be to implement a CBTHook, handle HCBT_SETFOCUS and reject it (return 1 to prevent it) if the window becoming focused is a child window of the WebBrowser object.
Disclaimer: I myself haven't tried either of the above.

c# Close a form opened from and instance class from the main form that created the class

I'm having a difficulty in sizing my form!
I dynamically create buttons on a form and need to know if they are all fully visible or if I need to grow the form and in what direction to make all the buttons fully visible.
I don't want to use the autosize property as I need to control the layout.
So how do I tell if a dynamically created controls bounds are within that of the form?
thanks
This a .Net 4 classic forms app.
When you add the button to the controls collection, to see if it is visible check the contains on the forms bounds - Form.Bounds.Contains(button.Bounds));. If that returns false then you need grow your form. Here is some basic code to do the form growing, it will not necessarily produce the prettiest output and is not necessarily the best way, just written to give you a quick idea of how it could be accomplished.
// Add the control
form.Controls.Add(button);
var formBounds = form.Bounds;
var controlBounds = button.Bounds;
if (!formBounds.Contains(controlBounds))
{
formBounds.Left = Math.Min(controlBounds.Left, formBounds.Left);
formBounds.Right = Math.Max(controlBounds.Right, formBounds.Right);
// Do similar for top and bottom this will ensure your button is visible
form.Bounds = formBounds;
}
Can you add the button, can't you compare the Width of the container vs the Left + Width properties of the newly added button?

Calling .Parent from a form causes textbox problem, MDI

I want to make a form contained in another form. The problem is the application is already a MDI, and you can't nest MDI's.
If I do
childFrm.Parent = parentForm
some controls behave oddly. For example, if you click on the text in the textbox, usually the text cursor appears where you clicked, but it doesn't, it just goes to the end of the text.
Any suggestions?
Thanks,
Any particular reason why you can't host the content in a UserControl instead of a Form?
How about adding the child forms as owned forms to the MDI parent?
http://msdn.microsoft.com/en-us/library/system.windows.forms.form.addownedform.aspx
Look at SetWindowParent Windows API Call, and no you cannot use .Parent it won't work correctly as .NET itself doesn't support internally what you're wanting to do.

How do I select the previous control to a container control?

I have a control which contains a NumericUpDown. The updown is only shown when the container has focus, so the container has to be selectable (or else it could never receive focus). I want the control to behave as a single entity with regards to tab order; that is, when the user tabs to the control, it shows the updown and the updown is focused; when the user tabs away from the updown, it is as if they had tabbed away from the control.
It's easy enough to achieve the first part: in the container's OnEnter, I focus the updown. If the user tabs away without shift, it also works fine, since the next control in the tab order is the correct one. However, the previous control in the tab order to the updown is the container, since it had to be selectable; so when the user shift-tabs away from the updown, the container is selected, and therefore the updown gets selected again.
How do I select the previous control to the container control, when the user shift-tabs away from the updown?
UPDATE:
My problem isn't detecting when I need to do this - it's finding the control to send focus to.
UPDATE:
SelectNextControl only seems to work within the container's parent's controls; if the container is the only control on its parent, it doesn't change focus, even if there are other controls elsewhere in the hierarchy that ought to receive focus via tab.
if you know the direction of the tab you could use SendKeys.Send("+{TAB}"); and SendKeys.Send("{TAB}");
or you could use Control.SelectNextControl()
void UserControl1_Leave(object sender, EventArgs e)
{
this.numericUpDown1.Visible = false;
Control c = Parent.Controls[this.Name];
int i = Parent.Controls.IndexOf(c);
Parent.Controls[i - 1].Focus();
}
I've added this leave event to a custom control and its working for me. Basically when the user shift tabs away this event sets the focus to the previous control in the parent form's control collection. Don't know if its what your looking for exactly but hopefully it will send you in the right direction.
It's a hack, but you can use the OnEnter event coupled with a boolean variable. If the variable is set to true then you were already in your container and go to the previous control (which could be a property of your container control so you know where you are going).
If the variable is false, your just getting to your custom control and focus on the up/down.
On the exit of the container, set the variable back to false.
I'm sure there's something simpler out there, but offhand this is the quickest thing I can think of.
Actually this seems to be the default behavior for me?

Categories