vb.net - WPF/VB ComboBox SelectionChanged Fires Twice -


i new wpf. project working on has requirements use vb language , flat files data (i can not change requirements). found (2) books on wpf vb compared dozens in c , web searches scarce. hoping out there has insight on issue vb experience.

this cascading event on page. load combobox called unit. when selection made in unit, makes facility combobox available , loads data based on unit value selected. if unit changes again, clears facility box , reload based on new unit value.

my code below. confuses me results of load of facility combobox. loaded if reading of flat file looped twice. @ top of function code clear list before loading combobox. so, if if function called twice, code should technically clear list, load, clear list again , load. put in series of popups confirm selectionchange called twice , therefor load facility called twice.

i looked @ sender , e arguments , same in both calls not sure how trap , stop second call or triggering it.

oh, strange thing happened popups. placed in vb code, makes issue go away. see 1 occurrence of facility in combobox. if take away popups, 2 occurrences again.

anyone see missing? in advance review , assistance.

xaml:

<grid  x:name="gridtreat1" background="{themeresource applicationpagebackgroundthemebrush}">     <textblock x:name="lbltreat1title" horizontalalignment="left" margin="22,20,0,0" textwrapping="wrap" text="treatment details     1 of 3" verticalalignment="top" width="459" fontsize="36" fontweight="bold"/>     <textbox x:name="lblunit" text="unit:" horizontalalignment="left" margin="66,95,0,0" verticalalignment="top" width="141" fontsize="18" height="34" borderbrush="white" isreadonly="true" tabindex="0" istabstop="false"/>     <combobox x:name="cbounit" horizontalalignment="left" margin="234,95,0,0" verticalalignment="top" width="586" height="34" fontsize="18" selectionchanged="cbounit_selectionchanged">     </combobox>     <textbox x:name="lblfacility" text="facility:" horizontalalignment="left" margin="66,202,0,0" verticalalignment="top" width="141" fontsize="18" height="34" borderbrush="white" isreadonly="true" tabindex="0" istabstop="false"/>     <combobox x:name="cbofacility" horizontalalignment="left" margin="234,202,0,0" verticalalignment="top" width="586" height="34" fontsize="16" selectionchanged="cbofacility_selectionchanged">     </combobox>     <textbox x:name="lblphysician" text="physician:" horizontalalignment="left" margin="66,256,0,0" verticalalignment="top" width="141" fontsize="18" height="34" borderbrush="white" isreadonly="true" tabindex="0" istabstop="false"/>     <combobox x:name="cbophysician" horizontalalignment="left" margin="234,256,0,0" verticalalignment="top" width="586" height="34" fontsize="16">     </combobox>     <textbox x:name="lblstep2" text="------------------------------------------------------------------------------------------------------" horizontalalignment="left" margin="66,153,0,0" verticalalignment="top" width="754" height="34" fontsize="18" fontweight="bold" borderbrush="white" isreadonly="true" tabindex="0" istabstop="false"/>     <button x:name="cmdcancel" content="cancel" horizontalalignment="left" margin="104,502,0,0" verticalalignment="top" width="165" height="50" fontsize="18" />     <button x:name="cmdnext" content="next &gt;&gt;" horizontalalignment="left" margin="618,502,0,0" verticalalignment="top" width="165" height="50" fontsize="18"/>     <rectangle x:name="recpatient" fill="#fff4f4f5" horizontalalignment="left" height="57" margin="544,18,0,0" stroke="black" verticalalignment="top" width="329"/>     <textbox x:name="lblprn" text="prn:" horizontalalignment="left" margin="550,20,0,0" verticalalignment="top" width="42" height="20" background="#fff4f4f5" borderbrush="#fff4f4f5" isreadonly="true" istabstop="false" tabindex="0"/>     <textbox x:name="lblpatname" text="patient:" horizontalalignment="left" margin="550,41,0,0" verticalalignment="top" width="70" height="20" background="#fff4f4f5" borderbrush="#fff4f4f5" fontsize="14" isreadonly="true" istabstop="false" tabindex="0"/>     <textbox x:name="txtprn" horizontalalignment="left" height="11" margin="625,20,0,0" textwrapping="wrap" verticalalignment="top" width="235" background="#fff4f4f5" borderbrush="#fff4f4f5" fontweight="bold" fontsize="14" isreadonly="true" istabstop="false" tabindex="0"/>     <textbox x:name="txtpatname" horizontalalignment="left" height="27" margin="625,41,0,0" textwrapping="wrap" verticalalignment="top" width="235" background="#fff4f4f5" borderbrush="#fff4f4f5" fontweight="bold" fontsize="14" isreadonly="true" istabstop="false" tabindex="0"/>     <textbox text="confidential peer review privileged" horizontalalignment="left" margin="290,558,0,0" verticalalignment="top" fontweight="bold" foreground="red" borderbrush="white" fontsize="14" istabstop="false" tabindex="0" isreadonly="true"/>     <textbox x:name="txttest" horizontalalignment="left" margin="423,399,0,0" textwrapping="wrap" text="textbox" verticalalignment="top"/>     <textbox x:name="txttest2" horizontalalignment="left" margin="423,436,0,0" textwrapping="wrap" text="textbox" verticalalignment="top"/> </grid> 

vb code behind:

    private sub cbounit_selectionchanged(sender system.object, e selectionchangedeventargs) handles cbounit.selectionchanged     dim myunit string = ""     dim cb combobox     dim cbi comboboxitem     cb = ctype(cbounit, combobox)     cbi = ctype(cb.selecteditem, comboboxitem)     myunit = cbi.tag.tostring     select case globalvariable.gvaction         case "a"             call load_cbofacility(myunit)             me.cbofacility.visibility = xaml.visibility.visible             me.lblfacility.visibility = xaml.visibility.visible         case "e"             call load_cbofacility(myunit,"2")     end select end sub  private async sub load_cbofacility(byval myunit string, optional myissel string = "")     dim nextline string = ""     dim mytag string = ""     dim mykey string = ""     dim mycontent string = ""     dim localfolder windows.storage.storagefolder = windows.storage.applicationdata.current.localfolder     dim myfile storagefile = await localfolder.getfileasync("dicfacility.txt")      dim cb combobox     dim cbi comboboxitem     dim mymsg2 new messagedialog("1")     await mymsg2.showasync      cb = ctype(cbofacility, combobox)     cb.items.clear()      using reader streamreader = new streamreader(await myfile.openstreamforreadasync)         dim mymsg new messagedialog("2")         await mymsg.showasync         nextline = await reader.readlineasync         while (nextline <> nothing)             mykey = nextline.substring(0, 2)             mykey = mykey.trim             if myunit = mykey                 mytag = nextline.substring(7, 4)                 mytag = mytag.trim                 mycontent = nextline.substring(18, nextline.length - 18)                 cbi = new comboboxitem                 cbi.tag = mytag                 cbi.content = mycontent                 if myissel <> ""                     if myissel = mytag                         cbi.isselected = true                     end if                 end if                 cb.items.add(cbi)             end if             nextline = await reader.readlineasync         end while     end using  end sub 

revision question:

i got code working variation of think rachel suggested do. not elegant functions. if has cleaner way, open suggestions:

modified xaml adding dropdownclosed event:

<combobox x:name="cbounit" horizontalalignment="left" margin="234,95,0,0" verticalalignment="top" width="586" height="34" fontsize="18" selectionchanged="cbounit_selectionchanged" dropdownclosed="cbounit_closed" /> 

modified vb code:

in constants.vb file, created new variable called gvunitcount , set 0 initially. when drop down closes, count reset zero. when selection changed, runs once , if statement kicks out if increments higher.

private sub cbounit_closed(sender system.object, e object) handles cbounit.dropdownclosed     globalvariable.gvunitcount = 0 end sub  private sub cbounit_selectionchanged(sender system.object, e selectionchangedeventargs) handles cbounit.selectionchanged     dim myunit string = ""     dim cbunit2 combobox     dim cbiunit2 comboboxitem     cbunit2 = ctype(cbounit, combobox)     cbiunit2 = ctype(cbunit2.selecteditem, comboboxitem)     myunit = cbiunit2.tag.tostring     me.txtunittag.text = cbiunit2.tag.tostring      if globalvariable.gvunitcount = 0         select case globalvariable.gvaction             case "a"                 me.cbofacility.visibility = xaml.visibility.visible                 me.lblfacility.visibility = xaml.visibility.visible                 call load_cbofacility(myunit)                 me.cbophysician.visibility = xaml.visibility.visible                 me.lblphysician.visibility = xaml.visibility.visible                 call load_cbophysician(myunit)             case "e"                 call load_cbofacility(myunit)                 call load_cbophysician(myunit)         end select         globalvariable.gvunitcount = globalvariable.gvunitcount + 1     end if  end sub 

like said, not pretty functions. consistently getting 1 load of facility combobox way.

my solution still working other not prettiest of code solutions. giving rachel credit helping push me in direction working. don't see way give official credit because in comments. so, just, rachel.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -