Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
eg.I have an instance of System.Windows.Forms.Control.
System.Windows.Forms.Control _control=new System.Windows.Forms.Control()
I can create a Click event dynamically using delegate,
_control.Click+=new EventHandle(control_Click);
Now,there is a protect virtual property in Control Class:
protected virtual bool CanEnableIme { get; }
What I want to do is to override the "CanEnableIme" property through the instance reference:_control.How to implement that? I have no idea about it.
Then for example,there is public virtual function:public virtual bool PreProcessMessage(ref Message msg),How can I override the function through "_control".
Thanks!


Thanks.As you can see that,I'm a Chinese,my ultimate goals is that:
I want to write some Chinese char on a draw board, such as Form,Panel,PictureBox ,...,which is usually a Type Inherit from Control.
Now I can write some English char on the board using DrawString function in GDI+ ,but I can't write Chinese char correctly, the board failed to get the focus of IME. I found that if I inherit a Type from Control, and override the "CanEnableIme" property,and let it return True(the property is readonly), the new Type could get the focus.
And then override the "CanEnableIme" property, I found that when I input one char using the Chinese IME ,the GDI+ draw the char twice,not only the Chinese char, also the English char.The reason of the error is that:when you open the IME and input something,the windows send two message "WM_CHAR" and "WM_IME_CHAR", the KeyPress event Triggered twice, so each char draw twice. That is why I want to override the PreProcessMessage function, for filtering the unnecessary msg.
That is all.
Posted
Updated 19-Jun-12 5:19am
v2
Comments
Sergey Alexandrovich Kryukov 19-Jun-12 10:22am    
The question makes no sense. What would be your idea on the purpose of such thing?
--SA

The term concept or virtual and overridden method/properly is related to a type (a class), not an instance of a class.

Therefore, the question makes no sense and just demonstrates that you did not get how OOP works in principle, well, not just yet; you need to understand it thoroughly, otherwise the further programming activity would be pretty much useless.

I am not sure you can understand that, but: I'm not saying you cannot create an overridden method/property during run time, but it is possible only if you create a brand new assembly during run time, by compilation some source code (it can be done via CodeDOM) or by creating one from scratch using System.Reflection.Emit. I don't think explaining further detail would be appropriate — these are way more advanced issues compared with "classic" OOP.

At the same time, I think your attention was to achieve some useful functionality. That's why it is important to start your question with explanation of your ultimate goals, before sharing your considerations on how you want to achieve them, as such considerations could be "infected" with some ideas you were preoccupied with, which could be reasonable or wrong. In this case, I cannot guess what would be your intentions, so there is nothing I can add to help you.

—SA
 
Share this answer
 
v2
I fully agree with Sergey about the quality of your question and his other explainations - listen to him!

I'm just guessing: you want to switch between base class behavior and some overriden behavior (this could then be dynamic...)? Maybe you don't need a realy dynamic solution - so a "poor man's" solution could be the usage of a "prepared" override with a "switch" condition like this.

C#
class Program
{
    static void Main(string[] args)
    {
        Child child = new Child();
        child.DoIt();
        child.UseOverride = true;
        child.DoIt();
        Console.ReadKey();
    }
}


class Base
{
    public virtual void DoIt()
    {
        Console.WriteLine("DoIt was called");
    }
}

class Child : Base
{
    public bool UseOverride { get; set; }

    public override void DoIt()
    {
        if (UseOverride) // prepared override
        {
            Console.WriteLine("DoIt override was called");
        }
        else // call base class method
        {
            base.DoIt();
        }
    }
}
 
Share this answer
 
Sometimes a very frustrating aspect of programming wanting to do something, and not being able to do it. Sealed classes can sometimes be in your way, and sometimes it is that a method in a control is not marked as virtual. You can use the New keyword to replace a method, but if the class is viewed as the base class, like for controls, then your method will not be visible, and the original method will be invoked. Microsoft has generally been pretty good about allowing methods (properties) to be overridden, but there are cases where they do not allow overriding.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900