Bloom326984

Hi there,

I am getting a strange error with my SplitContainer when trying to manually set the SplitterDistance value.

The SplitContainer.Orientation is set to Orientation.Horizontal, the SplitContainer.Panel1MinSize is set to 25, the SplitContainer.Width is set to 150, and the SplitContainer.Panel2MinSize is set to 25.

When I try to set the SplitContainer.SplitterDistance value to 100, I get an error saying "SplitterDistance must be between Panel1MinSize and Width - Panel2MinSize". However, I am doing just that.

Any ideas why I am getting this error message

The exception message is of type InvalidOperationException. According to my MSDN this exception means "The value is incompatiable with the orientation". However, MSDN specfically states that SplitterDistance is valid for use with both the Orientation.Horizontal and Orientation.Vertical values.

Also, I should note that I only get this error message when I try to manually set the SplitterDistance value after having added the SplitContainer to another Control. If I set the SplitterDistance value before adding the SplitContainer to the parent Control, I don't get the error message. However, the SplitterDistance value doesn't actually change either.

Any help would be appreciated. Thank you



Re: Windows Forms General Splitter Distance Error

element109

The splitter itself has a width, that is why you are getting an error.

150-25-25-100=0 there is no width left for the splitter itself.





Re: Windows Forms General Splitter Distance Error

Bloom

Thank you for the response. However, this doesn't seem to be the solution to my problem.

The SplitContainer.SplitterWidth is set to 5. I tried setting the SplitterDistance value to 50. A value of 50 should more then compensate for the SplitterWidth value, but I still get the error.

However, I am not quite sure how the SplitterWidth actually effects the SplitterDistance value. The error message says "SplitterDistance must be between Panel1MinSize and Width - Panel2MinSize". Not taking the SplitterWidth into account, that would mean values for SplitterDistance are valid in the range from 25 - 125. But, what would the range be with the SplitterWidth taken into consideration 30 - 120

Thank you again for any help you can provide





Re: Windows Forms General Splitter Distance Error

nobugz

There is a nasty bug in the SplitContainer control that causes this exception. The only known workaround is to set the SplitterDistance value after the InitializeComponent() call or in the form's Load event. Check this thread for reference.





Re: Windows Forms General Splitter Distance Error

Bloom

Thank you Nobugz for the response. I am glad to know that this wasn't some silly math error on my part Smile I am still having some difficulties though.

First off, my settings were actually already being done after the Form's call to InitializeComponent.

I should note that I am not using the designer to create this SplitContainer (or any of the other Controls for that matter). I am creating them in code and manually attaching them to the appropriate parent Controls. All of the allocation, settings adjustments, and placement is being done after the Initialize Component call. I am not sure if this changes anything, but just letting you know.

I read the thread that you reffered me to and found that another possible solution is to set my SplitterDistance value in the Form's Load event. Therefore, I added a Load event, and in that Load event I then created all my Controls and set all their values (including the problematic SplitterDistance value). I still get the error message.

I tried moving the SplitterDistance assignment around to various locations to see if it would effect anything. I have tried setting the SplitterDistance right after allocating the SplitContainer but before adding the SplitContainer to a control, didn't help. I tried setting the SplitterDistance after adding the SplitContainer to it's parent Control, didn't help. I also tried setting the SplitterDistance value after adding all of the various Controls to their various parent Controls, didn't help either.

If you have any other suggestions, I would very much appreciate them. Thank you again for your time.





Re: Windows Forms General Splitter Distance Error

nobugz

I cannot reproduce the exceptions with the values you gave. Please provide the values of the properties that are shown in bold in the Properties Window.





Re: Windows Forms General Splitter Distance Error

Bloom

Hi nobugz. I am not using the designer to place these Controls and therefore apparently can't view the SplitContainer values via the PropertiesWindow. So, if I forget to provide you with a value please just let me know. I think the values you want are:

Dock = Fill

Location = (0, 0)

Size = (Width 150, Height 25)

SplitterDistance = 71

Orientation = Horizontal

SplitterWidth = 5

Panel1MinSize = 25

Panel2MinSize = 25

These are the values right before I try to modify the SplitterDistance value. Trying to set the SplitterDistance manually to anything other then 71 generates an error message.

Thank you again for your help with this





Re: Windows Forms General Splitter Distance Error

nobugz

Your SplitContainer is not nearly tall enough at 25 pixels. Ignore the mention of "width" in the error message, with Orientation = Horizontal, it is the Height that counts.





Re: Windows Forms General Splitter Distance Error

Bloom

Hello again nobugz. Once again, thank you for your assistance.

The SplitContainer.Dock is set to Full. Setting the SplittContainer.Height value manually to allow for a larger SplitterDistance value is not an option. Setting the Height value manually does nothing and I assume this is because my Dock is set to Full.

After allocating and attaching the SplitContainer to it's parent Control, the SplitContainer.Size value is 150,25. However, the 'docking' has not yet occured at this point. I don't know when this 'docking' actually takes place, but after it is done the SplitContainer.Size value is 391,402. I have verified this by setting a breakpoint on some arbitrary button press and viewing the values. If there was some way to find out when the 'docking' was completed, I could hopefully set my SplitContainer.SplitterDistance value there. I am going to look into this. This seems like a lame fix, but at this point I don't much care Smile

Thank you





Re: Windows Forms General Splitter Distance Error

Bloom

Ok, I figured it out.

In order to prevent the error message and actually be able to adjust the SplitterDistance value I had to do everything in the following order:

InitializeComponent()

...

Allocate SplitContainer

....

Dock = Full

Orientation = Horizontal

Height = Some Large Value

SplitterDistance = Desired Value

...

Add SplitContainer to parent Control

If I did any of those steps out of order, I either got the error message or the values just wouldn't change =/

Thank you nobugz for all your help