Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
507 views
in Technique[技术] by (71.8m points)

vb.net - Using variable in for next loop code

I have following code,

My.Settings.h301 = TextBox301.Text
My.Settings.h302 = TextBox302.Text
My.Settings.h303 = TextBox303.Text
My.Settings.h304 = TextBox304.Text
My.Settings.h305 = TextBox305.Text

I want to convert above code like following;

For i = 301 To 305 Step 1
    My.Settings.h & i = TextBox & i.Text
Next

So, please provide me correct for next loop code. Thank you.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • To pass a string as a control name, you can use: ParentControl.Controls("ControlName").
  • To pass a string as an application setting name, you can use: My.Settings("SettingName").

Hence, your code should look something like the following:

For i = 301 To 305 Step 1
    My.Settings("h" & i.ToString) = Me.Controls("TextBox" & i.ToString).Text
Next

Please note that if the parent of your textboxes is not the form, you'll need to replace Me with the parent control name.

Hope that helps :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...