I would like to remove the original event behavior of controls within a form (similar to design mode).
So, when the user clicks on the button, i only want to capture that event. I do not want the original button event to be fired. Is this somehow possible?
I am looking for a generic solution. So it should work with any form and any control within the form.
Reason: I wrote a form validation rules designer. It uses reflection to enumerate all form-types in the entry assembly. The user can then select a form type, the designer creates that form, enumerates the controls, and embedds the form in the designer panel.
clicking on a control, opens a formular designer panel, and the user can now create a formular for that control and saves the formular to a DB.
When the form is then opened in the normal "runtime" mode, it loads its validation formulars.
Events are not in fact disabled in the Winforms designer. The designer executes the constructor of the form through Reflection, everything in the InitializeComponent() method executes, including the event subscriptions. Wherever this might cause a problem, the controls check the DesignMode property (prevents a Timer from starting for example) or by custom designers. The form is displayed underneath a transparent layered window on top of which the selection rectangle and drag handles are painted. Which prevents issues with mouse clicks and keyboard focus.
You probably ought to look at this magazine article to get this working for you.
From what I understand from your question, I guess, you can still use the "DesignMode" property for this as well. In your event handling routine, you may want to bypass execution by checking on this property:
if (this.DesignMode) return;
as the first statement in your event handling block of code.
Related
I have an application that contains a form with multiple controls.
I have subscribed to the form mouse up event. However when I click on the form if thewre is an other control placed on the form the event is not fired.
So, I would like to capture an form event on the form (even when an control is in place). Is this possible?
Thanks in advance.
As far as i know windows forms doesn't implement the concept of event bubbling. So you should manually tweak controls to handle the event. You can do it manually looping through all controls, or you can create some kind of wrapper for your form/container to subscribe to the event automatically. You may check general implementation of this idea here. .
Simple question. I have a MainForm and a settingsForm. The settings form is initialized once and then shown every time the user clicks a button. I need it to do something when this button is clicked.
m_inputSettings.ShowDialog(this); //edit settings selected, so show that form
This is the MainForm calling the settings form, which it does fine. But I need the SettingsForm to do something every time this happens. Presently, I cant figure out if this call actually triggers any events I can set handlers for. Does it trigger an event? If not, is there another way I can tell my SettingsForm to do something every time this call happens?
Note: Any code in the mainform after that line doesn't get executed until the SettingsForm returns, but that is intentional.
Thanks.
Edit: One of the things I want my form to do it select a specific control when this happens, but it seems that that is impossible until after the form is done loading everything.
You can override the OnVisibleChanged method in your settings form. Make sure to call base.OnVisibleChanged though as to not screw up any potential observers of the event (and anything else the base class may do inside of that method.)
FormShown event - raised only once when form is displayed first time.
OnPaint / OnActivate - every time form is activated, but these events raised even when you switch with other application, which probably you don't want to do.
If you are changing form visbility, then you can use OnVisibleChanged
If you are minimizing the form, you can use OnSizeChanged / OnLocationChanged event.
If none suits you, make a public property and set false when form is closed / hidded, and set true before showing it. OnActivate, use this property to do your task.
Maybe use VisibleChanged event.
Override OnShown() event in your form this will raise only once after the form is opened
The disadvantage of OnVisibleChanged is it will also get raised when the form is closed.
On Paint , On Activate and OnEnter will raise before form is shown to the user.
WinForms: I don't want any tab order. I want myself be able to programatically handle all the tab orders on the form with some logic that I need.
How can I completely disable tab order? I assume after that I should deal with KeyDown event of each contorl or some similar event ....
You need to override the form's ProcessCmdKey() method. Test keydata == Keys.Tab and (Keys.Shift | Keys.Tab) to detect respectively a forward and a backward tab. Return true to indicate that you've used the key and it shouldn't be used anymore. Which defeats Winforms default handling for the Tab key. No additional changes are needed to the controls.
The form's ActiveControl property tells you which control currently has the focus, you'll need to use it to figure out which control should be focused next. Beware that it can technically be null. Watch out for controls that are embedded in a container control, like a Panel or UserControl. Making this work is definitely unpleasant, also very hard to maintain. Only do this if there are a limited number of controls on the form.
As Adrian said by setting tab stop to false you can disable it
a Function like this can be usefull to diable all tabstop
private void DiableTabStop(Control ctrl)
{
ctrl.TabStop = false;
foreach (Control item in ctrl.Controls)
{
DiableTabStop(item);
}
}
and calling it at form load
DiableTabStop(this);
One approach is to set the TabStop property of every control in the form to false. This will prevent the tab key from giving the controls focus.
If you don't want to do this manually for every control (e.g. in the design view), you can create a method that will iterate over all of the controls in the form's Controls collection and set the property on each one, then call it from your form's constructor.
In addition to disabling the tab stops for the pageframe, as you mentioned, YOU want to control which "tab" is active. You can have a custom property on your form of "WhichTab" should be shown. Then, override the click event and check if the incoming sender/eventarg page is that of another page... no matter what, force focus back to the "WhichTab" YOU are in control of setting... When ready to activate said page, tell the tab control object to ACTIVATE the new page to get displayed to the user.
Let's say I have a created a Form class and a CustomControl class.
On my Form I have two instances of the CustomControl, and a Panel.
Panel has 4 event handlers: MouseEnter (to give a different cursor), MouseLeave (to reset the cursor), MouseDown (to start the dragging thread), and MouseUp (to kill the dragging thread and do post-drag logic).
I can drag the Panel onto the CustomControl. When I do this, the code in Form detects what I have done and deletes Panel from Form.Controls, passes some meta-information to CustomControl, which then creates a Panel on itself.
Basically, it is a hand-over. The Panel object now belongs to the CustomControl.
(This is necessary. It's complicated to explain why, but imagine the custom control has something like scrollbars, and it's necessary for the Panel to belong to the CustomControl so that it will scroll with the CustomControl.)
Now, when I click down on the Panel in the CustomControl, the Panel's MouseDown is triggered, it gets deleted from CustomControl.Controls and sends some meta-information back to the Parent (the Form), which then re-creates the Panel as it was at the start - however already in a dragging state so that the user can re-position the Panel onto the second CustomControl, or perhaps put it back onto the Form. The function which creates the Panel when the Form is first initialised is exactly the same function which creates it now.
However, the Panel's MouseDown has not been triggered. The mouse is down, but the event is not firing because the mouse was already down when it was created. So, I manually call the MouseDown handler in the function in Form which accepts the meta-information from CustomControl.
Unfortunately, this only half-works. The MouseUp handler isn't firing. I can pick up the Panel off the CustomControl and drag it around on the Form as expected, but when I release the mouse, the Panel is stuck to the cursor.
I'm not really sure how to get around this?
An ideal solution would be for, when the meta-information is passed back to Form and the new Panel is created, the MouseDown event to somehow fire naturally as though the user had just clicked down on the Panel.
It sounds like you are creating a new instance of Panel when you move it from CustomControl to Form and back and loosing it's state.
You should either try to pass the actual instance owned by Form to CustomControl without creating a new one or you could capture the state of the Panel in another object which you can pass to the constructor when you create a new Panel so that it is in the same state as the one you were dragging?
It seems as though you are trying to manually fire mouse events to compensate for problems in your design.
Always better to give some example code if you can than lengthy textual explanations.
Look at this
Instant of custom control is disappear when click outside it
I have problem like you.
you shouldn't use a local variable for handling mouseEvent.
Try to use "Capture" function. It's work for me.
I want to set focus to an MDI Parent Form when I click on the background of the form. However, the only way I can get it to set focus is when I resize the form.
I have tried using mouse click event, click event, key press event etc to manually set the focus when you click on the MDI Parent but none of these events fire. Is there ANY way to set the focus to the MDI Parent when you click on the background of the form?
That background is a separate control, try to find it in MainForm.Controls and assign it's click event.
You may want to look at the Win32 WM_MDIACTIVATE message. Now that we've discussed a possible solution, the real question can begin:
I think you should look long and hard at what your trying to accomplish. You risk (not necessarily will, but risk) creating a behavior that is abnormal and confusing to users. Why do you want to move the focus? What will you once it gets moved? How will you indicate to the user that this has been done? How will then get out of this state?