Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
487 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 Answer

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 :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.8k users

...