Button Erases Text
- I'm having some trouble getting my button to erase the text that is in the textboxes when I hit it. I thought it would be something like txtAnswerText.Text = "" in side of the Next1_Click sub but that didn't do anything at all. Does anyone have any tips?
Réponses
- The text boxes with the answers aren't txtAnswer. They are Textboxes(0), TextBoxes(1), TextBoxes(2) etc. These are the textboxes that need to be cleared. Eg:
For I as Integer = 0 to NumberofBlanks - 1
TextBoxes(i).Text = ""
Next i- Marqué comme réponseJeff ShanMSFT, Modérateurvendredi 13 novembre 2009 02:50
That code is doing something quite different. There is also a syntax error in the first line. That code is clearing the answer box list if the answer boxes already exist, otherwise it is incrementing the paragraph pointer and displaying the whole text. The effect of that code will be to put the answer box list out of sync with the controls displayed on the form, and the whole thing will be a shambles. You cannot clear the list without also removing the answer boxes!
Perhaps your requirements have changed since you first posted the query, but it was my understanding that you wanted to remove the text that had been typed by the user into the answer boxes. This was your query:
"I fixed the code in the Show sub and the added the code back in the Try Again sub so now when I hit Try Again it erases the user's answers. However, for some reason when I hit next it gave me an Argument Out Of Range Exception."
The error was occuring at this line:
TextBoxes(I).Text = ""
The reason that the error was occurring is that you are executing that code when the answer boxes don't exist. This happens if you select TryAgain without having first selected Hide. For instance, if you do Show and TryAgain. That sequence of user actions could happen, so you need to fix the error.
If that line of code executes when the answer boxes don't exist, you will get the error, so the way to fix it is to check and see if the answer boxes exist before executing that code. If the answer boxes don't exist, then don't execute the code. There was nothing wrong with the original code, and it didn't need to be changed at all. You simply needed logic to ensure that it wasn't executed if the answer boxes didn't exist. This was exactly the same situation whith the Check routine - the check routine should not execute if the answerr boxes don't exist, becasue there is nothing to check, and attempting to do the check would create an error. That's why I directed you to the Check routine for an example.
1. Keep your original code for deleting teh text from the answer boxes - there was nothing wrong with that code.
2. Put that code at the spot I indicated after the test you copied from the start of the Check routine. In other words:Private Sub cmdClear_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdClear.Click If TextBoxes.Count = 0 Then Exit Sub End If For I As Integer = 0 To NumberofBlanks - 1 TextBoxes(I).Text = "" Next I End Sub
That example assumes the code executes in its own button click event. That would be a good way to test it. If you are calling it from somewhere in your existing code just code it like this:
Private Sub cmdClear<br/> If TextBoxes.Count = 0 Then Exit Sub End If For I As Integer = 0 To NumberofBlanks - 1 TextBoxes(I).Text = "" Next I End Sub
and call it with 'cmdClear'. Or, you could put the body of the code (ie, without the Sub and End Sub lines) at the end of the tryAgain routine. It has to be at the end because of the Exit Sub.
- Marqué comme réponseDeMolay8613 samedi 14 novembre 2009 22:23
That should work fine, but you may like to comment out the last six lines and see if it still works properly. The reason I think this is worth trying is that the Show function should be removing the answer boxes when it displayes the new piece of text. Therefore TextBoxes.Count will always be zero, and the routine will always exit immediately.
It was my understanding that you wanted this code in the TryAgain routine, which was invoked when you wanted the user to enter a new guess without doing Show/Hide. In that case the answer boxes might already exist, but would have text from previous answers displayed, and that was when you needed to specially erase the text in those boxes, without using Show to remove the boxes and Hide to create new boxes.
- Marqué comme réponseDeMolay8613 samedi 14 novembre 2009 22:22
Toutes les réponses
- You can also use ".clear", but what you showed should also work.
It's odd that it doesn't and makes me wonder what else is going on with the code, but try that and see if it works. - So how would I write that? txtAnswerText.clear and that's it?
- Are you sure that Next1_Click is set up to handle the button click event? That code would be correct if the name of the text box is txtAnswerText and the button click event is
Private Sub Next1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles Next1.Click
txtAnswerText.Text = ""
End Sub
If that's not working then perhaps some other procedure is placing text into the text box.
- I'm assuming that txtAnswerText is a text box name, correct?
Acamar is right ... something isn't adding up here but anyway, I hope this helps:
Private Sub Next1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Next1.Click txtAnswerText.Clear() End Sub
- Hello again Acamar,
I do believe there is another procedure preventing from entering the text. As you know we had the code that created 5 textboxes and placed them underneath the main textbox that contains the text. After the person checks his/her answer and hit's either Next or Try Again the answers are still in the blanks. I'm trying to figure out how to set it up so that when you hit either of the buttons the blanks will clear.
That's the code that places the textboxes.'For each hidden word For I As Integer = 0 To NumberofBlanks - 1 'Create a text box for it Dim T As New TextBox 'Add the text box to the form Me.Controls.Add(T) 'Set the position and sixe of the new text box T.Top = txt2memorize.Top + txt2memorize.Height + 10 T.Width = 60 T.Left = txt2memorize.Left + (T.Width + 10) * I 'Add it to the list of answer text boxes so we can refer to it when needed) TextBoxes.Add(T) Next
- The text boxes with the answers aren't txtAnswer. They are Textboxes(0), TextBoxes(1), TextBoxes(2) etc. These are the textboxes that need to be cleared. Eg:
For I as Integer = 0 to NumberofBlanks - 1
TextBoxes(i).Text = ""
Next i- Marqué comme réponseJeff ShanMSFT, Modérateurvendredi 13 novembre 2009 02:50
Ok, wow. That worked great! How about the Try Again button? You already have For I as Integer = 0 to NumberofBlanks -1 in the code so wouldn't that mess the code up if you entered it in again?
- The simplest way to clear the answer text boxes in the TryAgain routine (because they are going to be re-created anyway) is to invoke the Show routine which resets everything back to its starting state (except the counter for the paragraph).
So simply adding the following at the start of the TryAgain routine:
cmdShow_Click(Me, New System.EventArgs)
would be the best way to remove any previous answers from the text boxes. Hello again Acamar,
I do believe there is another procedure preventing from entering the text. As you know we had the code that created 5 textboxes and placed them underneath the main textbox that contains the text. After the person checks his/her answer and hit's either Next or Try Again the answers are still in the blanks. I'm trying to figure out how to set it up so that when you hit either of the buttons the blanks will clear.
That's the code that places the textboxes.'For each hidden word For I As Integer = 0 To NumberofBlanks - 1 'Create a text box for it Dim T As New TextBox 'Add the text box to the form Me.Controls.Add(T) 'Set the position and sixe of the new text box T.Top = txt2memorize.Top + txt2memorize.Height + 10 T.Width = 60 T.Left = txt2memorize.Left + (T.Width + 10) * I 'Add it to the list of answer text boxes so we can refer to it when needed) TextBoxes.Add(T) Next
Another thing to remember is that when you add controls at runtime they do not have a name . A name is given to control added at design time by the IDE for you . For example you drag a textbox onto your form and it is given the name TextBox1 by the IDE that does not happen when a control is added by code . If you had not added a reference to the TextBox in your TextBoxes array then you would probably have trouble if you need to reference it later . You would probably have to search the controls collection of a form or control to change it .
coding for fun Be a good forum member mark posts that contain the answers to your questions or those that are helpful
Please format the code in your posts with the
button . Makes it easier to read .- One issue with that code in the Try Again sub is that with the whole Show1_Click thing when you tell it to do this it no long shows the underscores in the text. It basically cancels out all the code in the Try Again sub and just shows the text and the textboxes below that where you enter your answers.
- If the show routine is called at the start of the tryagain routine then it will remove the text boxes and display the whole text without the missing words. Then the remainder of the tryagain routine will select the words to hide, display the text with the selected words replaced by underscores, and create and display the text boxes for the entry of the answers.
The overall effect is therefore to re-show the text with different hidden words and blank text boxes.
The code in the tryagain routine would only be cancelled out if the show routine was called at the end of the tryagain routine instead of at the beginnig. - I moved it to the beginning of the Try Again sub and it does remove the blanks once again but the textboxes are not cleared.
- Do This.
Comment out the line you just added at the start of the try again routine.
Start the program. Click Show.
Click tryagain and enter some text in one of the answer boxes.
Click show. Do the answer boxes disappear?
Click tryagain. Do the answer boxes re-appear and are they all blank?
Adding that line at the start of the tryagain routine means that clicking tryagain is exactly the same as clicking Show then tryagain. If you add that line back, and clicking tryagain is not exactly the same as clicking Show then tryagain, then something else is going on that is not related to any of the code that has been posted. You will need to trace the program through line by line and watch what happens to all the variables to sort out where it is going astray. - When I hit Show the second time the textboxes just stay where they are they don't disappear like you said they should.
- Has this changed recently? The show command has always removed the answer boxes, so if it's not removing them now something has changed in that command. It should look like this:
Private Sub cmdShow_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdShow.Click For Each t As TextBox In TextBoxes Me.Controls.Remove(t) Next TextBoxes.Clear() txtMemoryText.Text = Qtext(QPointer) End Sub - I fixed the code in the Show sub and the added the code back in the Try Again sub so now when I hit Try Again it erases the user's answers. However, for some reason when I hit next it gave me an Argument Out Of Range Exception.
- If you get an error message is is essential to provide the line of code at which the error occurred, and the values of the variables associated with the error.
What was the fix required in the Show routine, and why had it changed? - The change in the Show sub was I had the line Private TextBoxes As New List(Of TextBox) in there because when I was originally working on the program it said TextBoxes was not declared so I added that and it fixed the error and now it works like it should. I took that out and added the Show1_Click routine in the Try Again sub which fixed both problems.
The error that I'm getting in the Next sub occurred at this line:
Here is all the values for the Next sub.TextBoxes(I).Text = ""
NumberofBlanks 5 Integer
TextBoxes Count = 0 System.Collections.Generic.List(Of System.Windows.Forms.TextBox)
I 0 Integer
QPointer 1 Integer - You can't put the code for clearing the text from the text boxes in the Next routine, becasue that routine can be called at any time, whether or not the text boxes are displayed (if they aren't displayed, you will get that error). Only use the code for clearing the textboxes in a case where you know that the textboxes are displayed, such as the Check routine.
If you make the Next routine as per the original code, you will not have a problem with text remaining in the answer boxes because they will be cleared when they are removed. use:
Note that if you are concerned about too many buttons you can simply remove the Show button - the Next button now does effectively the same thing - ie, it can be a First/Next button if you initialise your paragrpah counter to -1 instead of zero.Private Sub cmdNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdNext.Click QPointer = (QPointer + 1) Mod 5 cmdShow_Click(Me, New System.EventArgs) End Sub
Private TextBoxes As New List(Of TextBox) must be at the module level - the program will not work if you include it in a procedure. - Right now my code in the Next Routine looks like this:
The For statement is the reason that we have the line Textboxes(I).Text = ""Private Sub Next1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Next1.Click QPointer = (QPointer + 1) Mod 10 Show1_Click(Me, New System.EventArgs) For I As Integer = 0 To NumberofBlanks - 1 TextBoxes(I).Text = "" Next I End Sub - Then if you remove those last three lines it will work OK. As I said, you cannot execute code to clear the textboxes unless you know that the textboxes exist, and as the Next button can be selected at any time, if you put code in there to clear the textboxes it will fail if Next is clicked before Hide.
Yaay! It works! I think the reason we had the For statement was so that we could clear the textboxes without having to remove the textboxes like what we see with the current code inside of the Next sub.
- I'm not 100% if that is the reason we did that but that is what I believe.
- The For loop was provided to enable you to clear the text from the answer boxes, but it will only work if the answer boxes exist. See the Check routine for an example of code that only executes if the answer boxes exist. If you wanted to clear the answer boxes, the you can use the For loop to do it, but if there is a possibility that the answer boxes don''t exist, then you must have a test like the test at the start of the check routine so that the For loop only executes if the answer boxes exist.
- So then wouldn't it be better to use a combination of the two? I mean you can't really predict what button a user will hit so if you used a combination of the two then it works no matter which button he/she hits first.
I'm not sure what you mean by a combination of the two. You can't predict what button the user will hit, so you can't predict what state the answer boxes will be in. Therefore, don't execute the For loop without first checking that the answer boxes exist. Clearly, if they don't exist then they don't need to be cleared, so the For loop does not need to be executed. So you can check that they exist: if they do then execute the For loop and if they don't then don't execute the For loop. It's not a question of combining the two - it's simply controlling the logic of the program based on the state of the answer boxes. As mentioned, the Check routine is an example of exactly this sort of program flow.
- Sorry for not replying so long I had some deadlines with school that I had to make and wasn't able to post anything for a while. Ok so the Check Answers sub is an example of checking to see if something is there before it executes a command. Let's use this as an example:
If Textboxes.Visible - True Then QPointer = (QPointer + 1) Mod 10 Show1_Click(Me, New System.EventArgs) Else If Textboxes.Visible = False Then TextBoxes.Add(T)
Or something like that. Sorry about replying after a moderater marked something as an answer like I said I had some deadlines with school and was not able to reply. - I'm afraid that I can't see that the above code has anything to do with the current problem.
The error was occurring because of the code that was attempting to clear the answer boxes when the answer boxes don't exist. It has nothing to do with whether they are visible or not - in fact I don't think there is anywhere in that code that the visible property is referenced, let alone adjusted. Certainly I didn't mention visible in the above comments. The above code doesn't resemble anything in the Check routine.
A subroutine that clears the text from the answer boxes needs to check that the answer boxes exist before attempting to clear them. So you would have code like this:
Private Sub cmdClear_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdClear.Click If TextBoxes.Count = 0 Then Exit Sub End If 'Insert here the code to clear the answer boxes.
- Would that work? If my syntax and logic is correc then according the code I just gave you it should clear the textboxes if there is one or more of them exist and then if none exist it continues to show the next set of text in the program.
If TextBoxes.Count = +=1 Then Textboxes.Clear() Else If TextBoxes.Count= 0 Then QPointer = (QPointer + 1) Mod 10 Show1_Click(Me, New System.EventArgs)<br/><br/>
That code is doing something quite different. There is also a syntax error in the first line. That code is clearing the answer box list if the answer boxes already exist, otherwise it is incrementing the paragraph pointer and displaying the whole text. The effect of that code will be to put the answer box list out of sync with the controls displayed on the form, and the whole thing will be a shambles. You cannot clear the list without also removing the answer boxes!
Perhaps your requirements have changed since you first posted the query, but it was my understanding that you wanted to remove the text that had been typed by the user into the answer boxes. This was your query:
"I fixed the code in the Show sub and the added the code back in the Try Again sub so now when I hit Try Again it erases the user's answers. However, for some reason when I hit next it gave me an Argument Out Of Range Exception."
The error was occuring at this line:
TextBoxes(I).Text = ""
The reason that the error was occurring is that you are executing that code when the answer boxes don't exist. This happens if you select TryAgain without having first selected Hide. For instance, if you do Show and TryAgain. That sequence of user actions could happen, so you need to fix the error.
If that line of code executes when the answer boxes don't exist, you will get the error, so the way to fix it is to check and see if the answer boxes exist before executing that code. If the answer boxes don't exist, then don't execute the code. There was nothing wrong with the original code, and it didn't need to be changed at all. You simply needed logic to ensure that it wasn't executed if the answer boxes didn't exist. This was exactly the same situation whith the Check routine - the check routine should not execute if the answerr boxes don't exist, becasue there is nothing to check, and attempting to do the check would create an error. That's why I directed you to the Check routine for an example.
1. Keep your original code for deleting teh text from the answer boxes - there was nothing wrong with that code.
2. Put that code at the spot I indicated after the test you copied from the start of the Check routine. In other words:Private Sub cmdClear_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles cmdClear.Click If TextBoxes.Count = 0 Then Exit Sub End If For I As Integer = 0 To NumberofBlanks - 1 TextBoxes(I).Text = "" Next I End Sub
That example assumes the code executes in its own button click event. That would be a good way to test it. If you are calling it from somewhere in your existing code just code it like this:
Private Sub cmdClear<br/> If TextBoxes.Count = 0 Then Exit Sub End If For I As Integer = 0 To NumberofBlanks - 1 TextBoxes(I).Text = "" Next I End Sub
and call it with 'cmdClear'. Or, you could put the body of the code (ie, without the Sub and End Sub lines) at the end of the tryAgain routine. It has to be at the end because of the Exit Sub.
- Marqué comme réponseDeMolay8613 samedi 14 novembre 2009 22:23
- That is correct I still want to clear the answers from the textboxes. Nothing has changed from what I originally wanted. The code I had posted I thought would erasethe answers not the textboxes and the error in the code is probably at the +=1 part. I was trying to tell it if the TextBoxes are there then it clears the answers and if they are not there then it doesn't execute the program it just continues on to the next set of text to learn. That was my intent with the code I gave you.
- I think I got it to work with this code:
Private Sub Next1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Next1.Click QPointer = (QPointer + 1) Mod 10 Show1_Click(Me, New System.EventArgs) If TextBoxes.Count = 0 Then Exit Sub End If For I As Integer = 0 To NumberofBlanks - 1 TextBoxes(I).Text = "" Next I End Sub That should work fine, but you may like to comment out the last six lines and see if it still works properly. The reason I think this is worth trying is that the Show function should be removing the answer boxes when it displayes the new piece of text. Therefore TextBoxes.Count will always be zero, and the routine will always exit immediately.
It was my understanding that you wanted this code in the TryAgain routine, which was invoked when you wanted the user to enter a new guess without doing Show/Hide. In that case the answer boxes might already exist, but would have text from previous answers displayed, and that was when you needed to specially erase the text in those boxes, without using Show to remove the boxes and Hide to create new boxes.
- Marqué comme réponseDeMolay8613 samedi 14 novembre 2009 22:22
- With or without those 6 lines it still does the same thing. Thank you for your help.

