vb.net - If and Else Statements causing multiple messageboxes to show up one after another -


i @ slump here these if , else statements, not cup of tea confuse me. issue experiencing follows: have 5 checkboxes, 1 button, , progress bar, , issue @ hand when checkbox1 ticked, , click on button1, infested multiple messageboxes popping 1 after another. doesn't matter tick off in checkbox, same sequence of things happens on , over, , can not life of me figure out. how correct this? here line of code need resolving.

i want check if file directory exists, , if doesn't downloads file. if exist, message comes once saying "file exists" , on , forth.

public sub server1()     if (checkbox1.checked = true)         if (file.exists("c:\something1"))             messagebox.show("some message", "hi", messageboxbuttons.ok, messageboxicon.exclamation)         else             s1.downloadfileasync(new uri("https://dl.dropboxusercontent.com/something1"), "c:\something1")         end if         if (file.exists("c:\something2"))             messagebox.show("some message", "hi", messageboxbuttons.ok, messageboxicon.exclamation)         else            s2.downloadfileasync(new uri("https://dl.dropboxusercontent.com/something2"), "c:\something2")         end if         if (checkbox2.checked = true)         elseif (file.exists("c:\something3"))             messagebox.show("some message", "hi", messageboxbuttons.ok, messageboxicon.exclamation)         else             s3.downloadfileasync(new uri("https://dl.dropboxusercontent.com/something3"), "c:\something3")         end if         if (file.exists("c:\something4"))             messagebox.show("some message", "hi", messageboxbuttons.ok, messageboxicon.exclamation)         else             s4.downloadfileasync(new uri("https://dl.dropboxusercontent.com/something4"), "c:\something4")         end if         if (checkbox3.checked = true)         elseif (file.exists("c:\something5"))             messagebox.show("some message", "hi", messageboxbuttons.ok, messageboxicon.exclamation)         else             s5.downloadfileasync(new uri("https://dl.dropboxusercontent.com/something5"), "c:\something5")         end if         if (file.exists("c:\something6"))          messagebox.show("some message", "hi", messageboxbuttons.ok, messageboxicon.exclamation)         else             s6.downloadfileasync(new uri("https://dl.dropboxusercontent.com/something6"), "c:\something6")         end if         if (checkbox4.checked = true)         elseif (file.exists("c:\something7"))          messagebox.show("some message", "hi", messageboxbuttons.ok, messageboxicon.exclamation)         else             s7.downloadfileasync(new uri("https://dl.dropboxusercontent.com/something7"), "c:\something7")         end if         if (file.exists("c:\something8"))        messagebox.show("some message", "hi", messageboxbuttons.ok, messageboxicon.exclamation)         else             s8.downloadfileasync(new uri("https://dl.dropboxusercontent.com/something8"), "c:\something8")         end if         if (file.exists("c:\something9"))          messagebox.show("some message", "hi", messageboxbuttons.ok, messageboxicon.exclamation)         else             s9.downloadfileasync(new uri("https://dl.dropboxusercontent.com/something9"), "c:\something9")         end if         if (file.exists("c:\something10"))          messagebox.show("some message", "hi", messageboxbuttons.ok, messageboxicon.exclamation)         else             s10.downloadfileasync(new uri("https://dl.dropboxusercontent.com/something10"), "c:\something10")         end if         if (file.exists("c:\something11"))         messagebox.show("some message", "hi", messageboxbuttons.ok, messageboxicon.exclamation)         else             s11.downloadfileasync(new uri("https://dl.dropboxusercontent.com/something11"), "c:\something11")         end if         if (file.exists("c:\something12"))          messagebox.show("some message", "hi", messageboxbuttons.ok, messageboxicon.exclamation)          else             s12.downloadfileasync(new uri("https://dl.dropboxusercontent.com/something12"), "c:\something12")         end if         if (checkbox5.checked = true)         elseif (file.exists("c:\something13"))          messagebox.show("some message", "hi", messageboxbuttons.ok, messageboxicon.exclamation)         else             s13.downloadfileasync(new uri("https://dl.dropboxusercontent.com/something13"), "c:\something13")         end if         if (file.exists("c:\something14"))          messagebox.show("some message", "hi", messageboxbuttons.ok, messageboxicon.exclamation)          else             s14.downloadfileasync(new uri("https://dl.dropboxusercontent.com/something14"), "c:\something14")         end if         if (file.exists("c:\something15"))         messagebox.show("some message", "hi", messageboxbuttons.ok, messageboxicon.exclamation)          else             s15.downloadfileasync(new uri("https://dl.dropboxusercontent.com/something15"), "c:\something15")         end if         if (file.exists("c:\something16"))         messagebox.show("some message", "hi", messageboxbuttons.ok, messageboxicon.exclamation)          else             s16.downloadfileasync(new uri("https://dl.dropboxusercontent.com/something16"), "c:\something16")         end if         if (file.exists("c:\something17"))          messagebox.show("some message", "hi", messageboxbuttons.ok, messageboxicon.exclamation)         else             s17.downloadfileasync(new uri("https://dl.dropboxusercontent.com/something17"), "c:\something17")         end if     end if end sub 

your problem checking each file independently of last, , each if block has it's own message section.

this further clouded fact have same code repeated on , over. should move repeated code function , call it. additionally, can add files array object of sort, , use loop - way can add more files in future without having add blocks - hook sort of config file, etc, can avoid need changing code @ all.

private sub getfile(byval sourceurl string, byval targetfiledirectory string)     dim filename string = sourceurl.substring(sourceurl.lastindexof("/") + 1)     dim targetfilepath string = path.combine(targetfiledirectory, filename)       if (file.exists(targetfilepath))         messagebox.show("some message", "hi", messageboxbuttons.ok, messageboxicon.exclamation)     else         s1.downloadfileasync(new uri(sourceurl), targetfilepath)     end if end function  public sub server1()     dim items new dictionary(of checkbox, string)     items.add(checkbox1, "https://dl.dropboxusercontent.com/something1")     items.add(checkbox2, "https://dl.dropboxusercontent.com/something2")     items.add(checkbox3, "https://dl.dropboxusercontent.com/something3")     items.add(checkbox4, "https://dl.dropboxusercontent.com/something4")     '..... add more required.      dim targetdirectory string = "c:\"      each chk checkbox in items.keys          if chk.checked             dim url string = items(chk)              getfile(url, targetdirectory)         end if      next      if (checkbox1.checked = true)         each item string in items             getfile(item, targetdirectory)         next     end if end sub 

i've taken liberties refactoring make more generic, functionally, doing @ moment.

it becomes clear now, each file separate each other, messages keep on coming.

theres couple of ways deal this:

  • you display first failure, , stop download of every other file. isn't ideal
  • you inform user messages existed
  • you return list of files existed

i'll demonstrate last:

''' <returns>true if successful</returns> private function getfile(byval sourceurl string, byval targetfiledirectory string) boolean     dim filename string = sourceurl.substring(sourceurl.lastindexof("/") + 1)     dim targetfilepath string = path.combine(targetfiledirectory, filename)      dim retval boolean = false      if file.exists(targetfilepath) = false         s1.downloadfileasync(new uri(sourceurl), targetfilepath)         retval = true     end if      return retval end function  public sub server1()     dim items new dictionary(of checkbox, string)     items.add(checkbox1, "https://dl.dropboxusercontent.com/something1")     items.add(checkbox2, "https://dl.dropboxusercontent.com/something2")     items.add(checkbox3, "https://dl.dropboxusercontent.com/something3")     items.add(checkbox4, "https://dl.dropboxusercontent.com/something4")     '..... add more required.      dim targetdirectory string = "c:\"      dim existingfiles new list(of string)      each chk checkbox in items.keys          if chk.checked             dim url string = items(chk)              if getfile(url, targetdirectory) = false                 existingfiles.add(url)             end if         end if      next      if existingfiles.count > 0         dim msg string = string.format("one or more files exist locally, , have not been downloaded: {0}", string.join(vbcrlf, existingfiles))          messagebox.show(msg, "hi", messageboxbuttons.ok, messageboxicon.exclamation)     end if end sub 

you might alternatively have textbox (or similar) on form add failure messages to, rather having messagebox pop up. if did this, add text textbox within loop, rather waiting until end.

update

based on requirements comment. appears have checkbox relates each individual file. can handle making tweak method check checked value, , changing list dictionary containing checkboxes. have updated code blocks above accomodate this.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -