why in desin time do not work set "dataGrid" Property in my code.but property font,with working correct.
I used of this componet on the a Form.(i test it with design time debuging)
enter code here
namespace Example
{
public partial class Component1 : System.Windows.Forms.DataGridView
{
public Component1()
{
}
private DataGridViewColumn _gridcolumn;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
[TypeConverter(typeof ( ExpandableObjectConverter))]
public DataGridViewColumn gridcolumn
{
get
{
if (_gridcolumn == null)
_gridcolumn = new DataGridViewColumn();
return _gridcolumn;
}
set //Do not Work Set in designTime
{
_gridcolumn = value;
}
}
private System.Drawing.Font _MyFont;
public System.Drawing.Font MyFont
{
get
{
if (_MyFont == null)
_MyFont = new System.Drawing.Font("tahoma", 8);
return _MyFont;
}
set
{
_MyFont = value; //Work correctly in design time
}
}
int _with;
public int withcustom
{
get
{
return _with;
}
set
{
_with = value; //Work correctly in design time
}
}
}
}
Short Version
Either remove DesignerSerializationVisibility(DesignerSerializationVisibility.Content) from your property, or change it to DesignerSerializationVisibility(DesignerSerializationVisibility.Visible).
Long Version
Applying DesignerSerializationVisibility(DesignerSerializationVisibility.Content) to your property means that the WinForms designer will not generate code that sets the value of the property, but rather code that sets the value of the properties on the value of that property.
For example, say I have these two types:
public class MyControl : Control
{
public class MyControlProperties
{
public string Prop1 { get; set; }
public int Prop2 { get; set; }
}
private MyControlProperties props;
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public MyControlProperties Properties
{
get
{
if(props == null) props = new MyControlProperties();
return props;
}
}
}
When the designer generates the code for MyControl on a form, it will look something like this:
myControl.Properties.Prop1 = "foo";
myControl.Properties.Prop2 = 10;
So, instead of setting the value of the Properties property (which, in this code, is read-only; though it doesn't have to be, properties like this usually are), it's setting the values of the properties on the value of that property.
This is good example:
public partial class SCon : UserControl
{
public SCon()
{
InitializeComponent();
if (Persoanas == null)
{
Persoanas = new List<Persoana>();
}
}
[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public List<Persoan> Persoanas { get; set; }
}
[Serializable]
public class Persoan
{
public int Id { get; set; }
public String Name { get; set; }
}
Related
I have created a composite user control containing a label, a checkbox, and a button. I have set certain properties to be set in the form designer.
public partial class ctlBoundCheckButton : UserControl
{
[Browsable(true)]
public string _chkText { get; set; }
public string _btnText { get; set; }
public string _lblText { get; set; }
public ctlBoundCheckButton()
{
InitializeComponent();
checkBox1.Text = _chkText; // _chkText is null :(
button1.Text = _btnText;
label1.Text = _lblText;
}
When I drop this user control onto a form, the custom properties show up fine in the form designer, and I can assign values to them:
But the properties are null at run time. As you can see in the control's constructor, the values remain null. What is the proper way to set up custom properties in composite custom controls?
As others suggested, you can try to set the properties in the get set method.
Here is a code example you can refer to.
public partial class ctlBoundCheckButton : UserControl
{
[Browsable(true)]
public string _chkText
{
get { return checkBox1.Text; }
set { checkBox1.Text=value; }
}
public string _btnText
{
get { return button1.Text; }
set { button1.Text = value; }
}
public string _lblText
{
get { return label1.Text; }
set { label1.Text = value; }
}
public ctlBoundCheckButton()
{
InitializeComponent();
}
}
Result:
I have a ListView with an List<List<enum>> property and i have a View that shows each bool as button.
I want to cycle through the bound enum when someone clicks on the button. The problem is I cant use a normal click because it would be outside of my ViewModel.
Edit:
I have the class TruthTable that has 2 DynTable:
public sealed class Column<T>
{
public Column()
{
ColumnData = new List<T>();
ColumnHeader = "";
}
...
public List<T> ColumnData { get; set; }
public string ColumnHeader { get; set; }
}
public sealed class DynTable<T>
{
public DynTable()
{
Columns = new List<Column<T>>();
}
...
public List<Column<T>> Columns { get; set; }
}
public sealed class TruthTable
{
public TruthTable()
{
input = new DynTable<bool>();
results = new DynTable<BoolState>();
}
...
private DynTable<bool> input;
private DynTable<BoolState> results;
public DynTable<bool> Input { get { return input; } set { input = value; } }
public DynTable<BoolState> Results { get { return results; }}
}
public enum BoolState
{
False = 0,
True = 1,
DontCare = 2
}
And i have a ViewModel for the TruthTable. I dont think that the I have to show the code for the ViewModel because its just a TruthTable property. I hope thats enough code to understand my problem ._.
I have a user control and their are many textbox on it. I add this user control to a different project and I can use it, when I write every property on UserControl. I want to set textbox fields of this user control with using a class. These are my codes:
Class:
namespace IEUserControl
{
public class IEValue
{
public string IsEmriNo { get; set; }
public string Nevi { get; set; }
public string BrutKg { get; set; }
public string NetKg { get; set; }
}
}
User Control:
namespace IsEmriUserControl
{
public partial class UC_IsEmri : UserControl
{
public UC_IsEmri()
{
InitializeComponent();
}
//private IsEmriValue _isEmri;
//public IsEmriValue isEmri
//{
// get
// {
// return _isEmri;
// }
// set
// {
// _isEmri = value;
// }
//}
public string IsEmriNo
{
get { return txtIsEmriNo.Text; }
set { txtIsEmriNo.Text = value; }
}
public string Nevi
{
get { return txtNevi.Text; }
set { txtNevi.Text = value; }
}
public string BrutKg
{
get { return txtBrutKg.Text; }
set { txtBrutKg.Text = value; }
}
public string NetKg
{
get { return txtNetKg.Text; }
set { txtNetKg.Text = value; }
}
}
}
When I use properties, I can set textbox values. However I want to set my textbox values with my Class. Can anyone give me an example setting textbox values with using class? Thank you.
Make a method/property like this
public IEValue IE_Value
{
get
{
return new IEValue() {
IsEmrino = txtIsEmriNo.Text,
Nevi = txtNevi.Text,
BrutKg = txtBrutKg.Text,
NetKg = txtNetKg.Text
};
}
set
{
txtIsEmriNo.Text = value.IsEmrino;
txtNevi.Text = value.Nevi;
txtBrutKg.Text = value.BrutKg;
txtNetKg.Text = value.NetKg;
}
}
I am new to C# and am working on classes and understanding them. My problem is I am not understanding how to create a Get to retrieve the private variable _yourname and Set to set the private variable _yourname.
namespace WindowsFormsApplication1
{
class InputClass
{
private string _yourName;
public string _banner;
public virtual void GetInfo();
public InputClass(String _banner)
{
_banner = "Enter your name";
}
}
}
Maybe I am using the wrong function to GetInfo. But I am also wondering when I have the GetInfo if in the () I should write _yourname in it.
In C# there are properties, which have the function of public getter and setter methods in other languages:
class InputClass
{
private string _yourName;
public string _banner;
public InputClass(String _banner)
{
this._banner = _banner;
}
public string YourName
{
get { return _yourName; }
set { _yourName = value; }
}
}
But you can use auto properties, if you want:
class InputClass
{
public InputClass(String _banner)
{
Banner = _banner;
}
public string YourName
{
get; set;
}
public string Banner
{
get; set;
}
}
It sounds like you are trying to provide access to the _yourName field. If so then just use a property
class InputClass {
public string YourName {
get { return _yourName; }
set { _yourName = value; }
}
...
}
Now consumers of InputClass can access it as if it were a read only field.
InputClass ic = ...;
string yourName = ic.YourName;
ic.YourName = "hello";
Note: C# provides a special syntax for simple properties like this which are just meant to be wrappers over private fields. It's named auto-implemented properties
class InputClass {
public string YourName { get; set; }
}
You can override getters and settings using the get and set keywords. For example:
class InputClass
{
private string _yourName;
private string _banner;
public YourName
{
get { return _yourName; }
set { _yourName = value; }
}
public Banner
{
get { return _banner; }
set { _banner = value; }
}
public InputClass(String banner)
{
_banner = banner;
}
}
1.) Use properties instead of members, you get a free accessor (get) and mutator (set).
public string YourName { get; set; }
public string Banner { get; set; }
2.) You can take advantage of the default constructor, and declare it on the fly.
//the old way:
InputClass myClass = new InputClass();
myClass.YourName = "Bob";
myClass.Banner = "Test Banner";
//on the fly:
InputClass myClass = new InputClass()
{
YourName = "Bob",
Banner = "Test Banner"
}
I have the class PGMain as the SelectedObject in the propertygrid:
[DefaultPropertyAttribute("Basic")]
[Serializable]
public class PGMain
{
private TestClass m_TEST = new TestClass();
[CategoryAttribute("TEST")]
public TestClass TEST
{
get { return m_TEST; }
set { m_TEST = value; }
}
// More members are here
}
Now I would like to expand the members of the TestClass in the PropertyGrid. So I tried the following:
[Serializable]
[DescriptionAttribute("Expand to see the options for the application.")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class TestClass : ExpandableObjectConverter
{
[CategoryAttribute("test-cat"), DescriptionAttribute("desc")]
public string Name = "";
[CategoryAttribute("test-cat"), DescriptionAttribute("desc")]
public object Value = null;
[CategoryAttribute("test-cat"), DescriptionAttribute("desc")]
public bool Include = true;
public override bool CanConvertTo(ITypeDescriptorContext context, System.Type destinationType)
{
if (destinationType == typeof(TestClass))
return true;
return base.CanConvertTo(context, destinationType);
}
}
The result is that there is an expandable-icon in front of the TestClass in the propertygrid but it can not be expanded. What am I missing?
Just to be clear: I can show expandable members of the PGMain class but NOT expandable members of the members of the PGMain class like the Test-member in PGMain.
Edit:
No I have 2 classes NOT 1.
[DefaultPropertyAttribute("Basic")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class fooA
{
private fooB m_TestMember = new fooB();
[Browsable(true)]
[CategoryAttribute("Test category"), DescriptionAttribute("desctiption here")] // <<<<< this one works.
[TypeConverter(typeof(fooB))]
public fooB TestMember
{
get { return m_TestMember; }
set { m_TestMember = value; }
}
}
[DefaultPropertyAttribute("Basic")]
[TypeConverter(typeof(ExpandableObjectConverter))]
public class fooB
{
private string m_ShowThisMemberInGrid = "it works"; // <<<<< this doesn NOT work
[CategoryAttribute("Tile"), DescriptionAttribute("desctiption here")]
public string ShowThisMemberInGrid
{
get { return m_ShowThisMemberInGrid; }
set { m_ShowThisMemberInGrid = value; }
}
public override string ToString()
{
return "foo B";
}
}
But I did solve the problem (by coincidence). It appears that public variables are not listed in the propertygrid. It HAVE to be properties with getters and setters. That was the solution. So the above snippet solved the problem. Thanks for your replies anyway :).
Wrong:
[CategoryAttribute("Tile"), DescriptionAttribute("desctiption here")]
public string Name = "";
Good:
private string m_Name = new string();
[CategoryAttribute("Tile"), DescriptionAttribute("desctiption here")]
public string Name
{
get { return m_Name; }
set { m_Name = value; }
}
Sorry I misinterpret the question.
You can find more details on these links
http://msdn.microsoft.com/en-us/library/aa302326.aspx#usingpropgrid_topic6a
http://www.codeproject.com/KB/miscctrl/bending_property.aspx
http://msdn.microsoft.com/en-us/library/aa302334.aspx
Hope it helps :)
UPDATE:
I copied the code from here
And modified like this.
public class SamplePerson
{
public string Name
{
get;
set;
}
public Person Person
{
get;
set;
}
}
And in the form I have done something like
SamplePerson h = new SamplePerson();
h.Person = new Person
{
Age = 20,
FirstName = "f",
LastName = "l"
};
this.propertyGrid1.SelectedObject = h;
And its working for me.
Provide Browsable as false for properties you don't want to display in the property grid.
[Browsable(false)]
public bool Include
{
get;set;
}