136k views
3 votes
1. Create a procedure as part of a form that accepts one string and outputs a different string. Add code to the TextChanged event of a text box to call the procedure, passing the contents of the text box as the argument. Pass back as the result of the procedure the uppercase version of the string passed into it, and display this in the text box. (Hint: Use the Visual Basic UCase () function.) 2. Create a single procedure that calls itself. Call this procedure from the Click event of a button and observe the resulting error.

1 Answer

3 votes

Final answer:

The student's question can be addressed by creating a procedure that converts a string to uppercase using the Visual Basic UCase() function and calling it on the TextChanged event. Furthermore, creating a recursive procedure without an exit condition, as called from a button's Click event, will lead to a stack overflow error.

Step-by-step explanation:

Procedure to Convert String to Uppercase

To address the first part of the student's question, let's create a procedure in Visual Basic that accepts a string and outputs its uppercase equivalent. We will call this procedure from the TextChanged event of a text box.

Private Sub ConvertToUpper(ByVal inputText As String) As String
Return UCase(inputText)
End Sub

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
TextBox1.Text = ConvertToUpper(TextBox1.Text)
End Sub

It is important to note that continuously updating the text box within the TextChanged event can lead to erratic behavior, as the event is fired every time the text changes, including changes made programmatically.

Creating a Recursive Procedure

For the second part, here's an example of a recursive procedure that will lead to a stack overflow error if called without a base condition:

Private Sub RecursiveProcedure()
RecursiveProcedure()
End Sub

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RecursiveProcedure()
End Sub

Please be cautious with recursion as it needs to have a proper exit condition to prevent infinite recursion leading to a stack overflow error.

User Saswata
by
8.3k points