|
Question's in the title, how does the ListBox control show its name in design mode? I've used reflector but can't find the code anywhere. I've created a control that inherits from ListBox and uses custom drawing, so now the name won't show up in design mode. Does anyone know how I can do it? - Edited byScottyDoesKnow Saturday, July 18, 2009 12:16 AM
-
|
| ScottyDoesKnow Friday, July 17, 2009 4:53 PM |
While I can't solve your other problem, you can figure out whether it's in design mode in the ctor too. See : http://blog.voidnish.com/?p=191 http://blog.voidnish.com- Marked As Answer byScottyDoesKnow Saturday, July 18, 2009 12:15 AM
-
|
| Nishant Sivakumar Friday, July 17, 2009 8:54 PM |
using System; using System.Windows.Forms; public class MyListBox : ListBox { private DrawMode mDrawMode = DrawMode.Normal; public new DrawMode DrawMode { get { return mDrawMode; } set { mDrawMode = value; if (!DesignMode) base.DrawMode = value; } } } Hans Passant.- Marked As Answer byScottyDoesKnow Saturday, July 18, 2009 12:16 AM
-
|
| nobugz Friday, July 17, 2009 9:04 PM |
Ya I'm a little slow, I was talking about Nishant's suggestion. Combining the two works beautifully:
private DrawMode drawMode = DrawMode.Normal;
/// <summary>
/// Gets or sets the drawing mode for the control.
/// </summary>
[DefaultValue(DrawMode.OwnerDrawFixed), Description("Controls list box painting. If set to Normal, disabled items will not appear disabled.")]
public override DrawMode DrawMode
{
get { return drawMode; }
set
{
drawMode = value;
// So that the name shows in the designer
if (!IsDesignMode()) base.DrawMode = value;
}
}
private bool IsDesignMode()
{
return LicenseManager.UsageMode == LicenseUsageMode.Designtime
|| Process.GetCurrentProcess().ProcessName.ToLowerInvariant() == "devenv.exe";
}
And in the constructor it sets the DrawMode to OwnerDrawFixed. Thanks a lot guys. - Marked As Answer byScottyDoesKnow Saturday, July 18, 2009 12:16 AM
-
|
| ScottyDoesKnow Saturday, July 18, 2009 12:15 AM |
If you look at the ListBox control you can see that it has a custom designer specified:
[SRDescription("DescriptionListBox"),
ClassInterface(ClassInterfaceType.AutoDispatch),
Designer("System.Windows.Forms.Design.ListBoxDesigner, System.Design, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"),
DefaultEvent("SelectedIndexChanged"),
DefaultProperty("Items"),
DefaultBindingProperty("SelectedValue"),
ComVisible(true)]
public class ListBox : ListControl
If you take a look at the ListBoxDesigner you can see how the name is rendered on the listbox control at designtime (inside the private UpdateControlName method). You might be able to use the same designer for your inherrited control aswell.
The reason the name dissapears in your inherited control is most likely because the VS designer does not use designers specified on controls you inherit from even if you haven't specified one.
|
| nillebo Friday, July 17, 2009 5:19 PM |
It looks impossible to do. Even for the normal listbox if you set DrawMode to OwnerDraw it doesn't show. I found the code that ListBoxDesigner uses:
private void UpdateControlName(string name)
{
ListBox control = (ListBox) this.Control;
if (control.IsHandleCreated && (control.Items.Count == 0))
{
NativeMethods.SendMessage(control.Handle, 0x184, 0, 0);
NativeMethods.SendMessage(control.Handle, 0x180, 0, name);
}
}
And that's really as far as I'm willing to go. Unless some guru comes by that knows how to do this I'm just gonna drop it. |
| ScottyDoesKnow Friday, July 17, 2009 5:41 PM |
Scotty, this control ...
class MyListBox : ListBox { public MyListBox() { return; } }
..writes its' own name inside of itself when I drop one on a form using the Form Designer. Must be something else you are doing that is preventing from writing its' name. Mark the best replies as answers. "Fooling computers since 1971." |
| Rudedog2 Friday, July 17, 2009 7:20 PM |
There is, I'm setting DrawMode to OwnerDrawFixed. If you do this even on a normal ListBox you can no longer see the name. What I was hoping to find out was how ListBox does it when it's not owner drawn so I could copy the code and draw it myself in design mode. |
| ScottyDoesKnow Friday, July 17, 2009 7:25 PM |
Must the contol use that DrawMode when it is in DesignMode?
Mark the best replies as answers. "Fooling computers since 1971." |
| Rudedog2 Friday, July 17, 2009 7:40 PM |
if (DesignMode)
base.DrawMode = DrawMode.Normal;
else
base.DrawMode = DrawMode.OwnerDrawFixed;
I tried that in the constructor and it didn't work, the constructor isn't affected by the flag, just tested:
if (DesignMode)
throw new Exception();
else
base.DrawMode = DrawMode.OwnerDrawFixed;
Plus I don't want the DrawMode in the properties window to say "Normal" if I can help it. Really this is just a little bonus. I don't care if it's complicated, but I'm not gonna do it if I have to change something else for the worse (like having it say Normal in the designer). |
| ScottyDoesKnow Friday, July 17, 2009 8:03 PM |
While I can't solve your other problem, you can figure out whether it's in design mode in the ctor too. See : http://blog.voidnish.com/?p=191 http://blog.voidnish.com- Marked As Answer byScottyDoesKnow Saturday, July 18, 2009 12:15 AM
-
|
| Nishant Sivakumar Friday, July 17, 2009 8:54 PM |
using System; using System.Windows.Forms; public class MyListBox : ListBox { private DrawMode mDrawMode = DrawMode.Normal; public new DrawMode DrawMode { get { return mDrawMode; } set { mDrawMode = value; if (!DesignMode) base.DrawMode = value; } } } Hans Passant.- Marked As Answer byScottyDoesKnow Saturday, July 18, 2009 12:16 AM
-
|
| nobugz Friday, July 17, 2009 9:04 PM |
That did find it, but ya it shows Normal in the design mode, which I don't like. Thanks for the tip. |
| ScottyDoesKnow Friday, July 17, 2009 9:07 PM |
No it doesn't.
Hans Passant. |
| nobugz Friday, July 17, 2009 9:14 PM |
Ya I'm a little slow, I was talking about Nishant's suggestion. Combining the two works beautifully:
private DrawMode drawMode = DrawMode.Normal;
/// <summary>
/// Gets or sets the drawing mode for the control.
/// </summary>
[DefaultValue(DrawMode.OwnerDrawFixed), Description("Controls list box painting. If set to Normal, disabled items will not appear disabled.")]
public override DrawMode DrawMode
{
get { return drawMode; }
set
{
drawMode = value;
// So that the name shows in the designer
if (!IsDesignMode()) base.DrawMode = value;
}
}
private bool IsDesignMode()
{
return LicenseManager.UsageMode == LicenseUsageMode.Designtime
|| Process.GetCurrentProcess().ProcessName.ToLowerInvariant() == "devenv.exe";
}
And in the constructor it sets the DrawMode to OwnerDrawFixed. Thanks a lot guys. - Marked As Answer byScottyDoesKnow Saturday, July 18, 2009 12:16 AM
-
|
| ScottyDoesKnow Saturday, July 18, 2009 12:15 AM |