TextBox Leave Event suppresses Button Click - windows

I have a simple Windows Form: 'Message' TextBox has Enter and Leave events to allow user to enter text in another language only on that field. 'Send' button sends the form content. After the user fills the Message and clicks Send, Textbox's Leave event prevent button's Click event from firing. I need both handlers to run.
Here's the relevant code:
private void Message_Enter(object sender, EventArgs e)
{
inputLang = InputLanguage.CurrentInputLanguage;
foreach (InputLanguage lang in InputLanguage.InstalledInputLanguages)
{
if (lang.LayoutName == "United States-International")
{
InputLanguage.CurrentInputLanguage = lang;
break;
}
}
}
private void Message_Leave(object sender, EventArgs e)
{
InputLanguage.CurrentInputLanguage = inputLang;
}
private void Send_Click(object sender, EventArgs e)
{
string dest = ServerList.Text;
string msg = Message.Text;
if (dest.Length == 0 || msg.Length == 0 )
{
Log("Fill the destination server and the message");
return;
}
if (context.SendMessage(dest, msg))
{
if (!ServerList.Items.Contains(dest))
{
ServerList.Items.Add(dest);
}
}
else
{
if (ServerList.Items.Contains(dest))
{
ServerList.Items.Remove(dest);
}
}
}

The problem is now solved. The problem is caused by the change of input language. If the enter and leave handlers did other stuff then the click event will fire normally. Since I need to change the input language I solved it by monitoring the MouseDown, MouseClick and MouseUp events and generating a click if it was not automatically generated.

I've got same problem. When I changed the input language and then on leave event set it back to default one. When i click on other component it wont fire click event. I had to click twice then.
I thing it has something to do with focus.
I solved it by setting the focus back to form, after changing back the input language.
Here is the handler:
void textBoxSearch_LostFocus(object sender, EventArgs e)
{
InputLanguage.CurrentInputLanguage = InputLanguage.DefaultInputLanguage;
this.Focus();
}
Hope it helps...

Related

VSTO How to hide FormRegion in Reply email [in InlineResponse also]?

So, as the subject says...what is the easiest way to hide FormRegion if email is in Reply mode whether its in a new window or with InlineResponse?
Just set the FormRegion.Visible property which returns a Boolean value that indicates whether the form region is visible or hidden.
Went back to test the approach in my answer after the question from #EugeneAstafiev and -- of course -- this was more complicated than I first thought... but I did get it working with some additional code.
The issue is that when the user clicks "Reply", it opens a new inspector window with a new instance of the FormRegion. So setting the Visible property to false in the event handler sets it only on the current "Read" mode inspector window -- rather than the new "Compose" mode inspector window that gets opened. So, instead the code samples below set up a bool flag property in ThisAddIn called LoadFormRegion that can be toggled to "false" when the Reply or ReplyAll event is fired.
Also, I noticed that setting Visible to false on the FormRegion still draws the area of the FormRegion on the inspector window, just with nothing in it. To completely prevent the FormRegion from loading, you can test for "Compose" mode and then the ThisAddin.LoadFormRegion flag in the FormRegionInitializing event handler located inside of the "Form Region Factory" at the top of the code page -- which is usually folded away from view. In that code block, setting "e.Cancel = true" will prevent the FormRegion from loading at all.
Here is the revised code for the FormRegion, which now subscribes to all three email button click events (Reply, ReplyAll, Forward), and sets the ThisAddIn.LoadFormRegion flag accordingly:
namespace TESTINGOutlookAddInVSTO
{
partial class FormRegion1
{
#region Form Region Factory
[Microsoft.Office.Tools.Outlook.FormRegionMessageClass(Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Note)]
[Microsoft.Office.Tools.Outlook.FormRegionName("TESTINGOutlookAddInVSTO.FormRegion1")]
public partial class FormRegion1Factory
{
// Occurs before the form region is initialized.
// To prevent the form region from appearing, set e.Cancel to true.
// Use e.OutlookItem to get a reference to the current Outlook item.
private void FormRegion1Factory_FormRegionInitializing(object sender, Microsoft.Office.Tools.Outlook.FormRegionInitializingEventArgs e)
{
if (e.FormRegionMode == Outlook.OlFormRegionMode.olFormRegionCompose)
{
var myAddIn = Globals.ThisAddIn;
if (myAddIn.LoadFormRegion == false)
{
e.Cancel = true;
}
}
}
}
#endregion
// Occurs before the form region is displayed.
// Use this.OutlookItem to get a reference to the current Outlook item.
// Use this.OutlookFormRegion to get a reference to the form region.
private void FormRegion1_FormRegionShowing(object sender, System.EventArgs e)
{
// Reset ThisAddIn.LoadFormRegion flag to true (in case user starts
// composing email from scratch without clicking Reply, ReplyAll or Forward)
var myAddin = Globals.ThisAddIn;
myAddin.LoadFormRegion = true;
var myMailItem = this.OutlookItem as Outlook.MailItem;
// Track these events to set the ThisAddIn.FormRegionShowing flag
((Outlook.ItemEvents_10_Event)myMailItem).Reply += myMailItem_Reply;
((Outlook.ItemEvents_10_Event)myMailItem).ReplyAll += myMailItem_ReplyAll;
((Outlook.ItemEvents_10_Event)myMailItem).Forward += myMailItem_Forward;
}
// Sets FormRegionShowing flag on ThisAddin
private void SetFormRegionShowing(bool show)
{
var myAddIn = Globals.ThisAddIn;
myAddIn.LoadFormRegion = show;
}
private void myMailItem_Forward(object Forward, ref bool Cancel)
{
SetFormRegionShowing(true);
}
private void myMailItem_ReplyAll(object Response, ref bool Cancel)
{
SetFormRegionShowing(false);
}
private void myMailItem_Reply(object Response, ref bool Cancel)
{
SetFormRegionShowing(false);
}
// Occurs when the form region is closed.
// Use this.OutlookItem to get a reference to the current Outlook item.
// Use this.OutlookFormRegion to get a reference to the form region.
private void FormRegion1_FormRegionClosed(object sender, System.EventArgs e)
{
}
}
}
And here's the new code for ThisAddIn to setup the LoadFormRegion property flag:
namespace TESTINGOutlookAddInVSTO
{
public partial class ThisAddIn
{
private bool loadFormRegion;
public bool LoadFormRegion { get => loadFormRegion; set => loadFormRegion = value; }
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
LoadFormRegion = true;
}
private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{
// Note: Outlook no longer raises this event. If you have code that
// must run when Outlook shuts down, see https://go.microsoft.com/fwlink/?LinkId=506785
}
#region VSTO generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}
#endregion
}
}
Tested the above and works nearly perfectly... I noticed that if there is a "Draft" reply in the user's Inbox created before the add-in was loaded, clicking on that email to continue editing in the Preview window or a new inspector window will cause some wonky display issues with the FormRegion. However, once I closed out that draft, then everything seemed to return to normal.

windows form - show windows 8 keyboard when touch textbox

I've create a windows form application. I'd like that when the application is installed on a pc with Windows 8 and touchscren, if I click on a textbox, the virtual keyboard is automatic showed.
I've seen something like
private void textbox_Enter(object sender, EventArgs e)
{
System.Diagnostics.Process.Start("TabTip.exe");
}
but it works also when I use the mouse. I'd like that the virtual keyboards apppear just when I use touchscreen.
Thanks
I've found a "rude" solution.
I've seen that when I touch, the "587" WM Message occurs, followed by 2 "33" WM Message (the same when you click using mouse).
So I've seen that: before than "mouseclick" or "focus enter" event on textbox:
a) if you have used mouse, there is 1 "33" Wm Message
b) if you have used touch, there is 1 "587" message followed by 2 "33" Wm Message.
So, about the code.
In MyForm.cs I have a public list of bool, that save "touch status". If i never touch (or my pc doesn't support touchscreen) the status will never fill
namespace MyForm
{
public partial class MyForm: Form
{
public List<bool> v_touch = new List<bool>();
protected override void WndProc(ref Message msg)
{
switch (msg.Msg)
{
//touch
case 587:
v_touch.Add(true);
break;
//mouse or touch (on mouse click message 33 occurs 1 time; on touch tap message 587 occurs 1 time and message 33 occurs 2 times)
case 33:
//add just if some touch event (587) has occured (never fill v_touch for not touch devices)
if (v_touch.Count > 0)
{
v_touch.Add(false);
}
break;
}
base.WndProc(ref msg);
}
}
}
In all myUserControls, I need for each textbox, add moouseclick, focus enter and focus leave events.
MouseClick or focus enter call a function that enable virtual keybooard checking "touch status". Instead, focus leave event will kill keyboard.
namespace MyForm
{
public partial class MyUserControl : UserControl
{
public void enableVirtualKeyboard(List<bool> v_touch)
{
//check if came from "touch". When I use touch, the last v_touch values are (true, false, false)
if (v_touch.Count >= 3 && v_touch[v_touch.Count - 3] == true)
{
string progFiles = #"C:\Program Files\Common Files\Microsoft Shared\ink";
string onScreenKeyboardPath = System.IO.Path.Combine(progFiles, "TabTip.exe");
Process.Start(onScreenKeyboardPath);
}
}
public void disableVirtualKeyboard()
{
Process[] oskProcessArray = Process.GetProcessesByName("TabTip");
foreach (Process onscreenProcess in oskProcessArray)
{
onscreenProcess.Kill();
}
}
private void textbox_MouseClick(object sender, MouseEventArgs e)
{
this.enableVirtualKeyboard(((MyForm)this.ParentForm).v_touch);
}
private void textbox_Enter(object sender, EventArgs e)
{
this.enableVirtualKeyboard(((MyForm)this.ParentForm).v_touch);
}
private textbox_Leave(object sender, EventArgs e)
{
this.disableVirtualKeyboard();
}
}
}
}
We can write disableVirtualKeyboard and enableVirtualKeyboard in a common class, so we can use the 2 methods in all texbbx of each control of the application. Also, it's good use disableVirtualKeyboard on Form_Closing event of MyForm

Selection changed event also called Lostfocus event?

NET C# ,
In my windows phone 7.5 application , I want to make visible the application bar if any item has selected .. So I am making it visible in selected change event. But what is happening in my code is when ever selection change it also triggers LostFocus event and in that event I am making selected index = 0.
Now the resultant of the code is when ever I select any item , application bar gets visible then automatically invisible ( because of lost focus event).
Following is the piece of code .
private void ShopingListItemDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ShopingListItemDetails.SelectedIndex != -1)
{
ApplicationBar.IsVisible = true;
int selind = ShopingListItemDetails.SelectedIndex;
}
}
private void ShopingListItemDetails_LostFocus(object sender, RoutedEventArgs e)
{
ApplicationBar.IsVisible = false;
ShopingListItemDetails.SelectedIndex = -1;
}
I am just at start with .NET C#(XAML) so assuming that selection change event is also triggering LostFocus event.
Please help me what is the real problem behind.Thanks
Zauk
You can use the following hack. Initialize a variable, say selectChanged to False initially in the xaml.cs. In SelectionChanged function change it to True. Now, in the LostFocus function do processing only if the selectChanged variable is false, and if it is true set it back to False
Boolean selectChanged=false;
private void ShopingListItemDetails_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ShopingListItemDetails.SelectedIndex != -1)
{
ApplicationBar.IsVisible = true;
int selind = ShopingListItemDetails.SelectedIndex;
selectChanged=true;
}
}
private void ShopingListItemDetails_LostFocus(object sender, RoutedEventArgs e)
{
if(!selectChanged)
{
ApplicationBar.IsVisible = false;
ShopingListItemDetails.SelectedIndex = -1;
}
selectChanged=false;
}
I think this should solve your problem.

ListBox and selectedIndexChanged event after the user hit the back button

In my windows phone 7 app, I have the following code to handle the OnSelectedIndexChange of a ListBox.
private void wordList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
WordList selectedList = (WordList)e.AddedItems[0];
NavigationService.Navigate(new Uri("/Views/Game.xaml?ListName=" + selectedList.Name, UriKind.RelativeOrAbsolute));
}
The above code work fine, However if the user click on the hardware back button from the Game page, and click on the same listbox item, the above code is not called. I assume this is because the selected item is the same therefore the SelectionChanged event is not being called.
How do I make it so that if the user select the same item I can still send them to the Game page?
I looked at the Tap Event but I couldn't figure out a way to get the selected Item from the tab event.
When using SelectionChanged to navigate, surround your navigation logic with a check to see if the SelectedIndex = -1. After you navigate, set the index to -1, so that the event will not fire twice.
private void wordList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
var lb = sender as ListBox;
if (lb != null)
{
if (lb.SelectedIndex == -1) return;
WordList selectedList = (WordList)e.AddedItems[0];
NavigationService.Navigate(new Uri("/Views/Game.xaml?ListName=" + selectedList.Name, UriKind.RelativeOrAbsolute));
lb.SelectedIndex = -1;
}
}
This way you can get the selected Item from the Tap event.
private void wordList_Tap(object sender, GestureEventArgs e)
{
var selectedElement = e.OriginalSource as FrameworkElement;
if (selectedElement != null)
{
var selectedData = selectedElement.DataContext as WordList;
if (selectedData != null)
{
NavigationService.Navigate(new Uri("/Views/Game.xaml?ListName=" + selectedData.Name, UriKind.RelativeOrAbsolute));
}
}
}
I had such issue within a UserControl. Check the sender, and return if it is not the ListBox control which is triggering the event:
protected void cbEvents_SelectedIndexChanged(object sender, EventArgs e)
{
if (sender is DropDownList)
RebindGrid();
}

windows phone 7 ListBox event confusion

The app is like a small dictionary. I have a listbox and a textbox. The list box is already filled with words and when there is any entry in the textbox the listbox is refilled again with words starting with the letters in the textbox. I have a listbox SelectionChanged event implemented when the user clicks on a word its meaning appears. The problem is when user selects a word from the list and then types something in the textbox, listBox SelectionChanged event is called i dont want this to happen because at this point of time my listbox's selected item is empty.I would like to have a event that is fired only when user selects something from the listbox. It should not be fired when the content of the listbox changes. Thank you
You can use
1.if (lstWords.SelectedItem != null)
2.lstWords.SelectedIndex = -1;
for e.g. following is the source code for text changed event and list selection change event
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
try
{
if (textBox1.Text.ToString().Equals(""))
{
XmlDictionaryRepository test = new XmlDictionaryRepository();
lstWords.ItemsSource = test.GetWordList(categorySelected,xmlFileName);
}
else
{
XmlDictionaryRepository test = new XmlDictionaryRepository();
lstWords.ItemsSource = test.GetMatchWordList(categorySelected, textBox1.Text.ToString(),xmlFileName);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), (((PhoneApplicationFrame)Application.Current.RootVisual).Content).ToString(), MessageBoxButton.OK);
}
}
private void lstWords_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
try
{
if (lstWords.SelectedItem != null)
{
string wordSelected = ((Glossy_Test.Dictionary)(lstWords.SelectedItem)).Word;
if (lstWords.SelectedItem != null)
{
NavigationService.Navigate(new Uri(string.Format("/DescribeWord.xaml?param1={0}&param2={1}", wordSelected, categorySelected), UriKind.Relative));
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(), (((PhoneApplicationFrame)Application.Current.RootVisual).Content).ToString(), MessageBoxButton.OK);
}
finally
{
// lstWords.SelectedIndex = -1;
}
}

Resources