By Mimouni Mohamed - 2/20/2013
Hello,
I try to put images + text in a ComboBox.
I tried with a ComboBox DrawItem but it does not work so I searched the internet and I found this code:
Private Sub Ville_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load Me.LIB_PAYS.DrawMode = DrawMode.OwnerDrawVariable Me.LIB_PAYS.DropDownStyle = ComboBoxStyle.DropDownList End Sub
Private Sub LIB_PAYS_DrawItem(sender As Object, e As System.Windows.Forms.DrawItemEventArgs) Handles LIB_PAYS.DrawItem If e.Index <> -1 Then e.Graphics.DrawImage(Me.ImageList1.Images(e.Index), e.Bounds.Left, e.Bounds.Top) e.Graphics.DrawString(Me.LIB_PAYS.Items(e.Index).ToString, _ Me.LIB_PAYS.Font, _ System.Drawing.Brushes.Black, _ New RectangleF(e.Bounds.X + 15, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height)) e.DrawFocusRectangle() End If End Sub
I know that my ComboBox populated with BO
but it still not working so I turn to you to find out how? is what my code or code that I found wrong?
Here is the result that shows me:
cordially Mimouni
|
By StrataFrame Team - 2/20/2013
Doing owner draw overrides all of the formatting logic for the combo box. When you use the SF list population, we set the DataSource as a DataTable and set the DisplayMember and ValueMember. The DisplayMember is basically ignored when you turn on owner draw, so you have to do it all manually. You're really close, but you're problem is here: e.Graphics.DrawString(Me.LIB_PAYS.Items(e.Index).ToString, _ Me.LIB_PAYS.Font, _ System.Drawing.Brushes.Black, _ New RectangleF(e.Bounds.X + 15, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
The items collection is just returning a System.Data.DataRow, and it's ToString() method just returns "System.Data.DataRow". You need to cast it as a data row, pull your field, then to string the field value:
e.Graphics.DrawString(CType(Me.LIB_PAYS.Items(e.Index), DataRow)("FieldName").ToString, _ Me.LIB_PAYS.Font, _ System.Drawing.Brushes.Black, _ New RectangleF(e.Bounds.X + 15, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
Replace "FieldName" with what you need and you should be good to go.
|
By Mimouni Mohamed - 2/20/2013
Ben, thank you for the reply.
I applied your solution, I get an error : Unable to cast object of type 'System.Data.DataRowView' to type 'System.Data.DataRow'. I changed DataRow and DataRowView by in this case he displays another message : LIB_PAYS is not a DataColumn DataRelation or for the table.
|
By StrataFrame Team - 2/21/2013
You're right, the IListSource.GetList() of the DataTable returns the DataView. And if you're building the combo box off of the SF list population, we create the table with 2 or 3 values (depending upon whether you have a columns in your drop down):
display | dropdown | value So, try this: e.Graphics.DrawString(CType(Me.LIB_PAYS.Items(e.Index), DataRowView)("display").ToString, _ Me.LIB_PAYS.Font, _ System.Drawing.Brushes.Black, _ New RectangleF(e.Bounds.X + 15, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
|
By Mimouni Mohamed - 2/21/2013
Think you very match ben
|
|