0

Suppose you have the following class

public class Message : Control, IDisposable {

    internal static class _Text : RichTextBox {

        protected override void OnLinkClicked( LinkClickedEventArgs e ) {
            System.Diagnostics.Process.Start(e.LinkText);
        }

        public _Text() {
            BorderStyle = System.Windows.Forms.BorderStyle.None;
            BackColor = Color.Orange;
            ForeColor = Color.White;
            ReadOnly = true;
            this.Font=new Font( "Segoe UI", 10 );
            Text="";
            Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
            ScrollBars = RichTextBoxScrollBars.None;
        }
    }

    public Message() {
        SetStyle( ControlStyles.AllPaintingInWmPaint|ControlStyles.OptimizedDoubleBuffer|ControlStyles.ResizeRedraw|ControlStyles.SupportsTransparentBackColor|ControlStyles.UserPaint, true );
        UpdateStyles();
        DoubleBuffered = true;
        _Text.ContentsResized+=_Text_ContentsResized;
        _Text.Location = new Point(15,5);
        _Text.Width = Width - 15;
        this.Controls.Add( _Text );
        _Text.Visible = true;
    }


    void _Text_ContentsResized( object sender, ContentsResizedEventArgs e ) {
        _Text.Height = e.NewRectangle.Height;
        base.Height = _Text.Height + 10;
    }

}

The problems being reported in the above code (as an example), are as follows :

_Text.ContentsResized+=_Text_ContentsResized; reports

An object reference is required for the non-static method

.... (etc)

this.Controls.Add( _Text ); reports

Message._Text is a type but is being used as a variable

and the contents of the event handler _Text_ContentsResized reports the same error as reported for the first error described here (as follows)

An object reference is required for the non-static method

.... (etc)

Keeping the conceptual idea behind this, I would like the inner control _Text to be a static and more natural element of the parent control Message but to still be able to access it's properties and events. For the properties, I could likely create a static wrapper for get/set, however I still have the issue of being unable to handle any events from that object.

So the question is -- how can I raise the event from a static object/class to a non-static caller without creating a latebound copy of the object (_Text t = new _Text())

Kraang Prime
  • 9,981
  • 10
  • 58
  • 124
  • Instead of making '_Text' static (which seems to me like an awful idea), make it's constructor 'private' and implement it as Singleton – Nissim Jun 17 '15 at 06:49
  • I have to ask, why do you think `_Text` (I also suggest reading C# Naming Conventions) should be static? – Yuval Itzchakov Jun 17 '15 at 07:02
  • Static is faster, and requires less resources than typical `obj x = new obj()` .. eg >> https://moisadoru.files.wordpress.com/2010/03/php_singleton_vs_static.png?w=693&h=529 ( i deleted the last comment I made as I understand now the terminology singleton applies to the object initialization technique I wish to avoid - and have been using) There must be a way to inherit the event somehow. – Kraang Prime Jun 17 '15 at 07:05
  • "Static is faster" Why? because the IL emits a `call` instead of a `callvirt` for the method call? That doesn't mean you need to change you entire *design* around that, especially when it comes to winforms. Don't micro-optimize your code until there's an *actual problem*, create a proper design that will allow you to naturally do what you need. If `Text` wasn't static, you wouldn't be having this problem. – Yuval Itzchakov Jun 17 '15 at 07:14
  • I am already AT that problem -- this is just a miniscule part of the project that was frankensteined to demonstrate the problem --- redraw of potentially thousands of controls, makes a difference when the method is cached and referenced in memory vs init'd every single instance of this object. I am looking for natural inheritance of the events while maintaining this level of performance --- if you do not know how to do this, that is fine, but please (as much as it's appreciated), do not tell me to restructure everything to code X way, when that's not what I am asking. :) – Kraang Prime Jun 17 '15 at 07:19
  • I actually do care about the resources my applications require both in distribution size, as well as memory space, and processor requirements. I do not believe in bloat simply because it is the easy route, or because some problem seems impossible. In code, all problems can be overcome with patience and skill. If this was cookie-cutter code, I wouldn't really need to ask this question :) – Kraang Prime Jun 17 '15 at 07:23
  • I would simply draw the text on it, however that leaves it in a non-selectable state, and since hyperlinks + ole may be in use, i am using the richtextbox for the implementation. since that particular control is unfriendly to shaping (eg, padding etc... you are free to research this), i need to draw the rtb on a control, and inherit, which over multiple controls adds a lot of weight with obj=new() all over the place in every instantiation of the main object. – Kraang Prime Jun 17 '15 at 07:28
  • Think I may have found what I was looking for here >> http://stackoverflow.com/questions/289002/how-to-raise-custom-event-from-a-static-class – Kraang Prime Jun 17 '15 at 07:32

0 Answers0