2016-11-21 85 views
0

我有自定義的「組合框」和我使用的是阿拉伯語,我需要它從右到左,在默認的「組合框」我可以把它從性質改變,但在這個定製的「組合框」其沒有改變,所以我怎樣才能修改這個類,使它從右到左?組合框項目左

這是我使用的是什麼:

Class AdvancedComboBox 
Inherits ComboBox 
    Public Shadows Property DrawMode() As System.Windows.Forms.DrawMode 
     Get 
      Return m_DrawMode 
     End Get 
     Set 
      m_DrawMode = Value 
     End Set 
    End Property 
    Private Shadows m_DrawMode As System.Windows.Forms.DrawMode 
    Public Property HighlightColor() As Color 
     Get 
      Return m_HighlightColor 
     End Get 
     Set 
      m_HighlightColor = Value 
     End Set 
    End Property 
    Private m_HighlightColor As Color 

    Public Sub New() 
     MyBase.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed 
     Me.HighlightColor = Color.Orange 
     AddHandler Me.DrawItem, New DrawItemEventHandler(AddressOf AdvancedComboBox_DrawItem) 
    End Sub 

    Private Sub AdvancedComboBox_DrawItem(sender As Object, e As DrawItemEventArgs) 
     If e.Index < 0 Then 
      Return 
     End If 

     Dim combo As ComboBox = TryCast(sender, ComboBox) 
     If (e.State And DrawItemState.Selected) = DrawItemState.Selected Then 
      e.Graphics.FillRectangle(New SolidBrush(HighlightColor), e.Bounds) 
     Else 
      e.Graphics.FillRectangle(New SolidBrush(combo.BackColor), e.Bounds) 
     End If 

     e.Graphics.DrawString(combo.Items(e.Index).ToString(), e.Font, New SolidBrush(combo.ForeColor), New Point(e.Bounds.X, e.Bounds.Y)) 

     e.DrawFocusRectangle() 
    End Sub 
End Class 

回答

3

目前還不清楚爲什麼在屬性框中RightToLeft屬性不會粘住你,但由於你是自定義繪圖,因此應該使用右對齊的屬性。此外,最好使用TextRenderer代替的DrawString以匹配其他控件所使用的渲染器:

'e.Graphics.DrawString(combo.Items(e.Index).ToString(), e.Font, 
'      New SolidBrush(combo.ForeColor), New Point(e.Bounds.X, e.Bounds.Y)) 

TextRenderer.DrawText(e.Graphics, combo.Items(e.Index).ToString, e.Font, e.Bounds, 
         combo.ForeColor, Color.Empty, 
         TextFormatFlags.Right Or TextFormatFlags.RightToLeft) 
+0

謝謝你的完美,但如果你能幫助我,我將是偉大的充分,這個問題有點事在默認'combobox'其還組織(文本對齊方式),當在阿拉伯語和英語的用戶類型顯示爲用戶類型,並保存在數據庫中,現在在定製'combobox'如果我有3個字,如:Arabic1數據庫中的英語阿拉伯語2在自定義'combobox'中顯示如下:Arabic2 English Arabic1。 順便說一句,我在一行顯示2個值: 'ComboBox2.Items.Add(String.Format(「{0} |({1})」,p_id,p_name))' –

+0

@Rabeeaqabaha我道歉,但我正在努力處理您在評論中提出的問題。如果你想要三個項目,那麼它只是'String.Format(「{0} {1} {2}」,p_id,p_name,p_other)',但我懷疑這不是你面臨的問題。 – LarsTech

+0

我的問題是文本結構不正確,例如: 可以說用戶在數據庫中添加了3個單詞阿拉伯語和1個英語單詞,如[word1(Arabic1)word2(English)word3(Arabic2)] ', '組合框'顯示它就像這樣'[word3(Arabic2)word2(English)word1(Arabic1)]' –

0

如果它永遠是從右到左,那麼你可以在AdvancedComboBox構造做到這一點:

MyBase.RightToLeft = Windows.Forms.RightToLeft.Yes