• 0

[C#] 2 Questions


Question

Hello guys. I am a new VB.NET -> C# developer.

I have 2 questions:

1. Is there anything equivilant to a Module File(vb.net) in C#.

2. I want to make a textbox such that it does not take any character except numbers. I dont want it to take abc. i want it to take 123. so can anyone help me.

Umer.

Link to comment
Share on other sites

12 answers to this question

Recommended Posts

  • 0

If you want a textbox to accept only numbers, handle the KeyDown event and check the input, change e.Handled to false to ignore the character.

A C# source file is the same as a module file in VB.

Link to comment
Share on other sites

  • 0
If you want a textbox to accept only numbers, handle the KeyDown event and check the input, change e.Handled to false to ignore the character.

A C# source file is the same as a module file in VB.

agreed

Link to comment
Share on other sites

  • 0
If you want a textbox to accept only numbers, handle the KeyDown event and check the input, change e.Handled to false to ignore the character.

A C# source file is the same as a module file in VB.

Hrmm in VB6 you could use the isNumeric Bool does .NET have anything equal to this? :unsure:

Link to comment
Share on other sites

  • 0

umerh, you can create a class that contains only static members (keyword static). That would be equivalent to making a module.

Example:

public class SomeGlobalStuff

{

public static void GlobalMethod(int something)

{

//do something here

}

public const int GlobalConstant = 0;

public static int GlobalVariable = -1;

public static readonly int GlobalConstant = 2; // another way to declare a constant

}

And then in your program you just call the stuff as such:

SomeGlobalStuff.<member name>

Thus, you don't need to create a new instance of SomeGlobalStuff to use the stuff in it.

Link to comment
Share on other sites

  • 0

thanks dacris2000. u solved my prob. but its not exactly the solution. Now i have a class called "My" and now i have the global variables in it.

Thanks for the help guys.

Umer.

Link to comment
Share on other sites

  • 0
Hrmm in VB6 you could use the isNumeric Bool does .NET have anything equal to this?  :unsure:

public static bool IsNumeric(Object Input) {
 ? bool returnValue = true;
 ? try {
 ? ? ?Decimal.Parse(Input);
 ? } catch {
 ? ? ?returnValue = false;
 ? }

 ? return returnValue;
}

Don't know if that'll work, but:Dt should :D

Link to comment
Share on other sites

  • 0

this is a quick way of doing the same thing in C#. lets say that i have a text box called "TextBox_PhoneNumber" and i want the text box to ignore letters. i just use the KeyPressed event and the e.Handle property to do it.

in the follow code, the value of e.Handle is determined by whatever bool value "Shared.CheckTextBox_Letter()" returns, true or false.

private void TextBox_PhoneNumber_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
 ? ?{
 ? ?e.Handled = Shared.CheckTextBox_Letter(TextBox_PhoneNumber,e.KeyChar);
 ? ?}//end of TextBox_PhoneNumber_KeyPress()

i have it set up so that whatever character type (symbol, letter, number, whitespace) i want to ignore i just use one of the various methods that i have created for myself.

they are in a class that i call "Shared" and they are:

- bool CheckTextBox_Number(string str, char ch)

- bool CheckTextBox_Letter(string str, char ch)

- bool CheckTextBox_Symbol(string str, char ch)

- bool CheckTextBox_WhiteSpace(string str, char ch)

looking back on the code above, if the user does type in a letter, the "e.KeyChar" property will have that letter stored in it. i send it through the "CheckTextBox_Letter()" method and it checks to see if that is a letter or not. if it is a letter, the methods returns true, if it is not a letter, the method returns false. remember that if "e.Handle" is set to true, it will be "handled" and thus will be ignore and not displayed.

i know that the above methods and the approach is not efficient, but for the sake of explaination, ill just use those as an example.

umerh, i hope that this helps you,

STV

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.