How can I change `All Desired` properties of the windows controls? - c#

I'm using WinForms : C# .NET.
I'm facing a problem with ContextMenuStrip and Toolstrip. Visual Stuido's Property editor is not letting me to change the property I want.
Here is the snapshot of how I want my ContextMenuStrip to looklike & same is the case with Toolstrip. I don't understand how to do this.
If I need to learn something, please suggest appropriate good material (tutorials, articles etc.)
alt text http://f.imagehost.org/0289/KproxyChecker.jpg

You'll have to assign the Renderer property to a class that renders the CMS or tool strip the way you want it. Use this code as a template to get started:
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
contextMenuStrip1.Renderer = new myRenderer();
}
class myRenderer : ToolStripProfessionalRenderer {
protected override void OnRenderToolStripBackground(ToolStripRenderEventArgs e) {
// Replace this with your own drawing code...
base.OnRenderToolStripBackground(e);
}
}
}

There is no single property that you can set to make a ContextMenuStrip look like that.
You need to create your own ToolStripRenderer class that paints menus like that, then set the Renderer property of the ContextMenuStrip to an instance of your ToolStripRenderer.
Good luck.
EDIT: You can find sample code here.

Related

Cannot add controls to panel in c# Windows Forms

I am trying to add several controls like Labels, pictureboxes etc. to a Panel in c# Windows Forms. My code looks like this:
this.panel1.Controls.Add(this.label1);
Every time I put this in the panel1 section in my Designer.cs, it gets deleted after I switch between classes and forms. I am doing this so that I can "transform" the Panel into a Bitmap with all the other controls in it.
Well, as you can see, you are trying to modify code within
#region Windows Form Designer generated code
...
// Designer is supposed to put (or/and remove) any code within this region
// Do not put any custom code here manually
this.panel1.Controls.Add(this.label1);
...
#endregion
and you have a conflict with Designer. Just let it generate its code in the area which specially designed (and marked out) for that; put yours into, say, constructor:
public MyForm() {
// Let .Net initialize the form, create all constrols etc. first
InitializeComponent();
// Then, run your code here
this.panel1.Controls.Add(this.label1);
}
Try :
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.panel1.Controls.Add(this.label1);
}
}

C# CenterToScreen() Winforms Designer Screen Position

I have a project that has forms that inherit properties from a base form (called frmBase). I have run into an issue that is really confusing me:
I want the program to center on the user screen, so I added
this.CenterToScreen();
to frmBase_Load(). That works great when I run the app, BUT, when I try to design any of the forms that inherit from frmBase, they all get moved to the very bottom right corner of the designer screen and I have to use scrollbars to see them.
If I move the
this.CenterToScreen();
to the frmBase() code, the app defaults to the top-left of the screen when it runs, but the designer displays the form correctly for me. Any idea what is going on? I searched, but can't seem to find a similar question, although I know I can't be the first person this has happened to. . . . .
As indicated by Hans and Reza your base class is being instantiated by the Visual Studio Form Designer so the code in the constructor and its Load event run as well. See this great answer for a detailed explanation of the parse behavior of the designer. Using the property DesignMode you can either prevent code being executed or make a distinction. The following code sample demonstrates its use:
Base form
The baseform sets the background color to Red when in DesignMode and Green when not in DesignMode.
// Form1 inherits from this class
public class MyBase : Form
{
public MyBase()
{
// hookup load event
this.Load += (s, e) =>
{
// check in which state we are
if (this.DesignMode)
{
this.BackColor = Color.Red;
}
else
{
this.BackColor = Color.Green;
}
};
}
}
Form1 that inherits the base form
No magic in the code, but notice the use of MyBase instead of Form
// we inherit from MyBase!
public partial class Form1 : MyBase
{
public Form1()
{
InitializeComponent();
}
}
Leading to the following result:

How to open modal dialog on control double click in c#

I am writing a control for c# (WinForms) and I have one property of Collection type.
When user select this property, button with "..." will be shown and new modal dialog will open. All this work fine, I have create:
public class ItemsEditor : UITypeEditor
In this class I have override EditValue method and open modal editor with ShowDialog. As I say this work fine.
But, I want to open this dialog when user of control double-click on it.
For this purpose I have inherit ControlDesigner:
public class MyControlDesigner : ControlDesigner
and in this class I have inherit next method:
public override void DoDefaultAction()
{
string propertyName = "Items";
IServiceProvider provider = (IServiceProvider)GetService(typeof(IServiceProvider));
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(MyControl);
PropertyDescriptor property = properties[propertyName];
UITypeEditor editor = (UITypeEditor)property.GetEditor(typeof(UITypeEditor));
editor.EditValue(provider, null);
}
As may be seen, I have put some random code and of course don't work.
Can somebody help me how to solve this, and how to open property on double-click.
Thank you for all help
Best regards
Bojan
I am not certain about feasibility of showing the editor on double click but one of other way could be using ActionLists aka SmartTag - please see article: http://msdn.microsoft.com/en-us/library/ms171829.aspx

Set PropertyGrid Default Popup Editor Startup Size (WinForms)

). How can you set the default size with which the Popup Editor shows up when you invoke it from a Property Grid.
This is for everybody who is familiar with Windows Forms' Property Grid Editor.
You know that if you throw a List property to a Grid, it shows the little [...] button which if you press it pops up its default sub-value editor. I actually use the editor for another type of object, but I gave this example just so you know what I'm referring to. And here's a picture, at least until the link lives:
http://www.perpetuumsoft.de/sf/en/ims/rssSilverlight/GetStart/image032.jpg
My understanding is that (both for modal and non-modal editors) it is completely up to the whim of the control being shown. If the UITypeEditor involved chooses a big form, it will be big...
The only way to change that would be to define your own UITypeEditor and associate it with the types involved (sometimes possible with TypeDescriptor.AddAttributes(...), that creates the same form as the runtime wanted to show, but resizes it before showing.
You can achieve this by inheriting from the standard System.ComponentModel.Design.CollectionEditor and then set the desired size in the CreateCollectionForm override.
Decorate your collection to use the custom collection editor.
Below is an example that will start up the collection editor in full screen
class FullscreenCollectionEditor : System.ComponentModel.Design.CollectionEditor
{
protected override CollectionForm CreateCollectionForm()
{
var editor = base.CreateCollectionForm();
editor.WindowState = System.Windows.Forms.FormWindowState.Maximized;
return editor;
}
public FullscreenCollectionEditor(Type type) : base(type)
{
}
}
And then decorate your collection property with [Editor(typeof(FullscreenCollectionEditor), typeof(UITypeEditor))] i.e.
public class MyModel
{
[Editor(typeof(FullscreenCollectionEditor), typeof(UITypeEditor))]
public List<FileModel> Files { get; set; }
}

extend a user control

I have a question about extending a custom control which inherits from UserControl.
public partial class Item : UserControl
{
public Item ()
{
InitializeComponent();
}
}
and I would like to make a control which inherits from Item
sg like that
public partial class ItemExtended : Item
{
public ItemExtended():base()
{
InitializeComponent();
}
}
This works perfectly of course and the heritage works but my problem is in the designer
I just cannot open this ItemExtended in Design....
it says : Constructor on Type "Item" not found.
Does sy have an explanation?
is the best way to do it?
Thx
I'm of course using c# on .NET Winform :)
you invoke InitializeComponent() twice with calling InitializeComponent() on the very derived usercontrol.
This may lead to problem.
And there is some help property callad IsDesign or Design (something similar) of UC, which helps to avoid unnecessary UI operations on design time (in VS).
Edit: it is DesignMode. You can avoid to run RT functions by Design. Like if (!this.DesignMode) InitializeComponents();
You can also check this forumpost. http://www.c-sharpcorner.com/Forums/ShowMessages.aspx?ThreadID=41254

Categories