在Combobox中缩小字体 [英] Narrowing Font in Combobox

查看:89
本文介绍了在Combobox中缩小字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果Combobox1内的文本长度超过20个字符,如何将字体更改为窄?


我可以使用此命令更改字体大小:


'----保持字体大小为8,除非文本长度超过20个字符---------


如果Len(My_Form.Combobox1) )> 20然后My_Form.Combobox1.Font.Size = 6否则My_Form.Combobox1.Font.Size = 8


有没有办法??

解决方案

您好


可以尝试以下代码:

 Public Class Form1 
Private Sub Form1_Load(sender As Object,e As EventArgs)Handles MyBase.Load
ComboBox1.Items.Add(" 1")
ComboBox1.Items.Add( " 234")
ComboBox1.Items.Add(" abcdefg")
ComboBox1.DroppedDown = True
ComboBox1.DrawMode = DrawMode.OwnerDrawVariable
End Sub

Private Sub ComboBox1_DrawItem(sender As Object,e As DrawItemEve ntArgs)处理ComboBox1.DrawItem
Dim g As Graphics = e.Graphics
Dim bFont As Font = New Font(" Arial",8,FontStyle.Bold)
Dim lFont As Font = New Font(" Arial",10,FontStyle.Bold)


如果ComboBox1.Items(e.Index).ToString.Length> 5然后
g.DrawString(ComboBox1.Items(e.Index).ToString(),lFont,Brushes.Red,e.Bounds.X,e.Bounds.Y)
Else
g.DrawString(ComboBox1.Items(e.Index).ToString(),bFont,Brushes.Black,e.Bounds.X,e.Bounds.Y)
End if

End Sub

End Class

最好的问候,


Alex



How can i change font to narrow if length of text inside a Combobox1 exceeds 20 characters

i can change the font size by using this command:

'----keep font size at 8 unless the length of text exceeds 20 characters---------

if Len(My_Form.Combobox1)>20 then My_Form.Combobox1.Font.Size = 6 Else My_Form.Combobox1.Font.Size = 8

is there a way??

解决方案

Hi

Can try the code below:

Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ComboBox1.Items.Add("1")
        ComboBox1.Items.Add("234")
        ComboBox1.Items.Add("abcdefg")
        ComboBox1.DroppedDown = True
        ComboBox1.DrawMode = DrawMode.OwnerDrawVariable
    End Sub

    Private Sub ComboBox1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles ComboBox1.DrawItem
        Dim g As Graphics = e.Graphics
        Dim bFont As Font = New Font("Arial", 8, FontStyle.Bold)
        Dim lFont As Font = New Font("Arial", 10, FontStyle.Bold)


        If ComboBox1.Items(e.Index).ToString.Length > 5 Then
            g.DrawString(ComboBox1.Items(e.Index).ToString(), lFont, Brushes.Red, e.Bounds.X, e.Bounds.Y)
        Else
            g.DrawString(ComboBox1.Items(e.Index).ToString(), bFont, Brushes.Black, e.Bounds.X, e.Bounds.Y)
        End If

    End Sub

End Class

Best Regards,

Alex


这篇关于在Combobox中缩小字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆