vb.net - VB grab text from selected form -
i wondering how grab text textbox in selected form. have mdi form contains x number of kid forms. each kid form has textbox in text. when nail save button how know form selected , how grab text textbox.
private sub forms_clicked(sender object, e eventargs) handles forms.clicked globalvar = sender end sub i thinking create event detects when ever form clicked , save it's form/form id in global variable. thoughts?
like bodjo said, have [yourmdiform].activemdichild property.
what unclear, plutonix said save "button". did meant :
a save menuitem menustrip on mdi parent form ?
or save button within mdi kid form (or either form not part of mdi parent/child system)
the first case pretty straightforward : utilize .activemdichild of mdiparent form. secund may need valid way point actual active kid form (similar 1 tried globalvar... there improve ways it.
by way, when targeted mdichild through .activemdichild, must access textbox public, friend or protected variable. usually, controls in forms private. may have :
public class [yourmdichildclassname] ' ... ' create public property public readonly property contenttosave() string homecoming [yourtextbox].text end end property ' or create textbox public in form design public [yourtextbox] textbox ' ... 1 or other, not both. ' ... end class another way access "active" mdichild, assuming of mdichild instances of same class create static (shared) property in kid class :
public class [yourmdichildclassname] ' ... private shared _currentmdichild [yourmdichildclassname] = nil public shared readonly property currentmdichild() [yourmdichildclassname] homecoming _currentmdichild end end property ' ... end class and using same thing tried, using .activated instead of .clicked
public class [yourmdichildclassname] ' ... private sub mychildform_activated() handles me.activated _currenmdichild = me end sub ' , that's method should contain. ' if seek add together other activation tricks, activating form, ' or firing dialogbox (worst case), ' go wrong , application hang in endless loop !!! ' carefull when using .activated. ' ... end class then access active mdichild using static property :
[yourmdichildclassname].currentmdichild ' => gives straight valid instance of mdi kid (or nil !) ' can utilize in mdi parent : if [yourmdichildclassname].currentmdichild isnot nil dim texttosave string = [yourmdichildclassname].currentmdichild.contenttosave ' ... save text... end if however, because you've created static (shared) pointer lastly active mdichild (sort of, know it's not pointer in c) you must update pointer whenever close mdichild form !
public class [yourmdichildclassname] ' ... private sub [yourmdichildclassname]_formclosing( _ sender object, e formclosingeventargs) handles me.formclosing if _currentmdichild me ' , "me" closing right now... _currentmdichild = nil ' don't want memory leak or pointer form has been closed ! end if end sub ' ... end class we not supposed create custom way of handling kid form activation. mdiparent.activemdichild there that. however, 1 time want access extended/custom/specific properties of kid form doesn't exists in system.windows.forms.form, need cast mdiparent.activemdichild real form derived type of our mdichild. that's 1 other way it, it's me : don't castings much. set ide :
option explicit on alternative strict on alternative infer off forms.clicked ... event (clicked) exist ? i'm aware of .click not "clicked". anyway :
public class [yourmdichildclassname] ' ... private sub mychildform_clicked() handles me.click ' part executed when click within form, ' @ location there no control. ' if click on control, textbox, picturebox, panel, etc ' block not executed you've clicked on control, ' not on form ! end sub ' ... end class vb.net textbox mdi
No comments:
Post a Comment