2

I actually have two questions so Ill start with the first: I am getting ready for a project I'm working on and seeing if I can limit the range of characters in the text box field. It has to all be numbers and needs to be eight characters. no more no less. Is there any way I can do that. Because it does not like it when I use the .length line and the code I used when ran will not display the error message. Here is my attempt: (the GUI isn't complex. it has a text box to type in an ID. two text blocks being the result and error message. with a button saying submit.) and My second question is how do I store the ID and display it?

 public partial class MainWindow : Window { int range; public MainWindow() { InitializeComponent(); } private void sub_Click(object sender, RoutedEventArgs e) { range = int.Parse(Type.Text); while (range>0 || range < 8) { Error.Text = "ID must be eight characters long."; } } } 
0

2 Answers 2

2

You can select the Maximum Length property for any TextBox:

YourTextBox.MaxLength = 50; 
Sign up to request clarification or add additional context in comments.

Comments

1

I create a simple demo for your one need: enter image description here

You could create the Textbox in the UI with Validation and MaxLength:

<TextBox Name="txtMyLength" MaxLength="8" Width="200" Margin="140" HorizontalAlignment="Left" VerticalAlignment="Center" Text="{Binding validModel.MyLength, ValidatesOnDataErrors=True}" /> 

Then set the Style for the TextBox for error tip:

<Style TargetType="TextBox"> <Setter Property="Validation.ErrorTemplate"> <Setter.Value> <ControlTemplate> <StackPanel Orientation="Horizontal"> <Border BorderThickness="1" BorderBrush="Red" VerticalAlignment="Top"> <Grid> <AdornedElementPlaceholder x:Name="adorner" Margin="-1"/> </Grid> </Border> <Border x:Name="errorBorder" Background="Red" Margin="8,0,0,0" Opacity="0" CornerRadius="0" IsHitTestVisible="False" MinHeight="24" > <TextBlock Text="{Binding ElementName=adorner, Path=AdornedElement.(Validation.Errors)[0].ErrorContent}" Foreground="Black" Margin="8,3,8,3" TextWrapping="Wrap" VerticalAlignment="Center"/> </Border> </StackPanel> <ControlTemplate.Triggers> <DataTrigger Value="True"> <DataTrigger.Binding> ...... </DataTrigger.Binding> <DataTrigger.EnterActions> <BeginStoryboard x:Name="in"> <Storyboard> ...... </Storyboard> </BeginStoryboard> </DataTrigger.EnterActions> <DataTrigger.ExitActions> <StopStoryboard BeginStoryboardName="in"/> <BeginStoryboard x:Name="out"> ....... </BeginStoryboard> </DataTrigger.ExitActions> </DataTrigger> </ControlTemplate.Triggers> </ControlTemplate> </Setter.Value> </Setter> </Style> 

create the condition in the ValidModel like

public string this[string columnName] { get { int txtLength = 0; string result = string.Empty; if (MyLength != "" && MyLength != null) { txtLength = MyLength.ToString().Length; } switch (columnName) { case "MyLength": if (txtLength != 8 ) result = "ID be 8 numbers"; break; case ....... }; return result; } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.