Your Ads Here

Showing posts with label control. Show all posts
Showing posts with label control. Show all posts

Monday, 6 February 2012

Set tabs in a RichTextBox using C#.NET


This example shows how to set tabs in a RichTextBox in C#.NET.
Keywords: RichTextBox, tabs, C#.NET, CSharp.NET.

The RichTextBox control can set different tabs for different parts of its text so this example makes some text, selects it, and then sets its tabs.
In the following code, the program uses the control's SelectAll method to select all of the text. It sets the SelectionTabs property to an array of Integers giving the tab positions. It sets AcceptsTab to True to ensure that tab characters go into the control rather than tabbing off to the next control and then it moves the selection position to the beginning of the text. 

  richTextBox1.SelectAll();
  richTextBox1.SelectionTabs = new int[] { 20, 40, 80, 120 };
  richTextBox1.AcceptsTab = true;
  richTextBox1.Select(0, 0);

Here the complete code to set tabs in RichTextBox.

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 testc
{

    public partial class Form1 : Form
    {
      
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            richTextBox1.SelectAll();
            richTextBox1.SelectionTabs = new int[]
            { 20, 40, 80, 120 };
            richTextBox1.AcceptsTab = true;
            richTextBox1.Select(0, 0);
        }
      
    }
}

Make a countdown timer in C#.NET


This example shows how to make a countdown timer in C#.NET
Keywords: countdown timer, timer, alarm, CSharp.NET, C#.NET

If you enter a duration in the format h:mm:ss and click the button, the following code converts your text into a TimeSpan and adds it to the current time. It saves the result in m_StopTime and enables the timer1 timer.

private void button1_Click(object sender, EventArgs e)
        {
            TimeSpan duration = TimeSpan.Parse(textBox1.Text);
            m_StopTime = DateTime.Now.Add(duration);
            
           timer1.Enabled = true;
        }

When the timer fires, it calculates the difference between the current time and the desired stop time and displays the time remaining in the label lblRemaining. Then if the total seconds remaining is zero, the program maximizes its form and makes its form topmost so it appears above all other forms, hopefully attracting your attention.


private void timer1_Tick(object sender, EventArgs e)
        {
            TimeSpan remaining = m_StopTime.Subtract(DateTime.Now);
            remaining = new TimeSpan(remaining.Hours, remaining.Minutes, remaining.Seconds);
            if (remaining.TotalSeconds < 0)
                remaining = TimeSpan.Zero;

            label1.Text = remaining.ToString();

            if (remaining.TotalSeconds <= 0)
            {
                this.WindowState = FormWindowState.Maximized;
                this.TopMost = true;
                timer1.Enabled = false;
            }
        }

 
Here the full code.

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 testc
{

    public partial class Form1 : Form
    {
        System.DateTime m_StopTime = default(System.DateTime);
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            TimeSpan duration = TimeSpan.Parse(textBox1.Text);
            m_StopTime = DateTime.Now.Add(duration);
            
           timer1.Enabled = true;
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            TimeSpan remaining = m_StopTime.Subtract(DateTime.Now);
            remaining = new TimeSpan(remaining.Hours, remaining.Minutes,
                remaining.Seconds);
            if (remaining.TotalSeconds < 0)
                remaining = TimeSpan.Zero;

            label1.Text = remaining.ToString();

            if (remaining.TotalSeconds <= 0)
            {
                this.WindowState = FormWindowState.Maximized;
                this.TopMost = true;
                timer1.Enabled = false;
            }
        }
    }
}