Is there a way to disable a user from clicking within a text box? I've tried .ReadOnly but this disables the user from typing in the text box. I want to be able to let the user type their name maybe "Peter" but disallow clicking back so they can't type over.
Use the MouseUp event:
void textBox1_MouseUp(object sender, MouseEventArgs e) {
textBox1.SelectionStart = textBox1.Text.Length;
}
If WPF you can bind Focusable and just return false once a value has been set. Or just explicitly call tbDisable.Focusable = False;
Another possibility is listening to the "Preview..." mouse events of the TextBox and setting e.Handled = true. In contrast to the accepted answer this will fully prevent mouse interactions - and not just hide the selection on MouseUp.
Related
WPF's TextBox has a property named IsInactiveSelectionHighlightEnabled. I set this property to true in order to make a TextBox always show selection. However, it doesn't work in this case:
private void button_Click(object sender, RoutedEventArgs e) {
textBox.Select(0, 10);
}
I just want to see the selection after clicking the button. But selection will not appear until I right click the TextBox. Why? Am I miss something?
You should have the Keyboard focus on your textbox to select the text in it.
Add this code to your button click event before the selection.
Keyboard.Focus(textBox);
Hope it helps.
I've had an annoying issue with focus for controls on a ToolBar in WPF. My toolbar has a CheckBox and a TextBox control next to eachother. If you click and edit the text in the text box, then click the check box, the text box steals the focus back after clicking the check box, and the check box state is not changed when the user clicks on it.
I have similar issues with text boxes all over my application, but I believe this is the simplest case to explain, and I hope that the problem will be a common issue across all my text boxes.
Does anyone know what might be going on here?
Hook up a handler to the checkbox's Click event (or even the PreviewMouseUp event), and set the event's Handled property to 'true'.
Did you try that?:
private void MyCheckBox_Click(object sender, RoutedEventArgs e)
{
MyCheckBox.Focus();
}
Working in Windows Forms (C#), creating a Wizard, I'd like to require the user to select an option in a combobox before being allowed to click "Next" to the next page in the form.
I thought I saw where to do this in the past, but I cannot find anything now.
Thx for any help...!
J
There are multiple ways of doing this. And different application use their preferred way.
One way to have an empty or 'Select Value' option at the top of the list of your combo box. Then when the user click the 'Next' button, check whether this is the value which is selected. If so, don't allow to go next. Otherwise allow to proceed.
My way is to set 'SelectedValue' property to -1 (means select nothing) and check whether is it -1 when the user press 'Next'. (If any valid value is selected, then this property should have a value higher than -1.)
Trigger on the selection changed event for the combo box, and then set the button enabled property:
private void comboBoxSelectionChanged(obj sender, EventArgs e)
{
nextButton.enabled = true;
}
There are many ways you can validate the selection ...or force a selection ...2 off the top of my head:
set the combobox to CausesValidation (IIRC) to true and handle xxxValidating(o,e) and xxxValidated(o,e) events
handle Next button's OnClick event and check the combobox SelectedItem or SelectedIndex properties:
/* sudo */
(o, e) => {
if(fooCombo.SelectedIndex == {...}) {
// show dialog, etc.
}
}
I have a ToolStripButton that performs an action. The user clicks the button and the button is disabled to prevent the action being performed twice. After the action is complete the button is re-enabled. It all works perfectly...except:
Because the button is disabled it does not fire the "MouseLeave" event and as a result the appearance of the button is not updated. To be absolutely clear, when the mouse enters a ToolStripButton the button is highlighted in orange (by default) with a black box around it. This highlight is not being removed when I re-enable the button. The mouse cursor is, by this time, long gone from the control. Mousing over the button naturally fixes the button by redrawing it.
What I would like to do would be some method on the ToolStripButton that "resets" its appearance. Such a method may even exist on the ToolStrip, but despite searching I have been unable to find anything like this.
As an alternative I could fire the "Mouse Leave" event on the button directly. As far as I know there is no way to easily do this in C# .NET.
Any advice at this point in time would be most appreciated, naturally I don't want to tear up my application and replace the tool strip.
Update:
I reproduced your problem, trying figuring out!
I didn't get a better way other than reset the style in the click event
private void toolStripButton1_Click(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
toolStripButton1.Enabled = false;
}
private void toolStripButton1_MouseEnter(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.Red;
}
private void toolStripButton1_MouseLeave(object sender, EventArgs e)
{
toolStripButton1.BackColor = Color.FromKnownColor(KnownColor.Control);
}
Hope this helps!
Have you tried Control.Invalidate()?
from MSDN: Invalidates the entire surface of the control and causes the control to be redrawn.
I had the same problem. I "fixed" it by hiding and then showing back the ToolStripButton using the Visible property after the task was complete.
Before disabling ToolStrip or ToolStripItem:
private void RemoveHighlightFromToolStrip(ToolStrip toolStrip)
{
foreach (ToolStripItem item in toolStrip.Items)
{
if (item.Pressed || item.Selected)
{
item.Visible = false;
item.Visible = true;
}
}
}
also you can just hide and show entire ToolStrip, but this may affect other controls in your form (i.e. if you have some docked DataGridView it would be redrawn)
I have a combobox on the winforms. On Enter even I open it:
cbo.DroppedDown = true;
But if I do that the combo opens and closes immediately. What am I doing wrong?
The reason why I use this event is I need to open combo on Tab, when user click tab on the previous control, this combo opens properly. But if user clicks the combo it opens and closes. How to do that properly?
Thanks
I tried it just like this:
private void comboBox1_enter(object sender, EventArgs e)
{
comboBox1.DroppedDown = true;
}
no changes to mouseup or timers. it behaved just as expected. Whether I selected the comboBox with a mouse click or tabbed into it the drop down list appeared and stayed open until I selected something.
I would look to see if there is something else pulling focus off the box.
The reason you are having this problem is because the mouseup event occurs after the enter event and the default window procedure is closing the combobox.
In the enter you could check the mouse button status and if the button is down, do not open the combo. Then have another event handler for the mouseup event to open the combo.
Another option is to set a timer for a few milliseconds, and open the combo when it goes off.
Knowing this is a bit old, but I found that this works well. You can TAB into the combo box and it opens and if you click the arrow it doesn't close back up.
private void ComboBox_Enter(object sender, EventArgs e)
{
if (MouseButtons == MouseButtons.None)
((System.Windows.Forms.ComboBox)sender).DroppedDown = true;
}
Set DroppedDown = true in GotFocus event of the combobox. Otherwise, the dropdown list will show at wrong location.
void cbo_GotFocus(object sender, EventArgs e)
{
ComboBox cbo = sender as ComboBox;
cbo.DroppedDown = true;
}
I think you just need to focus it first before open the comboBox.
cbo.Focus();
cbo.DroppedDown = True
Hope it works for you.