There is a main form: mainform, which has a button named btnshow and a label control named lbinfo. Its text is "hello ". Click btnshow. The subform exerciseplan named chdform is displayed, and the exerciseplan is displayed in a non-modal dialog box (that is, chdform. show (). The child form contains N textbox controls named txtflighplanindex and txtstarttime_fpl. I first let txtstarttime_fpl get the focus, and then click the lbinfo control of the main form. Then, the text of lbinfo will be assigned to txtstarttime_fpl, I want to use a computer to automatically determine which control in the subform is used to obtain the focus, and then decide which control to assign the text of lbinfo? The description just now shows that the computer knows that txtstarttime_fpl gets the focus, so the text of lbinfo will be assigned to txtstarttime_fpl. The following example uses two textbox to obtain the focus in the textbox and then click lbinfo in the main form. In this case, the textbox loses the focus, at the same time, the text value of lbinfo is also assigned to the textbox with just lost focus.
Code in the subform:
/// <Summary> <br/> // used to record which textbox loses the focus, in this way, when you click the main form, the obtained text value is assigned to the textbox that just lost <br/> // focus <br/> /// </Summary> <br/> private control ctrrawstfocus = NULL; <br/> Public exerciseplan (mainform parent) <br/>{< br/> This. owner = parent; <br/> initializecomponent (); </P> <p> // register the event handler function <br/> (mainform) This. owner ). lbinfo. click + = lbinfo_clicked; <br/> (textbox?this.txt flighplanindex ). lostfocus + = textbox_lostfocus; <br/> (textbox+this.txt starttime_fpl ). lostfocus + = textbox_lostfocus; <br/>}
/// <Summary> <br/> // responds to the lbinfo Click Event of the main form, match the lostfocus event of the textbox of the child form <br/> /// that is, retrieve the textbox with just lost focus in the textbox_lostfocus event handler function and save it in ctrlostfocus, then, in the <br/> // plbinfo_clicked event handler, retrieve the textbox -- ctrlostfocus that has just lost the focus and assign a value to the text. <br/> /// </Summary> <br/> // <Param name = "sender"> </param> <br/> // <Param name = "E"> </param> <br/> private void lbinfo_clicked (Object sender, eventargs e) <br/>{< br/> If (this. ctrlostf Ocus! = NULL) <br/>{< br/> textbox edit = ctrlostfocus as textbox; <br/> edit. TEXT = lbinfo. text; <br/> This. ctrlostfocus = NULL; <br/>}</P> <p >}< br/> /// <summary> <br/> // responds to the lostfocus event of the textbox, save the textbox with just lost focus in ctrlostfocus, this control can be obtained from the lbinfo_clicked event handler <br/> /// </Summary> <br/> /// <Param name = "sender"> </param> <br/> // <Param name = "E"> </param> <br/> private void textbox_lostfocus (Object sender, eventargs e) <br/>{< br/> ctrlostfocus = sender as control; <br/>}
In this way, you can obtain information from the main form and assign it to the control of the subform. In other cases, this method can be used as a reference.