choose fonts in a C# application, beginner

Hi,

I'm new in C# (and also to programming in general), so I decided to make a really simple text editor. It is simply rich text box with a possibillity to choose fonts and thier size - nothing more.

Can someone help me how to make this work. I've tried many things but it doesn't seem to work for me.

What I want is a rich text box and two "controlls".

1: to be able to choose fonts from a scroll down list

2: choose size of the fonts with a simmillar scroll down list.

I've tried to choose size with a trackbar and it works great, and now I would like to have scroll down list with some sizes (like in Word)

Please help me some information regarding this.

//Lars




Answer this question

choose fonts in a C# application, beginner

  • alex_g_73

    Here is a sample application just to get you started:

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    /// <summary>

    /// When loading the form, we fill the lists with values

    /// </summary>

    /// <param name="sender"></param>

    /// <param name="e"></param>

    private void Form1_Load(object sender, EventArgs e)

    {

    // Size list items

    lstSize.Items.Add(5);

    lstSize.Items.Add(6);

    lstSize.Items.Add(7);

    lstSize.Items.Add(8);

    lstSize.Items.Add(10);

    lstSize.Items.Add(12);

    lstSize.Items.Add(14);

    lstSize.Items.Add(16);

    lstSize.SelectedIndex = 0;

    // Font list items

    lstFont.Items.Add("David");

    lstFont.Items.Add("Arial");

    lstFont.Items.Add("Times New Roman");

    lstFont.SelectedIndex = 0;

    }

    private void lstSize_SelectedIndexChanged(object sender, EventArgs e)

    {

    int size = (int)lstSize.SelectedItem;

    richTextBox1.Font =

    new Font(richTextBox1.Font.Name, (float)size);

    }

    private void lstFont_SelectedIndexChanged(object sender, EventArgs e)

    {

    string fontName = lstFont.SelectedItem.ToString();

    richTextBox1.Font =

    new Font(fontName, richTextBox1.Font.Size);

    }

    /// <summary>

    /// Required designer variable.

    /// </summary>

    private System.ComponentModel.IContainer components = null;

    /// <summary>

    /// Clean up any resources being used.

    /// </summary>

    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>

    protected override void Dispose(bool disposing)

    {

    if (disposing && (components != null))

    {

    components.Dispose();

    }

    base.Dispose(disposing);

    }

    #region Windows Form Designer generated code

    /// <summary>

    /// Required method for Designer support - do not modify

    /// the contents of this method with the code editor.

    /// </summary>

    private void InitializeComponent()

    {

    this.lstSize = new System.Windows.Forms.ComboBox();

    this.lstFont = new System.Windows.Forms.ComboBox();

    this.richTextBox1 = new System.Windows.Forms.RichTextBox();

    this.SuspendLayout();

    //

    // lstSize

    //

    this.lstSize.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

    this.lstSize.FormattingEnabled = true;

    this.lstSize.Location = new System.Drawing.Point(34, 12);

    this.lstSize.Name = "lstSize";

    this.lstSize.Size = new System.Drawing.Size(121, 21);

    this.lstSize.TabIndex = 0;

    this.lstSize.SelectedIndexChanged += new System.EventHandler(this.lstSize_SelectedIndexChanged);

    //

    // lstFont

    //

    this.lstFont.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;

    this.lstFont.FormattingEnabled = true;

    this.lstFont.Location = new System.Drawing.Point(161, 12);

    this.lstFont.Name = "lstFont";

    this.lstFont.Size = new System.Drawing.Size(121, 21);

    this.lstFont.TabIndex = 1;

    this.lstFont.SelectedIndexChanged += new System.EventHandler(this.lstFont_SelectedIndexChanged);

    //

    // richTextBox1

    //

    this.richTextBox1.Location = new System.Drawing.Point(34, 52);

    this.richTextBox1.Name = "richTextBox1";

    this.richTextBox1.Size = new System.Drawing.Size(521, 182);

    this.richTextBox1.TabIndex = 2;

    this.richTextBox1.Text = "Bla Bla Bla";

    //

    // Form1

    //

    this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);

    this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;

    this.ClientSize = new System.Drawing.Size(584, 246);

    this.Controls.Add(this.richTextBox1);

    this.Controls.Add(this.lstFont);

    this.Controls.Add(this.lstSize);

    this.Name = "Form1";

    this.Text = "Form1";

    this.Load += new System.EventHandler(this.Form1_Load);

    this.ResumeLayout(false);

    }

    #endregion

    private System.Windows.Forms.ComboBox lstSize;

    private System.Windows.Forms.ComboBox lstFont;

    private System.Windows.Forms.RichTextBox richTextBox1;

    }



  • taucher

    Great,

    I'll study this carefully.

    Thank you.



  • choose fonts in a C# application, beginner