[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 251
  • Last Modified:

How do I change fonts and sizes using the following code and a button

With the following code, HOW do I change the font and the size? Complete nooB

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(listBox1.SelectedIndex == 0)
            {
                MessageBox.Show("You selected the first item at index 0");
                //do something
            }
            else if(listBox1.SelectedIndex == 1)
            {
                MessageBox.Show("You selected the second item at index 1");
                   //do something
            }
            else if(listBox1.SelectedIndex == 2)
            {
                MessageBox.Show("You selected the third item at index 2");
                   //do something
            }
            else
            {
                MessageBox.Show("You selected the fourth item at index 3");
                //(listBox1.SelectedIndex == 3)
                   //do something
            }
        }

0
Calentador
Asked:
Calentador
  • 3
  • 2
2 Solutions
 
SStoryCommented:
Fonts and sizes for what?

Assuming you have a control, a textbox and want to change the font size

txtName.Font=new Font(<fill in params here>);

There are many possible constructors for the Font object so choose whichever your like.

If this isn't what you want, then look here:
http://cyotek.com/article/display/creating-a-wysiwyg-font-combobox-using-csharp
0
 
CalentadorAuthor Commented:
I apologize. I am writing a fontselector program similar to the following.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace FontSelector
{
      /// <summary>
      /// Summary description for Form1.
      /// </summary>
      public class Form1 : System.Windows.Forms.Form
      {
            private System.Windows.Forms.ListBox listBox1;
            private System.Windows.Forms.ListBox listBox2;
            private System.Windows.Forms.ListBox listBox3;
            private System.Windows.Forms.Label label1;
            private System.Windows.Forms.Label label2;
            private System.Windows.Forms.Label label3;
            private System.Windows.Forms.Label label4;
            private System.Windows.Forms.Button button1;
            /// <summary>
            /// Required designer variable.
            /// </summary>
            private System.ComponentModel.Container components = null;

            public Form1()
            {
                  //
                  // Required for Windows Form Designer support
                  //
                  InitializeComponent();

                  //
                  // TODO: Add any constructor code after InitializeComponent call
                  //
            }

            /// <summary>
            /// Clean up any resources being used.
            /// </summary>
            protected override void Dispose( bool disposing )
            {
                  if( disposing )
                  {
                        if (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.listBox1 = new System.Windows.Forms.ListBox();
                  this.listBox2 = new System.Windows.Forms.ListBox();
                  this.listBox3 = new System.Windows.Forms.ListBox();
                  this.label1 = new System.Windows.Forms.Label();
                  this.label2 = new System.Windows.Forms.Label();
                  this.label3 = new System.Windows.Forms.Label();
                  this.label4 = new System.Windows.Forms.Label();
                  this.button1 = new System.Windows.Forms.Button();
                  this.SuspendLayout();
                  //
                  // listBox1
                  //
                  this.listBox1.AllowDrop = true;
                  this.listBox1.Items.AddRange(new object[] {
                                                                                      "Times New Roman",
                                                                                      "Arial",
                                                                                      "Comic Sans MS",
                                                                                      "Georgia"});
                  this.listBox1.Location = new System.Drawing.Point(24, 72);
                  this.listBox1.Name = "listBox1";
                  this.listBox1.Size = new System.Drawing.Size(120, 95);
                  this.listBox1.TabIndex = 0;
                  this.listBox1.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
                  //
                  // listBox2
                  //
                  this.listBox2.AllowDrop = true;
                  this.listBox2.Items.AddRange(new object[] {
                                                                                      "8",
                                                                                      "12",
                                                                                      "20",
                                                                                      "25"});
                  this.listBox2.Location = new System.Drawing.Point(216, 72);
                  this.listBox2.Name = "listBox2";
                  this.listBox2.Size = new System.Drawing.Size(40, 95);
                  this.listBox2.TabIndex = 1;
                  //
                  // listBox3
                  //
                  this.listBox3.AllowDrop = true;
                  this.listBox3.Items.AddRange(new object[] {
                                                                                      "Red",
                                                                                      "Blue"});
                  this.listBox3.Location = new System.Drawing.Point(336, 72);
                  this.listBox3.Name = "listBox3";
                  this.listBox3.Size = new System.Drawing.Size(88, 95);
                  this.listBox3.TabIndex = 2;
                  //
                  // label1
                  //
                  this.label1.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
                  this.label1.Location = new System.Drawing.Point(40, 32);
                  this.label1.Name = "label1";
                  this.label1.TabIndex = 3;
                  this.label1.Text = "Font";
                  //
                  // label2
                  //
                  this.label2.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
                  this.label2.Location = new System.Drawing.Point(216, 32);
                  this.label2.Name = "label2";
                  this.label2.TabIndex = 4;
                  this.label2.Text = "Size";
                  //
                  // label3
                  //
                  this.label3.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
                  this.label3.Location = new System.Drawing.Point(352, 32);
                  this.label3.Name = "label3";
                  this.label3.TabIndex = 5;
                  this.label3.Text = "Color";
                  //
                  // label4
                  //
                  this.label4.BackColor = System.Drawing.Color.White;
                  this.label4.Font = new System.Drawing.Font("Microsoft Sans Serif", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
                  this.label4.ForeColor = System.Drawing.Color.Blue;
                  this.label4.Location = new System.Drawing.Point(136, 192);
                  this.label4.Name = "label4";
                  this.label4.Size = new System.Drawing.Size(208, 56);
                  this.label4.TabIndex = 6;
                  this.label4.Text = "C# is fun!";
                  this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
                  //
                  // button1
                  //
                  this.button1.Location = new System.Drawing.Point(160, 296);
                  this.button1.Name = "button1";
                  this.button1.Size = new System.Drawing.Size(136, 32);
                  this.button1.TabIndex = 7;
                  this.button1.Text = "Show Text";
                  //
                  // Form1
                  //
                  this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
                  this.ClientSize = new System.Drawing.Size(456, 350);
                  this.Controls.Add(this.button1);
                  this.Controls.Add(this.label4);
                  this.Controls.Add(this.label3);
                  this.Controls.Add(this.label2);
                  this.Controls.Add(this.label1);
                  this.Controls.Add(this.listBox3);
                  this.Controls.Add(this.listBox2);
                  this.Controls.Add(this.listBox1);
                  this.Name = "Form1";
                  this.Text = "Font Selectore Form";
                  this.ResumeLayout(false);

            }
            #endregion

            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                  Application.Run(new Form1());
            }

            private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
            {
           
            }
      }
}
0
 
SStoryCommented:
OK. Did you look at the link I sent you? It seems to do this exactly?

My question is change the font of what?  You said when something is selected you want to change the font.  There are many fonts on an application. The main form has a font and many controls like Label, textbox, etc have a font property. To what were you referring?
0
 
CalentadorAuthor Commented:
Thank you for your help. The link will help in the future, but not right now. I want to change the font and size of the displayed item from my choices in the list. If i choose Arial 20 pt. I want a display showing "Hello" in Arial 20 pt.
0
 
SStoryCommented:
I see. What you are wanting to do is called an owner drawn combo box. That means instead of DotNet/Windows drawing the text you will take full control of that. It will fire an event when the text needs drawing.  in the e parameter of that event will be a graphics object you can draw with. You will get the bounds also from the e param. That is the space you have to work in.  I think it will be hard to do with the usual combo as the size will always be what the combo was originally. Drawing a specific font type will not be a problem. Hope this helps.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now