How can I know Text changes for textboxes without Textchanged event - c#

In my C# Windows form MyForm I have some TextBoxes.
In these TextBoxes, we have to detect if the TextChanged event occurs,
if there're changes in these TextBoxes and click close button, it will ask if we want to cancel the changes when we close the form.
However, when I run the MyForm, I can't know text change for each textbox caused by user typing for without textchanged event property.
But I am thinking how do I make the TextBox's TextChanged know the
event cuased by user typing without textchanged event?
Thanks for help.
Sorry for my English.

There is no (decent) way of knowing what's typed without a TextChanged or a Leave event.
You need to use one of these events to get the typed content. Doing this enable you to set a "dirty" flag that you can check at close and clear at save.
Comparing old and new value has no point without this cause you won't know what the value should be set to without knowing something was changed.
With one exception: If your original data came from a database you could use the compare old/new approach as you would compare the textbox of that which came from the database.
Update:
Addressing this comment:
"Because Myform have many textboxes and if no text change ,this will
not display the confirm message.If I catch textchanged event for all
textboxes, this is so many code."
You can use a common handler to collect the changes for all textboxes in one single method. Use the sender object (cast it to Textbox) to identify which textbox is changed, if needed, or simply set a dirty flag for whatever textbox has a change.
bool isDirty = false;
void SomeInitMethod() //ie. Form_Load
{
textbox1.TextChanged += new EventHandler(DirtyTextChange);
textbox2.TextChanged += new EventHandler(DirtyTextChange);
textbox3.TextChanged += new EventHandler(DirtyTextChange);
//...etc
}
void DirtyTextChange(object sender, EventArgs e)
{
isDirty = true;
}
void Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
if (isDirty) {
//ask user
}
}
// to clear
void Save()
{
SaveMyDataMethod();
isDirty = false;
}
If you have a lot of textboxes in the form loop through the forms control collection and use typeof to address the textboxes. If you have textboxes requiring different approaches use the Tag property of the textbox to distinguish.

A possible approach is using the timer. Have a timer that ticks every 1000 ms (say) and checks the textBox.Text.
A second possible approach is overriding WndProc for the textbox (by inheriting a new class) and handling the change text message. This would be the same as overriding TextBox.OnTextChanged.

Why dont you use a timer which will check after a few intervals if the textboxes do contain any text

Related

WinForms field not updating [duplicate]

Is it possible to know if any of the textbox values have changed in the application.
I have around 30 textboxes and I want to run a part of code only if, any of the textboxes value has changed out of the 30. Is there a way I can know that.
Each text box will raise an event TextChanged when it's contents have changed. However, that requires you to subscribe to each and every event.
The good news is that you can subscribe to the event with the same method multiple times. The handler has a parameter sender which you can use to determine which of your 30 text boxes has actually raised the event.
You can also use the GotFocus and LostFocus events to keep track of actual changes. You would need to store the original value on GotFocus and then compare to the current value on LostFocus. This gets round the problem of two TextChanged events cancelling each other out.
You can assign an event handler to each of the TextBox's TextChanged events. All of them can be assigned to the same event handler in code. Then you'll know when the text changes. You can set a boolean flag field in your class to record that a change occurred.
This is perhaps on the rough and ready side, but I did it this way.
In the constructor, I created
bool bChanged = false;
In the TextChanged event handler of each control (actually same for each), I put
bChanged = true;
When appropriate, I could do some processing, and set bChanged back to false.
You can also just do this:
In your Constructor:
MyTextBox.TextChanged += new TextChangedEventHandler( TextChanged );
And Then this Method:
private void TextChanged(object Sender, TextChangedEventArgs e){
//Do something
}
try this. Add this code to the load/constructor. no need to specify the event in the XAML explicitly
this.AddHandler(TextBox.TextChangedEvent, new TextChangedEventHandler(TextChanged));
private void TextChanged(object Sender, TextChangedEventArgs e)
{
//ToDO (use sender to identify the actuale text from where it fired }
}

Repository Combo BoxEdit EditValueChanged fires many times while typing.

I have this Repository Item comboboxEdit in a Devexpress CustomGridView.
private void gridView1_CustomRowCellEditForEditing(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
{
if (e.Column == this.gcCol1)
{
var repositoryItem = new RepositoryItemComboBox();
foreach (var title in this.ViewModelList.Titles)
{
repositoryItem.Items.Add(title.TitleName);
}
repositoryItem.EditValueChanged += this.PostEditValueChanged;
repositoryItem.Validating+=this.validating;
e.RepositoryItem = repositoryItem;
}
}
private void PostEditValueChanged(object sender, EventArgs e)
{
this.gridView1.PostEditor();
}
EditValueChanged fires many times while typing. Is there a way to fire this EditValueChanged once after the user has completely finished editing the cell.
Something along these lines http://www.devexpress.com/Support/Center/Question/Details/Q288616
Devexpress Support had some fix for this problem but didn't seem to help.
Not sure why the activeedior is closing and resetting the cursor.
I don't want to be setting the caret position in EditValueChanged.
I also tried CellvalueChanged but this would require a click in the usercontrol.
Same with repository.validating
repositoryItem.EditValueChanged += this.PostEditValueChanged;
repositoryItem.Validating+=this.validating;
Is there a way to figure out if the user is done or still editing the combox box and then fire the editvaluechanged without having to worrying out clicks outside the combo box edit
A better approach:
Handle the GridView's CellValueChanged event, rather than EditValueChanged on the editor.
In the handler, determine which column fired the event. For example,
if (e.Column.Equals(this.gvColTitle))
{
//Access the repository item:
ComboBoxEdit editor = this.gridView1.ActiveEditor as ComboBoxEdit;
//Assign your values to the editor.
}
I'm not sure why you're adding the repository item at runtime, but you may be able to just create it in the XtraGrid Designer screen, and assign it to the column there. You can still update its item list at runtime using the above.
I was able to resolve this issue by not firing the EditvalueChanged and using the Validating event.
This event fires when the editor is about to lose focus. Its unlike CellvalueChanged where if the user clicks on the form and not on the usercontrol, the change is lost.
gridView1.PostEditor(); will show the editor after populating the values. Similarly, We can change the validating event to fire on 'Enter Key' to resolve as a quick fix.

Disable textbox after typing in a value C#

I want to disable a textbox after user has inserted a value. I have tried playing with the "KeyPress" property but the first digit I type in is also the "KeyPress", so it locks on the first character. Maybe I can get the textbox to lock after pressing Enter or using Tab. What will be the best way to do this?
It sounds like you want to disable the box after the person leave it. (I assume that from your Enter/Tab option.) If so, look into the TextBox.LostFocus event.
Though, the "Best" generally depends on the customer requirement, I would do the disabling act on the Lost Focus event.
private void textBox2_Leave(object sender, EventArgs e)
{
textBox2.Enabled = false;
}
Thanks
Just use Leave event and in body assign Enabled to false;
you can use the LostFocus event or the ManipulationCompleted event handler

How to disable the selection on a TextBox

I want to disable selecting text and clicking in the middle of text in a TextBox, but the user must be able to enter this TextBox and write at the end of earlier text, so I cannot make it ReadOnly or Enable = false.
I try to handle MouseDown and do the following:
input.Select(input.Text.Length, 0);
It helps with placing a cursor in the middle of text, but the user still can make a selection from the end.
I also make a MessageBox() on MouseDown event, but in this case the user cannot click on textBox and write anything.
The last try was to set a focus() in another Control and focus back, after a period of time, but it didn't work at all. User still can make a selection.
How can I do it?
How about this for Click event
Edit: Also do the same for DoubleClick and MouseLeave to cover all cases. You can have a common event handler.
private void textBox1_Click(object sender, EventArgs e)
{
((TextBox) sender).SelectionLength = 0;
}
If it fits the UI/user model, another approach is to use two text boxes: a read-only one with the previous text that the user can see and act on (if that is something he needs to do) and an editable one for the new text along with a button to commit the new text to the read-only text box (and persistence layer).
That approach is not only arguably more user-friendly—the editable box is completely editable rather than just "appendable", which gets confusing when the user hits Backspace—but also requires less fighting with the framework to make the boxes do what you need.
You're not far off with your MouseDown event handler, but probably better to catch MouseUp, as this is the event that will fire when they have finished selecting.
Alternatively, you could catch the SelectionChanged event.
Just put your:
input.Select(input.Text.Length, 0);
code in any of those event handlers.

Toolstripbutton not validating textbox

I have a textbox, a standard button and a toolstrip containing a couple of buttons.
In the validating event of the textbox I coded to check whether it is blank.
If yes then it shows a message 'Enter Value'. When the standard button is clicked while
the textbox is empty, it's validating properly and showing the message but when the
toolstripbutton is clicked it's not validating the textbox and no message is shown. It seems that I've got to write the validation code explicitly in the
toolstripbutton_click event which is too troublesome when there are multiple textboxes and toolstripbuttons on a single form.
What I want to know is whether the textbox_validating can be fired when the toolstripbutton is clicked? Handling toolstrips is really a headache.
The ToolStripItem classes are special, they don't derive from Control. One side-effect of that is that they don't take the focus away from the active control. And that prevents the Validating event from firing.
Several things you can do. You could call the textbox' parent's ValidateChildren() method. Or you could move the focus yourself:
private void toolStripButton1_Click(object sender, EventArgs e) {
btnSave.Focus();
if (btnSave.Focused) btnSave.PerformClick();
}
Write the following in toolstripbutton click event:
Me.Validate()
You can call the textbox_validating procedure from the procedure that handles the toolstripbutton click event, but you may have to add some logic to see if it passed validation before proceeding with the rest of the toolstripbutton_click event. Since you said you have a lot of textboxes to validate, you might want to consider making a Validate() function that returns true or false and checks all of the textboxes. Then all you have to do is check if Validate() = true and call the same function from all of your toolstrip buttons instead of copying the same code over and over again.

Categories