Sunday, August 8, 2010

♦ Using Textbox with newline

This will explain the code sample  how to use "for loop" and print out text with newline.
Figure 1. Output result screen.


  • We can use Textbox display some text into Textbox with newline. the i value is any variable we can put into below command.

    <code c#>
          textBoxMultiLine.Text += "" + i + Environment.NewLine;
    </code c#>






  • Example Form1.cs
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace forloop2
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
           
            private void button1Run_Click(object sender, EventArgs e)
            {
    
                //Check valid , Is start field empty ?
                if (textBoxStart.Text.Trim() !="" && textBoxEnd.Text.Trim() !="")
                {
                    // get values from start and stop 
                    int start = int.Parse(textBoxStart.Text);
                    int stop = int.Parse(textBoxEnd.Text);
                    
                    //check valid, is in 1-100 range allowed.
                    if (start >= 0 && stop <= 100)
                    {
                        //Start calculation with for loop command
                        for (int i = start; i <= stop; i++)
                        {
                            //Note: you must use operator [+= ""]
                            textBoxMultiLine.Text += "" + i + Environment.NewLine;
                        }
    
                    } else 
                    {
                        textBoxMultiLine.Text = "Out of range, Check...\r\n";
                    }
                }
                else
                {
                    textBoxMultiLine.Text = "Start or end value is empty.\r\n";
                }
                
                
            }
    
            private void button2Clear_Click(object sender, EventArgs e)
            {
                textBoxMultiLine.Text ="";
            }
    
            private void textBoxMultiLine_TextChanged(object sender, EventArgs e)
            {
    
            }
    
            
        }
    }
    
    






  • In Textbox properties you must change its to multiline.





  • Figure 2: Textbox properties


  • Some part of code .... ..Form1.Designer.cs
    ...
    ...
    ...
    // textBoxMultiLine
    // 
    this.textBoxMultiLine.Location = new System.Drawing.Point(231, 12);
    this.textBoxMultiLine.Multiline = true;
    this.textBoxMultiLine.Name = "textBoxMultiLine";
    this.textBoxMultiLine.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
    this.textBoxMultiLine.Size = new System.Drawing.Size(160, 111);
    this.textBoxMultiLine.TabIndex = 8;
    this.textBoxMultiLine.TextChanged += new System.EventHandler(this.textBoxMultiLine_TextChanged);
    // 
    ...
    ...
    ...
  • No comments:

    Post a Comment