How to use custom UI elements in designer mode? - winforms

I wanted to use a NumericUpDown box with custom text in my form, so I followed the solution outlined here. The code compiles and run perfectly!
However, I can no longer view my form in Designer mode to adjust the UI, etc. When I try to, MSVC2008 complains that it "Could not find type 'NumericUpDownEx'. Please make sure that the assembly that contains this type is referenced. If this type is part of your development project, make sure that the project has been successfully built."
My code is exactly the referenced solution's (as my initial test) and the only other change I have made otherwise is tweaking the InitializeComponent method slightly so it reads
this->nudData = gcnew NumericUpDownEx();
instead of
this->nudData = gcnew System::Windows::Forms::NumericUpDown();
Any help would greatly appreciated! ...making UI by code alone really sucks...

Well, I managed to make it work. I needed to specify the full namespace name of my new, extended control:
this->nudData = gcnew MyProject::NumericUpDownEx();
And that's it! Might just be a quirk of this old version of Visual Studio?

Related

Visual Studio breaks Visual Inheritance

I have made a FormBase, from which I inherit a FomBaseList and a FormBaseDetail.
All other forms in the project are derived from FormBaseList or FormBaseDetail.
Now it seems that VS has huge problems with that, and my biggest problem is that VS keeps writing property values from the Ancestor form into the designer.cs from the child form.
for example, in FormBaseList I have this property/value :
this.gttDXGridView1.OptionsView.ShowAutoFilterRow = true;
I expect that in a derived form, for example FormClientList, there is no mention for this value in the designer.cs, because it should fetch the value from its parent. In other words, just plain simple basic OOP.
And I also expect that when I change the property in FormClientList to
this.gttDXGridView1.OptionsView.ShowAutoFilterRow = false;
that this is seen as an override from the baseclass.
However, VS keeps overwriting the property in FormClientList.Designer.cs with the value found in FormBaseList.Designer.cs.
This breaks the rules of OOP in my opinion, other tools that support Visual Inheritance like Delphi for example do this correct.
How can I stop VS from doing this ?
The properties are changed using the designer.
All controls are DevExpress controls, or derived from a DevExpress control.
Another example, which works just opposite so its very strange.
For example put a Button on the BaseForm and give it an image.
The button with the image appears on all derived forms.
Now change the image on the button of the BaseForm.
You would expect the image to change on all derived forms also, but that does not happen.
I discovered that again VS has written the property value of the button in all derived designer.cs files, and this time it does not overwrites them.
I created a ticket about this at the DevExpress forum, and they where able to reproduce it.
It is now passed on to their developers.
https://www.devexpress.com/Support/Center/Question/Details/T692940/devexpress-controls-break-visual-inheritance-in-visual-studio
It also seems I was not the first to report a similar problem.
https://www.devexpress.com/Support/Center/Question/Details/T692244/imageoptions-are-serialized-in-a-successor-when-visual-inheritance-is-in-effect

Changing Namespace in a safe manner

This might be a very newbie question (coming from a newbie) but I had some problems regarding the namespace. I've created a User Interface in Visual Studio and gave it a test name to start off and learn how to use the program and the programming language. I kept building and building my UI and now I'm at the point that I'm actually pretty satisfied with my work (and this community's help!). So I decided to change the name of the file from "Test" to something more relevant for my work.
Yet, unfortunately, I ran into some problems regarding the change of the Namespace. I know there are a lot of tabs where it needs to be changed, but I was wondering if there is an easy way to change the file name/namespace of the UI?
Thank you in andvance for replying and taking the time to explain this newbie what to do without breaking his UI ;)
Use tool like Resharper, it will do it for you automatically and safely.
With Keyboard shortcut:
Put the keyboard cursor to the namespace, press Ctrl + R + R and set the new name. It will change the namespace inside XAML as well.
With Mouse:
Right mouse click on the name space, click Refactor > Rename.
When renaming classes and variables there is the lightbulb-menu (Ctrl + .) in witch you can give Visual Studio the instruction to Change it for you. But this will not work with Xaml.
Visual Studio will not change Namespaces as far as I'm concerned. But you can still compile and look at the "Namespace not found"-Errors to change them manually.
As Yohanes Nurcahyo said there is ReSharper but only has a free Trial of 30 days.

VS2010 Winform designer alterate decent code

I'm encountering a problem with the WinForm designer. I made a new UserControl, I added a DataGridView, some other elements and a TreeView. With the gui I named all those new components. Now it's time to code that stuff and I realise that the designer missnamed my node of my TreeView. The Designer also added new columns from my DataSource even if it was set to AutoGenerateColumn to false. I though: "Well time to clean some Designer crap again..." and I cleaned that stuff up in the InitializeComponent function (I know it's labeled "Do not modify with the code editor" but... do I have the choise?
Anyway, my problem is : When I go back on the Design view, the VS Designer seems to regenerate the code back but not even how it was. Now the designer declares my DataGridView and my TreeView as local members of InitializeComponent function. I can easily repair and undo my changes but I would like to understand and know if there is a way to disable the auto code generation of the designer.
Also, I tried to make another function which have all what I need so the designer don't screw it up and call it into the initialize component. This solution works at run time but not on Design view. I'm kinda low.
As far as I know, the short answer is no. If something is marked as Do not edit due to code generation., then do not edit it :). I would suggest reading up on partial classes, as that is how you can modify classes without actually messing with the auto generated code. In your case, you will need to go into the designer and fix everything there so that the auto generation works as you expect it to.

With the Telerik Silverlight controls, how can I cast a RadMaskedTextBox as a TextBox

I am trying to determine if a Telerik RadMaskedTextBox allows multiline input.
I the debugger, I was able to find and check the .AcceptsReturn property of the underlying textbox, using:
((System.Windows.Controls.TextBox)(((Telerik.Windows.Controls.RadMaskedTextBox)(this)).textBox)).AcceptsReturn;
However, when I paste that into my C# code, the compiler complains that RadMaskedTextBox cannot be cast as a TextBox
A little weird, since the Debugger allows it.
The debugger also allows:
((System.Windows.Controls.TextBox)(((Telerik.Windows.Controls.RadMaskedTextBox)(this)).textBox)).TextWrapping
this the compiler complains about
Any ideas?
I tried to do this in the past (sorry can't remember the exact way to go about this) but I had to traverse the object and find the textbox control. If I can find the project where I did it I will update my answer.
From the Telerik Support Forum:
The underlying TextBox is of type ExtendedTextBox. You can access it like so:
var innerBox = this.masktextBox.ChildrenOfType<Telerik.Windows.Controls.MaskedTextBox.ExtendedTextBox>().FirstOrDefault();

Is it OK to change a winforms designer file?

I have created a class that is simply THIS
Class UserControlBase
Inherits UserControl
End Class
Then I changed the Inherits clause in each of my UserControls designer file to
Inherits UserControlBase
I know that generally you shouldn't manually mod the designer file. But in cases like this what else can you do? Is this OK? Is there a best practice I don't know about? Is there some other way to accomplish the same end (extending UserControl) ?
I have not had issue changing the Inherits line, adding Namespacing, or adding Imports/Using statements. If you need to do any of these 3, you won't find many other ways to handle these requirements.
I change them all the time in my C# projects... often it's the easiest way to duplicate something that you've done once in the designer to a similar form and you want to do the same thing in a different form. Visual Studio is perfectly capable of reading in your changes and incorporating it into the designer. I really don't know why there is a comment saying not to edit it. My advice would be just make sure you use source control, go ahead and edit it, test it well, and if it works, great, if not you can always back out your edits.
No. It's never a good idea to modify a file that's generated.
The Designer files are pretty simple code; the only thing that you'll typically find in there to complicate matters (but only slightly) is BeginInit/EndInit calls at the top and bottom of the file--between those the code is pretty forgiving.
*That said, do not put any code in there that will only execute at Runtime. Any runtime-dependant code will fail at design-time, so trying to open your control in Design view will blow chunks. It used to give you the Red Screen of Darn, but I'm not sure what effect the IDE has notwadays--but if things blow up and the usual tricks fail to remedy them then try removing your customized sections.
Further on that note (not to scare you, but rather to hopefully head off some of the difficulties we had) the means of determining if your code is executing in Runtime or Designtime often fail if your code is not part of the currently built solution/project.
So to bring it all home, simple UI layout/winforms modifications are perfectly fine to do by hand in the designer code. Databinding and external dependencies (with the exception of calling third party control libraries) should be cautiously approached.

Resources