When I continuously open and close Form2, GDI count in TaskManager keeps increasing by 1.
After increasing the GDI count to some point(Say 70-80), it will automatically decreases to some count(Say 30-40) and so on.

when setting Font=null in Dispose() method of TextBoxEx,GDI count in TaskManagerwill be reset to 4 while closing the Form.

 protected override void Dispose( bool disposing ) 
 {
      if( disposing )
      {
         if (components != null) {
           components.Dispose();
          }
      }
     base.Dispose( disposing );
     Font=null;
}

Is it recommended to set Font as NULL in Dispose() method of TextBox control Or Should I ignore it and let the Font to get garbage collected ?

I have created a custom control like below.

 public partial class TextBoxEx : TextBox
{
  public TextBoxEx()
  {
    InitializeComponent();
    Font = Utility.normalFont;
  }

protected override void OnPaint(PaintEventArgs pe)
{
    base.OnPaint(pe);
 }
}
//A utility class to initialize font.   
class Utility
{

    internal static Font normalFont = new Font("Arial", 18);
}

I have two forms Form1 and Form2. This TextBoxEx is added to Form2. I am showing Form2 when clicking a button in Form1.

Continuously showing and closing Form2 causes GDI leak in my application. After analysing with a GDI detection tool(Bear.exe), it is found that the Font causes GDI leak.

My question is,

  1. Why the Font is not released even though Dispose() method of TextBoxEx is get called.(While closing Form2, Dispose() method of TextBoxEx will be invoked automatically).
  2. How can I solve GDI leak caused by Font?.
    (Font.Dispose() cannot be called in Dispose() method of TextBoxEx. Because it throws "Parameter is not valid" exception in the constructor).

Yes. I got it.
It will be true in the disposing event of base control. I have checked the MSDN implementation. In Dispose() method of Control class, the property is set as true in disposing code block and it is reset to false after disposed.

According to MSDN:

Control.Disposing Property Gets a value indicating whether the base Control class is in the process of disposing.

But when I checked, Disposing property of the control is always false (inside the Dispose() method itself).

When will this Disposing property become true?

What is the difference between

try
{
  //Some code
}
catch
{

}

and

try
{
  //Some code
}
catch(Exception)
{

}

Hi All,

I have a TextBox in my Form and I am drawing a string into it as below.

Font myFont = new Font("Arial",18F,FontStyle.Regular,GraphicsUnit.Point,128);
Graphics g = myTextBox.CreateGraphics();
Brush b = new SolidBrush(Color.Red);
g.DrawString("Item Drawn with DrawString",myFont ,b,new PointF(1,1));

The string displayed in the TextBox. Then I tried below code

Font myFont = new Font("Arial",18F,FontStyle.Regular,GraphicsUnit.Point,128);
Graphics g = myTextBox.CreateGraphics();
TextRenderer.DrawText(g,"Item Drawn with DrawText",myFont,new Point(1,1),Color.Red);

Here the problem comes. Even though two methods g.DrawString() and TextRenderer.DrawText() uses same font, there is a difference in font style. That is some characters are rendered differently. If I use "1" instead of "128" in the font both methods will render characters as unique.

If I change GdiCharSet(128) value in font, while using g.DrawString() method there will be no effect. My question is why g.DrawString() method excludes GdiCharSet value?

There is a solution.

If we set

ListView.Capture=true; 

we can keep the mouse pointer without hiding. While setting Capture property of ListView as true, It will capture the mouse events and mouse pointer wont be disappeared while typing in TextBox.

Another Problem.

I am using RichTextBox instead of TextBox. The above mentioned technique will not work with RichTextBox. That means mouse cursor will disappear while typing in RichTextBox even though ListView.Capture=true.

When using RichTExtBox and ListView.Capture=true, ListView captures mouse move and mouse click events but mouse pointer is disappearing. Any one know why it is happening like this?

I have created a custom combo box(Text box, button and List View).

While typing in the text box, cursor disappears. How can I disable "Hiding cursor while typing" in my application.

I goggled and found that it is the system behavior called SPI_SETMOUSEVANISH. It can be turned off but it will affect applications.

How can I turn-off this only in my custom control?

The class NativeListWindow inherited from System.Windows.Forms.NativeWindow.
http://msdn.microsoft.com/en-us/library/system.windows.forms.nativewindow.aspx

ComboBoxInfo is the structure contains combo box status information.
http://www.pinvoke.net/default.aspx/Structures/COMBOBOXINFO.html

Win32 is my-own class which encapsulates all unmanaged APIs like SendMessage(),GetComboBoxInfo() and SetFocus() etc.

I need it in C# not in C++.

Hi,

I am trying to customize default windows scrollbar in combobox like below.

public partial class ComboEx : ComboBox
{
    internal ScrollbarEx vScrollBar;
    NativeListWindow listControl;
    public ComboEx()
    {
        InitializeComponent();
        DropDownHeight = 100;
        vScrollBar = new ScrollbarEx();
    }

    protected override void OnPaint(PaintEventArgs pe)
    {
        base.OnPaint(pe);
    }

    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        COMBOBOXINFO combInfo = new COMBOBOXINFO();
        combInfo.cbSize = Marshal.SizeOf(combInfo);
        Win32.GetComboBoxInfo( this.Handle, ref combInfo );

        listControl = new NativeListWindow(this, combInfo.hwndList);
    }

    protected override void WndProc(ref Message m)
    {
         if (m.Msg == (Win32.WM_REFLECT + Win32.WM_COMMAND))
        {
            if (Win32.HIWORD( (int)m.WParam ) == Win32.CBN_DROPDOWN)
            {
                COMBOBOXINFO combInfo = new COMBOBOXINFO();
                combInfo.cbSize = Marshal.SizeOf(combInfo);
                Win32.GetComboBoxInfo( this.Handle, ref combInfo );

                vScrollBar.Location = new Point( this.Width-23, 1 );
                vScrollBar.Size = new Size( 23, DropDownHeight );
                vScrollBar.Visible = true;
                Win32.SetParent(vScrollBar.Handle, combInfo.hwndList);
                Win32.ShowWindow(vScrollBar.Handle, ShowWindowCommands.Show);
                Win32.SetWindowPos(vScrollBar.Handle,HWND.TopMost, 155, 1, 23, 105, SetWindowPosFlags.SWP_SHOWWINDOW);
            }
        }
        base.WndProc(ref m);
    }

    // MyNativeWindow class to create a window given a class name.
    [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
    internal class NativeListWindow : NativeWindow
    {
        // Constant values were found in the "windows.h" header file.
        private const int WS_CHILD = 0x40000000,
                          WS_VISIBLE = 0x10000000,
                          WM_ACTIVATEAPP = 0x001C;

        private int windowHandle;

        private ComboEx parent;

        public NativeListWindow(ComboEx owner,IntPtr handle)
        {
            AssignHandle(handle);
            parent = owner;

        }

        // Listen to when the handle changes to keep the variable in sync
        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        protected override void OnHandleChange()
        {
            windowHandle = (int)this.Handle;
        }

        private void AdjustClientRect(ref RECT rect)
        {
            rect.right -= 23;
        }

        [System.Security.Permissions.PermissionSet(System.Security.Permissions.SecurityAction.Demand, Name = "FullTrust")]
        protected override void WndProc(ref Message message)
        {
            // Listen for messages that are sent ...

Yes I did it. I can intercept the key press events in Listview to textbox. But I need to show the cursor in textbox when the ListView has the focus. How....?

Hi All,

Can I show input cursor in a non-focused RichTextBox?

In my project, I have a RichTextBox and a ListView control. The focus should be on ListView control. I could handle the KeyPress and KeyDown events of ListView and re-directed to RichTextBox to enable typing. But how can I show cursor on RichTextBox.

RichTextBox.focus() cann't be called. The focus must be in ListViewControl (like combobox ). Any Idea....?

Thank you very much. I will look into it.
Mean time I started working on it. Will update soon

I have a custom ObjectCollection class in my custom control and a public property to access this collection.

//My custom Property
[Editor("System.Windows.Forms.Design.StringCollectionEditor",typeof(UITypeEditor))]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public ObjectCollection Items
{
    get { return itemCollection; }
}

//My custom collection class
[ListBindable(false)]
public class ObjectCollection : IList, ICollection, IEnumerable
{
  //code
}

However, the values are NOT getting persisted. In design mode if I save strings in StringEditor by clicking OK button, close it, and reopen it, then the strings are gone.
Any idea?

Hi,

I have a custom combobox (say ComboExt). I need to host this control in datagridview. In MSDN (http://msdn.microsoft.com/en-us/library/7tas5c80.aspx), they are saying, to host custom controls, my own column types with cells should be created. That is, I need to create a custom ComboExtColumn derived from DataGridViewColumn and all properties in DataGridViewComboBoxColumn like DataSource,Items, AutoComplete etc should be implemetend. I feel it is very complicated.

My question is, Could I use DataGridViewComboBoxColumn instead of DataGridViewColumn to host my custom combobox(ComboExt) in DataGridView like as below?

public class ComboExtColumn : DataGridViewComboBoxColumn
{   
    //Code        
}
public class ComboExtCell : DataGridViewComboBoxCell
{
   //Code 
}
class ComboExtEditingControl : ComboExt, IDataGridViewEditingControl
{
}

Please provide some idea. Sorry for my bad English.