Bits & Bytes

Posts Tagged ‘text’

Creating a Simple Form to Display Text Messages in C#

Although it is possible to use a simple MessageBox for printing messages, it is sometimes convenient to use a window that you have more control over. Below, I have the C# code for a simple console application named “MyConsoleApplication.” I created the project using the Console Application template, which creates the empty Main() function shown below. The code file is named “Program.cs” for simplicity, but it could be named anything.

We need to add a reference to the assembly System.Windows.Forms, in order to be able create Forms in the code. We also need to add the corresponding using directive using System.Windows.Forms; at the top of the code file. Finally, we need to add a reference to the assembly System.Drawing in order to set the size of the Form in the line: qMyForm.ClientSize = qMyTextbox.Size;.

Beyond that, the code that I have added is all inside the Main() function. First, I create a Form and add the text “An Important Message” to the title bar. Next, I create a TextBox and set its size to 400 by 300 pixels. Then I set it to accept multiple lines of text, enable the vertical scrollbar to accommodate text overruns, and set it to be read only so that the text cannot be modified.

The middle block of code consists of several calls to the member function AppendText(). Each call adds a line of text from the Bible, Proverbs 4:10-13. The character sequence \u000D\u000A is the unicode representation of the carriage return and linefeed characters. So, that moves the text to the beginning of the next line. On a related note, the TextBox has word wrap enabled by default.

The third block of code sets the size of the containing Form to have a client area that is the same size as the TextBox. Then the TextBox is added to the Form, and the Form is displayed via a call to ShowDialog().

The output of the program looks like this:

Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FormStringOutput;
using System.Windows.Forms;

namespace MyConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            Form qMyForm = new Form();
            // Sets the title bar text
            qMyForm.Text = "An Important Message";
            TextBox qMyTextbox = new TextBox();
            qMyTextbox.SetBounds(0, 0, 400, 300);
            qMyTextbox.Multiline = true;
            qMyTextbox.ScrollBars = ScrollBars.Vertical;
            qMyTextbox.ReadOnly = true;

            // Add messages via AppendText, using '\u000D\u000A' for new line characters
            qMyTextbox.AppendText("Hear, my child, and accept my words,\u000D\u000A");
            qMyTextbox.AppendText("    that the years of your life may be many.\u000D\u000A");
            qMyTextbox.AppendText("I have taught you the way of wisdom;\u000D\u000A");
            qMyTextbox.AppendText("    I have led you in the paths of uprightness.\u000D\u000A");
            qMyTextbox.AppendText("When you walk, your step will not be hampered;\u000D\u000A");
            qMyTextbox.AppendText("    and if you run, you will not stumble.\u000D\u000A");
            qMyTextbox.AppendText("Keep hold of instruction; do not let go;\u000D\u000A");
            qMyTextbox.AppendText("    guard her, for she is your life.\u000D\u000A");
            qMyTextbox.AppendText("\u000D\u000A             Proverbs 4:10-13\u000D\u000A");

            // Set the client area of the form equal to the size of the Text Box
            qMyForm.ClientSize = qMyTextbox.Size;
            // Add the Textbox to the form
            qMyForm.Controls.Add(qMyTextbox);
            // Display the form
            qMyForm.ShowDialog();
        }
    }
}

Setting Colors in HTML with CSS

I have discussed how to specify a color with CSS in a prior post. Here, I will explain how the color specifications are used. To simplify the matter, I will use the hexadecimal color format for all of my color designations.

The basic block element has several distinct regions that can be specified, as I discussed in a another post on the CSS box model. Of these regions, three different colors may be selected for the border, background, and text, as shown below.

(Leviticus 10:9-11) You shall not drink wine nor any thing that may make drunk, thou nor thy sons, when you enter into the tabernacle of the testimony, lest you die: because it is an everlasting precept through your generations: And that you may have knowledge to discern between holy and unholy, between unclean and clean: And may teach the children of Israel all my ordinances which the Lord hath spoken to them by the hand of Moses.

The border is the outermost region that is colored bluish. When the border color is set, we need to specify the width and the pattern as well. In the example above, we set the border with this designation:
border:10px solid #88aacc;
To break this down, the border has a width of 10 pixels, a solid pattern, and is colored #88aacc.

Inside the border we have the background and the text. The background pinkish color is specified as
background-color:#ffddcc;
Against this background, we have a darker reddish, brown color for the text, which is specified as
color:#aa5522;
This is how the text color is set for any element.

The full specification for the div above, with all of the color and size styling, is given by this code below
<div style="width:420px; height:160px; padding:20px; border:10px solid #88aacc; background-color:#ffddcc; color:#aa5522; margin:20px;">
(Leviticus 10:9-11) You shall not drink wine nor any thing that may make drunk, thou nor thy sons, when you enter into the tabernacle of the testimony, lest you die: because it is an everlasting precept through your generations: And that you may have knowledge to discern between holy and unholy, between unclean and clean: And may teach the children of Israel all my ordinances which the Lord hath spoken to them by the hand of Moses.
</div>

 

© 2007–2024 XoaX.net LLC. All rights reserved.