8 Replies - 696 Views - Last Post: 28 April 2013 - 01:34 AM Rate Topic: -----

#1 nemacx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-April 13

Making a number-only textBox

Posted 27 April 2013 - 11:32 AM

Hello everyone, this is my first post on this forum :)

Let me get straight down to business, this is my problem, I'm writing a simple code for managing the computer shutdowns. This is what has been working so far:

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;
using System.Diagnostics;
using System.Threading;

namespace ShutdownTimer10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();    
        }

        ////   THE CODE FOR THE NUMERIC TEXTBOX - NOT WORKING   ////

        /*
        public void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }
            base.onkeypress(e);
        }
        */

        String shutdown; //string za checkBox
        private void button1_Click(object sender, EventArgs e)
        { 
            if (textBox1.Text == "") //uslov ukoliko je textBox prazan
            {
                MessageBox.Show("Enter a value");
            }

              
            else
            {
                Process.Start("Shutdown", shutdown + " -t " + textBox1.Text + @" -c ""Windows is going to shut down""");
                timer1.Start();
                label3.Text = textBox1.Text;
            }

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Process.Start("Shutdown", "-a");
            timer1.Stop();
            label3.Text = "0";
            label2.Visible = false;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            switch (comboBox1.SelectedIndex) 
            {
                case 0:
                    shutdown = "-s";
                    break;

                case 1:
                    shutdown = "-r";
                    break;
            }
        }

        private void timer1_Tick_1(object sender, EventArgs e) //Aktiviranje tajmera da se prikaze u labeli.
        {
            if (textBox1.Text != "")
            {
                label3.Text = ((Convert.ToInt32(label3.Text) - 1).ToString());
                label2.Visible = true;
            }
            
        }

        private void button3_Click(object sender, EventArgs e)
        {
            Process.Start("Shutdown", "-i");
        }

        private void button4_Click(object sender, EventArgs e)
        {
            Process.Start("Shutdown", "-l");
        }

    }
}



So, if someone has any idea how to make the textbox numbers only or how to make some kind of a check for string and chars in it, i would really appreciate it :)

Is This A Good Question/Topic? 0
  • +

Replies To: Making a number-only textBox

#2 andrewsw  Icon User is offline

  • It's just been revoked!
  • member icon

Reputation: 3809
  • View blog
  • Posts: 13,508
  • Joined: 12-December 12

Re: Making a number-only textBox

Posted 27 April 2013 - 11:46 AM

Looks okay but you should remove this line:

base.onkeypress(e);

(if you haven't handled the keystroke this will happen automatically.)

BTW This isn't perfect as it won't handle Copy-Paste actions.

This post has been edited by andrewsw: 27 April 2013 - 12:00 PM

Was This Post Helpful? 0
  • +
  • -

#3 Julyuary  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 109
  • Joined: 20-December 11

Re: Making a number-only textBox

Posted 27 April 2013 - 12:13 PM

Why not use a masked text box with a mask that consists of zeroes?
Was This Post Helpful? 0
  • +
  • -

#4 nemacx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-April 13

Re: Making a number-only textBox

Posted 27 April 2013 - 01:15 PM

View PostJulyuary, on 27 April 2013 - 12:13 PM, said:

Why not use a masked text box with a mask that consists of zeroes?


Well I should have said before that I'm just starting to get into the whole programming and developing thing, so I'm not so experienced as you might have thought.

So how do I do this with maskedTextBox?
Was This Post Helpful? 0
  • +
  • -

#5 Julyuary  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 109
  • Joined: 20-December 11

Re: Making a number-only textBox

Posted 27 April 2013 - 03:25 PM

Well assuming you already have your masked text box, you can set the mask to zero. You can add as many zeroes to the mask as you like to add possible digits. For instance..
maskedTextBox.Mask = "0";

Will only accept a single digit number.
maskedTextBox.Mask = "0000";
will accept up to a four digit number.
So by setting the mask to "0000", you can enter up to 9999 in your masked text box. Hope my explanation was clear!
Was This Post Helpful? 0
  • +
  • -

#6 nemacx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-April 13

Re: Making a number-only textBox

Posted 27 April 2013 - 05:14 PM

View PostJulyuary, on 27 April 2013 - 03:25 PM, said:

Well assuming you already have your masked text box, you can set the mask to zero. You can add as many zeroes to the mask as you like to add possible digits. For instance..
maskedTextBox.Mask = "0";

Will only accept a single digit number.
maskedTextBox.Mask = "0000";
will accept up to a four digit number.
So by setting the mask to "0000", you can enter up to 9999 in your masked text box. Hope my explanation was clear!


Yes it was, so i did, but i still can input as many numbers as i want, and also string, char, it doesn't matter, i don't get the output that i want.
Was This Post Helpful? 0
  • +
  • -

#7 nemacx  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 27-April 13

Re: Making a number-only textBox

Posted 27 April 2013 - 05:20 PM

View Postnemacx, on 27 April 2013 - 05:14 PM, said:

View PostJulyuary, on 27 April 2013 - 03:25 PM, said:

Well assuming you already have your masked text box, you can set the mask to zero. You can add as many zeroes to the mask as you like to add possible digits. For instance..
maskedTextBox.Mask = "0";

Will only accept a single digit number.
maskedTextBox.Mask = "0000";
will accept up to a four digit number.
So by setting the mask to "0000", you can enter up to 9999 in your masked text box. Hope my explanation was clear!


Yes it was, so i did, but i still can input as many numbers as i want, and also string, char, it doesn't matter, i don't get the output that i want.


I'm so sorry for double posting, i wrote a hasty post before i even run the debugging. It works like a charm, thanks a lot!
Was This Post Helpful? 0
  • +
  • -

#8 Julyuary  Icon User is offline

  • D.I.C Head

Reputation: 38
  • View blog
  • Posts: 109
  • Joined: 20-December 11

Re: Making a number-only textBox

Posted 27 April 2013 - 06:23 PM

Sure, no problem. Glad I could help
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5675
  • View blog
  • Posts: 12,192
  • Joined: 02-June 10

Re: Making a number-only textBox

Posted 28 April 2013 - 01:34 AM

If you google "c# numerictextbox" there are several how to articles including one on MSDN.
Alternatively just replace the textbox with a numericUpDown control.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1