vb.net - How do I pass a value from one TextBox to another TextBox in a different form? -


currently have textbox on first form called txtuserid , want pass value of textbox called useridtextbox on second form.

but when try run code below, nothing gets passed textbox on second form. i'm wondering how can pass value 1 form form?

here code:

private sub cmdlogin_click(sender object, e eventargs) handles cmdlogin.click     try         if cn.state = connectionstate.open             cn.close()          end if         cn.open()         cmd.commandtext = "select userid,state registration userid= " & _             "'" & txtuserid.text & "' , state='" & txtpw.text & "'"          dim dr oledb.oledbdatareader         dr = cmd.executereader         if (dr.hasrows)             while dr.read                  ' problem:                  ' code shows 2nd form useridtextbox value doesn't change?                 dim obj new sale                 obj.useridtextbox.text = txtuserid.text                 obj.show()             end while         else             msgbox("invalid username or pw")         end if         cn.close()     catch ex exception      end try end sub 

as general rule, it's not idea try accessing object/forms controls directly. instead, better way pass text in 1st form's textbox custom constructor on 2nd form (the sale one). constructor on 2nd form responsible setting value of textbox .

here example of 1 way this:

sale.vb

public class sale      dim secondforminputtext string      public sub new(inputtextfromfirstform string)     initializecomponent()      ' set class variable whatever text string passed form     secondforminputtext = inputtextfromfirstform     end sub      private sub sale_load(sender object, e eventargs) handles mybase.load     ' set textbox text using class variable     useridtextbox.text = secondforminputtext    end sub  end class 

login.vb

private sub cmdloginexample_click(sender object, e eventargs) handles cmdlogin.click     dim obj new sale(txtuserid.text)     obj.show() end sub 

so instead of setting sale form's textbox directly, can pass text on 1st form constructor of 2nd form. constructor can save text received class variable rest of 2nd form can use.

one of main benefits of this, if in future change textbox richtextbox or possibly control might not have text property, won't have go updating every single piece of code tries set textbox value directly.

instead can change textbox other control, update sales form once whatever changes need work new control, , none of code on other forms should need changed.

edit:

even though question how pass textbox value 1 form form, may read comments under question. in particular, plutonix had helpful advice on how can improve database code might of use you.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

php - How do you embed a video into a custom theme on WordPress? -