Posts

Showing posts with the label Custom Controls

Donate

Custom DateTimePicker Control With Background Color And Icon In Windows Forms

Image
Hello Here's a custom DateTimePicker with background color and image icon instead of using the ComboBoxRenderer class. The icon is an image that is added to the project as part of it's resource. The adding of icon is processed through the WndProc method while the setting of background color is handled in the OnPaint() event. Notice that in the constructor, the SetStyle()'s parameters are ControlStyles.UserPaint so that the control paints itself and true to apply the specified style to the control. public class DateTimePickerWithBackground : DateTimePicker { private Bitmap _bmp; enum BorderSize { One = 1, Two = 2 }; public DateTimePickerWithBackground() { _bmp = new Bitmap(ClientRectangle.Height, ClientRectangle.Width); this .SetStyle(ControlStyles.UserPaint, true ); } protected override void WndProc( ref Message m) { base .WndProc( ref m); if (m.Msg == 0xf) //WM_PAINT message { Graphics g = Graphics.FromHwnd(m.HWnd); g.Dr

Load Images in a Windows Form Custom Control

Image
Good day! Often times when developing custom controls you will add images or icons to enhance the UI through the Bitmap class. But when getting the image from file using Image.FromFile(AppDomain.CurrentDomain.BaseDirectory + @"\calendar.png") you may encounter issues such as "path is null" since the path pointed by the BaseDirectory isn't the executable path. A simple workaround is to set the path of the image using hardcoded like Image.FromFile(@"C:\Images\ProjectX\calendar.png") . While this is acceptable, it is ugly to look at and may cause potential issues once the image has been transferred to another location. An accepted solution is to add the image as a project resource then reference it in the custom control code. To accomplish that, here are the steps. * Right Click on the Project -> Properties * In Resources, select Add Resource Dropdown -> Add Existing File - Choose the image or icon to be used as resource. Once done, it wil

Custom Richtextbox Control With Watermark Support In VB.NET

Here's the VB.NET Version of this Custom Control from this post: Custom Richtextbox control with Watermark Support in C# Option Strict On Option Infer On Imports System.Drawing Imports System.ComponentModel ''' <summary> ''' Set Watermark in RichTextbox. ''' </summary> Public Class WatermarkRichTextbox Inherits Windows.Forms.RichTextBox Private m_watermark As String Private m_watermarkColor As Color Private m_foreColor As Color Private empty As Boolean <Browsable( True )> _ Public Property WatermarkColor() As Color Get Return m_watermarkColor End Get Set (value As Color) m_watermarkColor = value If empty Then MyBase .ForeColor = m_watermarkColor End If End Set End Property <Browsable( True )> _ Public Property Watermark() As String

Custom Richtextbox Control With Watermark Support In C#

There was a solution in MSDN Forums on how to add watermark to a Textbox control. I tried replacing the base class if this will work on a RichTextbox control. After testing, the custom control works as expected. /// <summary> /// Set Watermark in RichTextbox. /// </summary> public class WatermarkRichTextbox : RichTextBox { private string watermark; private Color watermarkColor; private Color foreColor; private bool empty; [Browsable (true)] public Color WatermarkColor { get { return watermarkColor; } set { watermarkColor = value ; if (empty) { base .ForeColor = watermarkColor; } } } [Browsable(true)] public string Watermark { get {

Creating And Consuming Custom Control Events In Windows Forms C#

In my autocomplete textbox control, I have a listbox that shows the suggested items based on user input. However, as a requirement I want to create an event that will trigger once a selection has been done in the listbox and this event will be consumed in the calling program (Main Form). To do that, you must define a custom event. After googling for a while, here are the references that made me accomplish the task: Creating Custom Events in C# Quick and Easy Custom Events Cheers!

Windows Forms Fire TextBox Control Events Defined In A User Cntrol From Main Form

In a scenario where in I have created a user control which has a textbox control with private keypress event. I drag and drop the user control to my main form and then I want to fire the keypress event defined in the user control. Assuming in my main form I have this code: private void customTextBox1_KeyPress_1( object sender, KeyPressEventArgs e) { //accept letters,numbers, spacebar and backspace key if ( char .IsLetterOrDigit(e.KeyChar) || e.KeyChar == '\b' || e.KeyChar == ( char )Keys.Space) { e.Handled = false ; } else { e.Handled = true ; } } Note: customTextBox1 is the user control name and customTextBox1_KeyPress_1 is the user control keypress event. In order to trigger the keypress event of the textbox defined in the user control, call Keypress user control in the textbox keypress event handler. See code below: private void textBox1_KeyPress( object sende

Custom CheckedListBox DataGridView Column

Image
Hello, Here's a working custom control class on how to embed CheckedListBox as a datagridview column. Source: Need a DataGridView Custom Column of Type ListView or CheckedListBox However, this class lacked functionalities such as obtaining the checked items and preserving the checked items during painting of the datagridview cell. Here's the custom class: public class CheckedListBoxColumn : DataGridViewColumn { public CheckedListBoxColumn() : base ( new CheckedListBoxCell()) { } public override DataGridViewCell CellTemplate { get { return base .CellTemplate; } set { if ( value != null && ! value .GetType().IsAssignableFrom( typeof (CheckedListBoxCell))) { throw new InvalidCastException( "Must be

Textbox Custom Controls With Autosuggest Features In C# Windows Forms

Image
A textbox custom control developed by Aland Li, MSDN Moderator was the basis for the creation of three new versions of textbox custom controls with Autosuggest features. I managed to fixed some bugs and improve the functionalities of the control. The first control integrates Suggest + Append feature. The second control integrates exact keyword searching which is a limitation to the built-in .NET textbox autosuggest feature that is using startswith algorithm. The third control is an enhancement of the second control with keyword highlighting. Thanks to the original creator of the control Cheers.

Measure Width Of A Given String In Pixel Using C# And Windows Forms

Here's a function to compute the text width of a given string in pixel. Original Author: Mongus Pong (Stack Overflow) protected int _MeasureDisplayStringWidth ( Graphics graphics, string text, Font font, float width, float height ) { if ( text == "" ) return 0 ; StringFormat format = new StringFormat ( StringFormat.GenericDefault ); RectangleF rect = new RectangleF ( 0 , 0 , width, 1000 ); CharacterRange[] ranges = { new CharacterRange ( 0 , text.Length ) }; Region[] regions = new Region[ 1 ]; format.SetMeasurableCharacterRanges ( ranges ); format.FormatFlags = StringFormatFlags.MeasureTrailingSpaces; regions = graphics.MeasureCharacterRanges ( text, font, rect, format ); rect = regions[ 0 ].GetBounds ( graphics ); return ( int )( rect.Right ); } Greg

Close ToolStripDropdown Object When Parent Container Or Form Is Clicked In C# Windows Forms

To close the ToolStripDropdown object which is part of the textbox custom control, add event handler signatures and event definition using the code below: //add statements in the custom control constructor this .Parent.Click += new EventHandler(Parent_Click); this .Parent.Move += new EventHandler(Parent_Click); //event body private void Parent_Click( object sender, EventArgs e) { _dropDown.Close(); //ToolStripDropdown object } Cheers!

Focus Or Caret On TextBox Control Gone If ToolstripDropdown Is shown In Custom Controls Windows Forms C#

Im trying to implement a custom textbox control with auto-suggest features. Upon rendering the ToolStripDropdown object just below the textbox control, the focus seems to be lost. I found the trick by setting the AutoClose property to false in the Textbox OnHandleCreated(EventArgs e) event. See the code below: _dropDown.AutoClose = false ; //ToolStripDropDown object Greg

Combobox With CheckListBox Supports Multi highlighting (C#)

Image
A control developed by: Stelios Alexandrakis which I found it cool is a combobox with checklistbox as the dropdown item. After fixing a few bugs, I integrate the feature of multi-highlighting based from the previous post on customizing CheckListBox control: CheckBoxList Multihighlight . Here's some screenshots to illustrate each of the custom controls.

CheckBoxList Control With Multi-Highlighting Support In C# Windows Forms

Image
If you want to customize the CheckBoxList control to support multi-highlighting (each checked item is highlighted), you can simply override the OnDrawItem() by adding Graphics statements as shown below: Here's an image sample of the extended CheckBoxList control: Code: protected override void OnDrawItem(DrawItemEventArgs e) { int index = e.Index; if ( this .GetItemCheckState(index) == CheckState.Checked) { string text = this .Items[index].ToString(); Graphics g = e.Graphics; Point point = this .GetItemRectangle(index).Location; point.X += estimated_point; //estimated point is a value you may set manually //background: SolidBrush backgroundBrush; backgroundBrush = reportsBackgroundBrushSelected; //estimated point is a value you may

Embedding A DataGridview In Combobox Item In Windows Forms

Image
Hi, There's a post in codeproject that will host a datagridview in a combobox. It is in vb.net and I converted it to C#. I made some changes on the custom controls to retrieve the datagridview selected row. This was not provided in the author's post, so I made some changes myself. In total, the control was purely awesome. So, here's the C# equivalent. I won't be posting all the codes since the custom control is posted in codeproject. I'll be posting the main class instead. Credits: Niemand25 of Lithuania using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Windows.Forms; using System.Reflection; namespace MyGridComboBoxCSharp { [ToolboxItem(true)] [ToolboxBitmap(typeof(ComboBox))] [DefaultBindingProperty("SelectedValue")] [LookupBindingProperties("DataSource", "

Custom DatagridviewCheckboxColumn In Windows Forms

Image
Hello, Here's a simple class implementation of a DatagridviewCheckboxColumn. public class DatagridviewCustomCheckboxColumn : DataGridViewCheckBoxColumn { public DatagridviewCustomCheckboxColumn() { this .CellTemplate = new DatagridviewCheckboxCustomCell(); } } class DatagridviewCheckboxCustomCell : DataGridViewCheckBoxCell { public int row_index { get ; set ; } public int CheckboxHeight { get { //your_desired_checkbox_height is a variable //that contains the desired height of your checkbox //you may set or get the property value.. return your_desired_checkbox_height; } } public int CheckboxWidth { get { //your_desired_checkbox_width is a variable //that contains the desired width of your checkbox //you may set or get the property value.. return your_d

WPF TimePicker Control

Image
Based from these sources:  a. (http://jobijoy.blogspot.com.au/2007/10/time-picker-user-control.html)  b. Dipitimaya Patra's datepicker in Datagrid I came up with a modified custom timepicker control embedded in WPF Datagrid with databinding features. I encountered bugs on migrating the present time picker custom control throughout development in .Visual Studio 2010 and I manage to fix them myself. The errors are specifically found on the keydown events and time updates (increment/decrement of values). Here are the things that I did to make this work: 1. I added a dependency property to the user control public DateTime TimeValue { get { return (DateTime)GetValue(TimeProperty); } set { SetValue(TimeProperty, value ); } } public static readonly DependencyProperty TimeProperty = DependencyProperty.Register( "TimeValue" , typeof (DateTime), typeof (TimeControlBinding), new UIPropertyMetadata(DateTi

Donate