Click here to Skip to main content
15,900,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I create a textbox that just gets 0 or 1 strings?

Please help me.

Thanks
Posted
Updated 12-Jun-11 21:46pm
v3
Comments
Dalek Dave 13-Jun-11 3:46am    
Edited for Grammar.

Hello Dear
first you must set MaxLength property of TextBox to 1 which determines user can not insert more than 1 character in TextBox then in event handler of KeyPress event of TextBox write this code
C#
if (e.KeyChar == '0' || e.KeyChar == '1')
    return;
else
    e.Handled = true;
 
Share this answer
 
Comments
Member 7904482 13-Jun-11 3:19am    
it is just for character but I need for Strings
Sergey Alexandrovich Kryukov 13-Jun-11 3:21am    
No, this is for string, all correct. Try it and you will see...
--SA
Sergey Alexandrovich Kryukov 13-Jun-11 3:21am    
My 5 for the answer.
--SA
Sergey Alexandrovich Kryukov 13-Jun-11 3:24am    
Please see my solution. For UI using text box is ridiculous.
--SA
Member 7904482 13-Jun-11 3:25am    
but it has this error
'System.Windows.Forms.KeyEventArgs' does not contain a definition for 'KeyChar' and no extension method 'KeyChar' accepting a first argument of type 'System.Windows.Forms.KeyEventArgs' could be found (are you missing a using directive or an assembly reference?)
Bad idea to use a text box for it. Use two radio buttons or a combo box or a list box with just two values. Selecting (or checking in case of radio button) will mean 0, second one 1; use appropriate label or text items to show to the user which one is what.

—SA
 
Share this answer
 
Comments
Member 7904482 13-Jun-11 3:26am    
I need text box to get different 0,1 Strings
Sergey Alexandrovich Kryukov 13-Jun-11 3:50am    
No you don't I daresay; it would by ugly.
--SA
Dalek Dave 13-Jun-11 3:47am    
Sage Advice!
Sergey Alexandrovich Kryukov 13-Jun-11 3:50am    
Thank you, Dalek.
--SA
thatraja 13-Jun-11 4:02am    
Best idea, 5
Add the following event-handler in the constructor:

C#
textBox1.KeyPress += new KeyPressEventHandler(textBox1_KeyPress);


Event:

C#
void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    if ((e.KeyChar.Equals('0')) || (e.KeyChar.Equals('1')))
    {
        e.Handled = false;
    }
    else
    {
        e.Handled = true;
    }
}
 
Share this answer
 
Comments
Dalek Dave 13-Jun-11 3:47am    
Good Call.
Tarun.K.S 13-Jun-11 3:56am    
Thank you!
it is working fine.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != Convert.ToChar( "0" )&& e.KeyChar != Convert.ToChar( "1"))
            {
                e.KeyChar = char.MinValue;
            }
        }
    }
}
 
Share this answer
 
v2
Comments
Dalek Dave 13-Jun-11 3:46am    
Good Answer.
bind a textChangedEvent to the textbox and add a code like this :

C#
private void textBox1_TextChanged(object sender, KeyEventArgs e)
{
    if (!((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D1) || (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad1) && !e.Shift))//limit keys into 0,1,num0 and num1
    {
        e.SuppressKeyPress = true;//self explanatory
    }
}
 
Share this answer
 
Comments
Member 7904482 13-Jun-11 3:22am    
I use this but it accepts all other things
Does it need else Condition?
Firo Atrum Ventus 13-Jun-11 3:45am    
Nah, it should've worked fine if you limit set maxlength to 1
Dalek Dave 13-Jun-11 3:48am    
Ought to work, don't know why OP is having probs.
Use an if condition to check the text entered is 0 or 1 and Why don't you show the error message if it is other than 0 or 1.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900