Can't seem to figure out how to do this. I have an inherited Control: MyControl with a property called MyOtherFont. How do I get MyOtherFont to inherit the ambient value of the Parent control's Font property?
For example, from the designer if I drag this control onto a Form where the font is Segoe UI, it should inherit that value from the Form and not show it as bolded in the property window.
Thanks
Figured it out. Here is a C# example that does exactly what my example describes. Hope this helps someone.
public class MyControl : Control
{
private Font myOtherFont;
public Font MyOtherFont
{
get
{
if (this.myOtherFont == null)
{
if (base.Parent != null)
return base.Parent.Font;
}
return this.myOtherFont;
}
set
{
this.myOtherFont = value;
}
}
private bool ShouldSerializeMyOtherFont()
{
if (base.Parent != null)
if (base.Parent.Font.Equals(this.MyOtherFont))
return false;
if (this.MyOtherFont == null)
return false;
return true;
}
private void ResetMyOtherFont()
{
if (base.Parent != null)
this.MyOtherFont = base.Parent.Font;
else
this.MyOtherFont = Control.DefaultFont;
}
}
Related
Is there any way to create a function in C# that can change the visibility of an given object that has the visibility property?
public void changeVisibilityState(Button test)
{
if (test.Visibility == Visibility.Collapsed)
{
test.Visibility = Visibility.Visible;
}
else
{
test.Visibility = Visibility.Collapsed;
}
}
Like this but for any object with the visibility property.
You can use more common object that implements Visibility property. For example UIElement.
From msdn:
UIElement is a base class for most of the Windows Runtime UI objects
that have visual appearance and can process basic input as part of
your app's user interface
So use something like this:
public void ChangeVisibilityState(UIElement test)
{
if (test.Visibility == Visibility.Collapsed)
{
test.Visibility = Visibility.Visible;
}
else
{
test.Visibility = Visibility.Collapsed;
}
}
This way you can pass to this function anything that inherits UIElement
As you wanted. Every object with "visibility" property :
public static void changeVisibility(this object obj, Visibility vis) {
var property = obj.GetType().GetProperty("visibility", BindingFlags.Instance);
if ( property != null ) {
property.SetValue(obj, vis);
}
}
I put a DataGridView in a UserControl and create a public property in my usercontrol that exposes datagridview's columns property.Here is the sample code:
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public DataGridViewColumnCollection MyDataGridColumns
{
get
{
return dataGridView1.Columns;
}
}
}
Then I add UserControl1 in my form and I click on MyDataGridColumns property in property window and add 1 or more columns. The problem happens when I rebuild my solution; All of the columns that I have just added disappear after rebuilding.
Can anyone explain to me why this happens? and how to solve it?
This works for me : I created a specific columns editor as it seems it is impossible to use the default columns editor for any control that does not extend DataGridView.
public partial class UserControl1 : UserControl, IDataGridView
{
public UserControl1()
{
InitializeComponent();
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
[Browsable(false)]
public DataGridView DataGridView
{
get { return dataGridView1; }
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[Editor(typeof(ExtendedDataGridViewColumnCollectionEditor), typeof(UITypeEditor))]
[MergableProperty(false)]
public DataGridViewColumnCollection MyDataGridColumns
{
get { return dataGridView1.Columns; }
}
}
public interface IDataGridView
{
DataGridView DataGridView { get; }
}
class ExtendedDataGridViewColumnCollectionEditor : UITypeEditor
{
private Form dataGridViewColumnCollectionDialog;
private ExtendedDataGridViewColumnCollectionEditor() { }
private static Form CreateColumnCollectionDialog(IServiceProvider provider)
{
var assembly = Assembly.Load(typeof(ControlDesigner).Assembly.ToString());
var type = assembly.GetType("System.Windows.Forms.Design.DataGridViewColumnCollectionDialog");
var ctr = type.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0];
return (Form)ctr.Invoke(new object[] { provider });
}
public static void SetLiveDataGridView(Form form, DataGridView grid)
{
var mi = form.GetType().GetMethod("SetLiveDataGridView", BindingFlags.NonPublic | BindingFlags.Instance);
mi.Invoke(form, new object[] { grid });
}
public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
{
if (provider != null && context != null)
{
var service = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
if (service == null || context.Instance == null)
return value;
var host = (IDesignerHost)provider.GetService(typeof(IDesignerHost));
if (host == null)
return value;
if (dataGridViewColumnCollectionDialog == null)
dataGridViewColumnCollectionDialog = CreateColumnCollectionDialog(provider);
//Unfortunately we had to make property which returns inner datagridview
//to access it here because we need to pass DataGridView into SetLiveDataGridView () method
var grid = ((IDataGridView)context.Instance).DataGridView;
//we have to set Site property because it will be accessed inside SetLiveDataGridView () method
//and by default it's usually null, so if we do not set it here, we will get exception inside SetLiveDataGridView ()
var oldSite = grid.Site;
grid.Site = ((UserControl)context.Instance).Site;
//execute SetLiveDataGridView () via reflection
SetLiveDataGridView(dataGridViewColumnCollectionDialog, grid);
using (var transaction = host.CreateTransaction("DataGridViewColumnCollectionTransaction"))
{
if (service.ShowDialog(dataGridViewColumnCollectionDialog) == DialogResult.OK)
transaction.Commit();
else
transaction.Cancel();
}
//we need to set Site property back to the previous value to prevent problems with serializing our control
grid.Site = oldSite;
}
return value;
}
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)
{
return UITypeEditorEditStyle.Modal;
}
}
This is because you didn't specify the type of the column. You should give the type of the column when adding a column (for example DataGridViewTextBoxColumn or DataGridViewCheckBoxColumn). In your Form1.cs do the following:
public Form1()
{
InitializeComponent();
DataGridViewColumn dgViewColumn = new DataGridViewTextBoxColumn();//Or DataGridViewCheckBoxColumn
dgViewColumn.DataPropertyName = "dgViewColumn";
dgViewColumn.HeaderText = #"dgViewColumn";
dgViewColumn.Name = "dgViewColumn";
userControl11.MyDataGridColumns.Add(dgViewColumn);
}
#Bioukh answer works in VS2019 and somewhat works in VS2022. However, the results of embedding the DataGridView control in my UserControl then adding and editing the Columns using the answer does not enable those Columns to migrate to another instance of the UserControl. For example: Copy/Paste the UserControl and all of the embedded DataGridView's columns disappear from the new copy.
To Work Around this issue I maintain my DataGridView instances as native and use a public DataGridView property in my UserControl with the binding and docking performed in the property setter. I then drop my_UserControl on my form, drop my_DataGridView on my form, and then set my_UserControl.DataGridView = my_DataGridView. This work around preserves the native properties and behaviors associated with the DataGridView.
In my_UserControl, I have a Panel named "GridPanel" and a VScrollBar. I then added the following property:
///<summary>
/// Associates a native DataGridView with this UserControl
/// then sets the DataGridView.Parent to the Panel in this UserControl
/// and sets the DataGridView.Dock to Fill the Panel
///</summary>
public DataGridView? ContainedDataGridView
{
get
{
try
{
// if we have a DataGridView in our Panel then return it
if ((this.GridPanel.Controls.Count == 1)
&& (this.GridPanel.Controls[0] is DataGridView view))
{
return view;
}
}
catch (Exception ex)
{
//// TODO Handle "ContainedDataGridView get error"
}
// Return null if there is no DataGridView or there was an error checking for it.
return null;
}
set
{
try
{
// Clear the panel to prevent adding more than one DataGridView
this.GridPanel.Controls.Clear();
if (value is not null)
{
this.GridPanel.Controls.Add(value);
value.Parent = this.GridPanel;
value.Dock = DockStyle.Fill;
}
// else the panel remains cleared
}
catch (Exception ex)
{
//// TODO Handle "ContainedDataGridView set error"
}
}
}
The above snippet is coded as C# 10, .NET 6, Windows Forms App, UserControl and tested in Visual Studio 2022 version 17.0.3
I have used the code from this blog, as below but abridged, and I see the WinForm inside my main window, but the sample text I placed on it in a label is not visible.
[System.Windows.Markup.ContentProperty("Child")]
public class WinFormsHost : HwndHost
{
public WinFormsHost()
{
var form = new ChildForm();
Child = form;
}
private System.Windows.Forms.Form child;
public event EventHandler<ChildChangedEventArgs> ChildChanged;
public System.Windows.Forms.Form Child
{
get { return child; }
set
{
HwndSource ps = PresentationSource.FromVisual(this) as HwndSource;
if (ps != null && ps.Handle != IntPtr.Zero)
{
throw new InvalidOperationException("Cannot set the Child property after the layout is done.");
}
Form oldChild = child;
child = value;
OnChildChanged(oldChild);
}
}
private void CheckChildValidity()
{
if (child == null || child.Handle == IntPtr.Zero)
{
throw new ArgumentNullException("child form cannot be null");
}
}
public Boolean ShowCaption
{
get
{
CheckChildValidity();
return (GetWindowStyle(Child.Handle) & WindowStyles.WS_BORDER) == WindowStyles.WS_CAPTION;
}
set
{
if (child == null)
{
this.ChildChanged += delegate
{
if (value)
{
SetWindowStyle(Child.Handle, GetWindowStyle(Child.Handle) | WindowStyles.WS_CAPTION);
}
else
{
SetWindowStyle(Child.Handle, GetWindowStyle(Child.Handle) & ~WindowStyles.WS_CAPTION);
}
};
}
else
{
if (value)
{
SetWindowStyle(Child.Handle, GetWindowStyle(Child.Handle) | WindowStyles.WS_CAPTION);
}
else
{
SetWindowStyle(Child.Handle, GetWindowStyle(Child.Handle) & ~WindowStyles.WS_CAPTION);
}
}
}
}
protected override HandleRef BuildWindowCore(HandleRef hwndParent)
{
CheckChildValidity();
HandleRef childHwnd = new HandleRef(Child, child.Handle);
SetWindowStyle(childHwnd.Handle, WindowStyles.WS_CHILD | GetWindowStyle(childHwnd.Handle));
WindowsFormsHost.EnableWindowsFormsInterop();
System.Windows.Forms.Application.EnableVisualStyles();
SetParent(childHwnd.Handle, hwndParent.Handle);
return childHwnd;
}
}
And:
<Window x:Class="WinFormsHost"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:cc="clr-namespace:XTime.Shell.WinformsHost"
Title="Hosting Form In WPF">
<cc:WinFormsHost ShowCaption="False">
<wf:Form/>
</cc:WinFormsHost>
</Window>
<cc:WinFormsHost ShowCaption="False">
<wf:Form/>
</cc:WinFormsHost>
Your XAML embeds a System.Windows.Forms.Form object inside the WinFormsHost. Which is what you got, just a blank form with no child controls embedded inside it. It looks like you made an attempt at creating your own in the WinFormsHost constructor, assigning the Child property, but your XAML is overriding it so you are just left with a blank form again.
I put a ChildForm class inside the same namespace:
using System.Windows.Forms;
using System.Drawing;
...
public class ChildForm : System.Windows.Forms.Form {
public ChildForm() {
this.BackColor = Color.FromKnownColor(KnownColor.Window);
var lbl = new Label { Text = "Hello world" };
this.Controls.Add(lbl);
}
}
And updated the XAML to:
<cc:WinFormsHost ShowCaption="False">
<cc:ChildForm/>
</cc:WinFormsHost>
To get:
Set the FormBorderStyle to None to get rid of the border. Etcetera.
Setting the form's TopLevel property to false and Visible property to true is the much simpler way to turn a Form into a child control btw. I left it this way since you hinted you might want to give a Delphi window the same treatment. In which case you might want to go back to your original approach again, creating the child in the form class constructor and just omitting the content assignment in the XAML.
I have my Control.When I change the properties of the control. I get this:
this.myLabel1.BorderShadow = true;
this.myLabel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
And I need to get this:
this.myLabel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.myLabel1.BorderShadow = true;
How to do so is done automatically in Form.Desinger.cs ?
If you say Why?
private bool BorderShadow_ = false;
public bool BorderShadow
{
get
{
return BorderShadow_;
}
set
{
if (Border_Style_ == BorderStyle.FixedSingle)
{
BorderShadow_ = value;
}
else
{
throw new ArgumentOutOfRangeException("BorderShadow", "BorderShadow can be true if BorderStyle=FixedSingle");
}
}
}
You could look into the ISupportInitialize interface. It allows you to skip the validity check when initializing your controls. For example (adapted from one of my projects):
public class MyControl : Control, ISupportInitialize
{
private bool _created = true;
public void BeginInit()
{
_created = false;
}
public void EndInit()
{
_created = true;
//check all your properties here too
}
private bool BorderShadow_ = false;
public bool BorderShadow
{
get
{
return BorderShadow_;
}
set
{
BorderShadow_ = value;
if (_created && Border_Style_ != BorderStyle.FixedSingle)
throw new ArgumentOutOfRangeException();
}
}
}
If I remember correctly, the VS designer will automatically add calls to BeginInit and EndInit for you as well.
I think they will appears in the same order as they are defined. So you can override with new old setting:
public new BorderStyle BorderStyle
{
get {return base.BorderStyle;}
set {base.BorderStyle = value;}
}
and then declare your BorderShadow setting.
The designer will always order the properties alphabetically and this can't be changed.
The sense of a property is that it is side effect free and that it can be changed at any time in any order. This means that if you have multiple properties which representing some kind of complex state and not all combinations are making sense, this error should not be reported while switching the property itself.
So to accomplish these problems you have two possibilities:
Like #Andrew already mentioned implement ISupportInitialize and take care if you are within this state.
Within the property setter call a method that checks if all settings currently made are making sense and perform the desired action only in this case:
public class MyControl : Control
{
private bool _BorderShadow;
private BorderStyle _BorderStyle;
public bool BorderShadow
{
get { return _BorderShadow; }
set
{
if(_BorderShadow != value)
{
_BordeShadow = value;
ApplyBorderShadowIfNeeded();
}
}
}
public BorderStyle BorderStyle
{
get { return _BorderStyle; }
set
{
if(_BorderStyle != value)
{
_BorderStyle = value;
ApplyBorderShadowIfNeeded();
}
}
}
private void ApplyBorderShadowIfNeeded()
{
if(_BorderStyle == BorderStyle.FixedSingle
&& _BorderShadow)
{
// ToDo: Apply the shadow to the border.
}
}
}
Lets say we have 0 displayed in value field of the control and I want that if the value is 0 - display string.Empty (I know that the type of value is decimal and there can be no string inserted instead of decimals in it, but still... Maybe there is some formatting possible there?).
Note: This is dependent on the current implementation of NumericUpDown.
What you need to do is create a new control that inherits from NumericUpDown such that:
public partial class SpecialNumericUpDown : NumericUpDown
{
public SpecialNumericUpDown()
{
InitializeComponent();
}
protected override void UpdateEditText()
{
if (this.Value != 0)
{
base.UpdateEditText();
}
else
{
base.Controls[1].Text = "";
}
}
}
public partial class MyNumericUpDown : NumericUpDown
{
public override string Text
{
get
{
if (base.Text.Length == 0)
{
return "0";
}
else
{
return base.Text;
}
}
set
{
if (value.Equals("0"))
{
base.Text = "";
}
else
{
base.Text = value;
}
}
}
}
It seems that there is only very limited support for changing the formatting.
I have not tried this myself. But you could create a subclass and override the UpdateEditText method to support your custom format. Something like this:
protected override void UpdateEditText()
{
this.Text = Value.ToString(); // Insert your formatting here
}
An easier solution is calling the ResetText() method. You can restore the text changing the Value property.
Example code to hide text when NumericUpDown control is disabled, and restore it on enabled
private void NumericUpDown_EnabledChanged(object sender, EventArgs e)
{
if (numericUpDown.Enabled)
{
if (numericUpDown.Tag != null)
{
// Restore last value
numericUpDown.Value = (decimal)numericUpDown.Tag;
}
}
else
{
// Save last value
numericUpDown.Tag = numericUpDown.Value;
// Just to force value change
numericUpDown.Value = (numericUpDown.Value > numericUpDown.Minimum ? numericUpDown.Minimum : numericUpDown.Maximum);
// Clear text
numericUpDown.ResetText();
}
}
If you only want to hide the value from the user, you can make ForeColor the same as BackColor so the value inside NumericUpDown will be invisible to the user.