ImageCombo.cs
Description du code
ImageCombo.cs est un fichier du projet BrolDev.Ce fichier est situé dans /var/www/bin/sniplets/bibliobrol/broldev/src/.
Projet BrolDev : Librairie de composants réutilisables pour les applications BrolDev en CSharp.
Code source ou contenu du fichier
Code c# (ImageCombo.cs) (443 lignes)
using System; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; using be.gaudry.model.drawing; namespace be.gaudry.view.controls { public class ImageComboBox : ComboBox { #region constructors and declarations private ImageList imageList; private ContentAlignment textAlignment; private ContentAlignment imageAlignment; private StringFormat stringFormat; private SolidBrush solidBrush; private bool allowDuplicates, allowTextEdition; public ImageComboBox() { this.DrawMode = DrawMode.OwnerDrawFixed; textAlignment = ContentAlignment.MiddleLeft; imageAlignment = ContentAlignment.MiddleLeft; //this.DropDownStyle = ComboBoxStyle.DropDown; setAlignment(); } #endregion #region Attributes /// <summary> /// Specifies alignment of text on the drawing surface. /// </summary> [ Category("ImageComboBox"), Browsable(true), Description("Specifies alignment of text on the drawing surface."), DefaultValue(ContentAlignment.MiddleLeft) ] public ContentAlignment TextAlignment { get{return textAlignment;} set{textAlignment = value;} } /// <summary> /// Specifies alignment of the image on the drawing surface. /// </summary> [ Category("ImageComboBox"), Browsable(true), Description("Specifies alignment of the image on the drawing surface."), DefaultValue(ContentAlignment.MiddleLeft) ] public ContentAlignment ImageAlignment { get{return imageAlignment;} set{imageAlignment = value;} } /// <summary> /// Gets or sets the <code>ImageList</code> to use. /// </summary> [ Category("ImageComboBox"), Browsable(true), Description("Provides methods to manage a collection of System.Drawing.Image objects."), DefaultValue(null) ] public ImageList ImageList { get{return imageList;} set{imageList = value;} } /// <summary> /// Allows duplicates in the collection of values /// Default value is true(quickest). /// </summary> [ Category("ImageComboBox"), Browsable(true), Description("Allows duplicates in the collection of values (quickest)."), DefaultValue(true) ] public bool AllowDuplicates { get { return allowDuplicates; } set { allowDuplicates = value; } } /// <summary> /// Allows text edition. /// Default value is false. /// </summary> [ Category("ImageComboBox"), Browsable(true), Description("Allows text edition."), DefaultValue(false) ] public bool AllowTextEdition { get { return allowTextEdition; } set { allowTextEdition = value; } } /// <summary> /// Gets or sets the text that is selected in the editable portion of a <code>be.gaudry.view.controls.ImageComboBox</code>. /// </summary> [ Category("ImageComboBox"), Browsable(false), Description("Gets or sets the text that is selected in the editable portion of a be.gaudry.view.controls.ImageComboBox."), DefaultValue(null) ] public String SelectedInnerText { get { return (SelectedItem!=null)?SelectedItem.ToString():null; } set { if (string.Empty.Equals(value)) return; int j = Items.Count; for (int i = 0; i > j; i++) { if ((value).Equals(((ImageComboBoxItem)Items[i]).Text)) { SelectedIndex = i; return; } } } } #endregion #region private methods private void setAlignment() { //Horizontal align if (this.TextAlignment == ContentAlignment.BottomLeft || this.TextAlignment == ContentAlignment.MiddleLeft || this.TextAlignment == ContentAlignment.TopLeft ) stringFormat.Alignment = StringAlignment.Near; if (this.TextAlignment == ContentAlignment.BottomCenter || this.TextAlignment == ContentAlignment.MiddleCenter || this.TextAlignment == ContentAlignment.TopCenter ) stringFormat.Alignment = StringAlignment.Center; if (this.TextAlignment == ContentAlignment.BottomRight || this.TextAlignment == ContentAlignment.MiddleRight || this.TextAlignment == ContentAlignment.TopRight ) stringFormat.Alignment = StringAlignment.Far; //Vertical align if (this.TextAlignment == ContentAlignment.BottomCenter || this.TextAlignment == ContentAlignment.BottomLeft || this.TextAlignment == ContentAlignment.BottomRight ) stringFormat.LineAlignment = StringAlignment.Far; if (this.TextAlignment == ContentAlignment.TopCenter || this.TextAlignment == ContentAlignment.TopLeft || this.TextAlignment == ContentAlignment.TopRight ) stringFormat.LineAlignment = StringAlignment.Near; if (this.TextAlignment == ContentAlignment.MiddleCenter || this.TextAlignment == ContentAlignment.MiddleLeft || this.TextAlignment == ContentAlignment.MiddleRight ) stringFormat.LineAlignment = StringAlignment.Center; } #endregion #region public methods /// <summary> /// todo /// </summary> /// <param name="text"></param> public void addItem(String text) { //create a new item } /// <summary> /// Add a new item, and select an image from the imagelist /// </summary> /// <param name="text"></param> /// <param name="imageId"></param> public void addItem(String text, int imageId) { if (!allowDuplicates && Items.Contains(iCBI)) return; Items.Add(iCBI); } /// <summary> /// todo /// </summary> /// <param name="text"></param> /// <param name="image"></param> public void addItem(String text, Image image) { //add image in the imagelist //create a new item if (imageList == null) { } image = ImageHelper.getResized(image, ImageList.ImageSize.Width, ImageList.ImageSize.Height); int i = imageList.Images.AddStrip(image); addItem(text, i); } /// <summary> /// todo /// </summary> /// <param name="text"></param> public void pushItem(String text) { //create a new item } public void pushItem(String text, int imageId) { if (!allowDuplicates && Items.Contains(iCBI)) Items.Remove(iCBI); Items.Insert(0, iCBI); } public void pushItem(String text, Image image) { //add image in the imagelist //create a new item } /// <summary> /// Add the current text as first item, set the image index, /// and set this item as selected /// </summary> /// <param name="imageId"></param> public void pushAndSelect(int imageId) { if (!allowDuplicates && Items.Contains(iCBI)) Items.Remove(iCBI); Items.Insert(0, iCBI); SelectedIndex = 0; } /// <summary> /// Add the current text as first item, set the image index, /// and set this item as selected /// </summary> /// <param name="imageId"></param> public void pushAndSelect(String text, int imageId) { if (!allowDuplicates && Items.Contains(iCBI)) { Items.Remove(iCBI); } Items.Insert(0, iCBI); SelectedIndex = 0; } public void cleanDuplicates() { for (int i = 0; i < Items.Count - 1; i++) { for (int j = Items.Count - 1; j > i; j--) { if ((Items[i]).Equals(Items[j])) { Items.RemoveAt(j); } } } } #endregion #region overrided methods protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e) { //this.DropDownStyle = ComboBoxStyle.DropDown; base.OnDrawItem(e); if (e.Index == -1) return; if (this.ImageList != null) { ImageComboBoxItem imageCBI = (ImageComboBoxItem)this.Items[e.Index]; Image imgToDisplay = this.ImageList.Images[imageCBI.ImageIndex]; e.Bounds.Left, e.Bounds.Top, e.Bounds.Width - this.ImageList.ImageSize.Width, e.Bounds.Height ); int iX = 0, iY = 0; switch (this.ImageAlignment) { case ContentAlignment.TopLeft: iX = e.Bounds.Left; iY = e.Bounds.Top; oRect.X = this.ImageList.ImageSize.Width; break; case ContentAlignment.MiddleLeft: iX = e.Bounds.Left; iY = e.Bounds.Top + (this.ItemHeight - this.ImageList.ImageSize.Height) / 2; oRect.X = this.ImageList.ImageSize.Width; break; case ContentAlignment.BottomLeft: iX = e.Bounds.Left; iY = e.Bounds.Top + this.ItemHeight - this.ImageList.ImageSize.Height; oRect.X = this.ImageList.ImageSize.Width; break; case ContentAlignment.TopRight: iX = e.Bounds.Right - this.ImageList.ImageSize.Width; iY = e.Bounds.Top; break; case ContentAlignment.MiddleRight: iX = e.Bounds.Right - this.ImageList.ImageSize.Width; iY = e.Bounds.Top + (this.ItemHeight - this.ImageList.ImageSize.Height) / 2; break; case ContentAlignment.BottomRight: iX = e.Bounds.Right - this.ImageList.ImageSize.Width; iY = e.Bounds.Top + this.ItemHeight - this.ImageList.ImageSize.Height; break; case ContentAlignment.TopCenter: iX = e.Bounds.Left + (e.Bounds.Width - this.ImageList.ImageSize.Width) / 2; iY = e.Bounds.Top; oRect.Width = e.Bounds.Width; break; case ContentAlignment.MiddleCenter: iX = e.Bounds.Left + (e.Bounds.Width - this.ImageList.ImageSize.Width) / 2; iY = e.Bounds.Top + (this.ItemHeight - this.ImageList.ImageSize.Height) / 2; oRect.Width = e.Bounds.Width; break; case ContentAlignment.BottomCenter: iX = e.Bounds.Left + (e.Bounds.Width - this.ImageList.ImageSize.Width) / 2; iY = e.Bounds.Top + this.ItemHeight - this.ImageList.ImageSize.Height; oRect.Width = e.Bounds.Width; break; } e.Graphics.FillRectangle(SystemBrushes.Window, e.Bounds); if ((e.State & DrawItemState.Focus) != 0) e.Graphics.FillRectangle(SystemBrushes.Highlight, e.Bounds); e.Graphics.DrawImage(imgToDisplay, iX, iY); e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font, solidBrush, oRect, stringFormat); return; } e.Graphics.DrawString(this.Items[e.Index].ToString(), this.Font, solidBrush, e.Bounds); } #endregion #region innerClass private class ImageComboBoxItem { #region declarations and constructors private object iCBiValue; private int imageIndex; public ImageComboBoxItem() { this.imageIndex = 0; } public ImageComboBoxItem(Object iCBiValue, Int32 imageIndex) { this.iCBiValue = iCBiValue; this.imageIndex = imageIndex; } #endregion #region properties public String Text { get { return iCBiValue.ToString(); } } public Object Value { get{return iCBiValue;} set{iCBiValue = value;} } public Int32 ImageIndex { get{return imageIndex;} set{imageIndex = value;} } #endregion #region overrides methods public override string ToString() { return iCBiValue.ToString(); } public override int GetHashCode() { return string.Concat(imageIndex.ToString(),iCBiValue.ToString()).GetHashCode(); } public override bool Equals(object obj) { ImageComboBoxItem icbi = obj as ImageComboBoxItem; if (icbi == null) { return false; } if (imageIndex != icbi.imageIndex) { return false; } if (iCBiValue == null && icbi.iCBiValue == null) { return true; } if (icbi.iCBiValue == null) { return false; } //icbi.iCBiValue.GetType() return icbi.iCBiValue.Equals(iCBiValue); } #endregion } #endregion } }
Structure et Fichiers du projet
Afficher/masquer...Icône | Nom | Taille | Modification |
Pas de sous-répertoires. | |||
Icône | Nom | Taille | Modification |
| _ | Répertoire parent | 0 octets | 1732284324 22/11/2024 15:05:24 |
Utilisation de l'explorateur de code
- Navigation :
- Un clic sur une icône de répertoire ouvre ce répertoire pour en afficher les fichiers.
- Lorsque le répertoire en cours ne contient pas de sous-répertoires il est possible de remonter vers le répertoire parent.
- La structure de répertoires en treetable (tableau en forme d'arborescence) n'est plus possibledans cette version.
- Un clic sur une icône de fichier ouvre ce fichier pour en afficher le code avec la coloration syntaxique adaptée en fonction du langage principal utilisé dans le fichier.
- Affichage :
- Il est possible de trier les répertoires ou les fichiers selon certains critères (nom, taille, date).
- Actions :
- Les actions possible sur les fichiers dépendent de vos droits d'utilisateur sur le site. Veuillez activer le mode utilisateur pour activer les actions.
English translation
You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.
Thank you in advance.
Document created the 16/10/2009, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/cs-broldev-source-rf-view/controls//ImageCombo.cs.html
The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.