-2

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.

4
  • Use My.Settings(h & i). Commented Jan 1, 2017 at 2:22
  • Doesnt work.... Commented Jan 1, 2017 at 2:32
  • Thank you. Solved. Commented Jan 1, 2017 at 2:50
  • Then another developer will try to improve readability of your code and change name of setting or change name of TextBox - and your application will stop working :) Use right tool for the job - Dictionary with shared key, where settings can be retrieved for correspondent textbox by the key Commented Jan 1, 2017 at 17:28

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

1

If this is a WinForm project, all of the controls are included in the form's Controls collection, which is accessible by name, like this:

For x As Integer = 300 to 400
Me.Controls("My.Settings.h" & i.ToString()).height = Me.Controls("TextBox" & i.ToString()).Text
Next

1 Comment

@AhmedAbdelhameed : It would also be good telling him what it actually is too. -- @Zenoheld : My.Settings is a class/object used to handle, store, and retrieve settings specific to the application. Read more on the MSDN documentation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.