Posts

Showing posts with the label Windows Forms

Donate

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", "

How To Change Position A Windows Forms MessageBox in C#

Here's a tip from CODE PROJECT on positioning message box. This utilize c++ dlls. using System.Runtime.InteropServices; using System.Threading; [DllImport("user32.dll")] static extern IntPtr FindWindow(IntPtr classname, string title); // extern method: FindWindow [DllImport("user32.dll")] static extern void MoveWindow(IntPtr hwnd, int X, int Y, int nWidth, int nHeight, bool rePaint); // extern method: MoveWindow [DllImport("user32.dll")] static extern bool GetWindowRect(IntPtr hwnd, out Rectangle rect); // extern method: GetWindowRect void FindAndMoveMsgBox( int x, int y, bool repaint, string title) { Thread thr = new Thread(() => // create a new thread { IntPtr msgBox = IntPtr.Zero; // while there's no MessageBox, FindWindow returns IntPtr.Zero while ((msgBox = FindWindow(IntPtr.Zero, title)) == IntPtr.Zero) ; // after the while loop, msgBox is

How To Get Textbox Or RichtextBox Value Using pInvoke In Windows API C#

Assuming you already get the pointer handle of the textbox/richtextbox control from source calling function, the snippet to get the content of the edit control is as follows. public const int GWL_ID = -12; public const int WM_GETTEXT = 0x000D; [DllImport("User32.dll")] public static extern int GetWindowLong(IntPtr hWnd, int index); [DllImport("User32.dll")] public static extern IntPtr SendDlgItemMessage(IntPtr hWnd, int IDDlgItem, int uMsg, int nMaxCount, StringBuilder lpString); [DllImport("User32.dll")] public static extern IntPtr GetParent(IntPtr hWnd); private StringBuilder GetRichEditText(IntPtr hWndRichEdit) { Int32 dwID = GetWindowLong(hWndRichEdit, GWL_ID); IntPtr hWndParent = GetParent(hWndRichEdit); StringBuilder title = new StringBuilder(128); SendDlgItemMessage(hWndParent, dwID, WM_GETTEXT, 128, title); return title; } Greg

Custom DatagridviewCheckboxColumn In Windows Forms

Image
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_desired_che

Prevent DataGridView Last Row From Being Sorted On Column Click

Image
Hello, You might have a row in the DataGridView typically the last one that computes total and the grid control is unbound to a datasource. And then if a sorting event occurs, you dont' wanna include that row during sort event. So given that your application has a form and a DataGridView control, the code to perform databinding is handled in the Form Load Event. DataGridViewRow dgRowTotalCount; DataTable dataTable; private void Form1_Load ( object sender, EventArgs e) { DataTable dt = new DataTable( "tblEntTable" ); dt.Columns.Add( "ID" , typeof ( string )); dt.Columns.Add( "Amount" , typeof ( decimal )); dt.Rows.Add( new object [] { "1" , 100.51 }); dt.Rows.Add( new object [] { "2" , 200.52 }); dt.Rows.Add( new object [] { "6" , 500.24 }); dt.Rows.Add( new object [] { "8" , 1000.11 }); dt.Rows.Add( new object [] { "4" , 400.31 }); dt.Rows.Add( new object [] { "5" , 6

How To Apply Fore Color Or Font Weight To DataGridView Cell

Here's the code to apply font weight and font color to your datagridview cell. DataGridViewCellStyle style = new DataGridViewCellStyle(); style.ForeColor = Color.Red; style.Font = new Font( this .Font, FontStyle.Bold); dg.Rows[dg.Rows.Count - 1].Cells[i + 2].Style = style;

Check If Mouse Coordinate Is Outside The Form Coordinates In Mouseup Event In C#

This snippet will check if mouse coordinate is outside the form coordinates in mouseup event using the object's ClientRectangle property. private void dgvScriptsTime_MouseUp( object sender, MouseEventArgs e) { if (! this .ClientRectangle.Contains(e.Location)) { FShowGrid showGrid = new FShowGrid( "Test" , dgvScriptsTime); showGrid.ShowDialog(); } }

Graphic Lines In Picture Box Gone After Switching Tab Pages In C#

In a program i'm working with, I have a timer event which draws lines or graphs on a picture box. This picture box is inside a tab and panel. When I switched views to other tabs and return to the tab that has the picture box, the previous lines or graphs disappears. I suspect, the controls are redrawn when switching tabs. I tried several solutions and options in picture box paint event and infact tried overriding it. Same results. Luckily, I found a tip in an article to draw the real time graphics on the image itself. This helped solve the issue. Greg

Limit Flickering On Winforms In C#

Here's the snippet to limit flicker in forms. Put it above the initialize component method in your constructor. //limit flicker SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true ); InitializeComponent(); Cheers! Source: mijalko

Enable/Disable ToolstripButton Using Delegates (Thread) In C#

Assuming you will access the toolstrip button from another thread, then here's the code on how to enable or disable ToolstripButton using delegates. delegate void EnableDisableButtonRunDeleg( bool value ); private void EnableDisableButtonRun( bool value ) { if (tsMenu.InvokeRequired) { this .tsMenu.Invoke( new EnableDisableButtonRunDeleg (EnableDisableButtonRun), value ); } else { ToolStripItem ts = tsMenu.Items[0]; ((ToolStripButton)ts).Enabled = value ; } } Cheers!

Prevent Listview Column From Being Resized In Windows Forms C#

I added the snippet below in the columnwidthchanging event: switch (e.ColumnIndex) { case 0: e.NewWidth = 400; break ; case 1: e.NewWidth = 200; break ; case 2: e.NewWidth = 100; break ; case 3: e.NewWidth = 100; break ; default : break ; } e.Cancel = true ; Where 400, 200, and 100 are the original column sizes.

AutoComplete Winforms Search Not Working If Search Item Is One Character Digit In C#

Textbox properites: autocompletemode = append, autcompletesource = customsource. Assuming the autocompletestringcollection of the textbox are as follows: James Philipp Mariah Clara 8 Ryan GregEsguerra If you type the number 8 in the textbox, the textbox autocomplete does not suggest. As defined, it will search the prefix of the source. So, i guess one character does not have a prefix at all. The workaround for this is to add a space character after the digit 8. So, "8" becomes "8 ". Below is the code: private void PopulateWebsiteName() { try { if (dtWebsiteNames != null ) { dtWebsiteNames.Clear(); } dtWebsiteNames = JobsReporting.Scripts.GetWebsiteNamesLocal(); if (dtWebsiteNames.Rows.Count > 0) { foreach (DataRow row in dtWebsiteNames.Rows) { if (row[1].ToString().Trim().Length == 1) { row[1] = row[1]

Disable WindowsForms Form From Being Resized At Design Time In C#

Here's a snippet on how to prevent or disable winforms form object from being resized at design time. this .MaximumSize = new System.DrawingSize(500, 400); this .MinimumSize = new System.DrawingSize(500, 400);

Accessing HttpUtility Class In C# Winforms

To use or access the HttpUtility class in C# winforms, do the following: 1. Add reference to System.Web dll 2. Use any methods available in HttpUtility class. System.Web.HttpUtility.HtmlDecode(mystring); System.Web.HttpUtility.UrlDecode(mystring); Cheers!

Donate