Você está na página 1de 7

Here is a quick example of how to use an AutoComplete ComboBox in DataGridView in a Windows Application. 1. 2.

Create one Windows Application and add DataGridView from toolbox to design. Now create two DataGridViewComboBoxColumns and add them to the DataGridView:

public void ComboList1() { DataGridViewComboBoxColumn combo1 = new DataGridViewComboBoxColumn(); combo1.HeaderText = "Country"; combo1.Items.Add("Antarctica"); combo1.Items.Add("Belgium"); combo1.Items.Add("Canada"); combo1.Items.Add("Finland"); combo1.Items.Add("Albania"); combo1.Items.Add("India"); combo1.Items.Add("Barbados"); dataGridView1.Columns.Add(combo1); } public void ComboList2() { DataGridViewComboBoxColumn combo2 = new DataGridViewComboBoxColumn(); combo2.HeaderText = "Types of Jobs"; combo2.Items.Add("Accounting"); combo2.Items.Add("HR"); combo2.Items.Add("Finance"); combo2.Items.Add("Transportation"); combo2.Items.Add("Testing"); dataGridView1.Columns.Add(combo2); } Call both these methods from the Form Constructor. Now Click on DataGridView and generate EditingControlShowing event and write the folllowing code in it: if (e.Control is DataGridViewComboBoxEditingControl) { ((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown; ((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems; ((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest; } This will work for all comboBoxes which are present in the DataGridView.

The AutoCompleteMode and AutoCompleteSource properties create a ComboBox that automatically completes input strings by comparing the prefix being entered to the prefixes of all strings in a maintained source. This is useful for ComboBox controls in which URLs, addresses, file names, or commands will be frequently entered. If there are duplicate entries in the maintained source, automatic completion behaves unpredictably.

Sau y l m t s th thu t x l v i winform trn C# >>>Lm th no thi t l p m t default button ? 1. Set tabindex = 0; 2. Set l p thu c tnh trn form cho button
Code:

form1.AcceptButton = button1;
>>>Lm th no in 1 form qua my in nh ? 1. Cho vo ch ng trnh component : PrintDocument 2. Dng hm m u sau :
Code:

private void printFormToPrinter() { PrintDialog printDialog1 = new PrintDialog();

printDialog1.Document = printDocument1; DialogResult result = printDialog1.ShowDialog(); if ( result == DialogResult.OK ) printDocument1.Print(); }


3. V form khi ra my in
Code:

using System.Drawing.Printing; private void printDocument1_PrintPage( object sender, PrintPageEventArgs e) { Graphics graphic = CreateGraphics(); Image memImage = new Bitmap( Size.Width, Size.Height, graphic ); Graphics memGraphic = Graphics.FromImage( memImage ); IntPtr dc1 = graphic.GetHdc(); IntPtr dc2 = memGraphic.GetHdc(); BitBlt( dc2, 0, 0, ClientRectangle.Width, ClientRectangle.Height, dc1, 0, 0, 13369376 ); graphic.ReleaseHdc( dc1 ); memGraphic.ReleaseHdc( dc2 ); e.Graphics.DrawImage( memImage, 0, 0 ); }
>>>Lm th no l y bitmap c a m t form ?
Code:

using using using using using

System; System.Drawing; System.Drawing.Imaging; System.Runtime.InteropServices; System.Windows.Forms;

class CustomForm : Form { [ DllImport( "gdi32.dll" ) ] private static extern bool BitBlt( IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, Int32 dwRop ); private const Int32 SRCCOPY = 0xCC0020; public void SaveImage( string filename ) { using ( Graphics g1 = CreateGraphics() ) { Image image = new Bitmap( ClientRectangle.Width, ClientRectangle.Height, g1 ); using ( Graphics g2 = Graphics.FromImage( image ) ) { IntPtr dc1 = g1.GetHdc(); IntPtr dc2 = g2.GetHdc(); BitBlt( dc2, 0, 0, ClientRectangle.Width, ClientRectangle.Height,

dc1, 0, 0, SRCCOPY ); g2.ReleaseHdc( dc2 ); g1.ReleaseHdc( dc1 ); } image.Save( filename, ImageFormat.Bmp ); } } }


>>>Lm th no l y gi tr c a Txtbox trong form ny trong khi n n m
Code:

form khc ?

string strValue = myFormA.TextBox1.Text;


>>>Khng c n lm form active m v n show Mu n show m khng c n active form :
Code:

TrickClass.SetVisibleNoActivate( myForm, true );


Mu n hide i :
Code:

TrickClass.SetVisibleNoActivate( myForm, false );


nh ngh a t m cho TrickClass v i cch xi method :
Code:

public class TrickClass { [ DllImport( "user32.dll" ) ] extern public static bool SetWindowPos( IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, int uFlags ); public public public public public const const const const const int int int int int HWND_TOPMOST = -1; // 0xffff SWP_NOSIZE = 1; // 0x0001 SWP_NOMOVE = 2; // 0x0002 SWP_NOACTIVATE = 16; // 0x0010 SWP_SHOWWINDOW = 64; // 0x0040

public static void ShowWindowTopMost( IntPtr handle ) { SetWindowPos( handle, (IntPtr) HWND_TOPMOST, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE | SWP_NOACTIVATE | SWP_SHOWWINDOW ); } public static void SetVisibleNoActivate( Control control, bool visible ) { if ( visible ) ShowWindowTopMost( control.Handle ); control.Visible = visible; }

}
>>>Lm th no
Code:

b th t

c cc ki u exception x y ra trn form nh ?

using System.Threading; [STAThread] public static void Main() { Application.ThreadException += new ThreadExceptionEventHandler( UnhandledExceptionCatcher ); Application.Run( new Form1() ); } private static void UnhandledExceptionCatcher(object sender, ThreadExceptionEventArgs e) { Console.WriteLine( "b t c Exception n :D !" ); }
>>>Lm th no
Code:

thi t l p icon c a form t

bitmap nh ?

Form form1 = new Form(); Bitmap bmp = imageList1.Images[index] as Bitmap; form1.Icon = Icon.FromHandle(bmp.GetHicon());
>>>Lm th no
Code:

ch nh s a thng tin v cng ty lm ch

ng trnh nh

// Vo file : AssemblyInfo.cs [assembly: AssemblyCompany( "Pete

i ca Corporation :D" )]
ang th c

>>>Lm th no hi n ra m t h p tho i status trong m t thread n n trong khi hi n vi c khc v cho php ng i dng stop n nh ?
Code:

private void button1_Click(object sender, System.EventArgs e) { BackgroundThreadStatusDialog statusDialog = new BackgroundThreadStatusDialog(); try { for (int n = 0; n < 1000; n++) { statusDialog.Percent = n/10; int ticks = System.Environment.TickCount; while (System.Environment.TickCount - ticks < 10) ; if (statusDialog.IsCanceled) return;

} statusDialog.Close(); MessageBox.Show(statusDialog.IsCanceled ? "Canceled" : "Success"); } finally { statusDialog.Close(); } }


>>>Lm th no bi t
Code:

c ng

i dng click vo m t m t form khc t

m t model dialog ?

/* Dng Form.Deactivate */ Deactivate += new EventHandle( OnDeactivate ); // ... private void OnDeactivate( object s, EventArgs e ) { Close(); }
>>>Lm th no load m t control t
Code:

m t assembly hay DLL ?

using System.Reflection // ca ny load control // v d l y SpecControls.ColorControl t SpecControls.dll. using System.Reflection; Assembly assembly = Assembly.LoadFrom( "SpecControls.dll" ); Type t = assembly.GetType( "SpecControls.ColorControl" ); // t o instance r i add n vo parent control Control c = (Control) Activator.CreateInstance( t ); parent.Controls.Add( c );
>>>Lm th no c m user thot kh i ch
Code:

ng trnh b ng nt X nh ?

private void Form1_Closing( object sender, CancelEventArgs e ) { if ( NotOkToClose() ) e.Cancel = true; //don't close }
>>>Lm th no t o form khng c title bar v resize
Code:

c?

form1.Text = string.Empty; form1.ControlBox = false;

>>>Lm th no thi t l p Dng thu c tnh Opacity


Code:

trong su t cho form nh y ?

form1.Opacity = X // v i X l ki u double 100% :D

0 <= X <= 1 , ho c 0% <= X <=

>>>T o form hnh ch nh t chn qu, t o hnh khc V d cho form c ki u 1 hnh ch nh t *****g m t elipse
Code:

c khng?

GraphicsPath gp = new GraphicsPath(); gp.AddEllipse( 0, 0, 100, 100 ); gp.AddRectangle( 50, 50, 100, 100); this.Region = new Region( gp );
>>>Lm th no form c style ki u WinXP nh ?
Code:

public virtual void Main() { Application.EnableVisualStyles(); // Calling DoEvents after the above method call seems to fix this issue Application.DoEvents(); Application.Run( new Form1() ); }
>>> Lm th no t o form ph
Code:

h t mn hnh k c taskbar ?

FormBorderStyle = FormBorderStyle.FixedSingle; Rectangle formrect = Screen.GetBounds( this ); Location = formrect.Location; Size = formrect.Size;

Você também pode gostar