How to force LostFocus when button is clicked - xaml

XAML C# not WEB page is a Window
At the click of a button I:
Need to trap the name of the last control OnFocus
Force the LostFocus event of the control.

// PROBLEM: clicking on btns does not force lostfocus event on the last entered element control (last entry control could be text,checkbox or others)
added save button, where calling such method it moves focus to parent forcing lostfocus on last element.
private void btnSave_Click(object sender, RoutedEventArgs e)
{
AcceptLastFocusedElement(sender, e);
}
private void AcceptLastFocusedElement(object sender, RoutedEventArgs e)
{
FocusManager.SetFocusedElement(this, (Button)sender);
}
NOTE: no need for task number 1 (getting the name of the element).

You could make use of the LayoutUpdated method.
So whenever there is any event happening, it goes into the LayoutUpdatedevent and you could trap the LastFocusObject .

Related

Scrolling up the screen when Keyboard appears

Is there a way to scroll-up view the required amount when the soft-keyboard shows up? The page in my app has a lot of textboxes and when the keyboard shows up, a lot of them hide under it and the user has to manually scroll down/hide keyboard to enter values in the other textboxes.
How can I scroll-up the page by some amount to improve UX?
You can have a KeyDown event set for each textbox and inside event handler check if Enter key is pressed shift focus to next textbox.
KeyDown="txtMessage1_KeyDown"
private void txtMessage1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
nextTextbox.Focus();
}
}

Modeless dialog created by modal dialog in Compact Framework

I am working on a Compact Framework application. This particular hardware implementation has a touchscreen, but its Soft Input Panel has buttons that are simply too small to be useful. There are more than one form where typed input is required, so I created a form with buttons laid out like a keypad. The forms that use this "keypad" form are modal dialogs. When a dialog requiring this "keypad" loads, I load the "keypad" form as modeless:
private void CardInputForm_Load(object sender, EventArgs e)
{
...
keypadForm = new KeypadForm();
keypadForm.Owner = this;
keypadForm.SetCallback(keyHandler);
keypadForm.Show();
}
The SetCallback method tells the "keypad" form where to send the keystrokes (as a Delegate).
The problem I'm having is that the modeless "keypad" form does not take input. It is displayed as I expect, but I get a beep when I press any of its buttons, and its caption is grayed-out. It seems like the modal dialog is blocking it.
I've read other posts on this forum that says modal dialogs can create & use modeless dialogs. Can anyone shed light on this situation? Is there a problem with my implementation?
I found the answer: Set the keypad form's Parent property, not its Owner property, to the form instance wanting the keystrokes. The keypad dialog's title bar stays grayed out, but the form is active.
private void CardInputForm_Load(object sender, EventArgs e)
{
// (do other work)
keypadForm = new KeypadForm();
keypadForm.Parent = this;
keypadForm.Top = 190; // set as appropriate
keypadForm.Show();
}
Be sure to clean up when done with the parent form. This can be in the parent's Closing or Closed events.
private void CardInputForm_Closing(object sender, CancelEventArgs e)
{
// (do other work)
keypadForm.Close();
keypadForm.Dispose();
}
There are two panels on the keypad form, one with numerals and one with letters and punctuation that I want. There is also an area not on a panel that is common to both, containing buttons for clear, backspace, enter/OK, and cancel. Each panel has a button to hide itself and unhide its counterpart ('ABC', '123', for example). I have all the buttons for input on the keypadForm fire a common event. All it does is send the button instance to the parent. The parent is responsible for determining what action or keystroke is desired. In my case I named the buttons "btnA", "btnB", "btn0", "btn1", "btnCancel", etc. For keystrokes, the parent form takes the last character of the name to determine what key is desired. This is a bit messy but it works. Any form wishing to use the keypad form inherits from a base class, defining a method for callback.
public partial class TimeClockBase : Form
{
public TimeClockBase()
{
InitializeComponent();
}
// (other implementation-specific base class functionality)
public virtual void KeyCallback(Button button)
{
}
}
The click event on the keypad form looks like this.
private void btnKey_Click(object sender, EventArgs e)
{
// play click sound if supported
(Parent as TimeClockBase).KeyCallback(sender as Button);
}
The method in the parent form looks like this.
public override void KeyCallback(Button button)
{
switch (button.Name)
{
case "btnCancel":
// setting result will cause form to close
DialogResult = DialogResult.Cancel;
break;
case "btnClear":
txtCardID.Text = string.Empty;
break;
// (handle other cases)
}
}

XAML Windows Store App - Ignore Slider.ValueChanged during drag

The default behavior for the Slider.ValueChanged event is to trigger ValueChanged multiple times as the user drags the control. Is there a way to only trigger this event when the drag is over?
One thing I already tried was binding to the Thumb.DragStarted and Thumb.DragCompleted events, and adding some flags to control the process, but these events aren't fired if the user clicks in the body of the slider, outside the thumb.
If you don't want to customize the control you can handle the KeyUp event and the PointerCaptureLost events instead of handling the ValueChanged event.
private void slider_PointerCaptureLost(object sender, PointerRoutedEventArgs e)
{
HandleSliderValueChange();
}
private void slider_KeyUp(object sender, KeyRoutedEventArgs e)
{
//Make sure an arrow key, Home, or End was pressed
//either explicitly perform the flag checks
//if(e.Key.HasFlag(VirtualKey.Up & VirtualKey.Down & VirtualKey.Left & VirtualKey.Right & VirtualKey.Home & VirtualKey.End))
//or check the int values
int keyVal = (int)e.Key;
if(keyVal >= 35 && keyVal <= 40)
HandleSliderValueChange();
}
private void HandleSliderValueChange()
{
//your value changed code
}
This should call the HandleSliderValueChange method when the user finishes dragging, clicks on the slider itself, or uses the arrow, Home, or End keys to change the value.

Forcing Default button on a gridview

I'm using gridview with templates to show and edit some information from a sql database.
When I edit and change the data in that row and then click enter it automatically presses the highest on page button which uses submit to server set to true which means it'll try to delete instead of update.
I've have tried setting a panel round the gridview and setting the panel's default button to the "updatebutton" but it won't allow that because it can't 'see' the buttons.
I had a similar problem and I found a very simple workaround:
Place a button below the GridView and make it invisible by CSS (i.e. position: relative; left: -2000px)
Create a Panel around the GridView and give it as DefaultButton the ID of the button we just created.
Write the following line of code for the click-event of the button:
myGridView.UpdateRow(myGridView.EditIndex, false);
Whenever you press enter in the GridView now, the edited row will be confirmed.
You need to precess KeyDown or KeyPress event of the grid, and check if pressed key if Keys.Enter :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
button1_Click(this, EventArgs.Empty);
}
}
private void button1_Click(object sender, EventArgs e)
{
// Your logic here
}
}

SCSF: display view from another view against button click

i am facing one problem in SCSF.
I have two workspaces
MdiWorkspace
DeckWorkspace
i have two views in a module
Viewer (display in mdiworkspace)
Property Viewer (in deckworkspace)
in Viewer i have a button in toolbar whose purpose is to display PropertyViewer (another View).
how can i display this PropertyViewer in deckworkspace agaist button click event.
NOTE: i am not using Command[CommandName].AddInvoker(control, "click:) and CommandHandler
I'm going to assume your toolbar sits in a SmartPart that implements the MVP pattern. Have the button click event handler in the SmartPart fire an event that its presenter will handle. Your presenter code would look like this:
// Presenter code
protected override void OnViewSet()
{
this.View.ToolbarButtonClick += View_ToolbarButtonClick;
}
public void View_ToolbarButtonClick(object sender, EventArgs e)
{
// remove the handler so the property viewer
// will only be added the first time
this.View.OnToolbarButtonClick -= View_ToolbarButtonClick;
var propertyView = new PropertyViewer();
this.WorkItem.Workspaces[WorkspaceNames.MyDeckWorkspace].Show(propertyView);
}