Solved

DataGridView / ComboBox Column?

Posted on 2016-11-04
15
27 Views
Last Modified: 2016-11-16
I'm using vb.net. I have a non bound DataGridView. I created a ComboBox column and added the values in the Items Collections property. I did this in the designer. I set the Column to be centered, and it does display it Centered. But How can I get the ComboBox List to appear centered when selecting an Item form the list?
0
Comment
Question by:Jess31
  • 8
  • 7
15 Comments
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 41874929
From https://social.msdn.microsoft.com/Forums/en-US/e5d22714-7d11-468b-b119-953be31bcc0c/how-to-put-the-text-in-the-center-in-combobox-in-vb?forum=vbgeneral

attach this sub to your combo box drawItem Event
  Private Sub CenteredComboBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles Me.DrawItem
    e.DrawBackground()
    Dim txt As String = ""
    If e.Index >= 0 Then txt = Me.Items(e.Index).ToString
    TextRenderer.DrawText(e.Graphics, txt, e.Font, e.Bounds, e.ForeColor, TextFormatFlags.HorizontalCenter)
    e.DrawFocusRectangle()
  End Sub
End Class

Open in new window

0
 

Author Comment

by:Jess31
ID: 41881219
I don't understand how would i use/attach this to my DataGridView ComboBoxColumn?
0
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 41881345
use this code to attach the event:

'attach the below to EditcontrolShowing of your datagridview.
Private Sub dgv_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs)
	Dim index As Integer = dgv.CurrentCell.ColumnIndex

       'if the column index is the combocolumn you want to center
	If index = FormatColumnIndex Then
              'ensure I get the combobox.
		Dim combobox = TryCast(e.Control, ComboBox)
		If combobox Is Nothing Then
			Return
		End If
              'set the draw mode.
		combobox.DrawMode = DrawMode.OwnerDrawFixed
              'clear the handler, if one existed.
                RemoveHandler combobox.DrawItem, AddressOf CenteredComboBox_DrawItem
              'add the draw handler.
		AddHandler combobox.DrawItem, AddressOf CenteredComboBox_DrawItem
	End If
End Sub

Open in new window

0
 

Author Comment

by:Jess31
ID: 41888295
on the Sub CenteredComboBox_DrawItem, I get an error on "Me.DrawItem"

Also on Items(e.Index).ToString I get an error on the me.Items
0
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 41888471
Try the following instead:

 
   Private Sub CenteredComboBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) 
    Dim combo as ComboBox = Ctype(sender, ComboBox)
    e.DrawBackground()
    Dim txt As String = ""
    If e.Index >= 0 Then 
        txt = combo.Items(e.Index).ToString
    end if
    TextRenderer.DrawText(e.Graphics, txt, e.Font, e.Bounds, e.ForeColor, TextFormatFlags.HorizontalCenter)
    e.DrawFocusRectangle()
  End Sub

Open in new window

0
 

Author Comment

by:Jess31
ID: 41889780
Still not working. Does not even get to CenteredComboBox_DrawItem.
Here is the code
    Private Sub DGVGuidesInResults_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) _
        Handles DGVGuidesInResults.EditingControlShowing
        Try
            If DGVGuidesInResults.Columns(DGVGuidesInResults.CurrentCell.ColumnIndex).Name = "GuideSortRank" Then
                Dim combo As ComboBox = CType(e.Control, ComboBox)
                If (combo IsNot Nothing) Then

                                  '
                    RemoveHandler Combo.DrawItem, AddressOf CenteredComboBox_DrawItem
                    AddHandler Combo.DrawItem, AddressOf CenteredComboBox_DrawItem

                End If
            End If
        Catch ex As Exception
            MsgBox(ex.Message, "DGVGuidesInResults_EditingControlShowing")
        End Try
    End Sub
    Private Sub CenteredComboBox_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) 'Handles me.DrawItem

        Dim combo As ComboBox = CType(sender, ComboBox)
        e.DrawBackground()
        Dim txt As String = ""
        If e.Index >= 0 Then
            txt = combo.Items(e.Index).ToString
        End If
        TextRenderer.DrawText(e.Graphics, txt, e.Font, e.Bounds, e.ForeColor, TextFormatFlags.HorizontalCenter)
        e.DrawFocusRectangle()

    End Sub

Open in new window

0
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 41890071
Can you confirm you're attaching the handler?  Are you able to find the combobox?
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:Jess31
ID: 41890148
I know that it never reaches  CenteredComboBox_DrawItem sub
0
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 41890211
I understand that.  But in order to reach the centeredComboBox sub we need to first ensure that the handler is being added.

Can you set a break point on the AddHandler line to ensure that it's actually getting called?  If it is, try actually clicking on the dropdown to see if that will cause the event to fire.  If it's not, then we need to trace why.
0
 

Author Comment

by:Jess31
ID: 41890243
the combo works fine. And if I break on line 6 in the immediate windows I can get result for combo.selectedvalue. But how can I know if the handler is getting created or just not being called?
0
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 41890390
Set a break on this line:
AddHandler Combo.DrawItem, AddressOf CenteredComboBox_DrawItem


If you hit the break point there it's definitely being added.  If not set a break point at the top of the function and trace thru, let me know what's preventing it from getting to that line.
0
 

Author Comment

by:Jess31
ID: 41890453
yes, it reaches that line (AddHandler) but it never does enter the AddressOf sub
0
 
LVL 39

Accepted Solution

by:
Kyle Abrahams earned 500 total points
ID: 41890475
Above the add handler add the following line:

combo.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawVariable

Need to tell it we are overriding the drawing.
0
 

Author Comment

by:Jess31
ID: 41890516
Yes, not it works.
what does the line do?
0
 
LVL 39

Expert Comment

by:Kyle Abrahams
ID: 41890533
It's a flag to tell it to fire the handler.  Without it the handler doesn't fire even if you attach one.

https://msdn.microsoft.com/en-us/library/system.windows.forms.drawmode(v=vs.110).aspx
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now