When to set properties in a user control - c#

Normally, if you want to set properties on a form, you can construct the form, set the properties and then showthe form. This gives you time to set the properties before the Load event is raised.
Code would look like follows:
MyForm form = new MyForm();
form.PropertyA = ...;
form.PropertyB = ...;
form.ShowDialog(this);
A user control also has a load event. If you put this user control on a form, then the Load event of the user control is raised at the end of the form's InitializeComponent(). So during construction of the form, way before the properties of the form are set.
If one of the properties of the form must be passed to the user control, then you are too late.
The solution I use now is give the user control an initialize function, that would do the things the user control would do during the Load event. This initialize function is called during the form's Load.
Although this works, this seems like bypassing Microsoft's way of initializing forms.
What is the proper way to load a user control if it needs some property values that are only known at run time?

Related

What events are triggered when ShowDialog(ParentForm) is called in C#

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.

Remove original event behaviour of WinForm Control

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.

totally disabling TabOrder on the form

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.

Suppress TreeView_AfterSelect() during Form.Show()

Is there a way to suppress the treeview_AfterSelect() event so it isn't called during form.show().
I have an application that is an MDI Container. One of the child windows contains a treevew. What the user selects on the treeview determines which child windows are shown. Due to a custom control I'm using the treeview form is also one of the windows that is closed and recreated. I've managed to mute the event handler and select the required node and then reenable the event handler in the constructor, but when the form is later shown the AfterSelect event is fired. Which is unwanted behavior in my situation.
Thanks in advance
The easiest approach is to use a member variable (e.g. "bool initialised"). It'll default to false.
At the end of your form's Shown event handler, set it to true.
In your AfterSelect, ignore the event if (!initialised)

C# WPF: webbrowser modal dialog close from win32 app

I am curious as to if this is even possible, but basically what I need is when opening a System.Windows.Forms.WebBrowser Modal Dialog from a WPF application I need to catch a value passed back from the page to WPF and keep a window up or close it based on the value that I return. Is this even possible or am I going to have to about in a different way?
Thanks, Andrew
surely is possible, just create the dialog, register an event handler for a custom event you have created in your second form and show it as modal, then inside your form you do what you need to do and when something happens you fire the event the main form has registered to, in the custom EventArgs class used in your event you can pass the value main form needs to get. from the main form you check the value and you do nothing or close the popup....

Categories

Resources