Click here to Skip to main content
15,437,947 members
Home / Discussions / C#
   

C#

 
AnswerRe: Parameterized Command Not Adding To DB Pin
MsJane30-Nov-17 5:13
MemberMsJane30-Nov-17 5:13 
Questionhow to get programmatically the selected item of a treeview dynamically built Pin
Hervend27-Nov-17 2:02
MemberHervend27-Nov-17 2:02 
AnswerRe: how to get programmatically the selected item of a treeview dynamically built Pin
Eddy Vluggen27-Nov-17 2:21
professionalEddy Vluggen27-Nov-17 2:21 
GeneralRe: how to get programmatically the selected item of a treeview dynamically built Pin
Hervend27-Nov-17 2:56
MemberHervend27-Nov-17 2:56 
GeneralRe: how to get programmatically the selected item of a treeview dynamically built Pin
Eddy Vluggen27-Nov-17 3:26
professionalEddy Vluggen27-Nov-17 3:26 
GeneralRe: how to get programmatically the selected item of a treeview dynamically built Pin
Hervend27-Nov-17 19:19
MemberHervend27-Nov-17 19:19 
GeneralRe: how to get programmatically the selected item of a treeview dynamically built Pin
Gerry Schmitz27-Nov-17 8:12
mveGerry Schmitz27-Nov-17 8:12 
QuestionWorkaround for a virtual method call in the constructor Pin
CodeWraith25-Nov-17 9:40
MemberCodeWraith25-Nov-17 9:40 
I have opened a project of mine and promptly stumbled over an old sin: A virtual method call in a constructor. The project involves a DIY UI that runs in a 3D engine and the virtual method call is needed in the constructor of the controls.

It works like a charm, but calling a virtual method in a constructor may cause trouble and I want to get rid of the warning. If you want to take a look: Video clickbait[^]

The problem is that I can't find a way around that virtual method when initializing the properties of a control. I can only do that in the constructor because the view layout is loaded from XAML, like this:

<cLoginView Id="LoginView" Width="405" Height="83" DockStyle="ADAPT"
             xmlns="clr-namespace:FoC.MemPraeUserModule.Presentation;assembly=MemPraeUserModule" 
             xmlns:foc="clr-namespace:FoC.Praetor4UI.Controls;assembly=Praetor4UI" 
             xmlns:fpd="clr-namespace:FoC.Praetor4UI.DataObjects;assembly=Praetor4UI" 
             xmlns:fpt="clr-namespace:FoC.Praetor4UI.Theme;assembly=Praetor4UI"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <foc:cPraeLabel Id="LblLoginName" PositionX="5" PositionY="17" Width="75" Height="20" Text="Login Name:" />
    <foc:cPraeTextbox Id="TbLoginName" PositionX="85" PositionY="15" Width="313" Height="25" />
    <foc:cPraeImageButton Id="BtnListAccounts" Image="Button_128" PositionX="270" PositionY="45" Width="128" Height="22" Text="List My Accounts" LeftButtonClicked="BtnListAccounts_LeftButtonClicked"/>
    <foc:cPraeListview Id="LvServers" Style="ServerList" PositionX="85" PositionY="85" Width="313" Height="150" />
    <foc:cPraeLabel Id="LblPasswd" PositionX="5" PositionY="242" Width="75" Height="20" Text="Password:" />
    <foc:cPraeTextbox Id="TbPasswd" PositionX="85" PositionY="240" Width="313" Height="25" PasswordCharacter="x" />
    <foc:cPraeImageButton Id="BtnLogin" Image="Button_128" PositionX="137" PositionY="275" Width="128" Height="22" Text="Login" LeftButtonClicked="BtnLogin_LeftButtonClicked"/>
    <foc:cPraeImageButton Id="BtnCancel" Image="Button_128" PositionX="270" PositionY="275" Width="128" Height="22" Text="Cancel" LeftButtonClicked="BtnCancel_LeftButtonClicked"/>
</cLoginView>


When a control is loaded this way, the constructor must do the following things before the properties are set by the XAML parser:

1) Set the default value for each property
2) Set the values from a control style, if one has been defined
3) Set the values from a named style, if one has been set for this control

The constructor of the base is simple enough:
public cPraeControl()
{
     m_oUserInterface = null;

     InitializeDefaultProperties(); // This line is the problem
     ControlStyleChanged(cPraeUserInterface.CurrentTheme);
}


Calling InitializeDefaultProperties() ensures that the whole procedure is done for all properties of the topmost derived class, including the inherited ones. The derived controls have no own constructors that could interfere. That's why this works perfectly. I just don't see a way to get around calling InitializeDefaultProperties().

================================================================================

Edit:

I think I got it. It took only a few hours of refactoring, headscratching and debugging. The virtual method call (plus some hidden ones) are gone. Now every control has a proper constructor, like this:

public cPraeTextbox()
{
    InitializeDefaultProperties();
    SetStyle(GetControlStyle(cPraeUserInterface.CurrentTheme));
}

InitializeDefaultProperties() is now a normal private method that simply sets the default values for the properties, just as SetStyle() now only takes the for the class relevant values from the style object.

The magic happens in GetControlStyle(), which has the hard job of figuring out which styles have to be applied. Thank god for reflection! Since each class in the hierarchy now can find what it needs, there is no more need for constructing the controls top down (= no more virtual calls).

Now I have one control left that does not look right, but that will probably only take some more more debugging.

Thanks for the moral support. I knew you were all with me. Silently. Poke tongue | ;-P
I have lived with several Zen masters - all of them were cats.


modified 26-Nov-17 9:57am.

Questionissues concerning c# object reference not set to an instance of an object error Pin
Hervend24-Nov-17 2:27
MemberHervend24-Nov-17 2:27 
AnswerRe: issues concerning c# object reference not set to an instance of an object error Pin
Eddy Vluggen24-Nov-17 2:49
professionalEddy Vluggen24-Nov-17 2:49 
GeneralRe: issues concerning c# object reference not set to an instance of an object error Pin
Hervend24-Nov-17 3:25
MemberHervend24-Nov-17 3:25 
GeneralRe: issues concerning c# object reference not set to an instance of an object error Pin
Eddy Vluggen24-Nov-17 3:33
professionalEddy Vluggen24-Nov-17 3:33 
GeneralRe: issues concerning c# object reference not set to an instance of an object error Pin
Hervend24-Nov-17 4:24
MemberHervend24-Nov-17 4:24 
GeneralRe: issues concerning c# object reference not set to an instance of an object error Pin
Hervend24-Nov-17 4:35
MemberHervend24-Nov-17 4:35 
GeneralRe: issues concerning c# object reference not set to an instance of an object error Pin
Eddy Vluggen24-Nov-17 5:15
professionalEddy Vluggen24-Nov-17 5:15 
AnswerRe: issues concerning c# object reference not set to an instance of an object error Pin
OriginalGriff24-Nov-17 3:11
mveOriginalGriff24-Nov-17 3:11 
GeneralRe: issues concerning c# object reference not set to an instance of an object error Pin
Hervend24-Nov-17 4:34
MemberHervend24-Nov-17 4:34 
GeneralRe: issues concerning c# object reference not set to an instance of an object error Pin
OriginalGriff24-Nov-17 4:50
mveOriginalGriff24-Nov-17 4:50 
QuestionDisable Update of xmlns into Report Viewer C# Pin
rikidev23-Nov-17 23:37
Memberrikidev23-Nov-17 23:37 
QuestionHow to locate a window by title Pin
Leif Simon Goodwin23-Nov-17 20:53
MemberLeif Simon Goodwin23-Nov-17 20:53 
AnswerRe: How to locate a window by title Pin
Eddy Vluggen24-Nov-17 2:48
professionalEddy Vluggen24-Nov-17 2:48 
GeneralRe: How to locate a window by title Pin
Leif Simon Goodwin24-Nov-17 3:32
MemberLeif Simon Goodwin24-Nov-17 3:32 
QuestionDelete only the rows that has a particular Column value using C# code Pin
Member 1245769023-Nov-17 14:43
MemberMember 1245769023-Nov-17 14:43 
AnswerRe: Delete only the rows that has a particular Column value using C# code Pin
Eddy Vluggen24-Nov-17 2:46
professionalEddy Vluggen24-Nov-17 2:46 
QuestionHow to refresh reportviewer when SQL input data changed Pin
C Sharp coder 201823-Nov-17 14:29
MemberC Sharp coder 201823-Nov-17 14:29 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.