簡體   English   中英

如何為WinForm C#設置文本框MaximumSize和MinimumSize應用程序的寬度

[英]How to set textbox MaximumSize and MinimumSize application wide for WinForm C#

我是Winform C#的新手。

我想將TextBox或TextEdit屬性的“最大大小”和“最小大小寬度”應用程序設置為寬。 我該如何設置?

創建一個從TextBox繼承的class ,將您的設置實現到構造函數中,如下所示:

public class MyTextBox : TextBox {
    public MyTextBox() : base {
        //Set your properties as you like
    }
}

正如@Hans所說,您可以擴展現有的就地編輯器,然后可以在任何應用程序中輕松使用這些自定義控件。

只是您需要創建一個單獨的項目並創建繼承現有DevExpres控件的類。 根據您的要求,可以使用繼承到某些TextEditEx:TextEditTextEdit類。

來自: 自定義編輯器
考慮這個用於創建自定義編輯器(CustomEdit)的簡單示例。 它代表TextEdit類的后代。 新的存儲庫項目類是RepositoryItemCustomEdit。 出於說明目的,它引入了一個新屬性(UseDefaultMode)。

using System.Drawing;
using System.Reflection;
using System.ComponentModel;
using System.Windows.Forms;
using System.Reflection;
using DevExpress.XtraEditors;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraEditors.Registrator;
using DevExpress.XtraEditors.Drawing;
using DevExpress.XtraEditors.ViewInfo;
using DevExpress.Accessibility;

namespace DevExpress.CustomEditors {

    //The attribute that points to the registration method 
    [UserRepositoryItem("RegisterCustomEdit")]
    public class RepositoryItemCustomEdit : RepositoryItemTextEdit {

        //The static constructor that calls the registration method 
        static RepositoryItemCustomEdit() { RegisterCustomEdit(); }

        //Initialize new properties 
        public RepositoryItemCustomEdit() {            
            useDefaultMode = true;
        }

        //The unique name for the custom editor 
        public const string CustomEditName = "CustomEdit";

        //Return the unique name 
        public override string EditorTypeName { get { return CustomEditName; } }

        //Register the editor 
        public static void RegisterCustomEdit() {
            //Icon representing the editor within a container editor's Designer 
            Image img = null;
            try {
                img = (Bitmap)Bitmap.FromStream(Assembly.GetExecutingAssembly().
                  GetManifestResourceStream("DevExpress.CustomEditors.CustomEdit.bmp"));
            }
            catch {
            }
            EditorRegistrationInfo.Default.Editors.Add(new EditorClassInfo(CustomEditName, 
              typeof(CustomEdit), typeof(RepositoryItemCustomEdit), 
              typeof(TextEditViewInfo), new TextEditPainter(), true, img, typeof(TextEditAccessible)));
        }

        //A custom property 
        private bool useDefaultMode;

        public bool UseDefaultMode {
            get { return useDefaultMode; }
            set {
                if(useDefaultMode != value) {
                    useDefaultMode = value;                        
                    OnPropertiesChanged();
                }
            }
        }

        //Override the Assign method 
        public override void Assign(RepositoryItem item) {
            BeginUpdate(); 
            try {
                base.Assign(item);
                RepositoryItemCustomEdit source = item as RepositoryItemCustomEdit;
                if(source == null) return;
                useDefaultMode = source.UseDefaultMode;
            }
            finally {
                EndUpdate();
            }
        }
    }

    [ToolboxItem(true)]
    public class CustomEdit : TextEdit {

        //The static constructor that calls the registration method 
        static CustomEdit() { RepositoryItemCustomEdit.RegisterCustomEdit(); }

        //Initialize the new instance 
        public CustomEdit() {
            //... 
        }

        //Return the unique name 
        public override string EditorTypeName { get { return 
            RepositoryItemCustomEdit.CustomEditName; } }

        //Override the Properties property 
        //Simply type-cast the object to the custom repository item type 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
        public new RepositoryItemCustomEdit Properties {
            get { return base.Properties as RepositoryItemCustomEdit; }
        }

    }
}

請參閱以下DevExpress文檔鏈接以獲取詳細信息:
如何擴展DevExpress控件
如何在DevExpress容器中的就地模式下使用自定義控件
繼承的TextEdit-在構造器中進行設置后,“ Properties-Properties”是固定的
如何創建透明的TextEdit

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM