How to auto cap a textbox - silverlight

How do I make it so that when a user types letters into a textbox in silverlight that it changes these letters to all capitals? And at the same time still fires all the same events such as keypress.

If you want to avoid code behind or custom controls (preferable), this sort of functionality is available via behaviours.
e.g. this one in the Expression blend Gallery converts any text box to which it is attached into uppercase.
Then is just a matter of drag-drop in Blend or adding the trigger manually in VS2010.

There was no easy way to do it except to use an embedded all caps font. All other ways interfered with how the textbox worked.

The simplest way would be:
private void tb_TextChanged(object sender, TextChangedEventArgs e)
{
var tb = (TextBox)sender;
var caret = tb.CaretIndex;
tb.Text = tb.Text.ToUpper();
tb.CaretIndex = caret;
}
If you wanna save even more performance, handle the LostFocus event instead (which you then don't have to worry about carret position either):
private void tb_LostFocus(object sender, RoutedEventArgs e)
{
var tb = (TextBox)sender;
tb.Text = tb.Text.ToUpper();
}
But if the data is bound to a model/entity I would call the ToUpper() on the bound object property setter / OnPropertyChanged, OnTextChanged or whatever it is in the entity.

Related

Winforms- Stop SelectedItem being highlighted without using the style DropDownList

This is about ComboBox used in Winforms. I need to stop the selected item being heighlighted. I know I can get it done if I set the style of the combobox to ComboBoxStyle.DropDownList.
But I'm looking for a solution where I don't have to use that. Instead, at the moment what I have done is using ComboBoxStyle.DropDown.
I don't have any other option, because if I set it to DropDown, I have to deal with some other issue in my code. It's due to something else which I cannot avoid.
Can someone suggest an alternative pls ?
use the following code in your form's Paint event.
private void myForm_Paint(object sender, PaintEventArgs e)
{
comboBox1.SelectionLength = 0;
}
or pass focus to another control in your combo box selected index changed event:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
comboBox2.Focus();
}

How can I get more than one character to show in a messagebox from a textbox user input?

I have made a textbox and I want the user to type in a string of numbers and hit enter. I have setup the following:
private void textBox1_TextChanged(object sender, EventArgs e)
{
String UserBarcode;
Focus();
UserBarcode = Console.ReadLine();
MessageBox.Show(UserBarcode);
}
When I enter any key into the textbox, I get a message box with nothing in it. I want to have the program wait til it hears the enter key then display the contents of the textbox.
The Textbox.TextChanged event fires as soon as the text in the textbox is changed at all. If you want a message box with the full string, you probably want to consider using the Textbox.LostFocus event or a button's Click event.
So you could have something like (I'm taking a stab at this here, as I've used VB rather than C#)
private void textBox1_LostFocus(object sender, EventArgs e)
{
MessageBox.Show(sender.Text)
}
If you're using a button, the above function should work, but you'll want to substitute textBox1.Text for sender.Text.
Take a look at Focus and Validation Events
There are several events that you can handle, depending on your goals and how your application is designed. If you want to perform validation and/or are using data binding, you may want to go with handling the validating/validated events. By default data bindings update a bound property after OnValidating. If you use LostFocus and read the value from a bound object, instead of your control, you will get inconsistent results.
I was able to figure it out finally. For some reason when I manually entered the code I kept getting multiple random errors. I started a new Visual C # Windows Forms Application, Made a textbox, chose the keydown property and double clicked on it to have the program inject the code for the keydown function and then I filled in the if statement pointing to the enter key. The final code looks like this:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
MessageBox.Show(textBox1.Text);
}
}

Which control to show readonly colorful text in WPF?

I want an element or a control to show readyonly, colorful, selectable, scrollable text which is a kind of log in my application. I don't know whether it is fixed document or flow document.
The RichText may be the seeming choice, but it originally supports editing. I believe even I set readonly=true, the build-in editing support takes some resources. I want to find a lighter-weight one.
Perhaps the FlowDocumentScrollViewer? It is readonly and do not show tool bar by default. Even I turn IsToolBarVisible on, the tool bar is just a small control.
The Block came into my mind. Although it may be the lightest control, I cannot select the text in it without other effort.
Maybe other choices exist? What's your opinions?
I made an experiment to help me choose my preferable control among FlowDocumentScrollViewer, RichTextBox, and TextBlock. I find FlowDocumentScrollViewer is the best.
In each window I have two controls of same type: FlowDocumentScrollViewer, RichTextBox, or TextBlock. And I made three such windows, as the MainWindow has three buttons.
private void prepareButton_Click(object sender, RoutedEventArgs e)
{
document1 = HelperClass.GetDocument();
document2 = HelperClass.GetDocument();
}
private void loadButton_Click_1(object sender, RoutedEventArgs e)
{
Stopwatch watch = new Stopwatch();
watch.Start();
viewer1.Document = document1;
viewer2.Document = document2;
this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
new Action(() =>
{
watch.Stop();
MessageBox.Show("Took " + watch.ElapsedMilliseconds + " ms",Title);
}));
}
Where viewer1 and viewer2 can be FlowDocumentScrollViewer or RichTextBox.
For TextBlock, I use
private void prepareButton_Click(object sender, RoutedEventArgs e)
{
inlines1 = HelperClass.GetInlines();
inlines2 = HelperClass.GetInlines();
}
private void loadButton_Click_1(object sender, RoutedEventArgs e)
{
Stopwatch watch = new Stopwatch();
watch.Start();
viewer1.Inlines.AddRange(inlines1);
viewer2.Inlines.AddRange(inlines2);
this.Dispatcher.BeginInvoke(DispatcherPriority.Loaded,
new Action(() =>
{
watch.Stop();
MessageBox.Show("Took " + watch.ElapsedMilliseconds + " ms");
}));
}
The test indicates FlowDocumentScrollViewer has best performance among the three:
FlowDocumentScrollViewer RichTextBox TextBlock
Working set 65400 67252 82124
Loading Time 1045 1414 45119
I'm not sure what type of resources you think are being taken up by "editing" functionality. The ability to select text goes hand in hand with ability to edit text.
If you want one, you'll have to put up with the other. Luckilly, setting IsReadOnly to "True" will satisfy your functional requirements.
If your application machine is capable of running the .NET Framework with WPF, I wouldn't worry about tiny amounts of resources which may (or may not) be consumed by the ability to edit simple text.

How to give an initial value to textBox?

I want my C# program to have initial values for its textboxes. For example, in one of the textboxes, it should say "Please enter your name".
When you click (or tabStop) on the textbox, the initial value should disappear and the user will be able to enter their input to the textbox.
I can do all this with click_event, but using this method the initial text would not have less opacity. How am I able to achieve this?
This is how I finally did it:
Boolean first_time_click = true;
private void Form1_Load(object sender, EventArgs e)
{
textBox1.ForeColor = System.Drawing.Color.Gray;
textBox1.Text = "Enter the Text";
}
private void For_First_Click()
{
if (first_time_click)
{
textBox1.Clear();
textBox1.ForeColor = textBox1.ForeColor = SystemColors.WindowText;
}
first_time_click = false;
}
private void textBox1_Click(object sender, EventArgs e)
{
For_First_Click();
}
I assume you are talking about winform (tabstop) you have to handle it within the event key-press. you can use the below code:
TextBox1.Select(0, TextBox1.Text.Length);
this will select the text and window will remove it for you as soon as the user start to typing
you can use the same code to have this behavior also for TabStop
All you need to do is set the Textbox's .Text property and use GotFocus event to clear the box when the person clicks (or tabs) into it to start typing.
Always remember that there are more ways than the mouse to navigate a form, so use the GotFocus event to determine when the user enters a control, and use the Validated event to determine when they've changed data and exited the control.
For this type of effect you need java script.Because java script provide you functionality of mouse hover and mouse out these are the functions which provide you the same functionality which u seeing in this page of search bar. If you need code reply me i can give you.

WPF ComboBox, force input to UpperCase

I have an editable WPF ComboBox with TextSearchEnabled. I need to force the user's text input to uppercase when they type to filter the ComboBox.
I was thinking of modifying the textbox that is part of the control (named 'PART_EditableTextBox') to set CharacterCasing="Upper", however I can't quite figure out how to do this.
Do I need to use a trigger, or modify the template in some way?
This works and seems like a reasonable solution:
protected void winSurveyScreen_Loaded(object sender, RoutedEventArgs e)
{
(comboBox.Template.FindName("PART_EditableTextBox", cbObservation) as TextBox).CharacterCasing = CharacterCasing.Upper;
}
Ensure that the combobox is not collapsed on loaded otherwise the template will not be found.
IMO, the quicker way is to set the UpdateTrigger to PropertyChanged and, in the data object, uppercase the value when it is updated.
I found that post where the attached property is used. That permit to use that for all of your ComboBox without rewriting the code.
private void TextBox_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
Textbox editableTextbox = sender as Textbox;
foreach (char ch in e.Text)
{
if (Char.IsLower(ch))
{
editableTextbox.Text += Char.ToUpper(ch);
e.Handled = true;
}
}
}
or try creating an attached behaviour for the textbox

Resources