Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. TextBox text color

TextBox text color

Scheduled Pinned Locked Moved C#
graphicsquestion
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Paddy

    Does anyone know if it is possible to change the colour of the text in a text box? I have tried textBox1.forecolor = System.drawing.color.red; and also changing the color in the textbox properties but it doesn't seem to work! Am I missing something? Thanks Paddy

    N Offline
    N Offline
    Nick Parker
    wrote on last edited by
    #2

    Hmmm... Your question caught me as setting the ForeColor property in the designer should work, in fact it works on my box :confused:. However if you would like to change the color programtically you can do:

    this.textBox1.ForeColor = Color.Aqua;
    

    You should see call kinds of different colors in the intellisence once you type Color. You may also need to prepend this. in front of your textBox1 HTH Nick Parker

    P 1 Reply Last reply
    0
    • N Nick Parker

      Hmmm... Your question caught me as setting the ForeColor property in the designer should work, in fact it works on my box :confused:. However if you would like to change the color programtically you can do:

      this.textBox1.ForeColor = Color.Aqua;
      

      You should see call kinds of different colors in the intellisence once you type Color. You may also need to prepend this. in front of your textBox1 HTH Nick Parker

      P Offline
      P Offline
      Paddy
      wrote on last edited by
      #3

      Thanks for the quick reply Nick. I don't know why it wont work for me! Do you need to add Using Systems.color or anything like that at the start of your code? Paddy

      N 1 Reply Last reply
      0
      • P Paddy

        Thanks for the quick reply Nick. I don't know why it wont work for me! Do you need to add Using Systems.color or anything like that at the start of your code? Paddy

        N Offline
        N Offline
        Nick Parker
        wrote on last edited by
        #4

        Try pasting this code in exactly, I just made this up really quickly.

        using System;
        using System.Drawing;
        using System.Collections;
        using System.ComponentModel;
        using System.Windows.Forms;
        using System.Data;
        
        namespace ChangeColor
        {
        	/// 
        	/// Summary description for Form1.
        	/// 
        	public class Form1 : System.Windows.Forms.Form
        	{
        		private System.Windows.Forms.TextBox textBox1;
        		private System.Windows.Forms.Button button1;
        		private System.Windows.Forms.Button button2;
        		/// 
        		/// Required designer variable.
        		/// 
        		private System.ComponentModel.Container components = null;
        
        		public Form1()
        		{
        			//
        			// Required for Windows Form Designer support
        			//
        			InitializeComponent();
        
        			//
        			// TODO: Add any constructor code after InitializeComponent call
        			//
        		}
        
        		/// 
        		/// Clean up any resources being used.
        		/// 
        		protected override void Dispose( bool disposing )
        		{
        			if( disposing )
        			{
        				if (components != null) 
        				{
        					components.Dispose();
        				}
        			}
        			base.Dispose( disposing );
        		}
        
        		#region Windows Form Designer generated code
        		/// 
        		/// Required method for Designer support - do not modify
        		/// the contents of this method with the code editor.
        		/// 
        		private void InitializeComponent()
        		{
        			this.textBox1 = new System.Windows.Forms.TextBox();
        			this.button1 = new System.Windows.Forms.Button();
        			this.button2 = new System.Windows.Forms.Button();
        			this.SuspendLayout();
        			// 
        			// textBox1
        			// 
        			this.textBox1.ForeColor = System.Drawing.Color.Aqua;
        			this.textBox1.Location = new System.Drawing.Point(72, 80);
        			this.textBox1.Name = "textBox1";
        			this.textBox1.Size = new System.Drawing.Size(104, 20);
        			this.textBox1.TabIndex = 0;
        			this.textBox1.Text = "textBox1";
        			// 
        			// button1
        			// 
        			this.button1.Location = new System.Drawing.Point(16, 128);
        			this.button1.Name = "button1";
        			this.button1.Size = new System.Drawing.Size(104, 24);
        			this.button1.TabIndex = 1;
        			this.button1.Text = "Change To Red";
        			this.button1.Click += new System.EventHandler(this.button1_Click);
        			// 
        			// button2
        			// 
        			this.button2.Location = new System.Drawing.Point(128, 128);
        			this.button2.Name = "button2";
        			this.button2.Size = new System.Drawing.Size(104, 24);
        			this.button2.TabIndex = 2;
        			this.button2.Text = "Change To Green";
        			this.button2.Click += new System.EventHandler(this.button2_Click);
        
        P 1 Reply Last reply
        0
        • N Nick Parker

          Try pasting this code in exactly, I just made this up really quickly.

          using System;
          using System.Drawing;
          using System.Collections;
          using System.ComponentModel;
          using System.Windows.Forms;
          using System.Data;
          
          namespace ChangeColor
          {
          	/// 
          	/// Summary description for Form1.
          	/// 
          	public class Form1 : System.Windows.Forms.Form
          	{
          		private System.Windows.Forms.TextBox textBox1;
          		private System.Windows.Forms.Button button1;
          		private System.Windows.Forms.Button button2;
          		/// 
          		/// Required designer variable.
          		/// 
          		private System.ComponentModel.Container components = null;
          
          		public Form1()
          		{
          			//
          			// Required for Windows Form Designer support
          			//
          			InitializeComponent();
          
          			//
          			// TODO: Add any constructor code after InitializeComponent call
          			//
          		}
          
          		/// 
          		/// Clean up any resources being used.
          		/// 
          		protected override void Dispose( bool disposing )
          		{
          			if( disposing )
          			{
          				if (components != null) 
          				{
          					components.Dispose();
          				}
          			}
          			base.Dispose( disposing );
          		}
          
          		#region Windows Form Designer generated code
          		/// 
          		/// Required method for Designer support - do not modify
          		/// the contents of this method with the code editor.
          		/// 
          		private void InitializeComponent()
          		{
          			this.textBox1 = new System.Windows.Forms.TextBox();
          			this.button1 = new System.Windows.Forms.Button();
          			this.button2 = new System.Windows.Forms.Button();
          			this.SuspendLayout();
          			// 
          			// textBox1
          			// 
          			this.textBox1.ForeColor = System.Drawing.Color.Aqua;
          			this.textBox1.Location = new System.Drawing.Point(72, 80);
          			this.textBox1.Name = "textBox1";
          			this.textBox1.Size = new System.Drawing.Size(104, 20);
          			this.textBox1.TabIndex = 0;
          			this.textBox1.Text = "textBox1";
          			// 
          			// button1
          			// 
          			this.button1.Location = new System.Drawing.Point(16, 128);
          			this.button1.Name = "button1";
          			this.button1.Size = new System.Drawing.Size(104, 24);
          			this.button1.TabIndex = 1;
          			this.button1.Text = "Change To Red";
          			this.button1.Click += new System.EventHandler(this.button1_Click);
          			// 
          			// button2
          			// 
          			this.button2.Location = new System.Drawing.Point(128, 128);
          			this.button2.Name = "button2";
          			this.button2.Size = new System.Drawing.Size(104, 24);
          			this.button2.TabIndex = 2;
          			this.button2.Text = "Change To Green";
          			this.button2.Click += new System.EventHandler(this.button2_Click);
          
          P Offline
          P Offline
          Paddy
          wrote on last edited by
          #5

          Thanks for the help. I'm in poxy work at the moment with no visual studio so I wont get a chance to try adding it to my code till lunch time! Do you know if the textBox.ForeColor changes all the text in the texbox or can I make different words different colors by changing the forecolor before I append text to the texbox? Thanks, Paddy.

          R 1 Reply Last reply
          0
          • P Paddy

            Thanks for the help. I'm in poxy work at the moment with no visual studio so I wont get a chance to try adding it to my code till lunch time! Do you know if the textBox.ForeColor changes all the text in the texbox or can I make different words different colors by changing the forecolor before I append text to the texbox? Thanks, Paddy.

            R Offline
            R Offline
            Rocky Moore
            wrote on last edited by
            #6

            Paddy wrote: Do you know if the textBox.ForeColor changes all the text in the texbox or can I make different words different colors by changing the forecolor before I append text to the texbox? If you wish to use different colors for different words you would have to use a control such as the RichTextBox control which supports formatting. The text field is only a single color unless you make your own text control and draw it yourself. Rocky Moore

            P 1 Reply Last reply
            0
            • R Rocky Moore

              Paddy wrote: Do you know if the textBox.ForeColor changes all the text in the texbox or can I make different words different colors by changing the forecolor before I append text to the texbox? If you wish to use different colors for different words you would have to use a control such as the RichTextBox control which supports formatting. The text field is only a single color unless you make your own text control and draw it yourself. Rocky Moore

              P Offline
              P Offline
              Paddy
              wrote on last edited by
              #7

              Thanks for the help, Is it difficult to implement a RichTextBox control? Paddy

              R 1 Reply Last reply
              0
              • P Paddy

                Thanks for the help, Is it difficult to implement a RichTextBox control? Paddy

                R Offline
                R Offline
                Rickard Andersson20
                wrote on last edited by
                #8

                You just declare a RichTextBox object or you could find it in the Toolbox. To change the color/font or something you just use SelectionFont/SelectionColor property to change the color/font of selected text. Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C# and C++!

                P 1 Reply Last reply
                0
                • R Rickard Andersson20

                  You just declare a RichTextBox object or you could find it in the Toolbox. To change the color/font or something you just use SelectionFont/SelectionColor property to change the color/font of selected text. Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C# and C++!

                  P Offline
                  P Offline
                  Paddy
                  wrote on last edited by
                  #9

                  Thanks Rickard, I'll give that a try later, doesn't sound to difficult. Paddy

                  R 1 Reply Last reply
                  0
                  • P Paddy

                    Thanks Rickard, I'll give that a try later, doesn't sound to difficult. Paddy

                    R Offline
                    R Offline
                    Rickard Andersson20
                    wrote on last edited by
                    #10

                    Paddy wrote: doesn't sound to difficult. Believe me! It's too easy! :) Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C# and C++!

                    P 1 Reply Last reply
                    0
                    • R Rickard Andersson20

                      Paddy wrote: doesn't sound to difficult. Believe me! It's too easy! :) Rickard Andersson@Suza Computing C# and C++ programmer from SWEDEN! UIN: 50302279 E-Mail: nikado@pc.nu Speciality: I love C# and C++!

                      P Offline
                      P Offline
                      Paddy
                      wrote on last edited by
                      #11

                      Thanks I'll give it a try when I get out here!

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups