Xtreme Visual Basic Talk

Xtreme Visual Basic Talk (http://www.xtremevbtalk.com/)
-   .NET General (http://www.xtremevbtalk.com/-net-general/)
-   -   Code not executing (http://www.xtremevbtalk.com/-net-general/328573-code-executing.html)

Daigon Ali 02-14-2017 09:24 AM

Code not executing
Hi All,

I'm using Visual Studio 2015 Community Version

I have a form which has fields for data entry. These consist of textbox, combobox and listbox controls.

Initially just one textbox is enabled where the user enters a product number.
The validated method of the textbox checks the status of the entered number and either allows the user to proceed or gives an appropriate warning.

When a warning is generated a sub called "refreshfields" is called which clears all the fields and and as result resets the enabled status of the controls ready for a fresh number.

The first problem I have is that, following a warning message, the "refreshfields" sub is not always called. Stepping through the code it doesn't go to the "refreshfields" routine.


  Private Sub txtNumber_Validated(sender As Object, e As EventArgs) Handles txtNumber.Validated
        Dim sStatus As String
        txtNumber.Text = UCase(txtNumber.Text)
        If Len(txtNumber.Text) > 0 Then
            sStatus = GetStatus(txtNumber.Text) 'function to check the status which returns a string
            Select Case sStatus
                Case "0"
                    MsgBox("Warning message", vbCritical)
                    Call RefreshFields() 'stepping through, code line is executed by doesn't go to the routine

Secondly, on the occasions where "refreshfields" is entered successfully (it's called form other events also such as clicking a Clear All button), my loop to clear the controls doesn't have any effect even though it appears to execute without error.
So, after executing the below, textboxes still contain text etc.

    Sub RefreshFields()
        Dim ctrl As Control
        Me.Cursor = Cursors.Default
        Me.Enabled = True
        For Each ctrl In Me.Controls
            If TypeOf ctrl Is ComboBox Then
            ElseIf TypeOf ctrl Is TextBox Then
                ctrl.Text = vbNullString
            ElseIf TypeOf ctrl Is ListBox Then
            End If
    End Sub

Any suggestions much appreciated.

Cerian Knight 02-14-2017 04:07 PM

First, confirm this works for you in a new project:

'Create a new Windows Forms project and add a TextBox and Button to the form
Public Class Form1
    Private Sub TextBox1_Validated(sender As Object, e As EventArgs) Handles TextBox1.Validated
        MsgBox("Warning message", vbCritical)
    End Sub

    Sub RefreshFields()
        For Each ctrl As Control In Controls
            If TypeOf ctrl Is TextBox Then
            End If
    End Sub
End Class

Daigon Ali 02-15-2017 03:42 AM

Hi Cerian,

Yes, that does work.

Cerian Knight 02-15-2017 09:39 AM

Good. So what is the real difference (simplify your code until it works and add back in until it breaks)?

I notice that you have a 'Me.Enabled = True'. Is that to imply that you are disabling the form (and therefore some of the event handling) at some point?

Daigon Ali 02-16-2017 03:08 AM

Thanks for your help.

Yes, the form is disabled during an update to a database to prevent any user interaction while this takes place. It may or may not be disabled when refreshfields is called.

I've had another look and now have it calling Refreshfields successfully :) (not sure what I did though).

There was still an issue with Refreshfields which I found was due to:
  1. My controls being in Groupboxes so I found I had to iterate the controls in each Groupbox separately.

            For Each ctrl In Me.Controls
                If TypeOf ctrl Is GroupBox Then
                    For Each ctrl2 In ctrl.Controls 'iterate through each control in Gbox

  2. The Typeof .... IS was not successfully identifying the controls so the associated resettext etc was never executed. I'm not sure why this didn't work but I changed to using GetType which does.

    'Didn't work
                        If TypeOf ctrl2 Is TextBox Then
                            ctrl2.Text = vbNullString
                        End If
    'Does work
                        If ctrl2.GetType() Is GetType(TextBox) Then
                            ctrl2.Text = vbNullString
                        End If

All times are GMT -6. The time now is 03:34 AM.

Powered by vBulletin® Version 3.8.9
Copyright ©2000 - 2018, vBulletin Solutions, Inc.
Search Engine Optimisation provided by DragonByte SEO v2.0.15 (Lite) - vBulletin Mods & Addons Copyright © 2018 DragonByte Technologies Ltd.
All site content is protected by the Digital Millenium Act of 1998. Copyright©2001-2011 MAS Media Inc. and Extreme Visual Basic Forum. All rights reserved.
You may not copy or reproduce any portion of this site without written consent.