Open file dialog and select a file using WPF controls and C#

Something like that should be what you need private void button1_Click(object sender, RoutedEventArgs e) { // Create OpenFileDialog Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); // Set filter for file extension and default file extension dlg.DefaultExt = “.png”; dlg.Filter = “JPEG Files (*.jpeg)|*.jpeg|PNG Files (*.png)|*.png|JPG Files (*.jpg)|*.jpg|GIF Files (*.gif)|*.gif”; // Display OpenFileDialog by calling ShowDialog method Nullable<bool> … Read more

Set keyboard caret position in html textbox

Excerpted from Josh Stodola’s Setting keyboard caret Position in a Textbox or TextArea with Javascript A generic function that will allow you to insert the caret at any position of a textbox or textarea that you wish: function setCaretPosition(elemId, caretPos) { var elem = document.getElementById(elemId); if(elem != null) { if(elem.createTextRange) { var range = elem.createTextRange(); … Read more

How to automatically select all text on focus in WPF TextBox?

We have it so the first click selects all, and another click goes to cursor (our application is designed for use on tablets with pens). You might find it useful. public class ClickSelectTextBox : TextBox { public ClickSelectTextBox() { AddHandler(PreviewMouseLeftButtonDownEvent, new MouseButtonEventHandler(SelectivelyIgnoreMouseButton), true); AddHandler(GotKeyboardFocusEvent, new RoutedEventHandler(SelectAllText), true); AddHandler(MouseDoubleClickEvent, new RoutedEventHandler(SelectAllText), true); } private static void … Read more

Watermark / hint / placeholder text in TextBox?

You can create a watermark that can be added to any TextBox with an Attached Property. Here is the source for the Attached Property: using System; using System.Collections.Generic; using System.ComponentModel; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Documents; /// <summary> /// Class that provides the Watermark attached property /// </summary> public static class WatermarkService { … Read more

How do I automatically scroll to the bottom of a multiline text box?

At regular intervals, I am adding new lines of text to it. I would like the textbox to automatically scroll to the bottom-most entry (the newest one) whenever a new line is added. If you use TextBox.AppendText(string text), it will automatically scroll to the end of the newly appended text. It avoids the flickering scrollbar … Read more

How do I get a TextBox to only accept numeric input in WPF?

Add a preview text input event. Like so: <TextBox PreviewTextInput=”PreviewTextInput” />. Then inside that set the e.Handled if the text isn’t allowed. e.Handled = !IsTextAllowed(e.Text); I use a simple regex in IsTextAllowed method to see if I should allow what they’ve typed. In my case I only want to allow numbers, dots and dashes. private … Read more