Bagikan melalui


RichTextBox.SelectionFont Properti

Definisi

Mendapatkan atau mengatur font dari pilihan teks atau titik penyisipan saat ini.

public:
 property System::Drawing::Font ^ SelectionFont { System::Drawing::Font ^ get(); void set(System::Drawing::Font ^ value); };
[System.ComponentModel.Browsable(false)]
public System.Drawing.Font SelectionFont { get; set; }
[System.ComponentModel.Browsable(false)]
public System.Drawing.Font? SelectionFont { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.SelectionFont : System.Drawing.Font with get, set
Public Property SelectionFont As Font

Nilai Properti

Font yang mewakili font untuk diterapkan ke pilihan teks saat ini atau ke teks yang dimasukkan setelah titik penyisipan.

Atribut

Contoh

Contoh kode berikut mengubah pengaturan gaya tebal font saat ini untuk pemilihan teks atau teks yang dimasukkan setelah titik penyisipan dalam RichTextBox kontrol. Contoh ini mengharuskan kode terkandung dalam metode dalam Form. Contoh ini juga mengharuskan RichTextBoxbahwa , bernama richTextBox1, telah ditambahkan ke Form.

void ToggleBold()
{
   if ( richTextBox1->SelectionFont != nullptr )
   {
      System::Drawing::Font^ currentFont = richTextBox1->SelectionFont;
      System::Drawing::FontStyle newFontStyle;
      if (richTextBox1->SelectionFont->Bold)
      {
         newFontStyle = FontStyle::Regular;
      }
      else
      {
         newFontStyle = FontStyle::Bold;
      }
      richTextBox1->SelectionFont = gcnew System::Drawing::Font( currentFont->FontFamily,currentFont->Size,newFontStyle );
   }
}
private void ToggleBold()
{
   if (richTextBox1.SelectionFont != null)
   {
      System.Drawing.Font currentFont = richTextBox1.SelectionFont;
      System.Drawing.FontStyle newFontStyle;

      if (richTextBox1.SelectionFont.Bold)
      {
         newFontStyle = FontStyle.Regular;
      }
      else
      {
         newFontStyle = FontStyle.Bold;
      }

      richTextBox1.SelectionFont = new Font(
         currentFont.FontFamily, 
         currentFont.Size, 
         newFontStyle
      );
   }
}
Private Sub ToggleBold()
   If richTextBox1.SelectionFont IsNot Nothing Then
      Dim currentFont As System.Drawing.Font = richTextBox1.SelectionFont
      Dim newFontStyle As System.Drawing.FontStyle

      If richTextBox1.SelectionFont.Bold = True Then
         newFontStyle = FontStyle.Regular
      Else
         newFontStyle = FontStyle.Bold
      End If

      richTextBox1.SelectionFont = New Font( _
         currentFont.FontFamily, _
         currentFont.Size, _
         newFontStyle _
      )
   End If
End sub

Keterangan

Jika pilihan teks saat ini memiliki lebih dari satu font yang ditentukan, properti ini adalah null. Jika saat ini tidak ada teks yang dipilih, font yang ditentukan dalam properti ini diterapkan ke titik penyisipan saat ini dan ke semua teks yang diketik ke dalam kontrol setelah titik penyisipan. Pengaturan font berlaku hingga properti diubah ke font yang berbeda atau hingga titik penyisipan dipindahkan ke bagian lain dalam kontrol.

Jika teks dipilih dalam kontrol, teks yang dipilih dan teks apa pun yang dimasukkan setelah pemilihan teks akan memiliki nilai properti ini yang diterapkan padanya. Anda dapat menggunakan properti ini untuk mengubah gaya font teks di RichTextBox. Anda dapat membuat teks dalam kontrol tebal, miring, dan bergaris bawah. Anda juga dapat mengubah ukuran teks dan font yang diterapkan ke teks.

Untuk mengubah warna teks dalam kontrol, gunakan SelectionColor properti .

Berlaku untuk

Lihat juga