I have a custom RichTextBox Control derived from RichTextBox Control that Windows provides.
I am unable to capture the dragDrop Event though the DragEnter event is getting captured, but I dont know why the dragDrop event is not.
I have following properties set as true:
EnableAutoDragDrop=true;
AllowDrop=true;
What am I missing ??
Daniel is probably correct here:
private void DragOver(object sender, System.Windows.Forms.DragEventArgs e)
{
if (!e.Data.GetDataPresent(typeof(System.String))) {
e.Effect = DragDropEffects.None;
DropLocationLabel.Text = "None - no string data.";
return;
}
see also the example in:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.dodragdrop.aspx
You need DragDrop and DragOver in your RichTextBox.
http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26532918.html
Set EnableAutoDragDrop=false or your user will have 2 entries(1 duplicate) rather than just one entry. Eg. user picks"cat5" and when dropped in RichTextBox "cat5" appears twice.
Just a guess - maybe you missed to correctly set the drag'n'drop effect.
Related
Does anyone know how I could easily detect if the Windows OS IME (Input Method Editor) is active in the Silverlight framwork. Let me explain the scenario which is causing me issues:
I have hit an issue where using a Chinese (Simplified, China) Microsoft Pinyin causes a CLR exception. The scenario is when a TextBox is handling its TextChanged event. For example:
A TextBox should not accept characters but only numbers. In order to achieve that it is handling its TextChanged event and in that event it handles the input by reversing its Text property to the last correct input character by character. In this scenario if an (Chinese (Simplified, China) Microsoft Pinyin) IME is used an a FatalExecutionEngineError is thrown.
In WPF it is possible to overcome this issue by not reversing the Text in the TextChanged event by using the InputMethod.Current.ImeState to check if an IME is active. Unfortunately this is not available in the Silverlight framework which is why I am posting this question.
Currently the only thing I have found is that I could set IsInputMethodEnabled property of the InputMethod class to the TextBox control in order disable all IME input but this of course will not only disable the incorrect input but also the correct one.
Anyone has any ideas how I could detect if a IME is used/active in the Silverlight platform? Thanks.
I was able to resolve the issue in both the WPF and Silverlight frameworks. The issue was caused by the fact that by handling the TextBox Text while a IME is inputting symbols that Text was making the IME itself change its input which it looks like is not handled gracefully by the Windows OS and was causing a CLR exception.
What I did was:
In the WPF framework as mentioned I used the static InputMethod.Current.ImeState value to determine if IME is active and if it was with On value I skipped reverting the TextBox Text property in its TextChanged event.
In the Silverlight framework I use a combination of the TextInputStart, TextInputUpdate events and a local private field to store if IME was detected. The TextInputUpdate event is only triggered if IME is active and used as input and the TextInputStart is always triggered. What I did was:
Created a bool IsImeActive = false; filed
Hook to the TextInputStart event of the TextBox
In that event set the IsImeActive field to False
Hook to the TextInputUpdate event of the TextBox
In that event set the IsImeActive field to True
Finally in the TextChanged event add a condition that checks the IsImeActive field and if it is False run the logic which handles (reverses) the input.
Hope this is helpful.
I also get FatalExecutionEngineError, error code 0x80131623 when some IME is active.
My fix: I could get IME event with
TextCompositionManager.AddPreviewTextInputHandler(myTextbox, OnPreviewTextInput);
TextCompositionManager.AddPreviewTextInputStartHandler(myTextbox, OnPreviewTextInputStart);
TextCompositionManager.AddPreviewTextInputUpdateHandler(myTextbox, OnPreviewTextInputUpdate);
private void OnPreviewTextInput(object sender, TextCompositionEventArgs e)
{
ImeFlag = false;
}
private void OnPreviewTextInputStart(object sender, TextCompositionEventArgs e)
{
ImeFlag = true;
}
private void OnPreviewTextInputUpdate(object sender, TextCompositionEventArgs e)
{
if (e.TextComposition.CompositionText.Length == 0)
ImeFlag = false;
}
I remove any TextChanged event when current IME is active, then add back event after an IME text is completed:
private bool _imeFlag = false;
private bool ImeFlag
{
get => _imeFlag;
set
{
if (_imeFlag == value)
return;
if (value)
myTextbox.TextChanged -= MyTextbox_TextChanged;
else
myTextbox.TextChanged += MyTextbox_TextChanged;
_imeFlag = value;
}
}
I have a SizeChanged event in one of my windows. One of the user controls Width's is behaving interestingly, so I decided to staticly set the width of my window in a SizeChanged event. Problem is, when I set the size of the window in the size changed event, it fires another size changed event! I want the user to be able to resize the window, and then only have the event fire once. I have tried :
e.Handled = true;
As well as adding an event handler in the window constructor, and removing it in the size changed event. (This makes it only be able to fire once and won't ever fire again in the window's lifetime). Any ideas?
you should use a private bool and change its value when the size changed
bool _sizeChanged=false;
void handleResize(Object sender, EventArgs e)
{
if (_sizeChanged==false)
{
// do stuff
}
_sizeChanged=true;
}
But is is not enough, because you should change its value again somewhere else. if you do not change its value (for example to false somewhere else) it will never pass the 'if' condition again. So the question is, where you should change its value.
I think you can change the value at MouseButtonUp event, since resizing is done with the mouse.
You can use a boolean to determine whether or not to handle your event.
private bool m_handleResizeEvent;
private void HandleResize(object sender, EventArgs e)
{
if (m_handleResizeEvent)
{
m_handleResizeEvent = false;
// perform your resize here
m_handleResizeEvent = true;
}
}
Turns out it was the SizeToContent="Width" property in my Window's XAML that was causing the SizeChanged to be called multiple times. Removing this property fixed my issue and allowed me to resize the window without the event being fired multiple times. Thanks everyone else for your answers and input!
I'm trying to call a method as soon as a TextBox on my screen gets 'un-focused' if that makes any sense? The user types in a username and as soon as that textbox loses focus I want to fire an event that checks if that username is indeed available.
Thanks!
There is a Control.Leave in C#, which I think is perfect for your purpose.
you can go to events of the textbox in visual studio, and find the Leave event.
The code generated will be like :
private void txtbox_Leave(object sender, EventArgs e)
{
//Check for available operation Code
}
Please explore the LostFocus event in a textbox. Hope it helps
select your object and to choose the panel of the properties and click on event.To unroll until LEAVE and double-click above.that will give you:
private void object_Leave( object sender, EventArgs e)
{
////put your code here
}
In WPF Try this:
TextBox.LostFocus
This will work:
Go to the "properties" of your textbox. You will see a yellow lightning bolt in the first line tab. There you will find all possible events that can be triggered. Search for "Leave" entry, double-click it. There you can put whatever you want.
I am working on a C# WinForm application.
I want to trigger some processing once the form has been "shown" and the layout of the form is complete.
I am using the "_Shown" event, but this seems to trigger before the layout of the form has completed. Is there event I can use that fires once the layout is complete?
Put Application.DoEvents() at the start of the form's Shown event handler. This will force all the controls to be rendered.
I don't see an event after Shown you can use for this purpose. Could you not use a timer to delay your processing in the Shown event?
An old trick in VB6 used to be to use the Paint event:
bool firstShown = false;
void form_Paint(Object sender, EventArgs e) {
if ( !firstShown ) {
YourMethodThatNeedsToRunOnShown();
firstShown = true;
}
//the rest of your paint method (if any)
}
It is a little hacky, but it does work
This works for me and is much less "hacky" than other suggestions:
protected override void OnLayout(LayoutEventArgs levent)
{
base.OnLayout(levent);
if(someControl == null)
return; // be careful of OnLayout being called multiple times
// otherwise, do some stuff here, set control sizes, etc.
}
AS far as I can remember the event order is something like
Form.Load
Form.Layout
Form.VisibleChanged
Form.GotFocus
Form.Activated
Form.Shown
So if something is still happening after Form.Show it's because of the way you coded it.
Are you maybe creating the form dynamically?
The best solution is the Shown() event: http://msdn.microsoft.com/en-us/library/system.windows.forms.form.shown.aspx
"The Shown event is only raised the first time a form is displayed; subsequently minimizing, maximizing, restoring, hiding, showing, or invalidating and repainting will not raise this event."
Try using Form.GotFocus (inherited from control)..
something like this.
private void Form1_Load(object sender, EventArgs e)
{
this.GotFocus += new EventHandler(Form1_gotFocus);
this.Focus();
}
private void Form1_gotFocus(object sender, EventArgs e)
{
// You will need to Switch focus from form at the end of this function,
//to make sure it doesnt keep Firing.
}
According To msdn , the following happens:
When you change the focus by using the keyboard (TAB, SHIFT+TAB, and so on), by calling the Select or SelectNextControl methods, or by setting the ContainerControl..::.ActiveControl property to the current form, focus events occur in the following order:
Enter
GotFocus
Leave
Validating
Validated
LostFocus
In WinForms I could handle the Validated event to do something after the user changed text in a TextBox. Unlike TextChanged, Validated didn't fire for every character change; it only fired when the user was done.
Is there anything in WPF I can use to get the same result, an event raised only after the user is done changing the text?
LostFocus will fire when the user moves from your textbox onto any other control.
It seems that there is no native solution.
The LostFocus event is a good idea. But when the user click on Enter, he wants the TextBox to validate the change.
So here is my suggestion : use the LostFocus event and the KeyDown event when the key is Enter.
private void TextBox_LostFocus(object sender, RoutedEventArgs e)
{
// code to lauch after validation
}
private void TextBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Enter)
{
// call the LostFocus event to validate the TextBox
((TextBox)sender).RaiseEvent(new RoutedEventArgs(TextBox.LostFocusEvent));
}
}
LostFocus is not equivalent to Validate. It creates lots of problem when you have multiple text boxes on one screen and every text box has some logic written in Validate. In validate event you can control focus easily but not in LostFocus so easily.
You can also try Binding.ValidationRules
Documented at : http://msdn.microsoft.com/en-us/library/system.windows.data.binding.validationrules.aspx
Here is the article to get started:
How to implement binding validations :
http://msdn.microsoft.com/en-us/library/ms753962.aspx