I'm trying to create a custom control by deriving from a ZedGraphControl
I need to add a ProgressBar to the control, but I encountered some problems.
When I create a custom control and add both, ZedGraphCOntrol and ProgressBar to it, everything is OK:
MyCustomControl
{
ZedGraphControl
ProgressBar
}
All elemnets are visible and working as expected.
But I need to derive from ZGC and when I add a progress bar as a subcontrol of ZedGraphControl:
MyCustomControl : ZedGRaphControl
{
ProgressBar
}
The progress bar is not visible.
Is there any way to force the visibility of ProgressBar? Is it possible, that ZedGraphControl is not displaying its subcontrols? I tried do the same thing with a simple button and it's also not being displayed.
it seems that I solved the problem...
There must be a call to the InitializeComponent() method in the Control's constructor.
When I was creating a custom control, it was added by default by the VS. But while deriving from ZedGraphControl I was creating the constructor from scratch, and that's why the subcontrols were not visible.
So the question can be closed.
Related
I have a custom control that has a refresh method, something similar to this:
public class MyControl : Canvas
{
// Dependency property for "data" used to draw the control here
public void Refresh()
{
Children.Clear();
// Using data, draw the control
Children.Add(new Line(...));
Children.Add(new Rectangle(...));
// etc.
}
}
Right now, I have to call Refresh() manually each time I want the look of the control to update. My dependency properties are set up for FrameworkPropertyMetadataOptions.AffectsArrange, so WPF knows that modifying the properties will affect the arrangement of the control and that it should be redrawn. So here's the question:
What does WPF use to tell a custom control that it should be redrawn? Is it an event, or an override, and how should it be used? I've tried handling various events and overrides and nothing seems to work. So, what's the correct way to do this? I want to replace / wrap the Refresh() method above in something "automatic" that WPF will handle automatically.
m developing a sort of project which I needed a cool customizable interface, so I designed a 'parent-form' from which all childs would get 'stylized', according to XML customization options.
I added a TableLayoutPanel to draw borders and a Panel in the middle, where child forms would supposedly add their components and make their jobs.
The problem I face is, even though I set that 'content panel' to 'public', the designer wont let me add controls to it from the child forms.
Is there any different way I can make designable forms deriving from a 'customizable' superclass?
Edit: The parent class is public, every container containing the Content-Panel are also set to public.
I manually added to child's designer.cs a new Panel inside the parent's content pane, set it to DockStyle.Fill. When I came back to the Designer, it will now let me add components to child's content Panel.
A bit messy and I'm pretty sure there shall be another way around...
But I'll work along like this until i can figure out a better workaround.
I have added a new public Panel from code other than designer in the parent's class scope, Then in the parent constructor I added it to the TableLayoutPanel, configured docking and colspan from constructor code, below InitializeComponents() call and BAM!
public Panel contentPane = new Panel();
public Dialogo()
{
InitializeComponent();
Content.Controls.Add(contentPane);
contentPane.Dock = DockStyle.Fill;
// More code
}
So its a contentPane inside 'Content' which is another panel in the second line of the table ocupying 5 columns (so the table surrounds it and allows me to draw the borders around.
I don't know why, but having added the content-panel in code other than on the designer allowed me to directly add components to the panel from the Designer in child forms.
I need to display a ListView in WinForms which should not have any lines between columns. I tried GridLines=false and also tried setting HeaderStyle to ColumnHeaderStyle.None. But this is not working. I want to remove the 2 vertical lines coming in the middle.
screenshot http://www.freeimagehosting.net/uploads/4bcee1f639.png
GridLine = False should work. (See Pramodh answer.) If not, you have something else wrong. If you setting this property in code, make sure the control's handle has been created. The method CreateControl and IsHandleCreated are helpful.
A common problem in C# that affects controls is accessing control properties in the form's constructor. Usually, the control handles are not created until they are "visible", which doesn't happen until the form is loaded. Therefore, if setting these properties in the form constructor, move the code to the Load event which is recommended by Microsoft.
Control.CreateControl Method
The CreateControl method forces a
handle to be created for the control
and its child controls. This method is
used when you need a handle
immediately for manipulation of the
control or its children; simply
calling a control's constructor does
not create the Handle.
CreateControl does not create a
control handle if the control's
Visible property is false. You can
either call the CreateHandle method or
access the Handle property to create
the control's handle regardless of the
control's visibility, but in this
case, no window handles are created
for the control's children.
just change the view into Details
and GridLines=false
like:
this.listView1.View = System.Windows.Forms.View.Details;
this.listView1.GridLines = false;
I have a lot of different UserControls and would like to maintain consistent UI settings (mainly colors and fonts). My first try was this:
public class UISettings
{
//...
public void SetupUserControl(ref UserControl ctrl)
{
ctrl.BackColor = this.BackColor;
}
}
to be called in every control like this:
settings.SetupUserControl(ref this);
As this is read-only it cannot be passed by ref argument so this does not work. What are other options to keep consistent UI without manually changing properties for every item?
Inheritance! If you have a form or control that will constantly be using the same styles and you want to set that as your base, just create your own user controls that inherit from a form/control. By default all of your forms will inherit from "Form". Instead of inheriting from the default form, create a new user control that inherits from Form, and then have that as your base class.
CustomForm : Form // Your custom form.
Form1 : CustomForm // Inherit from it.
...the same works for components. If you want a button to have the same styles across the board, create a user control and have it inherit from the button control -- then use the custom control.
Whenever you want to make a change to your base styles, or any settings, simply change your custom controls settings -- your new forms/controls will automatically be updated!
Do the same thing. Don't pass it by ref. UserControl is a reference object already, so there's no need to pass it into your method using the ref keyword.
You may also want to consider a recursive method that will find all the UserControls on the form and pass it into your method.
How about a base class which provides such settings?
Two answers:
You don't need ref, controls are objects are reference types. Just drop it.
Create a Base UserControl and derive your controls form that base. You can still do that, just edit the class definitions of the controls. For new controls you can follow the Wizard.
A tip: setup the styling in the baseControl. Then make sure the derived controls don't override, the best way to do that is scanning the *.Designer.cs files and remove all settings that you know should come from the base.
I would like to create a custom version of the TabControl so that when a new TabPage is added I can ensure some custom processing is performed.
The question is how do I override the TabPages.Add() method to achieve this?
thanks,
Richard
Unfortunatelly, you cannot override Add() method of TabPageCollection class. What you may try is to subscribe to TabControl.ControlAdded event in hope that it will be raised when a TabPage (which is essentially a Control as well) will be added.
You could create the custom version which inherits from the TabControl, and has a public new void Add(string) method. But if anyone casts your control back to the TabControl, they would go around your logic. You could try creating a custom control which inherits from System.Windows.Forms.Control and expose all the methods of a private TabControl, modifying the Add method as needed. This would give you much more control.