I have a user control that contains a textbox and some buttons.
I can't respond to the text changed event from the textbox from outside of that user control (say from the parent form that the control is placed on).
How can I get an event that raised when the text has changed from outside of the user control?
Please see this answer on how to create a custom event in WPF.
Following this approach, you can handle the "TextChanged" event for the textbox inside your user control, then raise a custom event from there.
in your textcontrol event change, put this:
Me.OnTextChanged(e)
Or create your own event
Upper code:
Public Event ClaveModificada(Causa As String)
and when you need:
RaiseEvent ClaveModificada("")
Related
I have a ScatterView in which I can add custom scatter view items. They are represented in a grid containing two viewbox:
One for the custom shape
One for a TextBlock (elmtNameTextBlock)
The TextBlock can be modified by double clicking on it: it opens a window with a simple TextBox which closes on "Enter" press and updates elmtNameTextBlock.
As my program has to be designed for surface. I have to handle a DoubleTap event for that. I saw two methods for that:
DoubleTap Event
DoubleTapped Event
I tried to add both event:
public event EventHandler<GestureEventArgs> DoubleTap;
and
public event DoubleTappedEventHandler DoubleTapped;
but my program cannot find GestureEventArgs while I have the correct namespace and DoubleTappedEventHandler.
Have I for only choice to handle the DoubleTap event manually ?
I need to hook up an event that shows a WPF Popup control when a TextBox has focus and a keyboard shortcut is clicked. For instance. When typing in the TextBox field, the user can press ALT+H for help, to get a popup dialog showing input help. Pressing ALT+H "outside" the TextBox should not open the popup.
Any ideas?
Looks like a job for an Attached Event.
From MSDN:
The concept of
an attached event enables you to add a handler for a particular event
to an arbitrary element rather than to an element that actually
defines or inherits the event. In this case, neither the object
potentially raising the event nor the destination handling instance
defines or otherwise "owns" the event.
You can find details here, on the MSDN
Use command binding:
ApplicationCommands.Help.InputGestures.Add(new KeyGesture(Key.H, ModifierKeys.Alt));
this.CommandBindings.Add(new CommandBinding(ApplicationCommands.Help, Help_Executed, Help_Enabled));
In function Help_Executed do some operations
In function Help_Enabled check if textbox selected, do e.CanExecute = true;
InputGestures assign ALT-H for help
Is there an event that gets fired when a user control gets added onto a Form?
I need this to get the size of the Parent control of the user control.
But when the user control gets initialized etc, the Parent = null.
Because the user control hasn't been added yet onto the Form. So i can't get the Parent control (which is the Form) at this point.
So i'll have to do the re-sizing of the user control afterwards.
There is a ParentChanged event that all Controls inherit. In the event handler method you can inspect the Parent property, which will be set to the new parent at that point.
You can try using the Control.ParentChanged event. This will fire whenever the parent of the control is changed, so you can check if the parent is the form and then continue with how you want to react.
Alternatively, you can use the Control.ControlAdded event.
You have the ControlAdded event on the form, wich is initialized in your InitializeComponent(). Most of the time this event is duely placed after the adding of components. If you place it before the adding of UserControls to the form you will fire events each time you add a component.
I would recommend adding the usercontrol you need such specific control over in your codebehind and not move the eventhandler.
If you do that you can add a parent to your usercontrol which might give you the information you need.
var textBox = new TextBox {Parent = this};
I'm having a Picture box in a user control window(Windows custom control library). and some functionality in the Form's Enter event and leave event.
Now my sample application is having two instances of the control. So when i run my sample application the fist control got selected and the enter event is triggered, and when i select the second control the first's leave and second's enter events are getting triggered.
Now, problem is that when i select(click) the second control's picturebox, the events are not triggering, i.e the control form is not getting the event.
So if i click whereever in the control(in the picturebox or in the control) the enter event should be triggered.
How to do this?
A picture box can't get focus. So clicking on it won't take the focus away from the previous control thus not triggering the events.
You need to add a click handler on the picture box in which you manually give focus to the associated focusable control.
private void PictureBox_Click(object sender, EventArgs e)
{
focusableControl.Focus();
}
I've got a custom control we'll call "TheGrid".
In TheGrid's controls is another control we'll call "GridMenu".
GridMenu has a button control in its own control collection.
I'd like to enable the developer using this control to associate a page level method with the OnClick of that button deep down inside GridMenu ala:
<customcontrols:TheGrid id="tehGridz" runat="server" onGridMenuButtonClick="mypagemethod" />
On the GridMenu (which I assume is another custom control), expose the event ButtonClick by declaring it as public:
public event EventHandler ButtonClick;
If you like, you can create a custom event handler by defining a delegate, and a custom event argument class. Somewhere in the logic of this control, you will need to raise the event (perhaps in the Clicked event handlers of buttons contained on GridMenu; events can cascade). Coding in C#, you'll need to check that the event is not null (meaning at least one handler is attached) before raising the event.
Now this event is visible to TheGrid, which contains your GridMenu. Now you need to create a "pass-through" to allow users of TheGrid to attach handlers without having to know about GridMenu. You can do this by specifying an event on TheGrid that resembles a property, and attaches and detaches handlers from the inner event:
public event EventHandler GridMenuButtonClick
{
add{ GridMenu.ButtonClick += value;}
remove { GridMenu.ButtonClick -= value;}
}
From the markup of a control containing a TheGrid control, you can now specify the event handler by attaching it to OnGridMenuButtonClicked the way you wanted.
You can register an event handler for this event using delegates. See the following MSDN articles:
http://msdn.microsoft.com/en-us/library/system.eventhandler%28VS.71%29.aspx
http://msdn.microsoft.com/en-us/library/aa720047%28v=VS.71%29.aspx