Sign in to follow this  

[.net] OnPaint problem!

This topic is 3861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi! Im creating a new class, inherited from TextBox. I have set the USerPaint to true, and overriden the OnPaint method. It looks like this (i just want a custom colour border): [CODE] protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); Pen pen = new Pen(new SolidBrush(Color.FromArgb(107, 142, 156))); e.Graphics.DrawRectangle(pen, 0, 0, Width - 1, Height - 1); } [/CODE] The border appears, everything is fine, until i sart using the control: -I focus on the control OK -Start typing: the text appears in black, bolded (i set the font to be "107;142;156, arial, regular" everywhere in my entire solution) -Leave the textbox: the text disappears -Re-enter the TextBox: text does not appear -Select some text in the box with mouse: the selected part displays highlighted Its weird! What am i doing wrong?

Share this post


Link to post
Share on other sites
Inherited painting behavior is going to happen in base.Paint so under normal, conditions, your rectangle is covering up any text in the box. Apparently, handling is different when the text box is accepting input. But in general you probably want to move base.Paint to the end of your Paint.

edit - actually, maybe that's not it. If you want control over your border color, might be easiest to make a CustomControl. Put a regular TextBox into that, set it to no border at all and size is to its always one pixel smaller than the control. Then you can map a "BorderColor" property to the BackColor of the control or something like that. It would take less time to do than it took me to write this edit ;)

[Edited by - dalep on May 17, 2007 3:27:56 PM]

Share this post


Link to post
Share on other sites
It would be good to make it work. There will be other modifications later, this border stuff is only the beginning :( damn

MSDN says TextBox is always renderd by Windows and can not be altered normally. To create a custom control is quite problematic, because i cant disable its own border which is not in the desired color. If i turn off border, the textbox shrinks, and its size can not be altered, and also displays weird :S

Share this post


Link to post
Share on other sites
Wait, I think I see what you need. I'll post some code in a moment. ...

I have no time to format it properly to make it easier so I'll just cut and paste from a project and maybe you'll be able to extract the info you need.

If it doesn't work because it needs data from other places (colors from configuration or something), try replacing with data that makes sense (any other color).

I hope you can make sense of this mess (it's a base custom control class and a labelled textbox):

[CODE]
public class ControlBase : System.Windows.Forms.UserControl
{
protected System.Drawing.Color ColorError = System.Drawing.SystemColors.Info;
protected System.Drawing.Color ColorWin = System.Drawing.SystemColors.Window;

private string text;

[EditorBrowsable(EditorBrowsableState.Always), Browsable(true),
DesignerSerializationVisibility(DesignerSerializationVisibility.Visible),
Bindable(true)]
public override string Text
{
get { return text; }
set { text = value; }
}

protected string labelText;
public string LabelText
{
get
{
return this.labelText;
}
set
{
this.labelText = value;
this.Invalidate();
}
}

private Control mainControl;
protected Control MainControl
{
get
{
if (mainControl == null)
mainControl = this;
return mainControl;
}
set { mainControl = value; }
}

protected int controlTop = 14;

protected SizeF textSize = SizeF.Empty;
[DefaultValue(50)]
private int labelWidth;
public int LabelWidth
{
get
{
return this.labelWidth;
}
set
{
this.labelWidth = value;
this.Invalidate();
}
}

[DefaultValue(false)]
private bool multiLineLabel;
public bool MultiLineLabel
{
get
{
return this.multiLineLabel;
}
set
{
this.multiLineLabel = value;
this.Invalidate();
}
}



private void InitializeComponent()
{
this.SuspendLayout();
//
// ControlBase
//
this.Name = "ControlBase";
this.Size = new System.Drawing.Size(192, 88);
this.ResumeLayout(false);
}

protected Color ControlColor0
{
get
{
return LibFormstyle.ColorOk;
}
}

protected Color ControlColor1
{
get
{
return LibFormstyle.ColorLight;
}
}

protected Color ControlColor1Grey
{
get
{
return LibFormstyle.ColorLightGrey;
}
}

protected Color ControlColor2
{
get
{
return LibFormstyle.ColorFont;
}
}

protected void PaintCurveLabel(bool forceFocus, PaintEventArgs e)
{
try
{
Graphics g = e.Graphics;

if (multiLineLabel)
{
if (labelWidth > 0)
textSize = e.Graphics.MeasureString(labelText, this.Font, labelWidth);
else
textSize = e.Graphics.MeasureString(labelText, this.Font, this.Width);
}
else
{
textSize = e.Graphics.MeasureString(labelText, this.Font);
}

//Borde
GraphicsPath gp = new GraphicsPath();
gp.AddPolygon(new Point[]{
new Point(0, 0),
new Point((int)textSize.Width + 10, 0),
new Point((int)textSize.Width, (int)textSize.Height),
new Point(0, (int)textSize.Height)});
LinearGradientBrush lgb = new LinearGradientBrush(
new Point(0, 0),
new Point((int)textSize.Width + 10, (int)textSize.Height),
this.MainControl.Focused || forceFocus ?
this.ControlColor0 :
this.MainControl.Enabled?
this.ControlColor1 :
this.ControlColor1Grey,
this.BackColor);
g.FillPath(lgb, gp);

g.DrawString(LabelText, this.Font, new SolidBrush(LibFormstyle.ColorFont), new RectangleF(new PointF(0.0f, 0.0f), textSize));
g.Dispose();
this.controlTop = (int)(textSize.Height);
}
catch (Exception ex)
{
this.Tag = ex;
}
}
}


//-------------------------------------------------------------------------

public class LabelledTextUp : ControlBase
{
private IContainer components;

#region Código generado por el Diseñador de componentes
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.txtTextBox = new System.Windows.Forms.TextBox();
this.chkCheck = new System.Windows.Forms.CheckBox();
this.SuspendLayout();
//
// txtTextBox
//
this.txtTextBox.Anchor = ((System.Windows.Forms.Anchorstyles)((((System.Windows.Forms.Anchorstyles.Top | System.Windows.Forms.Anchorstyles.Bottom)
| System.Windows.Forms.Anchorstyles.Left)
| System.Windows.Forms.Anchorstyles.Right)));
this.txtTextBox.Borderstyle = System.Windows.Forms.Borderstyle.None;
this.txtTextBox.Font = new System.Drawing.Font("Microsoft Sans Serif", 8.25F, System.Drawing.Fontstyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.txtTextBox.Location = new System.Drawing.Point(16, 14);
this.txtTextBox.Name = "txtTextBox";
this.txtTextBox.Size = new System.Drawing.Size(288, 13);
this.txtTextBox.TabIndex = 0;
this.txtTextBox.Enter += new System.EventHandler(this.txtTextBox_Enter);
this.txtTextBox.Leave += new System.EventHandler(this.txtTextBox_Enter);
this.txtTextBox.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtTextBox_KeyPress);
this.txtTextBox.TextChanged += new System.EventHandler(this.txtTextBox_TextChanged);
//
// chkCheck
//
this.chkCheck.AutoSize = true;
this.chkCheck.Location = new System.Drawing.Point(1, 14);
this.chkCheck.Name = "chkCheck";
this.chkCheck.Size = new System.Drawing.Size(15, 14);
this.chkCheck.TabIndex = 1;
this.chkCheck.UseVisualstyleBackColor = true;
this.chkCheck.Visible = false;
this.chkCheck.CheckedChanged += new System.EventHandler(this.chkCheck_CheckedChanged);

//
// LabelledTextUp
//
this.BackColor = System.Drawing.Color.White;
this.Borderstyle = System.Windows.Forms.Borderstyle.FixedSingle;
this.Controls.Add(this.chkCheck);
this.Controls.Add(this.txtTextBox);
this.Name = "LabelledTextUp";
this.Size = new System.Drawing.Size(308, 28);
this.Leave += new System.EventHandler(this.LabelTextUp_Leave);
this.EnabledChanged += new System.EventHandler(this.LabelledTextUp_EnabledChanged);
this.ResumeLayout(false);
this.PerformLayout();

}
#endregion

#region Diseñador

private System.Windows.Forms.TextBox txtTextBox;
#endregion

#region Variables propias

private string backupText;

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
public override string Text
{
get
{
return this.txtTextBox.Text;
}
set
{
this.txtTextBox.Text = value;
this.backupText = value;
}
}

public bool Multiline
{
get
{
return this.txtTextBox.Multiline;
}
set
{
this.txtTextBox.Multiline = value;
}

}

private bool allowWriting = true;
private CheckBox chkCheck;
private ToolTip ttHelp;

public bool PasswordField
{
get
{
return this.txtTextBox.PasswordChar == '*';
}
set
{
if (value)
this.txtTextBox.PasswordChar = '*';
else
this.txtTextBox.PasswordChar = (char)0;
}
}
public int MaxLength
{
get
{
return this.txtTextBox.MaxLength;
}
set
{
this.txtTextBox.MaxLength = value;
}
}
public bool EmptyText
{
get
{
return this.txtTextBox.Text.Trim().Length == 0;
}
}
public int TextMaxLen
{
set { this.txtTextBox.MaxLength = value; }
get { return this.txtTextBox.MaxLength; }
}
public bool ReadOnly
{
set { this.txtTextBox.ReadOnly = value; this.txtTextBox.BackColor = Color.White; }
get { return this.txtTextBox.ReadOnly; }
}

private ContentAlignment labelTextAlign = ContentAlignment.MiddleLeft;
public ContentAlignment LabelTextAlign
{
get
{
return this.labelTextAlign;
}
set
{
this.labelTextAlign = value;
}
}
public HorizontalAlignment Align
{
get
{
return this.txtTextBox.TextAlign;
}
set
{
this.txtTextBox.TextAlign = value;
}
}
public void Error(bool valor)
{
this.txtTextBox.BackColor = ColorWin;
if (valor) { this.txtTextBox.BackColor = ColorError; this.txtTextBox.Focus(); this.txtTextBox.SelectAll(); }
}
public delegate void CheckedChange();
public event CheckedChange CheckedChangeEvent = null;
public bool Checked
{
get
{
return this.chkCheck.Checked;
}
set
{
if (this.checkControlled)
{
this.chkCheck.Checked = value;
if (CheckedChangeEvent != null)
CheckedChangeEvent();
}
}
}

private bool checkControlled;
public bool CheckControlled
{
get { return checkControlled; }
set
{
this.checkControlled = value;
this.chkCheck.Visible = value;
if (value)
this.txtTextBox.Enabled = this.chkCheck.Checked;
}
}

public bool CheckValid
{
get
{
return (this.checkControlled && this.chkCheck.Checked);
}
}

public new bool Enabled
{
get
{
return base.Enabled;
}
set
{
base.Enabled = value;
this.Invalidate();
}
}

public delegate void Key(KeyPressEventArgs e);
public event Key KeyEvent = null;
public delegate void TextChanged(EventArgs e);
public event TextChanged TextChangedEvent = null;
public delegate void LeaveTxtBox(EventArgs e);
public event LeaveTxtBox LeaveTxtBoxEvent = null;
public int SelectionStart
{
get { return this.txtTextBox.SelectionStart; }
set { this.txtTextBox.SelectionStart = value; }
}


#endregion

#region Constructor
public LabelledTextUp()
{
InitializeComponent();
this.txtTextBox.Font = this.Font;
this.MainControl = this.txtTextBox;
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose(disposing);
}
#endregion

#region Event Management

private void txtTextBox_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
{
if (!allowWriting)
{
e.Handled = true;
return;
}

e.Handled = Utils.ValidacionEscrituraTxtBox(this.tipo, e.KeyChar);
if (KeyEvent != null)
KeyEvent(e);
}

private void txtTextBox_TextChanged(object sender, EventArgs e)
{
this.Invalidate();
if (TextChangedEvent != null)
TextChangedEvent(e);
}

private void LabelTextUp_Leave(object sender, EventArgs e)
{
this.Text = backupText;
if (LeaveTxtBoxEvent != null)
this.LeaveTxtBoxEvent(e);
}


public void SeleccionTextoAlFinal()
{
txtTextBox.SelectionStart = txtTextBox.Text.Length;
}

private void LabelledTextUp_EnabledChanged(object sender, EventArgs e)
{
allowWriting = this.Enabled;
this.BackColor = this.Enabled ? Color.White : Color.FromArgb(232, 232, 232);
this.txtTextBox.BackColor = this.BackColor;
this.Invalidate();
}

private void chkCheck_CheckedChanged(object sender, EventArgs e)
{
this.txtTextBox.Enabled = this.chkCheck.Checked;
if (CheckedChangeEvent != null)
CheckedChangeEvent();
}
#endregion

#region Interfaz
public void Deselect()
{
this.txtTextBox.Select(Text.Length, 0);
}

#endregion

#region Paint
protected override void OnPaint(PaintEventArgs e)
{
PaintCurveLabel(false, e);
this.txtTextBox.Top = controlTop;
this.chkCheck.Top = controlTop;

base.OnPaint(e);
}

#endregion

#region Enter/Leave
private void txtTextBox_Enter(object sender, EventArgs e)
{
this.Invalidate();
}
#endregion

}
[/CODE]

[Edited by - Zanshibumi on May 18, 2007 7:02:13 AM]

Share this post


Link to post
Share on other sites
TextBox is unsizable vertically when MultiLine == false.

But the reason it gets smaller is because it doesn't need a non-client area (i.e. the parts where the border are) any more. That margin around it would be replaced by the margin between the text box and the containing CustomControl. And then you can draw your border in the CustomControl's OnPaint. Or just use its Backcolor.

Share this post


Link to post
Share on other sites

This topic is 3861 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this