-1

I am trying to run the code,in windows form using vb.net where I have multiple pictureboxes with properties like backgroudimage and location.

So I want to eliminate these repeated initilisations,and hence I am trying to initialise all the controls using GetType.Getproperties command. But I am getting an exception error

Private Sub Read_csv_file_itextbox_Load_1(sender As Object, e As EventArgs) Handles MyBase.Load
    For Each c As Control In Me.Controls
        If c.GetType().Name = "TextBox" Then
            c.GetType().GetProperty("BackColor").SetValue(c, "Transparent", Nothing)
        End If
    Next
End Sub

And I also need to explicitly set properties for some textboxes That have naming TextBox1,TextBox2, Textbox3 so on.

And I want to access these Textbox in their even number indexing and perform my code.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • 2
    "I am getting an exception error" is not an appropriate problem description. Why "TextBox" appears in this code is unguessable. TextBox does not support transparency effects like PictureBox does. Eliminate duplicate code by deriving your own class from PictureBox, setting the properties in the constructor. – Hans Passant Sep 25 '21 at 11:09
  • Thank you for correction. Yes , the textbox doesn't support Transparency. And the SetValue(c, color.Red,Nothing) gives no error and the bg is changed to Red. – Sindhu Halesh Sep 27 '21 at 05:01

1 Answers1

1

The proper way to do what you're trying to do in the code you posted would be this:

For Each tb In Controls.OfType(Of TextBox)()
    tb.BackColor = Color.Transparent
Next

Don't use Reflection without good reason.

Note that setting the BackColor of a TextBox to Transparent doesn't necessarily make sense, but you can use this same principle on any type of control and any property and any value.

If you want to filter any list using LINQ then you call the Where method. It's up to you to determine what the condition(s) is that you want to filter by and write the appropriate Boolean expression, e.g.

For Each tb In Controls.OfType(Of TextBox)().
                        Where(Function(tb) Enumerable.Range(1, 10).
                                                      Where(Function(n) n Mod 2 = 0).
                                                      Select(Function(n) $"TextBox{n}").
                                                      Contains(tb.Name))
    tb.BackColor = Color.Transparent
Next

That will first generate a list of TextBoxes and then filter them on Name. It will include only those with a Name of TextBoxN where N is an even number in the range 1 to 10. This is a perfect example of determining the logic first, i.e. working out what the code needs to do, and then writing code to do that specifically.

jmcilhinney
  • 50,448
  • 5
  • 26
  • 46
  • @dr.null, that's what the OP is doing in the code they posted in the question, so I just went with that. The principle holds regardless of the type of the controls, the property being set and the value it's being set to though. – jmcilhinney Sep 25 '21 at 13:58
  • The TextBox Control doesn't support background transparency, so trying to set it only generates exceptions, no matter whether reflection or other means are used. Applies to other Controls. – Jimi Sep 25 '21 at 18:15
  • Thank you for your inputs, Now i am able to change the background color. I am also looking how to access certain Textbox controls in a group of Textbox controls. For eg; I have 10 textbox controls, and I want to edit the background of the textbox control with which contains even number as title, (TextBox2,TextBox4,TextBox6,TextBox8,TextBox10) and another color for odd numbering of TExtboxes (TextBox1,TextBox3,TextBox5,TextBox7,TextBox9). How can I access the indexing? Looking forward . Thank you in advance – Sindhu Halesh Sep 27 '21 at 05:02
  • @SindhuHalesh, the `OfType` method filters by type and you can use the `Where` method can be used to filter by any other criteria, e.g. `Controls.OfType(Of TextBox)().Where(Function(tb) Enumerable.Range(11, 10).Select(Function(n) $"TextBox{n}").Contains(tb.Name))` will get you all `TextBoxes` with names from `TextBox11` to `TextBox20`. If you want `n` to be only odd or only even then add that as a criterion in the `Where` method. – jmcilhinney Sep 27 '21 at 05:06
  • @SindhuHalesh, see the addition to my answer. – jmcilhinney Sep 27 '21 at 05:16
  • @jmcilhinney. Below is my code `Dim tb As TextBox For Each Picture In Controls.OfType(Of TextBox)().Where(Function(tb) Enumerable.Range(1, 4).Where(Function(n) n Mod 2 = 0).Select(Function(n) $"TextBox{n}").Contains(tb.Name)) tb.BackColor = Color.Red Next` And i am not seeing any changes in the background color, can u pls tell me where I am going wrong? **tb1,tb2,tb3,tb4,tb5 are the titles of my Textbox in Designer Tab** – Sindhu Halesh Sep 27 '21 at 05:57
  • @jmcilhinney. I am a newbie to the Visual Studio Platform. Nevertheless, the code is now working. Thanks to you. I appreciate your time and patience. :-D – Sindhu Halesh Sep 27 '21 at 06:25