So the scenario that I have is that there is a Form with three text boxes and a button. Clicking the button sets textBox1.Enabled = false, textBox2.Enabled = false, and textBox3.Focus().
The problem that I'm running into is that if either textBox1 or textBox2 has focus at the moment the user clicks the button, the text box becomes disabled but retains a greyed-out version of the focus rectangle. It's like the form isn't redrawing the disabled text box. Please observe the attached screenshot and notice the difference between the first and second text box.
How do I ensure that I move focus to textBox3 and get rid of the focus rectangle around textBox1?
I'm not sure if this behavior is a bug, but i found way to handle it. The trick is play around with BorderStyle property.
private void button1_Click(object sender, EventArgs e)
{
textBox3.Focus();
var borderStyle = textBox1.BorderStyle;
textBox1.BorderStyle = BorderStyle.None;
textBox2.BorderStyle = BorderStyle.None;
textBox1.Enabled = false;
textBox2.Enabled = false;
textBox1.BorderStyle = borderStyle;
textBox2.BorderStyle = borderStyle;
textBox1.Refresh();
}
Related
I have a textbox that requires data to be entered in a certain way. I have implemented some cell validating techniques to check the data after it has been entered, but I'd like to provide the user with some information before they enter the data.
To that end, I'd like to add a tooltip to the textbox that pops up when the user enters the toolbox, then exits when they begin to type.
For example I have the following code:
private void YearEdit_Enter(object sender, EventArgs e)
{
ToolTip tt = new ToolTip();
tt.IsBalloon = true;
tt.InitialDelay = 0;
tt.ShowAlways = true;
tt.SetToolTip(YearEdit, "Enter 4 digit year.");
}
This executes when the user enters the textbox, however the tooltip only appears when the mouse hovers over the textbox. Does anyone have any ideas to work around this? I thought that perhaps tt.ShowAlways = true might work, but obviously not.
Hook into the textbox.enter event and use the following code:
private void textBox1_Enter(object sender, EventArgs e)
{
TextBox TB = (TextBox)sender;
int VisibleTime = 1000; //in milliseconds
ToolTip tt = new ToolTip();
tt.Show("Test ToolTip",TB,0,0,VisibleTime);
}
Play with X/Y values to move it where you want. Visible time is how long until it disappears.
Tooltips only appear when the mouse is still by design.
You could try setting the InitialDelay to 0:
tt.InitialDelay = 0;
But this would still require the mouse to be stationary for an instant.
However there are other approaches. A common way of showing what input is required is to use a watermark (faded text) in the textbox that displays the formatting required until the user starts typing.
If you really want a tooltip then you could either add an information icon (usually an "i") which will show the tooltip when it's hovered over, or implement your own.
It might also work if you break the date into parts (separate day, month, year). This will allow you more control over what the user can enter - the month can become a drop down/combo box so it's always the correct format.
you can show a tooltip also like this:
ToolTip t = new ToolTip();
t.Show("Hello World", textBox1, 1000);
Try this. (based on an answer above)
Add event handlers for all controls that you want to have a ToolTip for. Point all the event handlers to the same method. Then construct you handling method like this
private void procToolTips(object sender, EventArgs e)
{
ToolTip tt = new ToolTip();
Control o = (Control)sender;
if ( o.Name == "label1") {
tt.Show("Lorem ipsum dolor sit ame", o, 1000);
}
}
You should use if ( o.Name == label1.Name) instead of if ( o.Name == "label1"), for if you rename label1, this line will be modified too.
More : if(o.equals(label1))...
I'm developing a windows forms application. And i have the following issue: in a form there's a panel, and in that panel i have a number of controls (just a label with a text box, the number is determined at runtime). This panel has a size that is smaller than the sum of all the controls added dynamically. So, i need a scroll. Well, the idea is: when the user open the form: the first of the controls must be focused, the the user enters a text and press enter, the the next control must be focused, and so until finished.
Well it's very probably that not all the controls fit in the panel, so i want that when a control inside the panel got focus the panel scrolls to let the user see the control and allow him to see what he is entering in the text box.
I hope to be clear.
here is some code, this code is used to generated the controls and added to the panel:
List<String> titles = this.BancaService.ValuesTitle();
int position = 0;
foreach (String title in titles)
{
BancaInputControl control = new BancaInputControl(title);
control.OnInputGotFocus = (c) => {
//pnBancaInputContainer.VerticalScroll.Value = 40;
//pnBancaInputContainer.AutoScrollOffset = new Point(0, c.Top);
// HERE, WHAT CAN I DO?
};
control.Top = position;
this.pnBancaInputContainer.Controls.Add(control);
position += 10 + control.Height;
}
If you set AutoScroll to true this will be taken care of automatically. As for the idea that Enter should move focus to next field, the best solution would be to execute enter as if it was TAB key in BancaInputControl:
protected override void OnKeyDown(KeyEventArgs e)
{
base.OnKeyDown(e);
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
// Move focus to next control in parent container
Parent.SelectNextControl(this, true, true, false, false);
}
}
If BancaInputControl is composite control (a UserControl containing other controls) each child control should hook up KeyDown event to this handler. It tries to move focus to next control in BancaInputControl; if it fails, moves focus to parent container's next control.
private void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
e.Handled = true;
if (!SelectNextControl((Control)sender, true, true, false, false))
{
Parent.SelectNextControl(this, true, true, false, false);
}
}
}
I have a class derived from TextBox in C#. I override OnClick method to show a file open dialog. Is it possible to lose focus after that? I don't want the user to be able to edit the text because at a moment the file name might be invalid. I tried to set ReadOnly = true, but one can change the text after selecting the file.
EDIT:
I added the relevant code for this. As it is now the focus will be set to next control from my Form.
class Property : TextBox
class FileSelectTextBox : Property
{
protected override void OnClick(EventArgs e)
{
OpenFileDialog dialog = new OpenFileDialog();
Enabled = false;
if (dialog.ShowDialog(this) == DialogResult.OK)
{
Text = dialog.FileName;
}
Enabled = true;
}
}
You have several options here:
Make the textbox ReadOnly. The textbox will still fire OnClick events but the text won't be editable by the user.
Disable the textbox at the end of your click event -- the disadvantage is that the click event won't fire a second time (which means the user won't be able to change their mind and pick a new file).
Simply set the focus somewhere else at the end of the click event. (someOtherTextBox.Focus())
Edit: Once last suggestion: you may want your file popup to happen in FocusGained rather than OnClick, that way the dialog will still pop up if the user tabs into the control. Of course it's your decision if that behavior is desired or not.
Edit 2: Ignore that last edit. It's a bad suggestion that I didn't think through. (Thanks for the heads up commenter)
set the ReadOnly = true property of the textbox (don't change it at any point of time) and it should work lonely..
and rest of the code goes like this..
protected override void OnClick(EventArgs e)
OpenFileDialog dialog = new OpenFileDialog();
//user can still change/edit some non-existing file/path and click OK, so set the followings
dialog.CheckFileExists = true;
dialog.CheckPathExists = true;
if (dialog.ShowDialog(this) == DialogResult.OK)
{
Text= dialog.FileName;
}
}
I am using the following code so that gridview cell support multiline while in edit mode :
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
((DataGridViewTextBoxEditingControl)e.Control).AcceptsReturn = true;
}
when I press SHIFT+ ENTER ,gridviewcell provides newline but whole content of cell is not displayed as current line moves up and displays only one line at a time at which your text caret is blinking.
Is there anyway I can show whole editing cell with multiline while in edit mode itself?
try this....
The best way is by handling the EditingControlShowing event of the grid and add the following code
if ((e.Control.GetType() == TextBox))
{
TextBox txtB = new TextBox();
txtB = e.Control;
txtB.Multiline = true;
txtB.ScrollBars = ScrollBars.Both;
}
I am trying to get a tooltip to display on a disabled textbox during a mouse over. I know because the control is disabled the following won't work:
private void textBox5_MouseHover(object sender, EventArgs e)
{
// My tooltip display code here
}
How can I get the tooltip to display on a mouse over of a disabled control?
Many thanks
MouseHover wont fire if control is disabled. Instead you can check in Form MouseMove event whether you hover the textbox
public Form1()
{
InitializeComponent();
textBox1.Enabled = false;
toolTip.InitialDelay = 0;
}
private ToolTip toolTip = new ToolTip();
private bool isShown = false;
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if(textBox1 == this.GetChildAtPoint(e.Location))
{
if(!isShown)
{
toolTip.Show("MyToolTip", this, e.Location);
isShown = true;
}
}
else
{
toolTip.Hide(textBox1);
isShown = false;
}
}
Late to the party, but had the same problem and found a better solution: you can just wrap your TextBox in another Item and put a ToolTip on it like:
<Grid ToolTip="ToolTip to display">
<TextBox IsEnabled="False" Text="Text to display" />
</Grid>
You can also drag a ToolTip object from the Toolbox in designer onto the form.
Then in the code you just call SetToolTip() and pass in the button or text box etc. you want the tool tip to assign to and the text you want it to show.
myToolTip.SetToolTip(myTextBox, "You just hovered over myTextBox!");