|
-
Jan 29th, 2008, 03:44 PM
#1
Thread Starter
Lively Member
[Resolved][2005]TextBox with just numbers and points, other stuff
Hello everyone.
We're currently working on our thesis about a preprocessor for different engineering simulations. Our intention is to make it as user friendly as possible, since the whole "simulation" idea is often considered hostile by most students. Therefore, we're working on it in Visual Basic 2005, since it's a pretty standard tool, visually appealing and friendly, both for the user and the programmer. Besides, since it's a project for the university, it is our intention to make the code as easy to understand as possible, in case other students want to work on it in the future.
However, due to our inexperience with VB (hey, we're engineers, not programmers), we've come to find several walls in our way. After some search on Google, most of them are solved, with only a few exceptions.
For example, there are quite a number of TextBoxes that we want to restrict their use to only numbers and a single point (for decimals). Since we want to have a tidy code, we would want to use a separate module, just calling a function to handle each textbox.
Now, in VB6, I've found this works:
On Module:
Code:
Function JustNumbersAndPoint(Value, KP)
If (KP > 47 And KP < 58) Or KP = 13 Or KP = 8 Then
KeyAscii = KP
Else
If KP = 46 Then
KeyAscii = KP
For H = 1 To Len(Value)
Symbol = Mid(Value, H, 1)
If Symbol = Chr(46) Then
KeyAscii = 0
H = Len(Value)
End If
Next H
End If
End If
JustNumbersAndPoint = (KeyAscii)
End Function
On each Form:
Code:
Private Sub Text1_KeyPress(KeyAscii As Integer)
KeyAscii = JustNumbersAndPoint(Text1.Text, KeyAscii)
End Sub
Now that's cool and all, but how do I do that in VB2005, using the same method? (A module function to handle all textboxes when called).
Around Google, I've found several direct solutions (without using the module). The best one (I think) is this one:
Code:
Private Sub TextBox1_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
If e.KeyChar = "." Then
If Me.TextBox1.Text.IndexOf(".") > -1 Then
e.Handled = True
End If
ElseIf Char.IsNumber(e.KeyChar) = False AndAlso Char.IsControl(e.KeyChar) = False Then
e.Handled = True
End If
End Sub
Now, how can I use this in a Module and just call it whenever I want, sort of like with this -> "KeyAscii = JustNumbersAndPoint(Text1.Text, KeyAscii)" ?
Also, I'd like the function to handle numbers, just one point and allow the use of Backspace, Delete and Tab.
Last edited by Vaitork; Feb 8th, 2008 at 04:44 PM.
Reason: [Resolved]
-
Jan 29th, 2008, 04:02 PM
#2
Re: [2005]TextBox with just numbers and points, other stuff
heres a numeric only textbox. it only allows positive decimal numbers
vb Code:
Public Class numericTextbox
Inherits TextBox
Const WM_PASTE = &H302
Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
e.Handled = Not (Char.IsControl(e.KeyChar) OrElse Char.IsDigit(e.KeyChar) OrElse e.KeyChar = "."c And Not Me.Text.Contains("."))
MyBase.OnKeyPress(e)
End Sub
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_PASTE Then
If Not IsNumeric(Clipboard.GetText) Then
Return
End If
End If
MyBase.WndProc(m)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 29th, 2008, 04:11 PM
#3
Thread Starter
Lively Member
Re: [2005]TextBox with just numbers and points, other stuff
@.paul.
Hmm, hold on a bit. Should I use that in the Module or in the Form?
I don't think you understood my intention. I don't want to fill my forms with the same thing all over again. I just want to have the JustNumbersAndPoint function once in my whole code, and have each textbox call it from their respective forms.
-
Jan 29th, 2008, 05:10 PM
#4
Re: [2005]TextBox with just numbers and points, other stuff
its a class that inherits a textbox. so you add a class to your project, paste the code into it, + run the project. then stop debugging. now if you look in your toolbox, you'll find numericTextbox which you can add to your forms + use just like a regular textbox.
theres an updated version in my signature with improved pasting.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 29th, 2008, 06:04 PM
#5
Thread Starter
Lively Member
Re: [2005]TextBox with just numbers and points, other stuff
You know, that's actually quite impressive. And we would have used it had we known about it two weeks ago
Problem is, all Forms are finished, and believe me... There are a LOT of TextBoxes in our program (Most of them are coded and everything)... Unless there's a way to replace the old TextBoxes with these new, fancy ones, I'm gonna have to look for another way.
Is there a way to adapt this class of yours to my current textboxes? Sorry if I'm coming across as stubborn not my intention.
-
Jan 29th, 2008, 06:43 PM
#6
Re: [2005]TextBox with just numbers and points, other stuff
you could try this.
add a numericTextbox to 1 form.
in your form designer code (i.e. form1.designer.vb), find the line that says (something similar to):
vb Code:
Me.NumericTextbox1 = New WindowsApplication7.numericTextbox
for each textbox that you want to change to a numericTextbox, find the line that declares it. i.e.
vb Code:
Me.TextBox1 = New System.Windows.Forms.TextBox
and change it to:
vb Code:
Me.TextBox1 = New WindowsApplication7.numericTextbox
try it with just 1 textbox first to be sure it won't cause any problems, but that worked for me
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 29th, 2008, 06:57 PM
#7
Re: [2005]TextBox with just numbers and points, other stuff
Just a side note. IsNumeric is NOT a good method of checking for numeric only, despite its name.
A value entered as "$150.00" will pass an IsNumeric() check, which is valid in its own right, but not valid if you are ONLY expecting numbers and the possible decimal place.
I recommend using something else to check the clipboard text for numeric value. Yes it is not likely to come up often, but why leave a potential bug in code when you can fix it.
Also keep in mind that this solution would not work properly in all cultures. Some places use , for . and . for , when writing numbers.
10.500,20
10,500.20
-
Jan 29th, 2008, 07:10 PM
#8
Re: [2005]TextBox with just numbers and points, other stuff
if you read post #4 you'll see i've included an improved version in my signature that uses decimal.tryparse instead
Last edited by .paul.; Jan 29th, 2008 at 07:19 PM.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 29th, 2008, 07:11 PM
#9
Re: [2005]TextBox with just numbers and points, other stuff
i haven't included any commas or any multicultural options though
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 29th, 2008, 08:50 PM
#10
Re: [2005]TextBox with just numbers and points, other stuff
You should use numericupdown controls instead of textboxes.
-
Jan 29th, 2008, 09:13 PM
#11
Thread Starter
Lively Member
Re: [2005]TextBox with just numbers and points, other stuff
@.paul.: I'd be willing to do what you mentioned in post #6, but, to be honest, I don't know how to do that. I mean, I know that NumericTextBox is WindowsApplication1.NumericTextBox, but I don't know how to change my TextBoxes into NumericTextBoxes. I don't know where to do that. It's not in the normal "View code" screen, if that's what you mean.
@kleinma: I am currently going for the one I mentioned up there (the code about "direct solutions"), and I'm not being able to use the $ character in there. I'm not sure about the situations you mention, but as you say, they surely don't come often.
Also, I'm forcing the use of "." instead of "," since the simulator is programmed with "." ... hence my interest in points.
@stanav: Since I need the user to input quite a bit of data (it's an engineering simulation program after all), I can't have a substitute for TextBoxes.
We just reached another wall... There's this Form with a lot of labels and TextBoxes, ordered from 1 to 10. Now, depending of the number of TextBoxes I need, I want them to become visible or not. If I need 2 of them, only the first four appear visible.
I know I can just write "Label3.visible = false; Label3.visible = false; etc"... but I rather use "For... Next"
That said, I'm having trouble making it work... I already renamed all labels and textboxes to things like _LabelX_1, _LabelX_2... But I can't make them work with "For... Next"
Help?
-
Jan 29th, 2008, 09:26 PM
#12
Re: [2005]TextBox with just numbers and points, other stuff
in your 'solution explorer' click the 'show all files' button.
for every form, theres a form.designer.vb
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Jan 30th, 2008, 09:15 AM
#13
Thread Starter
Lively Member
Re: [2005]TextBox with just numbers and points, other stuff
Thanks .paul., that worked perfectly I will definitely start using your NumericTextBoxes from now on.
Now I'm stuck with the "For... Next" issue I mentioned in Post #11. Care to give us a hand there?
As far as I know, I need to create an array of some sort (ArrayLabel? ArrayTextBox?) but I have no idea how to do that.
-
Jan 30th, 2008, 09:41 AM
#14
Re: [2005]TextBox with just numbers and points, other stuff
[QUOTE=Vaitork
@stanav: Since I need the user to input quite a bit of data (it's an engineering simulation program after all), I can't have a substitute for TextBoxes.
[/QUOTE]
NumericUpDown controls fit perfectly in your situation. They still allow the users to type in values just like textboxes while having the bonus spinning buttons to increment/decrement the values at mouse clicks. Besides, you don't have to worry about users typing in non-numeric data... And finally, I don't see any reasons why numbericUpDown controls can slow down data entry comparing to textboxes. My programming philosophy is: always try to use the right tool for the job. It will greatly reduce your efforts to find "workarounds" and also add stability to your application.
-
Jan 30th, 2008, 09:48 AM
#15
Re: [2005]TextBox with just numbers and points, other stuff
Stanav,
I agree with you, but I also will say that either a numeric updown, or an extended textbox are both valid options, wouldn't you?
-
Jan 30th, 2008, 10:10 AM
#16
Re: [2005]TextBox with just numbers and points, other stuff
 Originally Posted by kleinma
Stanav,
I agree with you, but I also will say that either a numeric updown, or an extended textbox are both valid options, wouldn't you?
Yes, they are both valid options. But with an extended textbox, one will have to create the class himself (more work, and probably more chance for bugs) while the numeric updown is a standard .net control and it is readily available in the toolbox.
-
Jan 30th, 2008, 10:40 AM
#17
Thread Starter
Lively Member
Re: [2005]TextBox with just numbers and points, other stuff
@stanav: Hmm, now I understand your proposal, though I don't think it's the ideal approach for the purposes of our program.
Typically, the program won't require over 2 or 3 elements in the array, and I rather have the user see those TextBoxes. I feel using a NumericUpDown, while more efficient and easier to program, might be slightly confusing for non-experienced users, and may give them a feeling of lesser control over the tools at hand. Having all data visible at all times is priority here.
Besides, I have two different forms that need this array. While the first one could possibly use a NumericUpDown, the second form is quite a bit more complicated, and definitely use an array of different controls (labels, textboxes, checkboxes...)
Anyway, I've been reading a bit, and it seems quite complicated to make an array on 2005. I've been mostly looking for ways to create one directly in the form.designer.vb, without any luck so far. Do any of you guys have a useful link or two? Or better yet, know how to edit the designer manually?
-
Jan 30th, 2008, 10:42 AM
#18
Re: [2005]TextBox with just numbers and points, other stuff
what type of array exactly? I mean array is a very broad term when it comes to programming.
Creating an array of strings is as simple as
Dim myStrings() as string = {"string 1", "string 2", "string 3"}
although I doubt that is exactly what you are looking to do, so please elaborate a bit, Arrays are not complicated in .NET
-
Jan 30th, 2008, 11:00 AM
#19
Thread Starter
Lively Member
Re: [2005]TextBox with just numbers and points, other stuff
Sorry.
Control Arrays.
According to the value of a NumericUpDown, I need to make Visible and Enable a number of controls. If the number in the NUD is 3, then the first 3 labels, the first 3 textboxes and so on will be visible, while the elements from 4 to 10 will be hidden.
By the way, this NumericUpDown also controls the size of the GroupBoxes and Form too. I'm not too familiar with the code in VB2005, but the migration of an older program shows me this:
Code:
Frame2.Height = VB6.TwipsToPixelsY(1440 + 285 * NZX + 75 * (NZX - 1) + 285)
That "VB6" part is worrying me. Is this the general way to change the size of a general container in 2005?
-
Jan 30th, 2008, 11:18 AM
#20
Thread Starter
Lively Member
Re: [2005]TextBox with just numbers and points, other stuff
I just found this article:
http://visualbasic.about.com/od/usin...ctrlarray2.htm
I think I will settle with that, since it does what I want.
Now moving with the Container sizing through code...
You could always rename our thread and call it Thesis Project - Many Questions or something like that, hehe.
-
Jan 30th, 2008, 11:23 AM
#21
Re: [2005]TextBox with just numbers and points, other stuff
control arrays as they were known in VB6 no longer exist, but that is partially because there is simply no need for them. In .NET you can make an array of controls, instead of a control array.
Lets say you have 3 labels on a form (label1, label2, label3)
You can make a control array in code like this:
Code:
Dim myLabels() as label = {Label1, Label2, Label3}
'just to make sure it works
For Each L as Label in myLabels
Messagebox.Show(L.Text)
Next
-
Jan 30th, 2008, 04:49 PM
#22
Thread Starter
Lively Member
Re: [2005]TextBox with just numbers and points, other stuff
Alright, I did the Control Array thing the way that link I posted explains. It's a bit rough but at least I can do my Arrays the way I want them.
Also solved the thing about sizing the containers. That was pretty easy.
Now we're facing a different problem:
I am trying to change the Size of said containers and the Visible status of several labels and textboxes via NumericUpDown.ValueChange, so that, when the value in the NumericUpDown is, say, 4, only the first 4 labels and the first 4 textboxes are Visible, and the Size of the containers adapt to the Visible objects.
These objects are in a Form belonging to the MDIParentForm. I have previously declared all of these forms as ChildForms, two weeks back.
The reason I'm saying this is because I am getting some error on the line:
Code:
Form1.MdiParent = Me
in the MDI Code.
The error message I'm getting goes something like this:
...System.InvalidOperationException was unhandled...
Object reference not set to an instance of an object
at System.EventHandler.Invoke(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
And so on... and so on...
Why is this happening?
I had to settle with NumericUpDown.Click for the event changes, but I figure Click isn't the ideal way to do this.
-
Jan 31st, 2008, 08:32 AM
#23
Thread Starter
Lively Member
Re: [2005]TextBox with just numbers and points, other stuff
Sorry for bumping...
Anyone can help on #22?
-
Feb 1st, 2008, 12:04 PM
#24
Thread Starter
Lively Member
Re: [2005]TextBox with just numbers and points, other stuff
@kleinma: I have several other questions, and I believe I have many more for the following weeks. Should I create a new thread for each one? Or should I use this one for all of them?
-
Feb 1st, 2008, 12:16 PM
#25
Re: [2005]TextBox with just numbers and points, other stuff
1 topic per thread is recommended.
we'll all know what you're asking about that way too
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Feb 1st, 2008, 12:23 PM
#26
Re: [2005]TextBox with just numbers and points, other stuff
I think I had a similar issue when I was setting MdiParent = Me AFTER I has told it to show the form. If set to before I showed the form, it worked.
That answer might be vice-versa... I might have changed to to set it after the form was shown...
-
Feb 7th, 2008, 08:29 AM
#27
Thread Starter
Lively Member
Re: [2005]TextBox with just numbers and points, other stuff
That's not the problem. First thing I do in the MDI is setting the Parent/Child structure of the program.
I'll just create a new thread. For now, I'd be happy to get this one closed with a [RESOLVED] tag thanks to everyone for your answers!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|