Create WPF TextBox that accepts only Text - wpf

private void txtLastName_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (!char.IsDigit((char)e.Key)) e.Handled = true;
}
But It not support all key in keyboard .

private void txtLastName_PreviewTextInput(object sender, TextCompositionEventArgs e)
{
if (!System.Text.RegularExpressions.Regex.IsMatch(e.Text, "^[a-zA-Z]"))
{
e.Handled = true;
}
}

You must use IsLetter.
private void txtLastName_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (Char.IsLetter((char)e.Key)) e.Handled = true;
}

This thread is really old, but if someone still needs it, here is code that is working for me
(Edited a little bit Usman's code)
private void TextValidationTextBox(object sender, TextCompositionEventArgs e)
{
Regex regex = new Regex("[^a-zA-Z]+");
e.Handled = regex.IsMatch(e.Text);
}
And don't forget to put code below to TextBox that you want to accept only text ( in xaml )
PreviewTextInput="TextValidationTextBox"

Related

Which textbox event will generate an email when the textbox is clicked?

All I want to do is input a Forename and a Surname into two textboxes and in the Email textbox generate an email address using the forename and surname. It isn't hard at all but none of the events I've tried have worked. I've even dummed it down so that if any of the events happen the email textbox should just say "hello".
None of these event have worked:
private void EmailAddressInputTextbox_TextChanged(object sender, EventArgs e)
{
EmailAddressInputTextbox.Text = ("Hello");
}
private void EmailAddressInputTextbox_GotFocus(object sender, EventArgs e)
{
EmailAddressInputTextbox.Text = ("Hello");
}
private void EmailAddressInputTextbox_Enter(object sender, EventArgs e)
{
EmailAddressInputTextbox.Text = ("Hello");
}
private void EmailAddressInputTextbox_Click(object sender, EventArgs e)
{
EmailAddressInputTextbox.Text = ("Hello");
}
Even tried:
private void SurnameInputTextbox_Leave(object sender, EventArgs e)
{
EmailAddressInputTextbox.Text = ("Hello");
}
Ideally I want the textbox to be filled when it has been clicked on and can be typed in but so far nothing happens...

changing datagridview selection color dynamically c#

the problem is that when i change the color of selected row by RowTemplate.DefaultCellStyle.SelectionBackColor in my form constructor it works but it does not work in button events when user clicks some button for changing grid selected back color!
any help please!
public Form1()
{
InitializeComponent();
dataGridView1.RowTemplate.DefaultCellStyle.SelectionBackColor=Color.Red; //this works fine
}
void button2_Click(object sender, EventArgs e)
{
dataGridView1.RowTemplate.DefaultCellStyle.SelectionBackColor=Color.Blue;//but this does not work
}
Try this ..
void dataGridView1_RowPrePaint(object sender,
DataGridViewRowPrePaintEventArgs e)
{
If (DatagridView1.Rows(DataGridView1.CurrentCell.RowIndex).Selected )
{
DataGridView1.Rows(DataGridView1.CurrentCell.RowIndex).DefaultCellStyle.SelectionBackColor=Color.Blue;
}
}
void button2_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow row in dataGridView1.Rows)
{
row.DefaultCellStyle.SelectionBackColor = Color.Blue
}
}
Update :
void button2_Click(object sender, EventArgs e)
{
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
col.DefaultCellStyle.BackColor = Color.Blue
}
}

Mouse click and drag Event WPF

I am developing an analog clock picker control.
The user is able to click on the minute or hour hand and drag to turn the needle to select the specific time. I was wondering how to detect such a click and drag event.
I tried using MouseLeftButtonDown + MouseMove but I cannot get it to work as MouseMove is always trigger when the mousemove happen despite me using a flag. Is there any easier way?
public bool dragAction = false;
private void minuteHand_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
dragAction = true;
minuteHand_MouseMove(this.minuteHand, e);
}
private void minuteHand_MouseMove(object sender, MouseEventArgs e)
{
if (dragAction == true)
{
//my code: moving the needle
}
}
private void minuteHand_MouseLeftButtonUp(object sender, MouseEventArgs e)
{
dragAction = false;
}
I think this is the easiest and most straightforward way :
private void Window_MouseMove(object sender, MouseEventArgs e) {
if (e.LeftButton == MouseButtonState.Pressed) {
this.DragMove();
}
}
public bool dragAction = false;
private void minuteHand_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
dragAction = true;
minuteHand_MouseMove(this.minuteHand, e);
}
private void minuteHand_MouseMove(object sender, MouseEventArgs e)
{
if (dragAction == true)
{
this.DragMove();
}
}
private void minuteHand_MouseLeftButtonUp(object sender, MouseEventArgs e)
{
dragAction = false;
}
does the trick
You can make things easier and need not handle mouse down / up :
private void minuteHand_MouseMove(object sender, MouseEventArgs e)
{
if (Mouse.LeftButton == MouseButtonState.Pressed)
{
//my code: moving the needle
}
}

How do I display a tooltip when focus is in a specific textbox?

For a textbox, I want to display a tooltip immediatly when the focus is in on the textbox, and stay there for the duration of the focus - not just when the mouse hovers over the textbox.
How can I do that?
The Enter and Leave events are probably useful here, and show it with a duration of 0 to keep it there.
private ToolTip tt;
private void textBox1_Enter(object sender, EventArgs e) {
tt = new ToolTip();
tt.InitialDelay = 0;
tt.IsBalloon = true;
tt.Show(string.Empty, textBox1);
tt.Show("I need help", textBox1, 0);
}
private void textBox1_Leave(object sender, EventArgs e) {
tt.Dispose();
}
Note: Calling the Show(...) method twice like in my example will force the "pointer" to point correctly to the control.
have tested, the event names:
private void textbox_Enter(object sender, EventArgs e)
{
toolTip1.Show("your tip here", textbox);
}
private void textbox_Leave(object sender, EventArgs e)
{
toolTip1.Hide(textbox);
}
tooltip is a control, needs to be added from toolbox.
using mouse hover and mouse leave events
private void textBox1_MouseHover(object sender, EventArgs e)
{
toolTip1.Show("your tip here", textBox2);
}
private void textBox1_MouseLeave(object sender, EventArgs e)
{
toolTip1.Hide(textBox2);
}
>
Windows Forms
public partial class FormWindow : Form
{
//Constructor
public FormWindow()
{
txtUrl.Text = "Enter text here";
txtUrl.ForeColor = Color.Gray;
txtUrl.GotFocus += TxtUrl_GotFocus;
txtUrl.LostFocus += TxtUrl_LostFocus;
}
private void TxtUrl_GotFocus(object sender, EventArgs e)
{
txtUrl.Text = "";
txtUrl.ForeColor = Color.Black;
}
private void TxtUrl_LostFocus(object sender, EventArgs e)
{
if (string.IsNullOrWhiteSpace(txtUrl.Text))
{
txtUrl.Text = "Enter text here";
txtUrl.ForeColor = Color.Gray;
}
}
}
Use a System.Windows.Forms.ToolTip and show it in textbox GotFocus event and Hide it in LostFocus event:
void textBox_GotFocus(object sender, EventArgs e)
{
toolTip.Show("your tip", textBox);
}
void textBox_LostFocus(object sender, EventArgs e)
{
toolTip.Hide(textBox);
}

WPF mediaelement

I have a MediaElement, but how can I call a function when the property "position" of MediaElement changes?
Position is not a DependencyProperty.
You can use a DispatchTimer. This article provides some good insight on how to get this working. MediaElement and More with WPF.
Here is some sample code that I took from a project I'm working on. It shows the position of the video using a slider control and allows the user to change the position.
I'm a bit of a newbie too, so it is possible that some of it is wrong (feel free to comment on problems in the comments section :).
private DispatcherTimer mTimer;
private bool mIsDragging = false;
private bool mTick = false;
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
medPlayer.Play();
medPlayer.Stop();
mTimer = new DispatcherTimer();
mTimer.Interval = TimeSpan.FromMilliseconds(100);
mTimer.Tick += new EventHandler(mTimer_Tick);
mTimer.Start();
}
void mTimer_Tick(object sender, EventArgs e)
{
if (!mIsDragging)
{
try
{
mTick = true;
sldPosition.Value = medPlayer.Position.TotalMilliseconds;
}
finally
{
mTick = false;
}
}
}
private void sldPosition_DragStarted(object sender, System.Windows.Controls.Primitives.DragStartedEventArgs e)
{
mIsDragging = true;
medPlayer.Pause();
}
private void sldPosition_DragCompleted(object sender, System.Windows.Controls.Primitives.DragCompletedEventArgs e)
{
mIsDragging = false;
if (chkPlay.IsChecked.Value)
medPlayer.Play();
}
private void sldPosition_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
var pos = TimeSpan.FromMilliseconds(e.NewValue);
lblPosition.Content = string.Format("{0:00}:{1:00}", pos.Minutes, pos.Seconds);
if (!mTick)
{
medPlayer.Position = TimeSpan.FromMilliseconds(sldPosition.Value);
if (medPlayer.Position == medPlayer.NaturalDuration.TimeSpan)
{
chkPlay.IsChecked = false;
medPlayer.Stop();
}
}
}

Resources