如何在自定义控件里自定义一个属性,可以在PropertyGird中像TabContrl控件中的TabPages属性一样可以弹出一个Form来维护这个属性?谁能给个简单的代码实例?

解决方案 »

  1.   

    WebUserControl.ascx:
    <%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:RadioButtonList ID="RadioButtonList1" runat="server">
    </asp:RadioButtonList>
    WebUserControl.ascx.cs
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;public partial class WebUserControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        string strValue;
        public string TextValue  //自定義一個屬性
        {
            set { TextValue = value; }
            get { return strValue; }
        }
    }
    在aspx中加載這個控件,選擇屬性就可以看到了.
      

  2.   

    楼上的仁兄能给个WinForm的代码示例?多谢。
      

  3.   

    要winForm的啊,應該差不多,我先試試,我習慣了用WebForm,哈哈....
      

  4.   

    在属性前加
    [Browsable(true)]
      

  5.   

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Forms;
    using System.ComponentModel;
    using System.Drawing;namespace CustomControlSample
    {
        public class FirstControl : Control
        {        public FirstControl()
            {        }        // ContentAlignment is an enumeration defined in the System.Drawing
            // namespace that specifies the alignment of content on a drawing 
            // surface.
            private ContentAlignment alignmentValue = ContentAlignment.MiddleLeft;        [
            Category("Alignment"),
            Description("Specifies the alignment of text.")
            ]
            public ContentAlignment TextAlignment
            {            get
                {
                    return alignmentValue;
                }
                set
                {
                    alignmentValue = value;                // The Invalidate method invokes the OnPaint method described 
                    // in step 3.
                    Invalidate();
                }
            }
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                StringFormat style = new StringFormat();
                style.Alignment = StringAlignment.Near;
                switch (alignmentValue)
                {
                    case ContentAlignment.MiddleLeft:
                        style.Alignment = StringAlignment.Near;
                        break;
                    case ContentAlignment.MiddleRight:
                        style.Alignment = StringAlignment.Far;
                        break;
                    case ContentAlignment.MiddleCenter:
                        style.Alignment = StringAlignment.Center;
                        break;
                }            // Call the DrawString method of the System.Drawing class to write   
                // text. Text and ClientRectangle are properties inherited from
                // Control.
                e.Graphics.DrawString(
                    Text,
                    Font,
                    new SolidBrush(ForeColor),
                    ClientRectangle, style);        }
        }
    }
      

  6.   

    达到lz的要求所需步骤比较多,建议在msdn上search ControlDesigner  和 ControlBuilder 类的用法
      

  7.   

    cooolchen,我想要得效果是弹出的界面是自己写的,不是用现有的替换。
      

  8.   

    hdt(倦怠) 能否简要说下?
      

  9.   

    把父类换掉,继承和tabpages同样的父类,把属性方法重写即可我在WEB中遇到过同样的问题
      

  10.   

    honkerhero(孤独的流浪) 都已经重写过了,还是不行。