Results 1 to 4 of 4

Thread: Update values before adding to listbox

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2020
    Location
    The Netherlands
    Posts
    6

    Update values before adding to listbox

    Hi everybody,


    I would like to create directories made up of an object number, designation, sequence number and revision number.
    Because each of my projects can contain multiple objects, I'd first like to add them to a listbox.

    But when I alter the input and press the Add button, still the initial values are passed through to the listbox. i.e.: 2015000 - Cart Handling (1)\Revision 0 instead of 2015005 - Dumping System (1)\Revision 0
    What am I missing here? Is there a way to "update" to the current set values before adding them to the listbox?

    Please find my code and printscreen below.
    Any help is much appreciated!


    Name:  error1.JPG
Views: 95
Size:  48.8 KB

    Code:
    Imports System
    Imports System.Collections.Generic
    Imports System.Linq
    Imports System.Text
    Imports System.Threading.Tasks
    Imports IWshRuntimeLibrary
    Imports System.IO
    Imports System.Environment
    Imports Autodesk.Connectivity.WebServices
    Imports Autodesk.Connectivity.WebServicesTools
    Imports System.ComponentModel
    
    Public Class Form1
        Public Class Settings
            Public Shared EUN As String = Form1.TextBox1.Text
            Public Shared EUC As String = Form1.ComboBox1.Text
            Public Shared PRN As String = Form1.TextBox5.Text
            Public Shared EUL As String = Form1.TextBox2.Text
            Public Shared EUA As String = Form1.TextBox3.Text
            Public Shared EUSN As String = Form1.TextBox4.Text
            Public Shared SBMN As String = Form1.TextBox6.Text & Form1.NumericUpDown1.Value
            Public Shared SBMRev As String = "Revision " & Form1.NumericUpDown2.Value
            Public Shared SBM As String = SBMN & "\" & SBMRev
            Public Shared SON As String = Form1.NumericUpDown3.Value
            Public Shared SOD As String = Form1.ComboBox2.Text
            Public Shared SOSeqN As String = Form1.NumericUpDown4.Value
            Public Shared SORev As String = "Revision " & Form1.NumericUpDown5.Value
            Public Shared Folder_ExtProj As String = "\\nt05\KANTOOR\Master Data Management\02. External Projects\01. Completed\"
            Public Shared Folder_SC_ExtProj As String = "\\nt05\KANTOOR\Master Data Management\02. External Projects\"
            Public Shared Folder_ProjTempl As String = "\\nt05\KANTOOR\Master Data Management\99. Procedures & Templates\Templates - Folder Structure\02. External Projects\PRN - EUN"
            Public Shared Folder_NewProj As String = Folder_ExtProj & PRN & " - " & EUN
            Public Shared Folder_Media As String = "\\Nas01\Mediadisk\External Projects\"
            Public Shared Folder_MediaTempl As String = "\\nt05\KANTOOR\Master Data Management\99. Procedures & Templates\Templates - Folder Structure\99. Mediadisk on NAS\EUN\EUL (ICC) - EUA\EUSN"
            Public Shared Folder_ServObj As String = "\\nt05\KANTOOR\Master Data Management\01. Service Objects\"
            Public Shared Folder_NewServObj As String = Folder_ServObj & Settings.SON & " - " & Settings.SOD & " (" & Settings.PUC & Settings.SOSeqN & ")\" & Settings.SORev
            Public Shared Folder_SBMTempl As String = "\\nt05\KANTOOR\Master Data Management\99. Procedures & Templates\Templates - Folder Structure\01. Service Objects\SBM-REV"
            Public Shared Folder_SOTempl As String = "\\nt05\KANTOOR\Master Data Management\99. Procedures & Templates\Templates - Folder Structure\01. Service Objects\SON-REV - SOD (SOC)\"
            Public Shared PUC As String
            Public Shared SO As String = SON & " - " & SOD & " (" & PUC & SOSeqN & ")\" & SORev
    
    
            Public Shared substring As String = Strings.Right(EUC, 4)
            Public Shared EUX = EUL & " " & substring & " - " & EUA
    
            Public Function PUCcheck()
                Select Case Settings.SOD
                    Case "Box Handling"
                        Settings.PUC = "BH"
                    Case "Buffering System"
                        Settings.PUC = "BS"
                    Case "Cart Handling"
                        Settings.PUC = "CH"
                    Case "Dumping System"
                        Settings.PUC = "DS"
                    Case "Filling System"
                        Settings.PUC = "FS"
                    Case "Information System"
                        Settings.PUC = "IS"
                    Case "Packing Line"
                        Settings.PUC = "PL"
                    Case "Palletising System"
                        Settings.PUC = "PS"
                End Select
            End Function
    
    
        End Class
    
        Public Sub TextBox5_KeyPress(sender As Object, e As KeyPressEventArgs) Handles TextBox5.KeyPress, TextBox8.KeyPress, TextBox7.KeyPress
            'Restrict entry to digits-only
            If Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar) Then
                e.Handled = True
            End If
        End Sub
    
    
        Public Function Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
            'Create Project Folders
            If System.IO.Directory.Exists(Settings.Folder_NewProj) Then
                MessageBox.Show("Folder already exists!")
            Else
                System.IO.Directory.CreateDirectory(Settings.Folder_NewProj)
                My.Computer.FileSystem.CopyDirectory(Settings.Folder_ProjTempl, Settings.Folder_NewProj)
                'rename template files
                My.Computer.FileSystem.RenameFile(Settings.Folder_NewProj & "\02. Project Management\PRN - Project Plan-v01.docx", Settings.PRN & " - Project Plan-v01.docx")
                My.Computer.FileSystem.RenameFile(Settings.Folder_NewProj & "\02. Project Management\PRN - Post-Project Evaluation-v01.docm", Settings.PRN & " - Post-Project Evaluation-v01.docm")
                My.Computer.FileSystem.RenameFile(Settings.Folder_NewProj & "\04. Planning\PRN - Project Planning-v01.xlsx", Settings.PRN & " - Project Planning-v01.xlsx")
                My.Computer.FileSystem.RenameFile(Settings.Folder_NewProj & "\04. Planning\PRN - Transport Planning Containers-v01.xlsx", Settings.PRN & " - Transport Planning Containers-v01.xlsx")
                My.Computer.FileSystem.RenameFile(Settings.Folder_NewProj & "\04. Planning\PRN - Transport Planning Trailers-v01.xlsx", Settings.PRN & " - Transport Planning Trailers-v01.xlsx")
                My.Computer.FileSystem.RenameFile(Settings.Folder_NewProj & "\04. Planning\PRN - Budgeted Hours-v01.xlsx", Settings.PRN & " - Budgeted Hours-v01.xlsx")
                My.Computer.FileSystem.RenameFile(Settings.Folder_NewProj & "\06. Eng-M\PRN - Project Transfer ENG-ASM-v01.docx", Settings.PRN & " - Project Transfer ENG-ASM-v01.docx")
                My.Computer.FileSystem.RenameFile(Settings.Folder_NewProj & "\09. Installation on-site\PRN - Open Item List-v01.xlsm", Settings.PRN & " - Open Item List-v01.xlsm")
                My.Computer.FileSystem.RenameFile(Settings.Folder_NewProj & "\09. Installation on-site\02. Installation Plan\PRN - Installation Plan-v01.docx", Settings.PRN & " - Installation Plan-v01.docx")
                My.Computer.FileSystem.RenameFile(Settings.Folder_NewProj & "\09. Installation on-site\02. Installation Plan\10. Commissioning Agreement\PRN - Commissioning Agreement-v01.docx", Settings.PRN & " - Commissioning Agreement-v01.docx")
    
                'create shortcut in 02. External Projects
                Dim ShellSrpt As WshShell = New WshShell()
                Dim startupPath As String = Settings.Folder_SC_ExtProj
                Dim myShortcut As IWshShortcut = CType(ShellSrpt.CreateShortcut(Path.Combine(startupPath, Settings.PRN & " - " & Settings.EUN) & ".lnk"), IWshShortcut)
                myShortcut.TargetPath = Settings.Folder_ExtProj & Settings.PRN & " - " & Settings.EUN
                myShortcut.WorkingDirectory = Settings.Folder_ExtProj & Settings.PRN & " - " & Settings.EUN
                myShortcut.Description = ""
                myShortcut.Save()
    
                'Message when done
                MessageBox.Show("Project Folder succesfully created")
            End If
    
    
            'Media Folders on Nas
            If System.IO.Directory.Exists(Settings.Folder_Media & Settings.EUN & Settings.EUX & Settings.EUSN) Then
                MessageBox.Show("Media Folder already exists!")
            Else
                System.IO.Directory.CreateDirectory(Settings.Folder_Media & "\" & Settings.EUN & "\" & Settings.EUX & " - Site " & Settings.EUSN)
                My.Computer.FileSystem.CopyDirectory(Settings.Folder_MediaTempl, Settings.Folder_Media & "\" & Settings.EUN & "\" & Settings.EUX & " - Site " & Settings.EUSN)
    
                'create shortcut in Project Folder
                Dim ShellSrpt As WshShell = New WshShell()
                Dim startupPath As String = Settings.Folder_ExtProj & "\" & Settings.PRN & " - " & Settings.EUN
                Dim myShortcut As IWshShortcut = CType(ShellSrpt.CreateShortcut(Path.Combine(startupPath, "11. Media for " & Settings.EUN) & ".lnk"), IWshShortcut)
                myShortcut.TargetPath = Settings.Folder_Media & Settings.EUN
                myShortcut.WorkingDirectory = Settings.Folder_Media & Settings.EUN
                myShortcut.Description = ""
                myShortcut.Save()
    
                'Message when done
                MessageBox.Show("Media Folder succesfully created")
            End If
        End Function
    
    
        '
        '
        '
        'SBM en Service-Objecten
        Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
            Dim Foldername As String
            For Each Foldername In Me.ListBox1.Items
                System.IO.Directory.CreateDirectory(Settings.Folder_ServObj & Foldername)
                My.Computer.FileSystem.CopyDirectory(Settings.Folder_SBMTempl, Settings.Folder_ServObj & Foldername)
    
                Dim ShellSrpt As WshShell = New WshShell()
                Dim startupPath As String = Settings.Folder_ExtProj & Settings.PRN & " - " & Settings.EUN & "\06. Eng-M"
                Dim myShortcut As IWshShortcut = CType(ShellSrpt.CreateShortcut(Path.Combine(startupPath, Settings.SBMN) & ".lnk"), IWshShortcut)
                myShortcut.TargetPath = Settings.Folder_ServObj & Settings.SBMN
                myShortcut.WorkingDirectory = Settings.Folder_ServObj & Settings.SBMN
                myShortcut.Description = " "
                myShortcut.Save()
    
                Dim ShellSrpt1 As WshShell = New WshShell()
                Dim startupPath1 As String = Settings.Folder_ExtProj & Settings.PRN & " - " & Settings.EUN & "\07. Eng-S & Eng-H"
                Dim myShortcut1 As IWshShortcut = CType(ShellSrpt1.CreateShortcut(Path.Combine(startupPath1, Settings.SBMN) & ".lnk"), IWshShortcut)
                myShortcut1.TargetPath = Settings.Folder_ServObj & Settings.SBMN
                myShortcut1.WorkingDirectory = Settings.Folder_ServObj & Settings.SBMN
                myShortcut1.Description = " "
                myShortcut1.Save()
    
                Dim ShellSrpt2 As WshShell = New WshShell()
                Dim startupPath2 As String = Settings.Folder_ServObj & Settings.SBMN
                Dim myShortcut2 As IWshShortcut = CType(ShellSrpt2.CreateShortcut(Path.Combine(startupPath2, "Media for " & Settings.EUN) & ".lnk"), IWshShortcut)
                myShortcut2.TargetPath = Settings.Folder_Media & Settings.EUN & Settings.EUX & " - Site " & Settings.EUSN
                myShortcut2.WorkingDirectory = Settings.Folder_Media & Settings.EUN & Settings.EUX & " - Site " & Settings.EUSN
                myShortcut2.Description = ""
                myShortcut2.Save()
            Next
    
    
            Dim Foldername2 As String
            For Each Foldername2 In Me.ListBox2.Items
                System.IO.Directory.CreateDirectory(Settings.Folder_ServObj & Foldername2)
                My.Computer.FileSystem.CopyDirectory(Settings.Folder_SOTempl, Settings.Folder_NewServObj & Foldername2)
            Next
    
        End Sub
    
        'Add SO
        Public Function AddSO(SO As String)
            ListBox2.Items.Add(Settings.SON & " - " & Settings.SOD & " (" & Settings.PUC & Settings.SOSeqN & ")\" & Settings.SORev)
    
        End Function
    
        'Add SBM
        Public Function AddSBM(SBM As String)
            If Not ListBox1.Items.Count = 1 Then
                ListBox1.Items.Add(Settings.SBM)
            Else MessageBox.Show("You can only add item at once")
            End If
    
        End Function
    
        Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
            AddSBM(Settings.SBM)
        End Sub
    
        Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
            AddSO(Settings.SO)
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Dim selected As String = Me.ListBox2.SelectedItem.ToString
            If selected IsNot "" Then
                Me.ListBox2.Items.Remove(selected)
    
            End If
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            Me.ListBox2.Items.Clear()
        End Sub
    
    
        Private Sub Button11_Click(sender As Object, e As EventArgs) Handles Button11.Click
            Me.ListBox1.Items.Clear()
        End Sub
        '
        '
        '
        'Tooltips
        Private Sub TextBox1_MouseHover(sender As Object, e As EventArgs) Handles TextBox1.MouseHover
            Dim but As New ToolTip()
            but.ToolTipTitle = "Bestemmingsrelatie"
            but.UseFading = True
            but.UseAnimation = True
            but.IsBalloon = True
            but.ShowAlways = True
            but.AutoPopDelay = 5000
            but.InitialDelay = 1000
            but.ReshowDelay = 500
            but.SetToolTip(TextBox1, "Volledige naam bestemmingsrelatie" & vbCr & "Bijvoorbeeld:" & vbCr & "Greenhill Produce" & vbCr & "Gaag Tomatenkwekerij, Van der" & vbCr & "Overkant BVBA, D'n" & vbCr & "Tuindershoek B.V., De")
        End Sub
    
        Private Sub TextBox2_MouseHover(sender As Object, e As EventArgs) Handles TextBox2.MouseHover
            Dim but As New ToolTip()
            but.ToolTipTitle = "Plaatsnaam"
            but.UseFading = True
            but.UseAnimation = True
            but.IsBalloon = True
            but.ShowAlways = True
            but.AutoPopDelay = 5000
            but.InitialDelay = 1000
            but.ReshowDelay = 500
            but.SetToolTip(TextBox2, "Engelse spelling van de plaatsnaam ZONDER postcode!" & vbCr & "Bijvoorbeeld:" & vbCr & "Antwerp ipv Antwerpen")
        End Sub
    
    
        'Project Completion
        Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
            'Delete shortcut
            My.Computer.FileSystem.DeleteFile(Settings.Folder_SC_ExtProj & "\" & Settings.PRN & " - " & Settings.EUN & ".lnk")
    
            'Set folder to read-only
            'Dim _folder As IO.DirectoryInfo = My.Computer.FileSystem.GetDirectoryInfo(Settings.Folder_ExtProj & "\" & Settings.PRN & " - " & Settings.EUN)
            '_folder.Attributes = IO.FileAttributes.ReadOnly
    
            ''FileSystem.SetAttr(Settings.Folder_ExtProj & "\" & Settings.PRN & " - " & Settings.EUN, FileAttribute.System + FileAttribute.ReadOnly)
    
            ''For Each file In Settings.Folder_ExtProj & "\" & Settings.PRN & " - " & Settings.EUN
            ''    FileSystem.SetAttr(Settings.Folder_ExtProj & "\" & Settings.PRN & " - " & Settings.EUN, FileAttributes.ReadOnly)
            ''Next
        End Sub
    
        Private Sub NumericUpDown3_Enter(sender As Object, e As EventArgs) Handles NumericUpDown3.Enter
    
        End Sub
    
    
    End Class

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    106,895

    Re: Update values before adding to listbox

    There's way too much code there. We shouldn't have to trawl through every line of code you have to work out what's relevant to the problem first, before even trying to work out the cause. You need to do what you can first, cut it down to only what's relevant and post that. If you need to create a test project that isolates just the relevant functionality then do that. That's something that you probably ought to be doing yourself anyway, because isolating problem functionality often makes the issue obvious.

  3. #3
    Frenzied Member
    Join Date
    Feb 2003
    Posts
    1,336

    Re: Update values before adding to listbox

    A few coding tips:

    1. Remove empty procedures cluttering the code.
    2. Use string interpolation. https://docs.microsoft.com/en-us/dot...olated-strings
    3. When referring to an objects methods multiple times you could use a With/End With block.
    4. Use Nothing instead of a pair of empty quotes when checking for or initializing empty strings.
    5. Unnecessary fully qualified references to methods/properties. Why are you importing System.IO at the top of your code for example?
    6. TextBox1, TextBox2, Button9, etc are not meaningful object names.
    7. Functions procedures should always explicitly return a value. At the very least add a "Return Nothing" at the end.
    8. Also I would recommend using:
    Option Compare Binary
    Option Explicit On
    Option Infer Off
    Option Strict On
    Last edited by Peter Swinkels; Apr 28th, 2021 at 04:43 AM.

  4. #4
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    33,388

    Re: Update values before adding to listbox

    Another coding tip - learn how to debug. That should be step #1. When you don't get the expected output, the first thing you should do is find out why. Add some breakpoints, step through the code, examine the flow, check your variables, see what things are, and compare them against your expectations. Are they what you expect them to be? If not, why not? Work it backwards. If something isn't the value it should be, why isn't it? Is something set wrong? Did you get the wrong value from somewhere?

    Once I deciphered the control names, it took me just a couple seconds to find the problem. 5 minutes with the debugger and you should have it figured out. Another 20 adding some code and you should have it fixed.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width