Hello programmers out there again :)

Im using this code (Thanks to adatapost):

private: System::Void textBox1_KeyPress(System::Object^sender,System::Windows::Forms::KeyPressEventArgs^ e)
      {
      if(Char::IsDigit(e->KeyChar))
      return;
      if(e->KeyChar=='\b')
      return;
      if(e->KeyChar=='-')
      return;
      e->Handled=true;
      }

To allows only digits, backspace and "-"
But the problem is that you can put the "-" anywhere and as
often as you want into the textbox. So, any suggestions on
how to make a "1 - limit" and a check if its the first letter in the textbox ?

Help is greatly appreciated.

you will have to keep track of where the '-' occurs -- it can only occur at the beginning of the text, no where else.

AFAIK the '\b' will never ever be in the text box. Windows Forms text control will hangle back spaces for you. So you should never have to be concerned about it.

you will have to keep track of where the '-' occurs -- it can only occur at the beginning of the text, no where else.

Yes, sure thats excatly what i mean.
But how could i do it ?

Add a flag (bool) to the class that indicates whether the '-' character has been seen in the text or not. Shouldn't be all that difficult for you to figure out.


KeyChar is not a member of KeyEventArgs. Do you mean KeyCode?


KeyChar is not a member of KeyEventArgs. Do you mean KeyCode?

I thought the same thing, but for the _KeyPress event the second parameter of the method is a KeyPressEventArgs (which has the KeyChar member) instead of a KeyEventArgs

Sorry, but i started C++ yesterday, so i don't
have a real idea how i could do that guys.

Could you post some code snippet ?

Oh yes -- I had the wrong event handler.

>>but i started C++ yesterday
This is not c++ -- its CLR/C++, more specifically Windows Forms.

Here is one way to do it

private: System::Void textBox1_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e) {
        if(Char::IsDigit(e->KeyChar) )
            return;
        if(e->KeyChar=='\b')
            return;
        if(e->KeyChar=='-')
        {
            if( this->textBox1->Text->Length == 0)
                return;
        }
        e->Handled=true;
             }
    };

Here is one way to do it

private: System::Void textBox1_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e) {
        if(Char::IsDigit(e->KeyChar) )
            return;
        if(e->KeyChar=='\b')
            return;
        if(e->KeyChar=='-')
        {
            if( this->textBox1->Text->Length == 0)
                return;
        }
        e->Handled=true;
             }
    };

Your code seems kinda logic, thanks.
But i actually get a error (In Debug mode, compiling works fine) when i try it (Translation) "The text got the wrong format".
-> error -> http://pastebin.com/qAcDpVaj
Could you post another way, please ?

what is that link you posted? That code worked perfectly for me using vc++ 2010 Express. Here is the complete header file, the function I posted is at the bottom

The program allows me to enter digits, '-' and backspace.  All others are ignored.
#pragma once

namespace test2 {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;

	/// <summary>
	/// Summary for Form1
	/// </summary>
	public ref class Form1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~Form1()
		{
			if (components)
			{
				delete components;
			}
		}
    private: System::Windows::Forms::TextBox^  textBox1;
    protected: 
	private:
		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		void InitializeComponent(void)
		{
            this->textBox1 = (gcnew System::Windows::Forms::TextBox());
            this->SuspendLayout();
            // 
            // textBox1
            // 
            this->textBox1->Location = System::Drawing::Point(52, 65);
            this->textBox1->Name = L"textBox1";
            this->textBox1->Size = System::Drawing::Size(201, 22);
            this->textBox1->TabIndex = 0;
            this->textBox1->KeyPress += gcnew System::Windows::Forms::KeyPressEventHandler(this, &Form1::textBox1_KeyPress);
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(8, 16);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(282, 255);
            this->Controls->Add(this->textBox1);
            this->Name = L"Form1";
            this->Text = L"Form1";
            this->ResumeLayout(false);
            this->PerformLayout();

        }
#pragma endregion
    private: System::Void textBox1_KeyPress(System::Object^  sender, System::Windows::Forms::KeyPressEventArgs^  e) {
        if(Char::IsDigit(e->KeyChar) )
            return;
        if(e->KeyChar=='\b')
            return;
        if(e->KeyChar=='-')
        {
            if( this->textBox1->Text->Length == 0)
                return;
        }
        e->Handled=true;
             }
    };
}

Oh, i guess the actual problem is this :

RangeY = Convert::ToInt32(this->textBox9->Text);

Int32 doesnt supports -, right ?
What else shall i use?

Use Int32::TryParse instead:

int result = 0;
if(Int32::TryParse(textBox9->Text, result))
{
    //etc.
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.