Solved

Windows forms; lock properties in a derivated object

Posted on 2010-09-17
5
517 Views
Last Modified: 2012-05-10
Hello
I work with VS2010.
In my application I hav a Main Form with the button "btn1". From this Mainform I have the deivatied form Form1.
I declerated the btn1 ad protected, becaus I want to modify it in Form1. How can I make some of the proporties of btn1 unchangable in the Form1? (f.ex. the position and the size shouldn't mustn't be changed in Form1)
0
Comment
Question by:ICSAutomation
  • 3
5 Comments
 
LVL 6

Expert Comment

by:ajb2222
ID: 33701911
instead of exposing btn1 - expose only the properties you want to be able to change.

protected string btn1text
{
get { return btn1.text; }
set { btn1.text = value; }
}
 
0
 

Author Comment

by:ICSAutomation
ID: 33702247
And how can i do this with the event handlers?

Actually I'm looking for a way, where I still can use the porperties in the Propertywindow of the VS. Some of the properties are editable and some are not...
Some event I can create and some not...

0
 
LVL 6

Accepted Solution

by:
ajb2222 earned 80 total points
ID: 33702434
to expose an event

public event EventHandler btn1Click
    {
        add { btn1.Click += value; }
        remove { btn1.Click -= value; }
    }
0
 
LVL 6

Expert Comment

by:ajb2222
ID: 33702683
You will see the properties and events on the form  but you can add a category and description to find them easily

[Category("Button1")]
[Description("Text to display on button 1")]
protected string btn1text ...

[Category("Button1")]
[Description("Event for when button 1 is clicked. ")]
public event EventHandler btn1Click...
0
 
LVL 16

Assisted Solution

by:kris_per
kris_per earned 20 total points
ID: 33704908

To make a property readonly in the Properties window:

[ReadOnly(true)]
public int Width
{
    get; set;
}

When this property is seen in the Properties window, it will be readonly. This is ok for our classes where we can add this ReadOnly attribute to a property. For the types which we dont have the code, we can set ReadOnly attribute at runtime as shown in the code below.

I used the below code once...just posting here...see if this is useful for you in some way.



// Make FindButton's Size property readonly in Properties window



PropertyDescriptor pd = TypeDescriptor.GetProperties(FindButton)["Size"];

AttributeCollection attributes = pd.Attributes;

ReadOnlyAttribute readOnlyAttribute = (ReadOnlyAttribute)attributes[typeof(ReadOnlyAttribute)];

FieldInfo isReadOnly = readOnlyAttribute.GetType().GetField("isReadOnly", BindingFlags.NonPublic | BindingFlags.Instance);

isReadOnly.SetValue(readOnlyAttribute, true);

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Windows Service to Receive TCP Packets 4 122
C# Error - Add Failed 12 78
Runtime Error 2 28
Finally almost got it 5 15
Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now