How to clear the value of an Autocomplete Box? - c#

Just like many things in WPF, sometimes the easiest things are the ones that are hardest to find examples for! How do you clear out the current text of an AutoCompleteBox? In my OnFocus event I want to make sure that the user is given a clear box for entry. So my event procedure looks like
private void autGlobal_GotFocus(object sender, RoutedEventArgs e)
{
AutoCompleteBox acb = (AutoCompleteBox)sender;
if (acb.SearchText == "Search Term")
{
//clear out the box if it has the focus
this.autGlobal.Text = "";
}
}
However, setting the text property directly does not seem to work. Am I missing something obvious?

You need reset Selected Item too.
private void SearchAutoCompleteBox_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
SearchAutoCompleteBox.SelectedItem = null;
SearchAutoCompleteBox.Text = string.Empty;
}

Have you tried setting the property on your local variable?
acb.Text = string.Empty;
I have a feeling there may be additional code affecting the .Text field when the focus or textchange events are firing.

I think I found the answer after spending a ton of time on this and the XAML. This code example will not work when the IsTextCompletionEnabled option is set to true in the XAML. I set it to false and this code works fine.

Related

Disabling a Textbox Using TextChanged Event

The form I am using requires a copy pasted URL. I am trying to have a textChanged event that will check the url as soon as it is pasted, telling the user whether it is valid or invalid. I also want to be able to lock out the textbox when this happens, with a message saying something like "Processing...".
The problem is with the code below, the textbox is never disabled, the program will do the checkUrl() method and the textbox is never disabled even though it is first to execute (I assume it is but the fact there is a function call right underneath it is messing around with something or getting higher priority).
How do I go about making the control visually disabled while the method runs?
private void urlTxtBx_TextChanged(object sender, EventArgs e)
{
urlTxtBx.Enabled = false;
checkUrl();
urlTxtBx.Enabled = true;
}
I think this is happening because the Application needs to complete all the active threads before disabling the TextBox. Please try the following code:
private void urlTxtBx_TextChanged(object sender, EventArgs e)
{
urlTxtBx.Enabled = false;
Application.DoEvents();
checkUrl();
urlTxtBx.Enabled = true;
}
This will let the UI to be updated. For more details check here.

Greying out a checkbox when another is checked

Basically, I have a list of delivery checkboxes one for deliver to this address and another for deliver to a separate address, I basically want to make it so once one has been checked the other can not be checked out (perhaps by greying it out or something along those lines)
Please be aware that both boxes use the same controls.
Listen to the first CheckBox's CheckedChanged event with a method like this one:
private void checkBox1_checkedChanged(object sender, EventArgs e)
{
this.checkBox2.Enabled = !this.checkBox1.Checked;
// If you want it to be unchecked as well as grayed out,
// then have this code as well:
if (!this.checkBox2.Enabled)
{
this.checkBox2.Checked = false;
}
}
But you should consider using RadioButtons instead of CheckBoxes, if it logically fits to your needs.
Use the following code,
checkboxToBeGreyed.Enabled = false;
You have write this code in other checkbox's checked event . Hope this helps.

C# .net Make checkboxes to behave as a radio button

I have a group box which has some radio buttons. I am trying to implement serialization with the help of a tutorial from Code Project. That tutorial supports serialization of checkboxes and not radio buttons. So i need to make the radio buttons in my app as checkboxes (that is they should be check boxes but work like a radiobutton).
I tried writing code, but what happens is when I find that a particular checkbox is checked and I go to uncheck or vice versa, it triggers that checked_changed event handler and this goes into an infinite loop.
Can someone help me out with this?
Thanks
UPDATE:
After seeing your replies, I would like to say thanks a lot. Yes, You are all right that we should not be messing with the basic properties. I will work with changing the serialization method.
P.S The link for the tutorial is http://www.codeproject.com/KB/dialog/SavingTheStateOfAForm.aspx
Final Update:
After following the replies posted here, I decided not to change the default properties but to change the serializer code. I did that and it now works perfectly. Thanks a lot, everyone.
I agree with all the commenters: do not make checkboxes that act like radio buttons, it flies in the face of UI conventions and confuses users.
The right way to do this is to fix your code to serialize the radio buttons, but without seeing your code it's hard to know how to help you. For a start, you can fix the CheckedChanged looping by temporarily removing the event handler before you do anything. For example:
myCheckBox.CheckedChanged -= MyCheckedChangedEventHandler;
myCheckBox.Checked = true;
myCheckBox.CheckedChanged += MyCheckedChangedEventHandler;
If this alone doesn't fix your issue, please show us your code and we'll try to help more.
Edit: Based on the tutorial listed in your update, I'm guessing the problem happens when you call FormSerialisor.Deserialise(), which triggers your controls' event handlers to fire? If that's the case, the quick fix is to just do what I mentioned: remove the radio button event handlers before calling FormSerialisor.Deserialise() and then re-add them afterwards. Example:
myRadioButton.CheckedChanged -= MyCheckedChangedEventHandler;
FormSerialisor.Deserialise(this, mySerialisepath);
myRadioButton.CheckedChanged += MyCheckedChangedEventHandler;
You may also need to edit the FormSerialisor class to handle RadioButtons; just copy the code that handles checkboxes but change all the references to RadioButton. It's not clear from your question whether this step will be necessary or not.
Like the comments say, you're better of getting serialisation to work with radio buttons than messing around with checkboxes. Having said that, to get the effect you need, just set a variable that indicates you're already handling a change event, and test for it. Something like this (it's terrible code, but demonstrates the idea):
private bool autoChange = false;
private void ChangeHandler() {
if (!autoChange) {
autoChange = true;
/* Do stuff */
autoChange = false;
}
}
Unregister from the Checkedevent (by using -=) before doing that. And re-register after you're done.
(I'm not arguing with the comments. Just answering the question.)
Hello, here a simple ansewer to transform the CheckBox to a RadioButton:
object clickBox = null;
private void checkBox_Click(object sender, EventArgs e)
{
clickBox = sender;
foreach (Control c in this.Controls)
{
if (c is CheckBox)
{
if (c != clickBox)
{
((CheckBox)c).Checked = false;
}
}
}
And add this Click event o every Checkbox
Finish
Take the CheckedListBox and call ItemCheck Event and use below code It will behave like Radio buttons (It works for me):-
private void chkListBox_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (e.NewValue == CheckState.Checked)
{
for (int item = 0; item < chkListBox.Items.Count; item++)
{
chkListBox.SetItemChecked(item, false);
}
}
}

Set focus to TextBox

In my WindowsPhone application I have a Text Box that needs to receive focus at a given time. What I've tried so far:
textBox1.Focus();
textBox1.UpdateLayout();
textBox1.Focus();
textBox1.IsTabStop = true;
textBox1.UpdateLayout();
textBox1.Focus();
textBox1.IsTabStop = true;
textBox1.Focus();
Nothing seems to work. In the Emulator, when the Focus() method is called, the keyboard starts to rise, but then crashes back. The TextBox has IsTabStop set to true in the properties.
Does this help?
this.ActiveControl = textBox1;
If you want the text box to be selected without you having to click into it (if that is what you mean by focus), try:
textBox1.Select();
This seems to be a Silverlight bug. I've used the TextChanged event on the TextBox, and set the Focus() in there. Kind of an ugly workaround, but it worked for me. If anybody has another solution, please post.
Even I have tried all the above solutions but didn't work for me. Finally I tried with the following thing, and it worked.
private void txtBox_LayoutUpdated(object sender, EventArgs e)
{
txtBox.Focus();
}
Just set focus property of any control at Form_Activated time..
private void Form_Activated(Object sender, EventArgs e){
Textbox.Focus();
}
you can accomplish this by programmatically giving it focus. This can be done by
calling its Focusmethod, although this call can fail (and return false) under certain conditions.
For example, you cannot set focus on a control from a page’s constructor; it’s too early. You can,
however, call it from a page’s Loadedevent.
if this is a textbox created in the runtime like this:
TextBox TextBox1 = new TextBox();
TextBox1.Name = "TextBox1";
then add this line
TextBox1.Loaded += new RoutedEventHandler(TextBox1_Loaded);
and then add this following event also:
private void TextBox1_Loaded(object sender, RoutedEventArgs e)
{
((TextBox)sender).Focus();
}
This may be a little late a couple years late but after playing around in windows phone 8.1 development environment I found the solution:
TextBox.Focus(FocusState.Keyboard);
You need to pass in the type of FocusState through the FocusState class.
It's worked for me
this.YourTextbox.TabIndex = 0;
After spend hours in debugging, I found the solution
await Task.Delay(100);
tbInput.Focus(FocusState.Programmatic);
if we have tow textboxes
exp:
txtPrincipal
txtPrincipal_Contact
if want move from txtPrincipal to txtPrincipal_Contact
we need to write second textbox name with focus function in leave event of the current textbox exp:
private void txtPrincipal_Leave(object sender, EventArgs e)
{
txtPrincipal_Contact.Focus();
}

How to change the behaviour of the DataGridViewComboBoxColumn in C#?

I'm working with a data grid that contains a Combo Box Column, but editing this Combo Box (by simply clicking on it) gets annoying sometimes, since one must click at least twice to change the value of that field. I want to change that behaviour, so I thought it would be very simple: just create a OnMouseOver event to make the mouse-overed combo box be selected, but the only available event is the Disposed one.
Is there any way to change this behaviour?
I just dealt with the same problem, and solved it by setting the DataGridView.EditMode to EditOnEnter.
If you don't like that behaviour for all your other columns, I found this suggestion for placing in the CellEnter event:
if (DataGridView1.Columns[e.ColumnIndex] is DataGridViewComboBoxColumn)
{
((DataGridViewComboBoxEditingControl)DataGridView1.EditingControl).DroppedDown = true;
}
I haven't tried it, but it looks promising. The same technique is discussed on this question.
In Winforms, there's a CellMouseEnter event (and a CellEnter event for non-mouse navigation) on the DataGridView. You can use that to set the selected cell.
The reason you are getting only the Disposed event (I think) is because you are trying to go too deep. I get only the Disposed event when I try to go all the way to dataGridView1.Columns["Column1"]...
Instead, as KeithS mentioned, you can assign the CellMouseEnter event to your DataGridView.
From there, you can do the following...
private void dataGridView1_CellMouseEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex==0 || e.ColumnIndex==2)
{
dataGridView1.CurrentCell = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex];
dataGridView1.BeginEdit(true);
}
}
That if-statement is just there to show how to restrict this functionality to certain columns, in case you want that.
The first line inside the if-statement sets the current cell and the second line begins the edit process.
This is a general process that should work with any type of column you can throw into a DataGridView. The DataGridView's EditMode shouldn't matter.
This works really good.
On the CellClick event of the datagridview:
void datagridview1_CellClick(object sender, .Windows.Forms.DataGridViewCellEventArgs e){if (e.ColumnIndex > 0)
{
W1.dGVReports.CurrentCell = W1.dGVReports.Rows[e.RowIndex].Cells[e.ColumnIndex];
W1.dGVReports.BeginEdit(true);
(W1.dGVReports.EditingControl as System.Windows.Forms.DataGridViewComboBoxEditingControl).DroppedDown = true;
}
}

Categories

Resources