-1

I'm using VisualStudio 2008 on a system using .NET Compact Framework 3.5 in VB.NET and have a form that contains a couple of Components, namely Timers and Messageboxes. Part of my application is a sub that iterates through all the Controls in the form and adjusting properties like the Front or BackColor so that we can reskin the entire application on demand.

While I'm iterating through these Controls I'd also like to iterate through the Components and set some properties of the Messageboxes on the form. I've tried using a For Each to access Me.components.Components but that collection appears to be private.

For Each comp As Object in Me.components
  <do something>
Next comp

Is there a way to iterate through the components?

EDIT:

I was wrong thinking that me.components.Component is private. Using Me.components.Components I get the following error:

'Components' is not a member of 'System.ComponentModel.IContainer".

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • A `MessageBox` is not a component nor a control. It is a dialog created only when you call `MessageBox.Show()`. You most likely cannot find it using .NET code. It would rather require performing Platform Invocation on for example the WinAPI's `EnumWindows()` function. – Visual Vincent Jul 13 '16 at 22:47
  • After doing a little digging I found that in the Compact Framework the components are not actually related to the form itself. They can still be found elsewhere though, see these two answers: [**\[ 1 \]**](http://stackoverflow.com/a/14628749/3740093) [**\[ 2 \]**](http://stackoverflow.com/a/371829/3740093). – Visual Vincent Jul 14 '16 at 23:32

1 Answers1

0

You can loop throught timers with this: ....

For Each c As Object In Me.components.Components
        If TypeOf c Is Timer Then
            Dim tim As Timer = CType(c, System.Windows.Forms.Timer)
            tim.Interval = 12345
        End If
    Next

Change the .Interval = 12345 with anithing you want to do to the timers.

.... And through other controls with:

For Each ctrl As Control In Me.Controls
        If (ctrl.GetType() Is GetType(TextBox)) Then
            Dim txt As TextBox = CType(ctrl, TextBox)
            txt.BackColor = Color.LightYellow
        End If
    Next

Change the TextBox with the type of control

And .BackColor = Color.LightYellow with anything you want to do with the control.

Unfortunatly i don't know how to do is with MesseageBoxes :(

BanForFun
  • 752
  • 4
  • 15
  • `I've tried using a For Each to access Me.components.Components but that collection appears to be private.` \*Ahem\* \*ahem\*? :) – Visual Vincent Jul 13 '16 at 22:31
  • `Me.components` is private. `Me.components.Components` is not. And if that doesn't work, the second does. – BanForFun Jul 13 '16 at 22:34
  • ...only that your second code does not take components. I'm just asking because I'm writing from my phone, thus I cannot test anything. Either way, `Me.components` might be private but since `Me` is the form, there is no problem in accessing it (like you say). :) – Visual Vincent Jul 13 '16 at 22:38
  • Me.components doesn't work but it's not private, it's not declared – BanForFun Jul 13 '16 at 22:43
  • It's not declared?? What are you talking about? -- Also, you cannot find a `MessageBox` with this. For that you'd most likely have to consult P/Invocation and the WinAPI. :) – Visual Vincent Jul 13 '16 at 22:44
  • Sorry i meant its not a collection type ;) And I don't remember how to use P/Invocation. – BanForFun Jul 13 '16 at 22:55
  • P/Invocation is somewhat complicated. Don't bother in this case, because you can only find a `MessageBox` _if it's opened_. So what the OP seems to be trying to do with message boxes is not possible. – Visual Vincent Jul 13 '16 at 22:59
  • ' For Each c As Object In Me.components.Components ' does not work. I updated my original question with the error message. – Dennis Groome Jul 14 '16 at 12:28
  • All code is on the same Class for the same Form. In fact, it's right behind the `For Each` loop I use to iterate through the `Controls` on the form. Could this be a limitation of using .NET Compact Framework? – Dennis Groome Jul 14 '16 at 13:45
  • It's listed at the start of the question and it's a tag. I'm using CF3.5 – Dennis Groome Jul 14 '16 at 13:57