Delete input if date is invalid : XAML datepicker - c#

I am working with datepicker control in silverlight. I want to delete input if date is invalid.
I have tried following code :
XAML:
<sdk:DatePicker Height="23" x:Name="datePicker1" Width="179"
DateValidationError="datePicker1_DateValidationError" />
C#:
private void datePicker1_DateValidationError(object sender, DatePickerDateValidationErrorEventArgs e)
{
try
{
if (datePicker1.SelectedDate != null && (Regex.IsMatch(datePicker1.SelectedDate.ToString(), #"^([1-9]|0[1-9]|1[0-2])[- / .]([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])[- / .](1[9][0-9][0-9]|2[0][0-9][0-9])$")))
{
//WORKING FINE
}
else
{
MessageBox.Show("Please enter a valid date");
DatePicker myElement = ((FrameworkElement)System.Windows.Application.Current.RootVisual).FindName("datePicker1") as DatePicker; // search for control named "datePicker1"
myElement.Text = String.Empty;
}
}
catch
{
}
}
But myElement returns null and I can not delete invalid input.

Here's some code I used to do a similar thing:
Code behind:
private void DatePicker_DateValidationError(object sender, DatePickerDateValidationErrorEventArgs e)
{
var MyDatePicker = sender as DatePicker;
DatePickerTextBox dateBox = MyDatePicker.GetVisualDescendants().OfType<DatePickerTextBox>().FirstOrDefault();
if (dateBox != null)
{
dateBox.Text = String.Empty;
}
// clear the SelectedDate property too, otherwise you'll be left with the last valid one
MyDatePicker.SelectedDate = null;
}
Xaml:
<sdk:DatePicker DateValidationError="DatePicker_DateValidationError"/>
I have a number of pickers all on the same form, and all sharing this DateValidationError handler, hence not finding the picker by name.

No need to search for datePicker1 if you have set x:Name already on it. You can access directly it in code behind:
datePicker1.Text = String.Empty;
Moreover, if it's part of some template which is restricting you from accessing it, it is already passed in you method as sender object. You can get it like this:
DatePicker myElement = (DatePicker)sender;
myElement.Text = String.Empty;

Related

How to access a windows forms comboboxes' variables? C#

I have a small problem regarding C# and WindowsForms.
I'm trying to get string SelectedItemName = combobox2.SelectedItem.ToString(); this variable in another class. For example I have this in my Form1.cs Class.
public void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
FileIniDataParser fileParser = new FileIniDataParser();
IniData data = fileParser.ReadFile("config.ini");
IniProgram classReference = new IniProgram();
string SelectedItemName = (string)comboBox2.SelectedItem.ToString();
// string _SelectedItemName = (string)comboBox2.SelectedText;
Console.WriteLine(SelectedItemName);
if (comboBox2.SelectedIndex > -1)
{
testvariabel2.GetSessionName();
}
}
And than my other Class CTestRack.cs looks like this:
if (_form1Object.comboBox2.SelectedIndex.ToString() != null)
{
string SelectedItemName = _form1Object.comboBox2.SelectedItem.ToString();
System.Threading.Thread.Sleep(1000);
if (newDictionary.ContainsKey(SelectedItemName))
Now I've tried getting and setting the variable in the Form1 class but I was just getting Loop errors, now with this method I'm getting a NULLReferenceException.
By the way I was already looking into several related posts here in SO but didn't found my answer yet.
My question is just how do I get the active Text from the Combobox in my other Class as a String?
Ensure that _form1Object, is indeed set to the instance of form you want to access... It's hard to tell from above code if correct initialization of _form1Object is the problem here.
Assuming _form1Object, is correctly initialized, one bug in above code is that ComboBox.SelectedIndex property is an int.
See http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindex(v=vs.110).aspx
It doesn't matter if there is SelectedItem or not, comboBox2.SelectedIndex.ToString() != null will always be true, but comboBox2.SelectedItem could still be null, and hence comboBox2.SelectedItem.ToString() would fail with NullReferenceException.
It's possible that comboBox2 doesn't have SelectedItem, you can check it
either by
comboBox2.SelectedItem != null
Or by
comboBox2.SelectedIndex >= 0
Something like that
if (comboBox2.SelectedItem != null) {
string SelectedItemName = comboBox2.SelectedItem.ToString();
Console.WriteLine(SelectedItemName);
testvariabel2.GetSessionName();
}
else {
// No selected item in the ComboBox
}
...
if (_form1Object != null)
if (_form1Object.comboBox2.SelectedIndex >= 0) {
string SelectedItemName = _form1Object.comboBox2.SelectedItem.ToString();
System.Threading.Thread.Sleep(1000);
if (newDictionary.ContainsKey(SelectedItemName))
...
}

Cannot find instance of dynamically added UserControl .Net

I have a UserControl which I am loading into a div which is inside an UpdatePanel. Here is my code for loading it:
controls.IDLControl IdlControl = LoadControl(#"~/controls/IDLControl.ascx") as controls.IDLControl;
IdlControl.ClientIDMode = ClientIDMode.Static;
IdlControl.ID = "IDLControl";
spGroup.Controls.Clear();
spGroup.Controls.Add(IdlControl);
And here is my code for trying to retrieve an instance of it:
controls.IDLControl IdlControl = RecursiveFindControl(this, "IDLControl") as controls.IDLControl;
private Control RecursiveFindControl(Control targetControl, string findControlId) {
if (targetControl.HasControls()) {
foreach (Control childControl in targetControl.Controls) {
if (childControl.ID == findControlId) {
return childControl;
}
RecursiveFindControl(childControl, findControlId);
}
}
return null;
}
But, all I get is null. I need help on figuring this out.
AFAIK, I need to re-add the control to the page on pre-init but it is one of the controls that can be added depending on which option is selected from a drop down list (which also is filled dynamically). I am stuck trying to figure out how to make this work.
You can try something like this to add your control back in the Page_Init based on the option selected in your DropDownList.
protected void Page_Init(Object sender, EventArgs e)
{
if (IsPostBack)
{
if (drpYourDropDown.Items.Count > 0 && drpYourDropDown.SelectedItem.Text == "yourOption")
{
AddIDLControl();
}
}
}
private void AddIDLControl()
{
controls.IDLControl IdlControl = LoadControl(#"~/controls/IDLControl.ascx") as controls.IDLControl;
IdlControl.ClientIDMode = ClientIDMode.Static;
IdlControl.ID = "IDLControl";
spGroup.Controls.Clear();
spGroup.Controls.Add(IdlControl);
}

Remove the last entered character in combobox

I am here with another problem.
I have setup my comboBox such that it accepts only those characters which matches with the name of any items in the comboBoxItems.
Now here I am stuck with a problem. Please have a look at my code then I will explain you the problem :
private void myComboBox_KeyUp(object sender, KeyEventArgs e)
{
// Get the textbox part of the combobox
TextBox textBox = cbEffectOn.Template.FindName("PART_EditableTextBox", cbEffectOn) as TextBox;
// holds the list of combobox items as strings
List<String> items = new List<String>();
// indicates whether the new character added should be removed
bool shouldRemoveLastChar = true;
for (int i = 0; i < cbEffectOn.Items.Count; i++)
{
items.Add(cbEffectOn.Items.GetItemAt(i).ToString());
}
for (int i = 0; i < items.Count; i++)
{
// legal character input
if (textBox.Text != "" && items.ElementAt(i).StartsWith(textBox.Text))
{
shouldRemoveLastChar = false;
break;
}
}
// illegal character input
if (textBox.Text != "" && shouldRemoveLastChar)
{
textBox.Text = textBox.Text.Remove(textBox.Text.Length - 1);
textBox.CaretIndex = textBox.Text.Length;
}
}
In the last if condition I am removing the last character from the combobox. But user can use arrow keys or mouse to change the position of the cursor and enter the text at the middle of the text.
So if by entering a character at the middle of the text if the text becomes invalid I mean if it does not match the Items in the ComboBox then I should remove the last entered character. Can anybody suggest me how to get the last inserted character and remove it?
Update :
string OldValue = "";
private void myComboBox_KeyDown(object sender, KeyEventArgs e)
{
TextBox textBox = cbEffectOn.Template.FindName("PART_EditableTextBox", cbEffectOn) as TextBox;
List<String> items = new List<String>();
for (int i = 0; i < cbEffectOn.Items.Count; i++)
{
items.Add(cbEffectOn.Items.GetItemAt(i).ToString());
}
OldValue = textBox.Text;
bool shouldReplaceWithOldValue = true;
string NewValue = textBox.Text.Insert(textBox.CaretIndex,e.Key.ToString()).Remove(textBox.CaretIndex + 1,textBox.Text.Length - textBox.CaretIndex);
for (int i = 0; i < items.Count; i++)
{
// legal character input
if (NewValue != "" && items.ElementAt(i).StartsWith(NewValue, StringComparison.InvariantCultureIgnoreCase))
{
shouldReplaceWithOldValue = false;
break;
}
}
//// illegal character input
if (NewValue != "" && shouldReplaceWithOldValue)
{
e.Handled = true;
}
}
Here I have tried to move all the code in KeyDown event to solve the above problem. This code works just fine but have 1 problem.
If I have any item named Birds & Animals then After typing Birds and a space I cannot type &.
I know what is the problem but don't know the solution.
The Problem is : To type & I have to press shift key and then press the 7 key. But both are sent as different keys.
Solutions that I think about :
1) I should move my code to KeyUp event. But here the problem of long press and fast typing will arise.
2) I think I should replace e.Key with something. But don't know what.
I'm not sure if this is what you're trying to do but I feel like you're trying to do what we typically see in visual studio Intellisense fitlering out results as we type.
Instead of removing the keystrokes, you should be using the validation mechanisms that WPF provides. Here's a sample of how this could work.
Scenarios covered:
Input matches a combox item completely: TypedInput & SelectedItem both show full match.
Input matches some element partially: TypedInput shortlists the popup list. The binding shows the matching text while SelectedItem remains null.
Input doesn't match any item in list either from start or at some
random point: user is visually given feedback (with possibility to
add additional feedback information) with typical red outline. The
TypedInput remains at last valid entry, SelectedItem may or may
not be null depending on if the last TypedInput matched any item or not.
Full Code:
MainWindow.xaml
<Window x:Class="Sample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:Sample"
Title="MainWindow" Height="350" Width="525"
DataContext="{Binding Source={x:Static l:MainWindowViewModel.CurrentInstance}}">
<StackPanel>
<TextBlock>
<Run Text="Typed valid text" />
<Run Text="{Binding TypedText}"/>
</TextBlock>
<TextBlock>
<Run Text="Valid SelectedItem" />
<Run Text="{Binding SelectedItem}"/>
</TextBlock>
<ComboBox ItemsSource="{Binding FilteredItems}" IsEditable="True" IsTextSearchEnabled="False" SelectedItem="{Binding SelectedItem}">
<ComboBox.Text>
<Binding Path="TypedText" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<l:ContainsValidationRule />
</Binding.ValidationRules>
</Binding>
</ComboBox.Text>
</ComboBox>
</StackPanel>
</Window>
MainWindow.xaml.cs
namespace Sample
{
public partial class MainWindow { public MainWindow() { InitializeComponent(); } }
}
ContainsValidationRule.cs -- Meat of the solution
namespace Sample
{
using System.Globalization;
using System.Linq;
using System.Windows.Controls;
public class ContainsValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
var result = MainWindowViewModel.CurrentInstance.Items.Any(x => x.ToLower(cultureInfo).Contains((value as string).ToLower(cultureInfo)));
return new ValidationResult(result, "No Reason");
}
}
}
MainWindowViewModel - Supporting ViewModel Singleton
namespace Sample
{
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
public sealed class MainWindowViewModel : INotifyPropertyChanged
{
private string _typedText;
private string _selectedItem;
private static readonly MainWindowViewModel Instance = new MainWindowViewModel();
private MainWindowViewModel()
{
Items = new[] { "Apples", "Apples Green", "Bananas", "Bananas & Oranges", "Oranges", "Grapes" };
}
public static MainWindowViewModel CurrentInstance { get { return Instance; } }
public string SelectedItem
{
get { return _selectedItem; }
set
{
if (value == _selectedItem) return;
_selectedItem = value;
OnPropertyChanged();
}
}
public string TypedText
{
get { return _typedText; }
set
{
if (value == _typedText) return;
_typedText = value;
OnPropertyChanged();
OnPropertyChanged("FilteredItems");
}
}
public IEnumerable<string> Items { get; private set; }
public IEnumerable<string> FilteredItems
{
get
{
return Items == null || TypedText == null ? Items : Items.Where(x => x.ToLowerInvariant().Contains(TypedText.ToLowerInvariant()));
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
var handler = PropertyChanged;
if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
}
}
}
Instead of KeyUp event, subscribe to TextChanged event on your ComboBox Textbox. In event handler you can get the offset where the change has occured. You can use your validation logic inside the hanlder and delete the character at the offset if it makes Text invalid.
private void Window_Loaded(object sender, RoutedEventArgs e)
{
TextBox textBox = cbEffectOn.Template.FindName("PART_EditableTextBox", cbEffectOn) as TextBox;
textBox.TextChanged += new TextChangedEventHandler(textBox_TextChanged);
}
void textBox_TextChanged(object sender, TextChangedEventArgs e)
{
int index = e.Changes.First().Offset;
}
Have you considered using a string variable to hold the last legal text value in the text box portion of the combo box?
Initially, this string would be empty, as the user has not typed anything yet, then as each KeyUp event is handled, if an invalid character is input, then the previous string value is used to replace the text of the text box; otherwise the previous string value is now updated with the new complete string; awaiting anymore input by the user.

FlipView SelectionChange C#

I am making a simple metro app to display an image and some content relevant to the image.
Ex :
image : data
img1 : "Image of a butterfly"
img2 : "Hello sky"
img3 : "Picture of a Golden Retriever"
I have loaded images into a flipview.And relevent data into an array.
<FlipView HorizontalAlignment="Left" Margin="102,147,0,0" VerticalAlignment="Top" Width="627" Height="429" Name="fiImage" SelectionChanged="fiImage_SelectionChanged">
<Image Source="Assets/image1.png" Name="Img1" />
<Image Source="Assets/image2.png" Name="Img2" />
</FlipView>
I have a TextBlock in the xaml named as "tbN". What I want to do is when I change the image using pointer, relevant data should display in the textblock.
I tried following code at selection change event
private void fiImage_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int i = ((FlipView)sender).SelectedIndex;
tbN.Text = a[i]; //error line
}
But when I execute the program, I get an error saying "NullReferenceException was unhandled by the user code: Object reference not set to an instance of an object."
What am I missing?
It seems a[i] has not being initialized and doesn't have values. Is it a global variable?.
Debug your code and check it's content. It must be null.
If it is not null, maybe that array doesn't have the i value. Or maybe it's exceding the array length.
tbN.Text = a[i]; //a[i] must be null, where is it initialized?
SelectionChanged event fires at initialization as well (first child "selected").
Your control (tbN) does not exist at the time.
Check if tbN is null to avoid NRef. Exception!
private void fiImage_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (tbN != null)
{
int i = ((FlipView)sender).SelectedIndex;
tbN.Text = a[i];
}
}
I've found the solution HERE
private void FlipView_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
FlipView fv = sender as FlipView;
if (fv.SelectedItem == null) return;
var item = fv.ItemContainerGenerator.ContainerFromItem(fv.SelectedItem);
if (item == null)
{
Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
{
var itemSecondTime = fv.ItemContainerGenerator.ContainerFromItem(fv.SelectedItem);
if (itemSecondTime == null)
{
throw new InvalidOperationException("no item. Why????");
}
});
}
}

How to validate a value without updating the binding target?

I want to allow the user to input only text that is validated by the TextBox binding validation rules. I figured out a way of doing that:
public static void PreviewTextChanged(
object sender,
PreviewTextChangedEventArgs e)
{
var textBox = e.Source as TextBox;
var bindingExpression = textBox.GetBindingExpression(TextBox.TextProperty);
if (!ReferenceEquals(null, bindingExpression))
{
// save original parameters for possible restoration
var originalSelectionStart = textBox.SelectionStart;
var originalSelectionLength = textBox.SelectionLength;
var originalText = textBox.Text;
// check validation
textBox.Text = e.Text;
if (!bindingExpression.ValidateWithoutUpdate())
{
// restore original values
textBox.Text = originalText;
bindingExpression.UpdateSource();
textBox.SelectionStart = originalSelectionStart;
textBox.SelectionLength = originalSelectionLength;
}
else
{
// correct the selection
var selectionStart = originalSelectionStart +
originalSelectionLength +
e.Text.Length -
originalText.Length;
textBox.SelectionStart = Math.Max(selectionStart, 0);
textBox.SelectionLength = 0;
}
e.Handled = true;
}
}
The code above works. But it would be much simpler, and less bug-prone, if I could find a way to check if the new value is valid without updating the binding target. Is there one?
I think it's easier in this case to relay on Binding Converters.
The converter will be called every time the data should be assigned to the binded field.
Inside methods of it, you can validate the input of the data and based on validation result return or old data (cause validation fails) recovered from ModelView or accept it, in case when validation succeeded.
Hope this helps.

Categories

Resources