Delphi Prism: How to load Winform without showing it? - winforms

I have a winform that needs to be loaded to update its controls' values or properties, before it is to be shown.
I found a stackoverflow question asking the same thing, but it's answer doesn't really help me. Load a form without showing it
Any sample code will be appreciated. Thank you,

Only you need create a new instance of the form and set the values of the controls.
check this code
Var
AForm : ChildForm;
begin
AForm:= new ChildForm;
AForm.textBox1.Text:='Foo'; //this control can be accessed here because the Modifiers property was set to public.
AForm.Show;
end;
Btw remember if you want modify or access the controls of another form you must set the property Modifiers of the control to access to public.

Create the form like this:
form := new MyForm();
Assuming you have implemented a method on MyForm to update the values, call it:
form.Update();//may need to pass parameters here
Show the form in the usual way:
form.ShowDialog();

From MSDN:
Form.Load
Occurs before a form is displayed for the first time.
So you can do all updates to the controls that are necessary before you show the form in this event handler.
But actually it is probably better to use databinding on the controls, so that they automatically reflect the current values you want them to show and you don't have to write any glue code bringing data on controls (and reading from them).

Related

Changing the default setting of a WinForms control property

I seek to change the default setting of a .NET Windows Forms control's property, without creating a custom control inheriting from it.
What I want is, for example, to add a TextBox on my Form that already has the TextAlign property set to HorizontalAlignment.Right instead of HorizontalAlignment.Left. Even if only solution-wide, if achievable, I would love to know. This would save a lot of time for when one is working with a LOT of controls and needs to set their properties to specific, non-default, values.
Creating a custom control is just too overkill for this, and would clutter my solution with unnecessary things. I have also considered running a regex on the designer files solution-wide (to add the non-default values to such controls), but writing regex like that can (also) be time consuming/problematic.
Any ideas?
There is a way to do that. But you need to set the binding initially. Later on you can set in one go to all the controls of your choice. This might be not your exact answer but this is how we define styles like as in WPF
(1) Click the Application Property Bindings
(2) Define Binding
(3)Now for textbox you can set the initial default
(4) Now You can set the property binding of every textbox intially or you can code which will set the application settings at starup or on form load.
This will you help to change the default propety at once later on

Form editing notifications

I'm developing a WPF application that contains a lot of forms (about 20 different forms)
each form is connected to a ViewModel class that usually hold a single object that the form is editing its properties.
I need to give a graphical sign to the user if he changed something like in Word when you edited the document and it tells you need to save it.
If the user edited even one property I need to show that sign.
Is there a simple way to accomplish that? I don't want to create an event for every property editor I have (there are more than 300 of them).
Why not use INotifyPropertyChanged?!
Since you are using WPF, you can opt for SourceTrigger to Property Changed.
Check here, http://www.codeproject.com/Articles/15822/Bind-Better-with-INotifyPropertyChanged to know about using INotifyPropertyChanged.

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.

FlowDocument table databinding

I'm trying to use the code from the following sample:
http://msdn.microsoft.com/en-us/magazine/dd569761.aspx
to dynamically create FlowDocuments with a table bound to xml.
The problem is that I first load the template and only then set the Databinding (because I use different data each time).
Anybody knows how to modify the code (maybe react to another event?) to make the code
from this example trigger code generation after the "dynamic" datacontext is set?
Thank you in advance
Ah, so you are using the 'BindableRun' class and are never getting bound? I recently encountered the same problem. You can trigger the data binding by updating the layout on any 'UIElement' object tied to the same dispatcher. The object does not even need to be in the visual tree - it is just invoking the context layout manager shared by all objects using the same dispatcher.
Simple example:
Button b = new Button();
b.UpdateLayout();
To get an accurate page count from the document paginator, you should update the layout first. Be careful though, as updating the layout again later will also invalidate any document paginators tied to the same dispatcher.

Show/hide controls based on user role in WPF

I need to do a quick sample WPF application where the controls on the forms should be made visible or hidden based on the user roles.
Something like this will be great,
How to manipulate WPF GUI based on user roles
I am not sure where to put the XAML defined in the thread(<Control ) so that the every control in the form uses RoleToVisibilityConverter to show or hide the controls.
i am very new to windows dev..could you please help me ?
Regards
Bala
You could solve this by binding the Visibility to a corresponding property in your code-behind/ViewModel.
This is an example from a binding in one of my testcontrols using WPF (in combination with Caliburn):
Visibility="{Binding Path=IsAdmin}"
Here I have a bool property in my ViewModel called IsAdmin.
For me, the easiest way was creating a global variable in my app. For that, go to your Project->Properties->Settings.settings and create a new variable (called, for example, Administrator), and set it to True of False depending on what you want to do. Something like this:
Then, at your MainWindow, when its initialized, you can write this:
if (!Properties.Settings.Default.Administrator)
{
DisableSettings();
}
And then, disable the fields you want to.

Resources