2011-10-29 44 views
0

我真的失去了控制這個小問題,無法糾正它?錯誤:屬性字體在Asp.Net中爲只讀

任何人都可以幫助我!

這裏是我的代碼:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
    Dim installed_fonts As New InstalledFontCollection 
    lstFont.Items.Clear() 
    For Each font_family As FontFamily In installed_fonts.Families 
     lstFont.Items.Add(font_family.Name) 
    Next font_family 
    lstFont.SelectedIndex = 0 
End Sub 

Private Sub txtSize_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtSize.TextChanged, chkBold.Click, chkItalic.Click, chkUnderline.Click, chkStrikeout.Click, lstFont.SelectedIndexChanged 
     ShowSample() 
    End Sub 

私人小組ShowSample()

' Compose the font style. 
Dim font_style As FontStyle = FontStyle.Regular 
If Bold.Checked Then font_style = font_style Or FontStyle.Bold 
If Italic.Checked Then font_style = font_style Or FontStyle.Italic 
If Underline.Checked Then font_style = font_style Or FontStyle.Underline 


' Get the font size. 
Dim font_size As Single = 8 
Try 
    font_size = Single.Parse(DropFontSize.SelectedValue) 
Catch ex As Exception 
End Try 

' Get the font family name. 
Dim family_name As String = DropFont.SelectedIndex.ToString 
If Not (DropFont.SelectedItem Is Nothing) Then 
    family_name = DropFont.SelectedItem.ToString 
End If 
' Make the new font. 
Dim new_font As New Font(_ 
    family_name, font_size, font_style) 

' Set the sample's font. 
TextBox1.Font.Name = new_font.Name 
TextBox1.Font.Size = font_size 
TextBox1.Font.Bold = Bold.Checked 
TextBox1.Font.Italic = Italic.Checked 
TextBox1.Font.Underline = Underline.Checked 

末次

物業字體是隻讀的,我已經嘗試了許多片斷我得到擺脫這個問題,但沒有工作!

回答

1

你必須設置如下圖所示,WebControl.Font Property是隻讀

Private Sub ShowSample() 
    ..... 
    Dim new_font As New Font(_ 
     family_name, font_size, font_style) 

    ' Set the sample's font. 
    txtSample.Font.Name = new_font.Name 
    txtSample.Font.Size = Font.Size 
    txtSample.Font.Bold = chkBold.Checked 
    txtSample.Font.Italic = chkItalic.Checked 
    txtSample.Font.Underline = chkUnderline.Checked 
    txtSample.Font.Strikeout= chkStrikeout.Checked 

End Sub 
+0

我在哪裏添加這個? – coder

+0

@ user944919檢查我的更新 – Damith

+0

雖然我已經更新了我的代碼,但它並未顯示任何區別。 – coder

0

因爲財產只讀的,因爲每definition from MSDN

Public Overridable ReadOnly Property Font As FontInfo 
Get 

您將需要設置Font的性質,如Name

txtSample.Font.Name = new_font.Name 
+0

如果我這樣設置txtSample.Font.Name = new_font.size和txtSample.Font.size = new_font.Name,那麼沒有屬性,比如txtsample.font 。樣式。那麼我該如何設置呢? – coder

+0

你想用style屬性來完成什麼?直接在控件上有一個Style屬性(即txtSample.Style),但是它用於設置CSS屬性。 –