string item = combo.Items[e.Index].ToString();
string item = ((DataRowView)combo.Items[e.Index]).Row["display"].ToString();
private void comboBox_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { if ( e.Index != -1 ) { ComboBox combo = (ComboBox)sender; string item = combo.Items[e.Index].ToString(); string debut, fin; debut = item.Substring(0, item.IndexOf(' ')); fin = item.Substring(item.IndexOf(' ')+1); // dessin StringFormat format = new StringFormat(); // fond SolidBrush brushFond, brushTexte; // si item sélectionné if ( ( e.State & DrawItemState.Selected ) == DrawItemState.Selected ) { // inversion des couleurs brushFond = new SolidBrush(e.ForeColor); brushTexte = new SolidBrush(e.BackColor); } else { brushFond = new SolidBrush(e.BackColor); brushTexte = new SolidBrush(e.ForeColor); } // fond e.Graphics.FillRectangle(brushFond, e.Bounds); // rectangle de sélection si sélection if ( ( e.State & DrawItemState.Selected ) == DrawItemState.Selected ) e.DrawFocusRectangle(); // debut format.Alignment = StringAlignment.Near; e.Graphics.DrawString(debut, e.Font, brushTexte, e.Bounds, format ); // fin RectangleF bounds = e.Bounds; bounds.X = bounds.Width - max_largeur_fin; e.Graphics.DrawString(fin, e.Font, brushTexte, bounds, format ); } } private float max_largeur = 0.0f; private float max_hauteur = 0.0f; private float max_largeur_fin = 0.0f; private const int max_largeur_ajout = 10; private void comboBox_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e) { if ( e.Index != -1 ) { ComboBox combo = (ComboBox)sender; SizeF temp; string item = combo.Items[e.Index].ToString(); temp = e.Graphics.MeasureString(item, combo.Font); if ( temp.Width > max_largeur-max_largeur_ajout ) max_largeur = temp.Width+max_largeur_ajout; if ( temp.Height > max_hauteur ) max_hauteur = temp.Height; // mesure de la chaine de fin string fin = item.Substring(item.IndexOf(' ')+1); temp = e.Graphics.MeasureString(fin, combo.Font); if ( temp.Width > max_largeur_fin ) max_largeur_fin = temp.Width; e.ItemHeight = (int)max_hauteur; e.ItemWidth = (int)max_largeur; combo.DropDownWidth = (int)max_largeur; } }
private float max_largeur = 0.0f; private float max_hauteur = 0.0f; private float max_largeur_fin = 0.0f; private const int max_largeur_ajout = 10; private void cbo_T_CIVILITE_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e) { if ( e.Index != -1 ) { ComboBox combo = (ComboBox)sender; SizeF temp; System.Data.DataRowView row = (System.Data.DataRowView) combo.Items[e.Index]; string item = row["display"].ToString (); temp = e.Graphics.MeasureString(item, combo.Font); if ( temp.Width > max_largeur-max_largeur_ajout ) max_largeur = temp.Width+max_largeur_ajout; if ( temp.Height > max_hauteur ) max_hauteur = temp.Height; // mesure de la chaine de fin string fin = item.Substring(item.IndexOf(' ')+1); temp = e.Graphics.MeasureString(fin, combo.Font); if ( temp.Width > max_largeur_fin ) max_largeur_fin = temp.Width; e.ItemHeight = (int)max_hauteur; e.ItemWidth = (int)max_largeur; combo.DropDownWidth = (int)max_largeur; } } private void cbo_T_CIVILITE_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { if ( e.Index != -1 ) { ComboBox combo = (ComboBox)sender; System.Data.DataRowView row = (System.Data.DataRowView) combo.Items[e.Index]; string item = row["display"].ToString (); string debut, fin; debut = item.Substring(0, item.IndexOf(' ')); fin = item.Substring(item.IndexOf(' ')+1); // dessin StringFormat format = new StringFormat(); // fond SolidBrush brushFond, brushTexte; // si item sélectionné if ( ( e.State & DrawItemState.Selected ) == DrawItemState.Selected ) { // inversion des couleurs brushFond = new SolidBrush(e.ForeColor); brushTexte = new SolidBrush(e.BackColor); } else { brushFond = new SolidBrush(e.BackColor); brushTexte = new SolidBrush(e.ForeColor); } // fond e.Graphics.FillRectangle(brushFond, e.Bounds); // rectangle de sélection si sélection if ( ( e.State & DrawItemState.Selected ) == DrawItemState.Selected ) e.DrawFocusRectangle(); // debut format.Alignment = StringAlignment.Near; e.Graphics.DrawString(debut, e.Font, brushTexte, e.Bounds, format ); // fin RectangleF bounds = e.Bounds; bounds.X = bounds.Width - max_largeur_fin; e.Graphics.DrawString(fin, e.Font, brushTexte, bounds, format ); } }
Vous n’avez pas trouvé la réponse que vous recherchez ?
Posez votre questionprivate void comboBox_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e) { ComboBox combo = (ComboBox)sender; float max_largeur = 0.0f; float max_hauteur = 0.0f; SizeF temp; foreach ( string item in combo.Items ) { temp = e.Graphics.MeasureString(item, combo.Font); if ( temp.Width > max_largeur ) max_largeur = temp.Width; if ( temp.Height > max_hauteur ) max_hauteur = temp.Height; } // ajout de 100 pixels en plus au max max_largeur += 10; e.ItemHeight = (int)max_hauteur; e.ItemWidth = (int)max_largeur; }
private void comboBox_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { if ( e.Index != -1 ) { ComboBox combo = (ComboBox)sender; string item = combo.Items[e.Index].ToString(); string debut, fin; debut = item.Substring(0, item.IndexOf(' ')); fin = item.Substring(item.IndexOf(' ')+1); // dessin StringFormat format = new StringFormat(); // fond SolidBrush brushFond, brushTexte; // si item sélectionné if ( ( e.State & DrawItemState.Selected ) == DrawItemState.Selected ) { // inversion des couleurs brushFond = new SolidBrush(e.ForeColor); brushTexte = new SolidBrush(e.BackColor); } else { brushFond = new SolidBrush(e.BackColor); brushTexte = new SolidBrush(e.ForeColor); } // fond e.Graphics.FillRectangle(brushFond, e.Bounds); // rectangle de sélection si sélection if ( ( e.State & DrawItemState.Selected ) == DrawItemState.Selected ) e.DrawFocusRectangle(); // debut format.Alignment = StringAlignment.Near; e.Graphics.DrawString(debut, e.Font, brushTexte, e.Bounds, format ); // fin format.Alignment = StringAlignment.Far; e.Graphics.DrawString(fin, e.Font, brushTexte, e.Bounds, format ); } }
private float max_largeur = 0.0f; private float max_hauteur = 0.0f; private const int max_largeur_ajout = 10; private void comboBox_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e) { if ( e.Index != -1 ) { ComboBox combo = (ComboBox)sender; SizeF temp; string item = combo.Items[e.Index].ToString(); temp = e.Graphics.MeasureString(item, combo.Font); if ( temp.Width > max_largeur-max_largeur_ajout ) max_largeur = temp.Width+max_largeur_ajout; if ( temp.Height > max_hauteur ) max_hauteur = temp.Height; e.ItemHeight = (int)max_hauteur; e.ItemWidth = (int)max_largeur; combo.DropDownWidth = (int)max_largeur; } }
debut = item.Substring(0, item.IndexOf(' ')); fin = item.Substring(item.IndexOf(' ')+1);
debut = item.Substring(0, item.IndexOf('\t')); fin = item.Substring(item.IndexOf('\t')+1);
using System; using System.Windows.Forms; using System.Drawing ; using ToolDataBase; using System.ComponentModel ; using System.Data; using System.Threading ; namespace Tools { /// <summary> /// Description résumée de cbo_T_CIVILITE. /// </summary> public class cbo_T_CIVILITE : System.Windows.Forms.ComboBox { private int _index_bdd; private string _ConnectionString; private Thread thread = null; public cbo_T_CIVILITE() { Font Font_Text = new System.Drawing.Font("Courier New", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0))); this.Font = Font_Text; this.DrawItem +=new DrawItemEventHandler(cbo_T_CIVILITE_DrawItem); this.MeasureItem += new MeasureItemEventHandler(cbo_T_CIVILITE_MeasureItem); this.DrawMode = DrawMode.OwnerDrawVariable; } /// <summary> /// Index de la base de donnée à Utiliser (0 : SQL SERVEUR 1 : Mysql) /// </summary> [Description("Index de base de données à utiliser (0 : SQL SERVEUR 1 : Mysql)")] public int Index_Bdd { get { return _index_bdd; } set { _index_bdd = value; } } /// <summary> /// Chaine de connexion à la base de donnée /// </summary> [Description("Chaine de connexion à la base de donnée")] public string ConnectionString { get { return _ConnectionString; } set { _ConnectionString = value; } } /// <summary> /// Fonction qui va remplir la Combo Box /// </summary> public void Rempli_Cbo() { thread = new Thread(new ThreadStart(_Rempli_Cbo)); thread.Start(); } private void _Rempli_Cbo() { try { ToolConnection MaConnect = new ToolConnection(this.Index_Bdd , this.ConnectionString ); ToolCommand MaCmd = new ToolCommand(this.Index_Bdd,"EXEC T_CIVILITE_SELECT",MaConnect); DataSet ds = new DataSet(); ds.Tables.Add("T_CIVILITE"); ds.Tables["T_CIVILITE"].Columns.Add("civnum",typeof(Decimal)); ds.Tables["T_CIVILITE"].Columns.Add("civcod",typeof(string)); ds.Tables["T_CIVILITE"].Columns.Add("civlib",typeof(string)); ds.Tables["T_CIVILITE"].Columns.Add("display",typeof(string), "civcod +' '+ civlib"); MaConnect.Open(); System.Data.IDataReader Monreader = MaCmd.ExecuteReader (); while(Monreader.Read()) { DataRow row = ds.Tables["T_CIVILITE"].NewRow(); row.BeginEdit (); row["civnum"] = Monreader.GetDecimal(0); row["civcod"] = Monreader.GetString(1); row["civlib"] = Monreader.GetString(2); row.EndEdit(); ds.Tables["T_CIVILITE"].Rows.Add(row); } Monreader.Close(); MaConnect.Close(); this.DataSource = ds.Tables["T_CIVILITE"]; this.DisplayMember = "display"; this.ValueMember = "civnum"; } catch(Exception xcp) { MessageBox.Show(xcp.Message.ToString()); } } /// <summary> /// Fonction qui va remplir la Combo Box /// </summary> /// (int) Index de base (0 : SQL SERVEUR, 1 : Mysql) /// (string) Chaine de Connection public void Rempli_Cbo(int Index_Bdd, string ConnectionString) { this.Index_Bdd = Index_Bdd ; this.ConnectionString = ConnectionString; thread = new Thread(new ThreadStart(_Rempli_Cbo)); thread.Start(); } private float max_largeur = 0.0f; private float max_hauteur = 0.0f; private const int max_largeur_ajout = 10; private void cbo_T_CIVILITE_MeasureItem(object sender, System.Windows.Forms.MeasureItemEventArgs e) { if ( e.Index != -1 ) { ComboBox combo = (ComboBox)sender; SizeF temp; string item = combo.Items[e.Index].ToString(); temp = e.Graphics.MeasureString(item, combo.Font); if ( temp.Width > max_largeur-max_largeur_ajout ) max_largeur = temp.Width+max_largeur_ajout; if ( temp.Height > max_hauteur ) max_hauteur = temp.Height; e.ItemHeight = (int)max_hauteur; e.ItemWidth = (int)max_largeur; combo.DropDownWidth = (int)max_largeur; } } private void cbo_T_CIVILITE_DrawItem(object sender, System.Windows.Forms.DrawItemEventArgs e) { if ( e.Index != -1 ) { ComboBox combo = (ComboBox)sender; string item = combo.Items[e.Index].ToString(); string debut, fin; debut = item.Substring(0, item.IndexOf(' ')); fin = item.Substring(item.IndexOf(' ')+1); // dessin StringFormat format = new StringFormat(); // fond SolidBrush brushFond, brushTexte; // si item sélectionné if ( ( e.State & DrawItemState.Selected ) == DrawItemState.Selected ) { // inversion des couleurs brushFond = new SolidBrush(e.ForeColor); brushTexte = new SolidBrush(e.BackColor); } else { brushFond = new SolidBrush(e.BackColor); brushTexte = new SolidBrush(e.ForeColor); } // fond e.Graphics.FillRectangle(brushFond, e.Bounds); // rectangle de sélection si sélection if ( ( e.State & DrawItemState.Selected ) == DrawItemState.Selected ) e.DrawFocusRectangle(); // debut format.Alignment = StringAlignment.Near; e.Graphics.DrawString(debut, e.Font, brushTexte, e.Bounds, format ); // fin format.Alignment = StringAlignment.Far; e.Graphics.DrawString(fin, e.Font, brushTexte, e.Bounds, format ); } } } }
System.Data.DataRowView row = (System.Data.DataRowView) combo.Items[e.Index]; string item = row["display"].ToString ();