Hi,
I need to develop several dialog boxes that use a common set of UI
elements. For this purpose, I've created a class (let’s call it
ucBaseFilter) that inherits from UserControl and contains lots of UI
elements and some programming logic.
Now, I need the user interface of ucBaseFilter to be user-configurable.
For that purpose, I’ve created 2 classes (ucFilter1 / ucFilter2) that
inherit from ucBaseFilter. Each one has its own interface, but the
programming logic is the same (the code is still in ucBaseFilter) and
it’s completely transparent for the dialog boxes that use the control.
- In design mode, I obviously don’t know which one of the two inherited
controls I’ll need, so I use ucBaseFilter (it’s blank, but I’m in design
mode).
- At Runtime, depending on the user settings, I need to dynamically
instanciate ucFilter1 or ucFilter2 in the New:
If Not Me.DesignMode Then
If MyPoste.LoadSimpleProductFilter Then
Me.UcProdFilter = New ucFilter1
Else
Me.UcProdFilter = New ucFilter2
End If
End If
InitializeComponent()
The problem is that in InitializeComponent, at runtime, UcProdFilter is
instanciated to ucBaseFilter. I’ve tried to change InitializeComponent
to skip that:
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
Dim resources As System.ComponentModel.ComponentResourceManager
= New System.ComponentModel.ComponentResourceManager(GetType(myForm))
If Me.DesignMode Then
Me.UcProdFilter = New ucBaseFilter
End If
but each time I open the form in the visual designer, my code is lost (I
know, I should not write code in InitializeComponenent).
Is there another way do dynamically decide which one of the two
components will be instanciated at runtime ?
Thanks
David
Jan Hyde (VB MVP) - 08 Nov 2007 12:56 GMT
David Scemama <dscemama@newsgroups.nospam>'s wild thoughts
were released on Thu, 08 Nov 2007 12:37:11 +0100 bearing the
following fruit:
>Hi,
Please don't post to VB6 groups unless the question relates
to VB6.
J
>I need to develop several dialog boxes that use a common set of UI
>elements. For this purpose, I've created a class (lets call it
[quoted text clipped - 43 lines]
>
>David
--
Jan Hyde
https://mvp.support.microsoft.com/profile/Jan.Hyde
Jonathan Boivin - 08 Nov 2007 21:59 GMT
I'm not quite sure, but I think your problem would solved by moving the call
to InitializeComponent() at the beginning of the constructor.
> Hi,
>
[quoted text clipped - 45 lines]
>
> David
David Scemama - 09 Nov 2007 09:58 GMT
Hi,
Since the InitializeComponent() creates ucBaseFilter and inserts the
object in the dialogbox, the instruction Me.UcProdFilter = New ucFilter1
creates another object that does not apear
David
Jonathan Boivin a écrit :
> I'm not quite sure, but I think your problem would solved by moving the call
> to InitializeComponent() at the beginning of the constructor.
[quoted text clipped - 48 lines]
>>
>> David
Linda Liu[MSFT] - 09 Nov 2007 03:57 GMT
Hi David,
I agree with Jonathan that moving the line of code to call the
InitializeComponent method before the code that determines which derived
UserControl is needed at run time within the sub New should solve your
problem. For example:
Sub New()
' move this line of code here
InitializeComponent()
If Not Me.DesignMode Then
If MyPoste.LoadSimpleProductFilter Then
Me.UcProdFilter = New ucFilter1
Else
Me.UcProdFilter = New ucFilter2
End If
End If
End Sub
However, I don't think this is a good practice to solve the problem.
Generally speaking, if we cannot determine which control should be used at
design time, we usually determine it at run time and create an instance of
this control and then add the instance to the Controls collection of the
Form.
Hope this helps.
If you have any question, please feel free to let me know.
Sincerely,
Linda Liu
Microsoft Online Community Support
David Scemama - 09 Nov 2007 10:13 GMT
Hi Linda,
I completly agree that the best solution would be to create and
initialize the control at runtime. But the dialog boxes that use the
control are quite complex and setting the tabOrder, position, size ...
of the control is simpler in design view.
I found a solution that seems to solve the issue:
I removed the modification of InitializeComponent and wrote:
InitializeComponent()
If Not Me.DesignMode Then
If MyPoste.LoadSimpleProductFilter Then
Me.UcProdFilter = New ucFilter1(UcProdFilter)
Else
Me.UcProdFilter = New ucFilter2(UcProdFilter)
End If
End If
And I changed the ucBaseFilter constructor so as I can pass the control
to copy values from.
David
Linda Liu[MSFT] a écrit :
> Hi David,
>
[quoted text clipped - 29 lines]
> Linda Liu
> Microsoft Online Community Support
Linda Liu[MSFT] - 09 Nov 2007 11:04 GMT
Hi David,
Thank you for your prompt response!
I agree with you that your new solution can solve the problem : )
In addition, you needn't use the condition "Not Me.DesignMode" on the code
to determine which control should be used in the sub New method.
Firstly, when a form/control is opened in the designer, only the
InitializeComonent method is called at design time. (Note: the constructor
of the form/control's base class will be called when the form/control is
opened at design time)
Secondly, it's too early to determe whether the form/control is in design
mode using the DesignMode property within a constructor-- the DesignMode
always returns false within a constructor.
It is because the DesignMode property depends on the Site property, which
is set by the designer when the form/control is opened at design time. That
is, if the value of the Site property is not null, the DesignMode property
returns true; othewise, the DesignMode property returns false. The problem
is that while the constructor has not finished executing, the Site property
remains null so that the DesignMode property always returns false.
Hope this helps.
Sincerely,
Linda Liu
Microsoft Online Community Support