windows phone 7 ListBox event confusion - windows-phone-7

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;
}
}

Related

TextBox Leave Event suppresses Button Click

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...

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.

Adding events dynamically

I have n picture boxes. They should perform the following events dynamically:
private void pictureBoxMouseHover(object sender, EventArgs e)
{
if (sender is PictureBox)
{
((PictureBox)sender).BorderStyle = BorderStyle.FixedSingle;
}
}
private void pictureBoxMouseLeave(object sender, EventArgs e)
{
if (sender is PictureBox)
{
((PictureBox)sender).BorderStyle = BorderStyle.None;
}
}
private void MainMaster_Load(object sender, EventArgs e)
{
foreach (var control in Controls)
{
if (sender is PictureBox)
{
PictureBox pb=new PictureBox();
pb.Name = sender.ToString();
pb.MouseHover += new System.EventHandler(this.pictureBoxMouseHover);
pb.MouseLeave += new System.EventHandler(this.pictureBoxMouseHover);
}
}
}
I couldn't find what wrong with this; please help me.
dbaseman is right, you used wrong variable when iterating through controls.
But if you want to add this behavior to all picture boxes, then better solution is to create custom picture box, and simply place it on your form:
public class MyPictureBox : PictureBox
{
protected override void OnMouseHover(EventArgs e)
{
BorderStyle = BorderStyle.FixedSingle;
base.OnMouseHover(e);
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
BorderStyle = BorderStyle.None;
}
}
Create this class, compile app and drag these custom picture boxes from toolbox to your form. They all will display border when mouse hover over picture box.
I think the mistake is here:
foreach (var control in Controls)
{
if (sender is PictureBox)
Sender in this case will be the window. I think you intended control.
foreach (var control in Controls)
{
if (control is PictureBox)

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();
}

Silverlight TabItem template not working correctly

In a SL4 application i need to restyle my TabItems (actually add a button in the header).
So i took the TabItem's control template from here and added the functionality i wanted.
This seems to work fine, (i could dynamically add tabitems) with one exception:
i think this posted control template is behaving somehow "arbitrary": every time the mouse hoovers over a non selected TabItem header, this gets selected WHITHOUT clicking!! (afaik this is not the default behavior: the user user has to click a header to make this tabitem the selected one).
I tried to find why it is behaving like this, with no luck!
Is there someone who can enlighten my darkness???
Thanks in advance!
Well it turns out the error was not in the control template but in the class, the style was applied to.
In detail: the class the style was applied to is the following (in it you will see my comment about the "wrong behavior"):
public class WorkspaceViewModel : TabItem
{
public WorkspaceViewModel()
{
DefaultStyleKey = typeof(WorkspaceViewModel);
}
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
Button closeButtonSel = base.GetTemplateChild("PART_CloseTopSelected") as Button;
Button closeButtonUnsel = base.GetTemplateChild("PART_CloseTopUnSelected") as Button;
if (closeButtonSel != null)
closeButtonSel.Click += new RoutedEventHandler(closeButtonSel_Click);
if (closeButtonUnsel != null)
closeButtonUnsel.Click += new RoutedEventHandler(closeButtonSel_Click);
//this part is causing the effect i was complaining about!
//and has to be removed
this.MouseEnter += delegate(object sender, MouseEventArgs e)
{
IsSelected = true;
};
}
void closeButtonSel_Click(object sender, RoutedEventArgs e)
{
//this is the close request method used in the CloseTabItemCommand
OnRequestClose();
}
#region CloseTabItemCommand
private RelayCommand closeTabItemCommand;
public ICommand CloseTabItemCommand
{
get
{
if (this.closeTabItemCommand == null)
this.closeTabItemCommand = new RelayCommand(p => this.OnRequestClose(), p => this.CanCloseTabItem());
return this.closeTabItemCommand;
}
}
private bool CanCloseTabItem()
{
return true;
}
public event EventHandler RequestClose;
private void OnRequestClose()
{
if (RequestClose != null)
RequestClose(this, EventArgs.Empty);
}
#endregion
}

Resources