最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

.net - Method won't modify controls within form when called from a usercontrol - Stack Overflow

programmeradmin8浏览0评论

I have a method sub inside my main form that when called, it should disable the buttons within the form. For some reason, when I try to call this method from a usercontrol within the main form, it will not disable the buttons. Despite this, it will still run all other code within the method without an issue, so I am sure that the method is being called correctly.

I have attempted various ways to get this to work and I have found that if I call the method within the form itself instead, it will disable the buttons, though this is not what I intend to do.

This is the method I coded below:

Public Sub ChangeButtonAccessibility(switch As Boolean)
    If switch = True Then
        MessageBox.Show("TEST")
        btnAudioMethod.Enabled = False
        btnVideoMethod.Enabled = False
        btnDocumentMethod.Enabled = False
        btnImageMethod.Enabled = False
    Else
        MessageBox.Show("TEST2")
        btnAudioMethod.Enabled = True
        btnVideoMethod.Enabled = True
        btnDocumentMethod.Enabled = True
        btnImageMethod.Enabled = True
    End If
End Sub

It is being called using:

ChangeButtonAccessibility(True)

As you can see, I placed some message boxes to test this and found that they will appear when the method is called from the usercontrol but won't disable the buttons. Again, if I call the same method from the form instead of the usercontrol, it will disable them.

It also will not work if I directly modify the buttons from the usercontrol instead of calling a method, in fact I originally tried doing that before switching to a method call.

I find this very strange. There may be a simple explanation to this, but after looking everywhere online I cannot find an answer.

I have a method sub inside my main form that when called, it should disable the buttons within the form. For some reason, when I try to call this method from a usercontrol within the main form, it will not disable the buttons. Despite this, it will still run all other code within the method without an issue, so I am sure that the method is being called correctly.

I have attempted various ways to get this to work and I have found that if I call the method within the form itself instead, it will disable the buttons, though this is not what I intend to do.

This is the method I coded below:

Public Sub ChangeButtonAccessibility(switch As Boolean)
    If switch = True Then
        MessageBox.Show("TEST")
        btnAudioMethod.Enabled = False
        btnVideoMethod.Enabled = False
        btnDocumentMethod.Enabled = False
        btnImageMethod.Enabled = False
    Else
        MessageBox.Show("TEST2")
        btnAudioMethod.Enabled = True
        btnVideoMethod.Enabled = True
        btnDocumentMethod.Enabled = True
        btnImageMethod.Enabled = True
    End If
End Sub

It is being called using:

ChangeButtonAccessibility(True)

As you can see, I placed some message boxes to test this and found that they will appear when the method is called from the usercontrol but won't disable the buttons. Again, if I call the same method from the form instead of the usercontrol, it will disable them.

It also will not work if I directly modify the buttons from the usercontrol instead of calling a method, in fact I originally tried doing that before switching to a method call.

I find this very strange. There may be a simple explanation to this, but after looking everywhere online I cannot find an answer.

Share Improve this question asked Feb 5 at 10:05 BorderLIGHTBorderLIGHT 31 bronze badge 6
  • 2 How does a UserControl know about child Controls of its Parent Form? -- This procedure is in any case wrong. If you have a Control in your UC that is meant to generate some effect in the Parent Form, the UC should raise a public event, notifying the User choice. The Parent Form can subscribe to that event, then perform some actions that involve its own child Controls, not the other way around (in normal conditions, i.e., the UC is not meant to be a subclassing vessel, an extender provider etc.) – Jimi Commented Feb 5 at 11:58
  • 1 Firstly, learn how to debug code. You should be using breakpoints, not message boxes. – jmcilhinney Commented Feb 5 at 13:36
  • 1 As for the issue, I'd wager that the code is actually being called on the wrong form. It's impossible to know for sure because you haven't provided enough code, but that would definitely explain why the code is running but the controls you expect to be affected are not. – jmcilhinney Commented Feb 5 at 13:38
  • 2 OT, there's no need for an If...Else there. You only need one block of code and just set all Enabled properties to Not switch. I would suggest that switch is a bad name for that parameter and the method name is bad too. I would suggest Public Sub EnableButtons(enable As Boolean) and then btnAudioMethod.Enabled = enable, etc. – jmcilhinney Commented Feb 5 at 13:41
  • 1 but why would you have a "general" user control that supposed to modify controls on the main form when you hard coded all the control names? Why not then ALSO put those controls into the user form then? I mean, why have to place both a user control and THEN also place controls with the exact defined name in the user control? Why not combine those controls into the user control then? Doing other wise makes no sense at all here, then, right? But, in comments, yes you can sink events from the main page into the user control, but you would be attaching events to those defined controls. – Albert D. Kallal Commented Feb 5 at 16:41
 |  Show 1 more comment

1 Answer 1

Reset to default 0

There are two common reasons you might have this issue:

  1. If ChangeButtonAccessibility() is called from another thread, you need to Invoke when setting control properties. You cannot set them directly, because the control memory belongs to the main UI thread. Certain kinds of control events can cause this in normal event methods, or sometimes when you go between different forms, and you can check by using the InvokeRequired property.

  2. Changing control properties does NOT immediately update the control on the screen. Rather, it goes through the Windows message pump in order to raise a Paint event before controls are redrawn. Until the code that calls your method returns execution flow back to the main message loop, you won't be able to see any changes. In other words, the method for the code that calls this method has to actually finish before you can see the changes.

Both of these issues are also often better solved by rethinking the design a bit.


Finally, as a side issue from the question we can simplify the code to reduce the size of the method by half:

Public Sub ChangeButtonAccessibility(switch As Boolean)
    MessageBox.Show(If(switch, "TEST", "TEST2"))
    switch = Not switch  
    btnAudioMethod.Enabled = switch
    btnVideoMethod.Enabled = switch
    btnDocumentMethod.Enabled = switch
    btnImageMethod.Enabled = switch
End Sub

And we can simplify it even further with a better name for the input boolean that avoids the need for negation.

发布评论

评论列表(0)

  1. 暂无评论