Loosely couple a modal dialog - is this possible? - winforms

I have a winforms custom UI control library which contains a control for displaying modal dialogs Picture-in-Picture.
This custom control receives as a parameter a pointer to the control which has initiated it's display. So they are tied together. This allows the control to be modally displayed over the window which launched it.
Dim f As New PiPCustomDialog 'this form wraps another form PictureInPicture style
f.FormToLoad = New PrintOptions() 'this is the form the user will interact with
f.Owner = Me 'used to determine the size of PiPCustomDialog
Dim dr As DialogResult = f.ShowDialog(Me) 'shows PiPCustonDialog coating, f's OnLoad event initiates display of FormToLoad centered within.
The fact that this control requires f.Owner to be set is what is stinky. User32.dll has a function GetActiveWindow() which would maybe allow the control to be more self-sufficient.
Anyone out there who would like to teach this old dog a new trick? I want to learn a better way.

I'll use this solution for now:
Remove any validation that requires
owner to be set before showing the
form modally.
if owner isn't set inside the class that is transparent cover
(first form) - do a system call into
GetActiveWindow to get owner so the size of the window can be set.

Related

Refresh the UI of Windows Form

I'm working on an old project that supports Windows Forms.
This project contains some ResourceManager for the support of a few localizations. The idea is that you call ResourceManager["SomeResource"] instead of Resource.SomeResource and it returns you a localized string.
And these localized strings are used in the code of the initialization of the form. For example, you have Form1, and in Form1.Design.cs there is some code like this:
Label label1 = new Label();
label1.Text = ResourceManager["SomeResource"];
So the label will be created with an already localized string in the Text.
And we need to add the functionality of changing the UI language without reloading the Form.
We can just set the every Text property of every controls again. But it's a lot of code, the form contains a lot of controls.
We can call the Form.InitializeComponents(), this method will recreate all controls with new localized strings, but in some cases, it works slowly because it reloads some big data again.
Is there some other way to refresh all UI controls and get the new localized strings? Do Windows Forms support some mechanism like Binding in the WPF to create the "connection" between the Text properties and localized resources?
I think that you can achieve this by use of Invalidate() either on the form itself or on a container control that your other controls may be encompassed by.

A combination of winforms' controls as a prefab?

In our winforms application we often have a situation where the same panel is used in different forms. Right now I simply copy the code and the designer elements from form to form but that is obviously a terrible practice.
So I thought of making a class that could be easily added to a different form as a "component". The only problem is to be able to prototype and maintain this panel in the visual designer in such a way that if I want to change the panel's appearance in the future it gets changed for every class instance I created.
For instance I have a panel that provides search functionality:
Alongside with the code for click/textChanged events. I want to encapsulate it in a class, which I would be able to instantiate and initialize in any form's constructor to instantly add these controls (alongside events) into that form. Now it is not necessary for me to see the controls in the designer of the recepient form, however, I need to be able to see them in the designer somewhere in order to modify them if I would ever require that.
And if I do modify the appearance somehow (for instance add an extra button) these controls instantly change across the entire project, everywhere I instantiated the class.
I do know that all this can easily be done just creating a new form and encapsulating everything in it, I just wonder if it can be done for a group of controls instead.
You just described the perfect use of a User Control. It's easy to use and direct.
First Add a user COntrol to the project:
Then add the desired controls on the user control:
Build the project and you will see the UserControl on the toolbox:
Add them to the form as a standard control:
If you change the code for the user control (in this case adding a button click handler) uit will affect all the intances of that user control:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox1.Text = "Button Clicked"
End Sub
Note: If you have the Control in another project on the same solution, make sure you build that project too if you make any change.

Winform passing data between user controls

I have windows form application which has several user controls - each one is displayed when the relevant option is selected from a listbox.
Some of the user controls need to have access to data stored in a different user control so User Control A needs to know a value of a textbox stored in User Control B. I have done my exposing some properties in the user control B. This all works fine when the application first loads and no values are changed.
The problem I am having is if the value of the textbox in user control B is changed it is not picked up by user control A.
Do I have to do something with NotifyPropertyChanged? Any suggestions please?
Two solutions here:
Create a series of public properties and handle passing values where the Form objects are newed up.
Create an event to communicate when things change and register an event handler in the target Form to accept the change. This is similar in theme to the INotifyPropertyChanged interface but that's only required/advised for formal databinding scenarios.
I prefer events for this kind of thing.

Winforms child window disappears behind owner

I have a winforms MDI app where a window that is a "child" of the MDI form pops up a dialog, like this:
AddPartsForm partsForm = new AddPartsForm( );
partsForm.StartPosition = FormStartPosition.CenterParent;
DialogResult result = partsForm.ShowDialog( this );
As you can see, I have attempted both to center the dialog within its parent, and to establish who the parent is. This generally works. The dialog itself is a form with a dropdown list from which to select something that fills a grid on the same form. Occasionally, the loading of the grid takes up to ten seconds.
During the loading, I am careful not to move my mouse or click on anything. Yet, every so often (but not always) when the grid loads, the dialog "disappears" behind the form that called it. I can find it in the system tray, but I want this not to happen. There must be a parent/child or owner/child convention that most everyone uses and I have missed, because I do not see this kind of behavior in most software that I use.

Tool to know Windows Form Application's Form fields

I am working on a WinForm Application.
The Form has many fields/components but is poorly built.
for example a field is used as user name on one case and used as folder path on the other case. Code is quite poorly maintaned.
Is is possible that when i run the application and GUI appears, i can use a tool like 'spy++' which can show me 'names' of the components (not ids). For instance the name of a button or name of a label.
Or if i can use SPY++ for 'names' please tell me?
I would solve the problem by adding a ToolTip control to your form and iterating over each control and adding a Tool Tip message to each control that is the name of the control.
First, add a ToolTip object to your form (from the Tools section of the designer.) You can rename it, but for the sake of my demo, I left it as the default name toolTip1.
Next, add a method similar to the one I'm posting below to the code page of your form. (I'm assuming this is for C# but the code is simple and can easily be modified for VB or C++).
public void AddNameToToolTip(Control c)
{
toolTip1.SetToolTip(c, c.Name);
foreach (Control child in c.Controls) AddNameToToolTip(child);
}
Finally, from within the Form constructor, add the following line of code after the call to InitializeComponent().
AddNameToToolTip(this);
This will add a ToolTip message to each control in your form. All you should have to do is hover your mouse over each control and the ToolTip will pop up a message after a second or two displaying the name of the underlying control.
Alternatively, you can recursively adding a MouseHover event to each control and when the event is fired, write the name of the control to the debugger. This would also work if you are already using a ToolTip control within your form.

Resources