Unable to Move Control on Windows Form Designer [duplicate] - vb.net

Setup:
I have created a Form that I wish to have serve as the base from which I will inherit other forms. This base form serves as a "template" of sorts, but it also provides a good deal of functionality related to the structure, as well as the interrelation of all of the controls provided.
A primer for the images that follow... The top info-colored bar is a custom control inherited from ToolStrip. The bottom strip is another custom, again inherited from ToolStrip. The left white block is a TreeView and the right block is a TabControl (having deleted all TabPages from it...I intend for these to be added in the inherited forms).
Image of base form in designer:
Image of inherited form in designer:
Clearly, the only difference is that when I open the inherited form, I get a little box icon superimposed over each control, and when I click them, I get the padlock telling me I cannot edit.
The problems:
All controls on the inherited form are locked. I have researched the issue of visual inheritance, and as far as I can tell, I'm not using any controls that expressly do not support it, as this link suggests there are. In this Q&A, Hans suggests changing the modifier on those controls, which I have done. In fact, I tried both Public and Protected, all to no good result.
I am stumped.

This is a technical restriction in the designer, it is specific to the SplitContainer control you are using. And some other ones. The trouble-maker is the ISupportInitialize interface.
Controls use this interface when they can't afford the properties of the control to be assigned in an arbitrary order. The designer helps when it sees that the control implements this interface, it calls the BeginInit() method when it starts assigning properties, EndInit() when it is done. The control uses these methods to delay the side-effect of property assignments, the EndInit() method makes them effective. Important for SplitContainer, the minimum sizes of the panels also affect the splitter position.
Perhaps you can see the rub, the InitializeComponent() method in the base form class has already called ISupportInitialize.EndInit(). So modifying properties again in the derived form class is unlikely to turn out well. The designer protects the control from this by locking it.
Very inconvenient, there is no simple workaround. If modifying the SplitContainer in the derived form class is a hard requirement then you'll have to give up on inheriting it or write the code by hand in the derived class constructor.

Related

Cannot add new controls when creating a composite user control

I am trying to broaden my knowledge of user controls or to be more specific, composite user controls. Msdn has a walkthrough on the subject here which although not in VB is easy enough to follow and get results.
What I had had in mind was to create a base user control comprised of a split panel, one half of which would be used to display dynamic help and the other half which could house whatever controls the new user control which would inherit from this required. The problem that I am facing is that when I then create a new inherited control based upon my base control I cannot add new controls to the design surface. If I don't have a split panel filling the entire design surface of the base control I can add new controls, but if I do I can't.
Either I am doing something wrong, or more likely failing to do something on the base control that I ought to do , or this can't be done which I find odd to believe. I'm hoping someone can explain what it is I've missed.
This is being targeted at winforms with vb.
Thanks
Well, Did you make the split panel's modifier to protected or protected internal or public? That should do the trick.
By default it is private and so designer will prevent you from accessing it.

Can one move controls around in a form class derived from another form?

I have created a forms which are derived from another form thus:-
Public Class MyForm
' ...etc
End Class
Public Class MyDerivedForm
Inherits MyBaseForm
' ...etc
End Class
Public Class MyOtherDerivedForm
Inherits MyBaseForm
' ...etc
End Class
This works quite nicely and I can add controls to the derived form using the form designer. But I'd like to move some of the inherited controls around a bit on MyDerivedForm without disturbing MyBaseForm or MyOtherDerivedForm. I can't see any way of doing this on the forms designer.
Is it possible to do this (preferably with the designer but with code if necessary)?
This is not a typical VB.NET problem so not so sure what's going on here. You'll get the lock icon on the inherited controls and a grayed-out Properties window for an inherited control when the Modifiers property of the control in the base class is Private. The Winforms designer observes the accessibility of the base class member. Private members can't be messed with. The default value for Modifiers is Friend in VB.NET, Private in C#.
Make it Friend to allow the derived form class to modify the control properties. If the base form class lives in another assembly then Friend isn't good enough, you'll need Public.
The Anchor property can be an issue, but only if you anchor to the right or bottom. The control has a knack for ending up in the wrong spot when the derived form has a different size from the base form if the control is anchored that way. Simply avoided by not anchoring at the right/bottom in the base class and changing the anchor in the derived class.
In my VB.NET WinForms application I have inherited forms and I can just grab the inherited controls in the designer and move them about as I would with non-inherited controls on the form.
However I have noticed two things in the past that stop this. If you change the position of the controls on the base form, or change some of the positioning properties (such as anchor or docking), then this can (but not always) move your inherited controls. Also I couldn't move some of my inherited controls in an earlier version of .NET (2.0 I believe), but I never figured out the cause for that so I had to resort to changing the locations via the property grid.

combining very similar controls in silverlight

I have two controls. The XAML's are big and very similar. One difference is this: they contain a listbox, in one control, it's bound to {StaticResource X}, and is multiselect, the other is bound to {StaticResource Y}, and is not multiselect. The code-behinds are also very similar. How should I combine these two classes into one? I thought about creating a base class and deriving my 2 controls from it, but I have no idea how to do that with XAML. I know I could make it easier if I set the differing properties in code instead of XAML (in which case the XAML's would become identical), but let's consider that plan B. Silverlight has no StyleSelector, it seemed like a possible solution though. Maybe VisualStateManager could do it, except it sounds bad, because my problem has nothing to do with visuals, but maybe I could define 2 states anyway. Except I think SL doesn't support binding in style definitions. Tough question for a beginner like me...
You should look into creating custom controls and using AlternateContent properties. Look these up and you'll find hundreds of tutorials.
Here's a quick google search to get you started with alternate content.
So, to sum it up, I want one control which can work in somewhat different modes, or states. The mode can affect XAML properties and code logic, too.
It seems like VisualStateManager is very limited in which properties it can manipulate. But when the differences are only visual, it's the best choice.
When there are other differences in XAML, then the obvious choice is to omit those properties from XAML and set them in code, like in the ctor. A nicer way is to expose those properties as dependency properties in code, bind to those properties in the XAML of the user control, and then you can specify those properties in other XAML's where you use this user control. When your control doesn't care what's in those properties, then it's a good design choice, too. In my case, though, when setting up those differing properties should be the responsibility of the user control itself, not its parent, and I want to expose a single mode property only, it's not good.
For this case, the best way I found so far is this:
create a normal user control (XAML+code), expose the differing properties (simple, not DP's) and bind to them in XAML
make this user control abstract, and possibly some properties, too
for each different mode the control needs to support, derive a class from this base control (code only, no XAML), provide implementations for the abstract properties
instead of using the base control in other places, use one of the derived implementations
This way, you can easily specify from outside which mode you want your control to work in. The drawback is that it's not easy to change the mode, since it's not a property you need to change but the type and instance of the control.
And finally, when there are code logic differences, too, then one way is exposing a mode property, or using the abstract class method I described above. For example, a button click handler function can be abstract, too.

how to do multiple pages in VB.NET form

I've inherited some VB.Net code that I think needs some restructuring. The project has three forms, each of which is its own Windows Form file that inherits from System.Windows.Forms.Form.
The problem is that all these forms share a common navigation menu bar that does not change as the user switches between forms, and so the original programmer has duplicated the menu code in each of the three files to generate the menu on each one! I figure this can't be right.
To restructure it, I thought I would create a base form that implemented the menu, and then let the other forms inherit from that, but I ran into the problem that Windows forms already inherit from the class mentioned above, and can't inherit from another class.
I noticed I can add an item called "inherited form", but is that the way to go here? The problem of creating multiple screens with a common menu bar has to be incredibly common. Is there one true way to do this? Should I use inherited forms, or should I by have just one base form and make the other screens just plain classes and not forms at all? Or something else I'm not thinking of?
Depending on the specifics; you might want to consider using MDI forms.
Another option I've seen is having the menu/shared toolbars be encapsulated in a UserControl and used where desired.
Make a class that inherits from Form class and add the common menu functionality there. Then make your three forms inherit from this new class instead of Form.

Can I change properties of inherited controls at design time?

I am using visual inheritance and was wondering if there is a way to change the properties of inherited controls at design time, preferably in the form designer. If not, then in the designer code.
I have my control declared as Public in the base class. I can access it in the child form code, but not in the form designer. Is this just not possible?
There are limitations placed within visual studio for visual inheritance. Unfortunately, derived forms\usercontrols cannot modify controls containing collections within the base, namely DataGridViewRows, ToolStrips, ListViewColumns, etc.
Microsoft Bug Report
There are ways around this in certain situations. Create a protected property in the base class that exposes the exact properties of the control you wish to modify (DataGridView.BackgroundColor, or ListView.Columns).
Your base form should be able access this property to change the components needed. I've done this for ListView.Columns, and DataGridView.rows successfully, however ToolStrip.Items would not work.
It seems to work only for certain controls, but not all and I can't understand why. On my base form I have a TabControl that within it is a ComboBox, a ToolStrip, and a DataGridView. All of them are set to Public, but I can only modify the properties of the ComboBox and not the other two controls.
I have no idea why this is.
You need to change your control visibility. Put the control property Modifiers on public and recompile the project and then you can change properties of the inherited control.