[Mono-winforms-list] Bug in System.Windows.Forms.Control:get_ContainsFocus ()?

Rafael Teixeira monoman at gmail.com
Wed Jun 21 08:49:31 EDT 2006


Hi Paddy,

Why do you return base.ContainFocus instead of base.Focused from your
overriden Focused? That way you are introducing infinite recursion as
Control.ContainsFocus calls Control.Focused, that in your case is
overriden.

In truth, unless this is just some small code to show the problem, I
don't see any need to override Focused... Even more to change its
semantics.

Anyway if that code works in MS.NET, we should not rely on Focused to
determine partially the value of ContainsFocus (ContainsFocus also
checks children if the Control per se isn't with focus). Test it in
MS.NEt and if it works OK, please fill a bug for MWF, thanks.

:|

On 6/21/06, Paddy Joy <mono at paddyjoy.com> wrote:
> How about this code, I'm pretty sure that it instantiates button2
> correctly now but still gives the segmentation error.
>
> I have declared the button with:
> private testbutton button2;
>
> and instantiated it with:
> this.button2 = new testbutton();
>
> Thanks for the help,
> Paddy
>
>
> using System;
> using System.Drawing;
> using System.Collections;
> using System.ComponentModel;
> using System.Windows.Forms;
> using System.Data;
>
> namespace WindowsApplication4
> {
>     /// <summary>
>     /// Summary description for Form1.
>     /// </summary>
>     public class Form1 : System.Windows.Forms.Form
>     {
>         private System.Windows.Forms.Button button1;
>         private testbutton button2;
>         /// <summary>
>         /// Required designer variable.
>         /// </summary>
>         private System.ComponentModel.Container components = null;
>
>         public Form1()
>         {
>             //
>             // Required for Windows Form Designer support
>             //
>             InitializeComponent();
>
>             //
>             // TODO: Add any constructor code after InitializeComponent call
>             //
>         }
>
>         /// <summary>
>         /// Clean up any resources being used.
>         /// </summary>
>         protected override void Dispose( bool disposing )
>         {
>             if( disposing )
>             {
>                 if (components != null)
>                 {
>                     components.Dispose();
>                 }
>             }
>             base.Dispose( disposing );
>         }
>
>         #region Windows Form Designer generated code
>         /// <summary>
>         /// Required method for Designer support - do not modify
>         /// the contents of this method with the code editor.
>         /// </summary>
>         private void InitializeComponent()
>         {
>             this.button1 = new System.Windows.Forms.Button();
>             this.button2 = new testbutton();
>             this.SuspendLayout();
>             //
>             // button1
>             //
>             this.button1.Location = new System.Drawing.Point(96, 32);
>             this.button1.Name = "button1";
>             this.button1.TabIndex = 0;
>             this.button1.Text = "button1";
>             this.button1.Click += new
> System.EventHandler(this.button1_Click);
>             //
>             // button2
>             //
>             this.button2.Location = new System.Drawing.Point(96, 80);
>             this.button2.Name = "button2";
>             this.button2.TabIndex = 1;
>             this.button2.Text = "button2";
>             //
>             // Form1
>             //
>             this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
>             this.ClientSize = new System.Drawing.Size(292, 262);
>             this.Controls.Add(this.button2);
>             this.Controls.Add(this.button1);
>             this.Name = "Form1";
>             this.Text = "Form1";
>             this.ResumeLayout(false);
>
>         }
>         #endregion
>
>         /// <summary>
>         /// The main entry point for the application.
>         /// </summary>
>         [STAThread]
>         static void Main()
>         {
>             Application.Run(new Form1());
>         }
>
>         private void button1_Click(object sender, System.EventArgs e)
>         {
>             Console.Write(button2.Focused);
>         }
>     }
>
>     public class testbutton : System.Windows.Forms.Button
>     {
>
>         public testbutton()
>         {
>
>
>         }
>
>         public override bool Focused
>         {
>             get
>             {
>                 return base.ContainsFocus;
>             }
>         }
>
>
>
>     }
> }
>
>
> > Hi,
> >
> >
> >> So do you think it could be a bug with resolving the base class? It
> >> seems from the trace that it is going into a continuous loop, like it is
> >> calling ContainsFocus on itself.
> >>
> >
> > To be honest, all I saw was the segfault rather than the tonnes of
> > output. The baseclass seems to be working fine as well
> >
> > I did the following to your code and if you run it, you'll see that the
> > baseclass is fine
> >
> > using System;
> > using System.Drawing;
> > using System.Collections;
> > using System.ComponentModel;
> > using System.Windows.Forms;
> > using System.Data;
> >
> > namespace WinApp4
> > {
> >   public class Form1 : System.Windows.Forms.Form
> >   {
> >     private System.Windows.Forms.Button button1;
> >     private System.Windows.Forms.Button button2;
> >     private System.ComponentModel.Container components = null;
> >
> >     public Form1()
> >     {
> >       InitializeComponent();
> >     }
> >
> >     protected override void Dispose(bool disposing)
> >     {
> >       if (disposing)
> >       {
> >         if (components != null)
> >         {
> >           components.Dispose();
> >       }
> >       }
> >       base.Dispose(disposing);
> >     }
> >
> >     private void InitializeComponent()
> >     {
> >       this.button1 = new System.Windows.Forms.Button();
> >       this.button2 = new System.Windows.Forms.Button();
> >       this.SuspendLayout();
> >
> >       this.button1.Location = new System.Drawing.Point(96, 32);
> >       this.button1.Name = "button1";
> >       this.button1.TabIndex = 0;
> >       this.button1.Text = "button1";
> >       this.button1.Click += new System.EventHandler(this.button1_Click);
> >
> >       this.button2.Location = new System.Drawing.Point(96, 80);
> >       this.button2.Name = "button2";
> >       this.button2.TabIndex = 1;
> >       this.button2.Text = "button2";
> >       this.button2.Click += new System.EventHandler(this.button2_Click);
> >
> >       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
> >       this.ClientSize = new System.Drawing.Size(292, 262);
> >       this.Controls.Add(this.button2);
> >       this.Controls.Add(this.button1);
> >       this.Name = "Form1";
> >       this.Text = "Form1";
> >       this.ResumeLayout(false);
> >     }
> >
> >     [STAThread]
> >     static void Main()
> >     {
> >       Application.Run(new Form1());
> >     }
> >
> >     private void button1_Click(object sender, System.EventArgs e)
> >     {
> >       Console.Write(base.ContainsFocus);
> >     }
> >
> >     private void button2_Click(object sender, System.EventArgs e)
> >     {
> >       Console.WriteLine(base.ContainsFocus);
> >     }
> >   }
> > }
> >
> > The output (at least on my box) is
> >
> > True (button 1 pressed - no return feed)
> > TrueTrue (button 2 pressed)
> >
> > >From the looks of the original code, you've created the second button,
> > but never properly instantated it (it is declared as private swf.button
> > button2 at the start, but never created using this.button2 = new
> > swf.button()). That (to me) is the most likely candidate for the
> > breakage.
> >
> > TTFN
> >
> > Paul
> >
> >
>
> _______________________________________________
> Mono-winforms-list maillist  -  Mono-winforms-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-winforms-list
>


-- 
Rafael "Monoman" Teixeira
---------------------------------------
"The reasonable man adapts himself to the world; the unreasonable one
persists in trying to adapt the world to himself. Therefore all
progress depends on the unreasonable man." George Bernard Shaw


More information about the Mono-winforms-list mailing list