In some code that I am doing i have the following in place to keep users from inputing text.
However, i want to make sure that following code also allows me to use "." to seperate dollars and cents. I want to use the following code if possible.
Private Sub txtPurchase1_KeyPress(ByVal sender As Object, _ ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPurchase1.KeyPress ' textbox1 just accept numeric data If Char.IsNumber(e.KeyChar) Thene.Handled =
False ElseMessageBox.Show(
"Please enter correct format. (i.e. 20.00)")e.Handled =
True End If End SubThanks in advance.

A bit of Help Please
rcook349
I would totally recommend that if you're unable to follow my explanation, if you want to learn to program, you learn some core VB first and then come back to forms programming. However, here is the full solution. I recommend using MSDN to look up the various parts of what I did, so you can understand them. The check for Char.IsControl is needed as your implimentation does not allow keys like arrows and delete.
Private Sub OnKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress Dim tb As TextBox = sender If (Not (Char.IsNumber(e.KeyChar) Or Char.IsControl(e.KeyChar))) Then If (tb.Text.Contains(".")) Thene.Handled =
True Elsee.Handled =
Not (e.KeyChar = ".") End If Else Dim s As String = tb.Text.Substring(0, tb.SelectionStart) + e.KeyChar + tb.Text.Substring(tb.SelectionStart, tb.Text.Length - tb.SelectionStart) If (s.Contains(".")) Then Dim sa As String() = s.Split(".")e.Handled = (sa(1).Length > 2)
End If End If End Subprogames25
//That's the sort of stuff that makes me say to buy a VB.NET language ( NOT IDE/Winforms) book.
I have the Visual Basic 2005 In A Nut Shell...is there a book titles VB.NET Language
//I'm sorry if I've upset you, or if my taking the time to generate the code you wanted because you were unable to follow my explantation offended you. I'm here precisely because I remember when I started learning, and how much help I got from people in (then USENET) forums. However, I do know that if I had taken the approach so many seem to take nowadays, I would have got no help at all. There was a far higher expectation of people to do some of their own research in those days. Most questions here I can answer with 10 seconds on google, to provide a link that explains exactly what is being asked. Your question was not one of those, but times have definately changed.
I agree with you. Most ppl want an anwser right away, others have spent time looking for the anwser and have become frustrated with the fact that they may not be finding the right anwser. You personally had not upset me, this forum in general has been frustrating to me.
//In fact, I deliberately wrote the code to be as verbose as possible, on the basis that I wanted each line to show you one step of the thought process required to achieve the (somewhat complex) result required. feel free to ask if you can't follow it. That's the sort of interaction I am trying to promote here, one of learning and not just of anonymous code fixes.
I had figured you had written the code to be verbose, it hasnt been wasted. I appreciate your effort in "That's the sort of interaction I am trying to promote here, one of learning and not just of anonymous code fixes." You said it best. I am not one who just wants an anwser spoon fed to me, but I b/c I am still very, very wet behind the ears i still need help and likle to have my "hand held".
//////// While your code likely works with the solution you implied it does not work in mine.
////What did I imply
Imply was the wrong word to use. Provided would have been a better word.
TA123
Private Sub OnKeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
Dim tb As TextBox = sender
If (Not (Char.IsNumber(e.KeyChar) Or Char.IsControl(e.KeyChar))) Then
If (tb.Text.Contains(".")) Thene.Handled =
True Elsee.Handled =
Not (e.KeyChar = ".") End If Else Dim s As String = tb.Text.Substring(0, tb.SelectionStart) + e.KeyChar + tb.Text.Substring(tb.SelectionStart, tb.Text.Length - tb.SelectionStart) If (s.Contains(".")) Then Dim sa As String() = s.Split(".")e.Handled = (sa(1).Length > 2)
End If End If End SubLike I said I am trying to make sure that if a user inputs anything other then a dolar amount it throws a window asking for the correct input format.
//////I forgot to add that, but I said in my original post, you're better off setting e.Handled, then at the end of the function, check if it's true, and if it is, show your message box./////
/////////OK, then you need to cast it. Or you can just refer to the actual TextBox that is handling the event, I was tring to write it to work generically. ///////
Dim
tb As TextBox = DirectCast(sender, TextBox)In the above you state that I can just refer to the actual TextBOx that the even is handling, I am assuming that it would be txtPurchase1.Text as this is the text box that the user inputs the number into then Clicks on the Calculate Button.
Dim tb As TextBox = txtPurchase1.TEXT
Would this work Or am I not getting it. In addition, in the above code I am not sure if I understand where the MsgBox is being referred to or is this code a stand alone to the code of my original post
The code that I am using in the bulk of the program is as follows:
Private Sub btnCalc_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnCalc.Click Dim ThePurchase, ThePayment As String ' Variables for Customer Purchase and Customer Payment Dim Change As Decimal ' Variable to Convert Sting to Decimal Dim Convert As Decimal ' Variable to be Converted into Integer Dim TotalPennies As Integer 'Variable to useThePurchase = txtPurchase1.Text
' Get the Customer PurchaseThePayment = txtPay.Text
' Get the Customer PaymentChange =
CDec(ThePayment) - CDec(ThePurchase) ' Determine the ChangeConvert = Change * 100 ' Convert to Change to Total Pennies Before Converting to Integer
'lblTest.Text = FormatNumber(Convert.ToString, 0)TotalPennies =
CInt(Convert) ' Convert Change to IntegerThe remainingpart of the code below this takes the TotalPennies and Divides them by the equal amount of pennies in a 20, 10, 5, and Dollar bill, it is also supposed to do this for Quarters, Dimes, and Nickles as well as present the remainder in Pennies.
It uses for example:
lblTwenties1.Text = (TotalPennies \ 2000).ToString
TotalPennies = TotalPennies Mod 2000
As a side issue, it does not show the Quarters, Dimes, Nickels or Pennies, it stops showing ouput at $1.00.
The assignment requires us to take an amount of a Customers Purchase, subtract out thier payment and then display the Total Change along with the number of each type of change (20's, 10's, etc., down to .01's) in an output. The fact that I want to have it prompt the user for the correct format is something that I want it to do so that it can't be broken that way.
Anyways, what are your suggestions
DeNiS-21
A maskedtextbox is your easiest bet here. otherwise, you need to write another check which says someting like
e.Handled = Not( Char.IsNumber(e.KeyChar) or (e.KeyChar = '.' and txtPurchase1.Text.IndexOf(".") = -1))
Then, if e.Handled = true, show your message box. Allowing only two decimal points requires more work again, you need also to work out where the cursor is and what the string will be after input to do your check.
Chandresh.theengineer
First, I am in class that just started on Intro to VB.net. So I am getting my basics. Second, when learning any new language (programming or otherwise) unless you have been exposed to a majority of the "verbs, nouns, adjectives, etc." then you have no basis to form a question about what you want to do if you do not know what it is that you are asking about in the first place. So coming here as a newbie to ask a specific question is becoming tedious with all of the various "veterans" that have likely been programming in VB for some time or even programming in another language for sometime.
You must remember Mr. Graus, that when a newbie asks a question they are doing so in an effort to learn from what they have learned before hand as well as attempting to learn what you are speaking of at the moment. If I remember correctly, in a post dated Jan 24, 2006 you stated and I quote " *grin* Everyone was new once. That's why people like me like to help newbies, we remember relying on help when we started." It seems to me that you have forgotten what it is like to be new.
While your comment is likely meant to be non-rude, it comes off as something totally different and more in the line of "listen you obviously don't know what you are doing, and until you do, do not attempt programming in VB until you know how to program in VB."
Can't learn it unless ppl like you remember what was or is to be new.
You supplying me with the anwser you believe to be correct is not really helping when it does not take into consideration my current code that I spent time learning how to get right.
I have some constraints that I need to adhere to as this is a class project (open internet, open forum, etc.).
Like I said I am trying to make sure that if a user inputs anything other then a dolar amount it throws a window asking for the correct input format.
While your code likely works with the solution you implied it does not work in mine. In addition, the use of " Dim tb As TextBox = sender" throws an error : "Option Strict On disallows implicit conversions from 'Object' to 'System.Windows.Forms.Textbox'.
PublicError
As a newbie I am not that far along so I am not quite following you. It took me a bit of time, and shuffling through some books, to figure out that code that I used.
So I am not quite sure what you are meaning. I have been reading the MSDN on MaskedTextBox and I am not sure how to use it.
Sorry to be of bother.
Thanks
SweptSquash
// First, I am in class that just started on Intro to VB.net.
Cool
// So I am getting my basics.
That may be possible, but too many books AND courses teach drag and drop IDE use without teaching core programming skills, IMO. That's the point I was making.
// unless you have been exposed to a majority of the "verbs, nouns, adjectives, etc." then
That's the sort of stuff that makes me say to buy a VB.NET language ( NOT IDE/Winforms) book.
// You must remember Mr. Graus, that when a newbie asks a question they are doing so in an effort to learn from what they have learned before hand as well as attempting to learn what you are speaking of at the moment
I'm sorry if I've upset you, or if my taking the time to generate the code you wanted because you were unable to follow my explantation offended you. I'm here precisely because I remember when I started learning, and how much help I got from people in (then USENET) forums. However, I do know that if I had taken the approach so many seem to take nowadays, I would have got no help at all. There was a far higher expectation of people to do some of their own research in those days. Most questions here I can answer with 10 seconds on google, to provide a link that explains exactly what is being asked. Your question was not one of those, but times have definately changed.
// It seems to me that you have forgotten what it is like to be new.
I'm sorry you think so. In fact, as someone who is a professional developer, I feel that I help more by steering people towards a path that allows them to help themselves, than to just spoon feed them ( although I did take time out of my work day to write up code that solved the problem, nevertheless. ) If you want to ask any questions about the code, feel free.
//do not attempt programming in VB until you know how to program in VB."
Well, that's just a dumb statement, and I'm sorry that anyone could assume I had made it.
// You supplying me with the anwser you believe to be correct is not really helping when it does not take into consideration my current code that I spent time learning how to get right.
In fact, I deliberately wrote the code to be as verbose as possible, on the basis that I wanted each line to show you one step of the thought process required to achieve the (somewhat complex) result required. feel free to ask if you can't follow it. That's the sort of interaction I am trying to promote here, one of learning and not just of anonymous code fixes.
My approach is pretty much an extension of yours, there is no dichotomy, it would simply have been a lot harder to read if I'd put it all into one line of code ( assuming that I even could, including the step to check for more than two digits after the . )
// Like I said I am trying to make sure that if a user inputs anything other then a dolar amount it throws a window asking for the correct input format.
I forgot to add that, but I said in my original post, you're better off setting e.Handled, then at the end of the function, check if it's true, and if it is, show your message box.
// While your code likely works with the solution you implied it does not work in mine.
What did I imply
// In addition, the use of " Dim tb As TextBox = sender" throws an error : "Option Strict On disallows implicit conversions from 'Object' to 'System.Windows.Forms.Textbox'.
OK, then you need to cast it. Or you can just refer to the actual TextBox that is handling the event, I was tring to write it to work generically.
Dim
tb As TextBox = DirectCast(sender, TextBox)