开发者

change label font at run-time gives error

i want to change the font of all labels of a web page. I write the follow开发者_开发技巧ing code but it gives error that font is read only property. I need it very urgent to do . Can anyone help

Code is here

 Dim ctrl As Control
        Dim fnt As New Font("Verdana", 8, FontStyle.Bold, GraphicsUnit.Point)

        For Each ctrl In Me.Controls
            If (TypeOf ctrl Is Label) Then
                lbl = CType(ctrl, Label)
                lbl.Font = fnt
            End If
        Next

thanks in advance


I think you are mixing Winforms and ASP.NET. You said that you want to change the font of labels on your WebPage, so i assume that it's an ASP.NET-Webaplication.

I strongly recommend to use CSS instead. But if you need something working fast, you should replace your code with this:

 Public Sub ApplyFontStyleRecursively(ByVal parentControl As System.Web.UI.Control, ByVal fontInfo As FontInfo)
    If TypeOf parentControl Is Label Then
        DirectCast(parentControl, Label).Font.CopyFrom(fontInfo)
    End If

    For Each c As System.Web.UI.Control In parentControl.Controls
        ApplyFontStyleRecursively(c, fontInfo)
    Next
End Sub

and call it:

Dim lbl As New Label
lbl.Font.Name = "Verdana"
lbl.Font.Bold = True
ApplyFontStyleRecursively(Me.Page, lbl.Font)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜