0

Whenever I try to say the volume of the media player like this:

Private Sub SndMasterSlider_ValueChanged(sender As Object, e As EventArgs) Handles SndMasterSlider.ValueChanged
    Player1.settings.volume = SndMasterSlider.Value
End Sub

I get this error: An exception of type 'System.Windows.Forms.AxHost.InvalidActiveXStateException' occurred in AxInterop.WMPLib.dll but was not handled in user code

alexanderd5398
  • 332
  • 1
  • 4
  • 16

1 Answers1

2

Assuming that the Slider is the TrackBar control, you will get that exception when creating the form (you did not say where, but I'll bet that is where). This is because the Designer code will set the Value thus firing the ValueChanged event before it has created your AxWMP control:

 Me.TrackBar1.Value = 50

This will fire the event even though the form is being created. You can set a flag to indicate when to process the ValueChanged event, manually add the handler or just use the Scroll event which will fire when the user actually moves the thumb.

Private Sub TrackBar1_Scroll(...
    AxWMP.settings.volume = TrackBar1.Value
End Sub
Ňɏssa Pøngjǣrdenlarp
  • 38,411
  • 12
  • 59
  • 178
  • The scroll event does not work for some reason. Whenever I scroll the trackbar, nothing happens. (It's a `gTrackBar`, not sure if that changes anything. – alexanderd5398 May 30 '14 at 19:29
  • gTrackBar sounds like a variable name. The standard MS/NET TrackBar uses the Scroll event as the default event (click on the control and it opens that event), if yours opens to ValueChanged, it might be a different control. The other alternative is to use a flag that starts as False, set it to true at the end of Form Load; then at the top of the ValueChanged event add: `If _FormLoadedFlag = False Then Exit Sub`. this will prevent it from trying to set the volume before the AxWMp control has been created. – Ňɏssa Pøngjǣrdenlarp May 30 '14 at 20:17