Laden...

Problem bei eigener TextBox mit Rahmen

Erstellt von baer999 vor 15 Jahren Letzter Beitrag vor 15 Jahren 1.177 Views
B
baer999 Themenstarter:in
375 Beiträge seit 2007
vor 15 Jahren
Problem bei eigener TextBox mit Rahmen

Hallo,

ich habe meine eigene TextBox erstellt, aus einer UserForm mit Textbox + den Rest der UserForm als Rand. Die Hintergrundfarben sollen sich verändern, wenn Enabled ode ReadOnly verändert wurden. Zusätzlich wird im PaintEvent ein Rahmen gezeichnet. Allerdings habei ch das Problem, dass er die BackColor der UserForm nie verändert, sodass eine weiße Textbox auf grauer UserForm mit schwarzen Rand liegt. Hier der Ccde, hoffe jemand kann mir helfen und sagen was ich falsch mache... thx

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace ADLib.Controls
{
    public partial class ADTextBox : UserControl
    {
        #region " Variables "

        #region " Border "

        //Private HasBorder Variable
        private bool _HasBorder;
        //Öffentliche HasBorder Variable
        public bool HasBorder
        { get { return _HasBorder; } set { _HasBorder = value; } }

        //Private HasBorder Variable
        private Color _BorderColor;
        //Öffentliche HasBorder Variable
        public Color BorderColor
        { get { return _BorderColor; } set { _BorderColor = value; } }

        #endregion
        
        private bool Loaded = false;
        private bool Updating = false;
        
		public override Color BackColor {
			get { return base.BackColor; }
			set { 				
				if (Loaded) 
				{
					if (!Updating) Set_BackColor();
				}
			}
		}

        public bool ReadOnly
        {
            get { return textBox.ReadOnly; }
            set { textBox.ReadOnly = value; 
            	if (Loaded)
            	{
            		if (!Updating) Set_BackColor();
            	}
            }
        }

        [EditorBrowsableAttribute()]
        public override String Text
        {
            get { return textBox.Text; }
            set { textBox.Text = value; }
        }

        #endregion

        #region " Constructor "

        public ADTextBox()
        {
            InitializeComponent ();
            
            this.SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw, true);
            
            this.Enabled = !this.Enabled;
            this.Enabled = !this.Enabled;

            textBox.Anchor = (AnchorStyles.Top | AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom);
        }

        #endregion

        #region " Events "
        
		[EditorBrowsableAttribute()]
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);
			
			Loaded = true;
			Set_BackColor();
		}

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);

            //Draw Border
            if (this._HasBorder) e.Graphics.DrawRectangle (new Pen (this._BorderColor), new Rectangle (0, 0, this.Width - 1, this.Height - 1));
        }
        
		[EditorBrowsableAttribute()]
		protected override void OnEnabledChanged(EventArgs e)
		{
			base.OnEnabledChanged(e);
			
			Set_BackColor();
		}
        
        private void Set_BackColor()
		{
            Color bgColor = (!textBox.ReadOnly && Enabled) ? Color.White : SystemColors.Control;

            Updating = true;
            
            BackColor = bgColor;
            textBox.BackColor = bgColor;
			this.Invalidate();
			
			Updating = false;
		}  
		
        #endregion
    }
}

T
177 Beiträge seit 2007
vor 15 Jahren

Hallo baer999

Hast du diesen Code schonmal kompiliert?

Denn ich versteh nicht, wie du zum Beispiel hier:

  
        public bool ReadOnly  
        {  
            get { return textBox.ReadOnly; }  
            set { textBox.ReadOnly = value;  
                if (Loaded)  
                {  
                    if (!Updating) Set_BackColor();  
                }  
            }  
        }  
  

Auf die Variable "textBox" zugreifen kannst, die ist nirgendas instanziert oder übergeben.
Meinst du nicht da gehört "this" hin?

Mit freundlichen Grüssen
Tobias

J
130 Beiträge seit 2008
vor 15 Jahren

Auf die Variable "textBox" zugreifen kannst, die ist nirgendas instanziert oder übergeben.
Meinst du nicht da gehört "this" hin?

Mit freundlichen Grüssen
Tobias

jop, da fehlt doch was ?!

**“DOH !” -Homer Simpson**
M
334 Beiträge seit 2007
vor 15 Jahren

public partial class ADTextBox : UserControl

Das passt schon so, der restliche Code ist in der Designer.cs-Datei (vermute ich mal)

B
baer999 Themenstarter:in
375 Beiträge seit 2007
vor 15 Jahren

Die textBox ist die TextBox, die auf der UserForm definiert ist. Befindet sich im Designer...

namespace ADLib.Controls
{
    partial class ADTextBox
    {
        /// <summary> 
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary> 
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose ();
            }
            base.Dispose (disposing);
        }

        #region Component Designer generated code

        /// <summary> 
        /// Required method for Designer support - do not modify 
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.textBox = new System.Windows.Forms.TextBox ();
            this.SuspendLayout ();
            // 
            // textBox
            // 
            this.textBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox.BorderStyle = System.Windows.Forms.BorderStyle.None;
            this.textBox.Location = new System.Drawing.Point (2, 3);
            this.textBox.Name = "textBox";
            this.textBox.Size = new System.Drawing.Size (119, 13);
            this.textBox.TabIndex = 0;
            // 
            // usrTextBox
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF (6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.White;
            this.Controls.Add (this.textBox);
            this.MaximumSize = new System.Drawing.Size (1024, 19);
            this.MinimumSize = new System.Drawing.Size (20, 19);
            this.Name = "usrTextBox";
            this.Size = new System.Drawing.Size (124, 19);
            this.ResumeLayout (false);
            this.PerformLayout ();

        }

        #endregion

        private System.Windows.Forms.TextBox textBox;
    }
}

T
210 Beiträge seit 2006
vor 15 Jahren

Du musst im Setter von BackColor auch base.BackColor setzen...