Code not executing
Code not executing
Code not executing
Code not executing
Code not executing
Code not executing Code not executing Code not executing Code not executing Code not executing Code not executing Code not executing Code not executing
Code not executing Code not executing
Code not executing
Go Back  Xtreme Visual Basic Talk > > > Code not executing


Reply
 
Thread Tools Display Modes
  #1  
Old 02-14-2017, 10:24 AM
Daigon Ali Daigon Ali is offline
Regular
 
Join Date: Aug 2014
Location: London
Posts: 59
Unhappy 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.

Code:
   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.
Code:
    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
                ctrl.ResetText()
            ElseIf TypeOf ctrl Is TextBox Then
                ctrl.Text = vbNullString
            ElseIf TypeOf ctrl Is ListBox Then
                ctrl.ResetText()
            End If
        Next        
    End Sub
Any suggestions much appreciated.
Reply With Quote
  #2  
Old 02-14-2017, 05:07 PM
Cerian Knight's Avatar
Cerian KnightCode not executing Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,193
Default

First, confirm this works for you in a new project:
Code:
'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)
        RefreshFields()
    End Sub

    Sub RefreshFields()
        For Each ctrl As Control In Controls
            If TypeOf ctrl Is TextBox Then
                ctrl.ResetText()
            End If
        Next
    End Sub
End Class
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #3  
Old 02-15-2017, 04:42 AM
Daigon Ali Daigon Ali is offline
Regular
 
Join Date: Aug 2014
Location: London
Posts: 59
Default

Hi Cerian,

Yes, that does work.
Reply With Quote
  #4  
Old 02-15-2017, 10:39 AM
Cerian Knight's Avatar
Cerian KnightCode not executing Cerian Knight is offline
Polymath (in disciplina)

Super Moderator
* Expert *
 
Join Date: May 2004
Location: Michigan
Posts: 4,193
Default

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?
__________________
I got all the answers wrong on the GLAT, apparently even #9 (where I put a period in the middle of the box and labeled it 'singularity ripe for rapid inflation').
Reply With Quote
  #5  
Old 02-16-2017, 04:08 AM
Daigon Ali Daigon Ali is offline
Regular
 
Join Date: Aug 2014
Location: London
Posts: 59
Default

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.
    Code:
            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.
    Code:
    '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
Reply With Quote
Reply

Tags
refreshfields, ctrl, called, textbox, status, controls, code, warning, typeof, sstatus, stepping, entered, message, dim, ctrl.resettext, elseif, string, routine, user, form, fields, combobox, listbox, executing, enabled


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Code not executing
Code not executing
Code not executing Code not executing
Code not executing
Code not executing
Code not executing Code not executing Code not executing Code not executing Code not executing Code not executing Code not executing
Code not executing
Code not executing
 
Code not executing
Code not executing
 
-->