Hi I've setup a few subforms all inheriting from a root form that sets up a few buttons and logos etc to then filter down to the children.
I used the visual studio wizard to add the subforms inheriting from the root and they are automatically declared with:
public partial class WelcomeForm : MynetInstaller.rootForm
I've now been asked to resize the subforms and move a button, things I hope would be done by simply changing the root form and allowing the changes to filter down.
The two problems I have are:
1/ I change the size, this does not affect the children at all, it seems after the initial setup the size is not inherited.
2/ I change the location of a button, this causes all subforms to break showing an error that:
To prevent possible data loss before loading the designer, the following errors must be resolved:
'child' is not a child control of this parent.
Instances of this error (1)
1. Hide Call Stack
at System.Windows.Forms.Control.ControlCollection.GetChildIndex(Control child, Boolean throwException)
at System.Windows.Forms.Control.ControlCollection.SetChildIndexInternal(Control child, Int32 newIndex)
at System.Windows.Forms.Control.ControlCollection.SetChildIndex(Control child, Int32 newIndex)
at System.Windows.Forms.Design.ControlDesigner.DesignerControlCollection.SetChildIndex(Control child, Int32 newIndex)
If i press continue it will load the page, but wont have moved the button and if i try and run it i get an error in:
this.Controls.SetChildIndex(this.btnNext, 0);
Saying:
'child' is not a child control
I noticed that when i move the control it stops becoming locked, but changing this manually doesn't help.
The base form's Size is inherited, the designer normally prevents setting the size again in the derived form. But mishaps can happen, a sub form might have been subtly altered once. Or it might have been designed on a machine that had a different video DPI setting from the machine on which the base form was designed.
There is only workaround for this that I can think of, edit the designer-generated code. In the Solution Explorer window, open the node next to the sub-form and double-click the Designer.cs file. Expand the region labeled "Windows Form Designer generated code" and locate the assignment to the ClientSize property. Delete it.
No idea what's going on in your second problem, this is not a normal issue. Take a look at what happens to the designer generated code when you manipulate the button.
For "'child' is not a child control" problem you must remove from .designer.cs file .setChildIndex command lines.
Related
I'm creating an OverflowPanel derived from the WPF Panel class. The intent is that it will fill with items in a single direction, and when there are too many items to display, excess items will be removed and replaced with another control to hold the overflow. Think of a website's breadcrumbs, or the address bar in Windows File Explorer. This is a .Net Core 3/C# 8 project.
I have a partially working solution: I've inherited from Panel and overridden MeasureOverride() and ArrangeOverride() to get the behavior I want. My problem now is getting a button or some other control to display in place of the items being removed.
My initial, naive approach was to just create a Button in code and try to Measure/Arrange it.
public class OverflowPanel : Panel
{
// First by itself, but I did also try to host this in a new UIElementCollection
private readonly Button _overflowButton = new Button();
public override Size MeasureOverride(Size availableSize)
{
...
_overflowButton.Measure(availableSize);
// Do stuff with _overflowButton.DerivedSize.
...
}
// Also attempted to draw int in ArrangeOverride()
}
This did give me non-zero result for the measurement. (I put some dummy content in the button.) My algorithm gives me space on the screen where the button should go, however, nothing gets rendered there.
I also confirmed that there wasn't simply a button being drawn with no visual style, by inspecting the Live Visual Tree in Visual Studio.
I tried to make a UIElementCollection and add the button to that to see if it would add it to the visual tree, but this also did not work.
Most Google/StackOverflow results I've seen suggest something along the lines of this.Children.Add(_overflowButton), but this does not work when hosted inside an ItemsControl, as it takes over managing the collection of objects and throws an exception if you attempt to mess with it.
After digging around in the code for Panel and UIElementCollection, I noticed that Panel lets you override
UIElementCollection CreateUIElementCollection(FrameworkElement logicalParent)
to use a derived implementation of UIElemenetCollection. I created a PinningUIElementCollection to trick WPF into rendering the extra element. It stores extra items and then slips them in whenever the iterator is accessed. It also does index mangling to access both the extra collection of items and the automatically generated one.
This actually worked. My button is now displayed (albeit without the correct styling, but that's a separate issue.)
However my issue with this approach is that it seems like a lot of work. It also seems error prone: I could easily miss when it tries to use a numerical index and forget to mangle it, causing unpredictable results.
Is there a simpler/more straightforward way, in my derived Panel implementation, to display an extra button or some other arbitrary control with only a few less hoops?
In my app namespace = DRT, I'm creating control classes (e.g., button, textbox) that derive fron their corresponding Windows control classes, e.g.,
internal abstract class DRT_Button_Abstract : Button
{
....
}
internal class DRT_Button_CancelSearch : DRT_Button_Abstract
{
....
}
internal class DRT_Button_StartSearch : DRT_Button_Abstract
{
....
}
All together I currently have 13 derived classes that derive either from one of my abstracts or from a Windows control class. After a successful build, I see my control classes (e.g., DRT_Button_CancelSearch and DRT_Button_StartSearch) on the Toolbox and I successfully drop them onto my main form. All is fine for a while, but ultimately, I'll go to open the main form.cs [Design] (i.e., the UI designer) and it will show the error The variable '{control property name}' is either undeclared or was never assigned. for some combination of my controls.
When I examine the main form Designer.cs file, the expected code for all the controls is present EXCEPT for the expected new statement. They are not present in the main form Designer.cs file. For example, I expect to see this.drt_Button_CancelSearch = new DRT.DRT_Button_CancelSearch(); but its missing
I've tried ignoring the error, proceeding to the UI designer windows to re-apply the lost controls, but the problem just repeats with the newly applied control
What the heck is going on? Is there a way to recover from this situation?
This is most likely a problem of the Designer not being able to clear/reload its cache. There is not much you can do. In the past I:
closed and reopened all designers that have user controls
put all the controls in a separate project (in the same solution)
put all the controls in a separate solution/Visual Studio instance and set a proper reference to the controls' dll (or even nuget package)
With the first two options I have had varying success. Reopening the designer is not very convenient and doesn't work.
That last option is the best but also the most annoying because every adjustment requires a rebuild of the project and update of the reference/package.
Also make sure that all controls that you create have public default constructors and function well when this constructor is used.
So here's my Question, I'm new to C#(teaching my self at that) Here's the thing, I'm working on a basic sim game, nothing to complex but I've got the design and basic functions done.
However In order to implement it, I'm currently using multiple Forms(Visual Studio 2013)
I have my "main" form which has the "action" buttons to it
So when i want to go to a user Profile page I have
Btn_profileview Click(object sender, EventArgs e){
Form profile = new Form();
profile.Show();
}
The User would then implement the changes(for instance change name) which is written to a text file, for use in other areas of the program.
However It opens a new windows, I've tried modal and nonmodal windows and while the benefit of Modal so they have to actual close the window solves the issue, i'd rather have it just overwrite the preexisting Form, and then on close go back to the "main" screen without actually using multiple windows.
Now I was told UserControl and/or Panel would solve the issue, but it would cause a complete redesign moving from the multiple forms to the multiple panel screens and figuring out how to get those to work(Visible and Invisible), i'm assuming it wouldn't be extremely difficult something along the lines of Panel"name".show(); and panel"name".close();
But would it be possible to actually add a line of code to the pre-existing code(so as not to cause a complete reesign) or are Panels and UserControl the only real way to implement within 1 continuous windows?
paqogomez is right: There are many ways to do it.
Here is one that combines a lot of the pros:
You create an invisible Tab on your window with as many pages as you need. Place a Panel on each tab and create all your controls on of them. This does not mean that you have to do it all over - you can move and drop the controls you already have without much hassle. Of course you need to turn off docking and maybe anchors, but other than that this is a simple process.
If you have controls on the 2nd form with the same name, these names should be changed to something unique though. I hope all Controls have proper names already, but especially Labels get neglected, at least here.. (With a little luck you can even use cut and paste to get Controls from another form to panel2!)
The big pro of this trick is that you can do all work in the designer of the same form. The Tab control serves only as a container where you keep your panels without adding to the UI and without giving the user control of what is shown.
Next you create one Panel variable in your main form:
Panel currentPanel;
On Load you assign the first 'real' Panel to it like this:
currentPanel = panel1;
this.Controls.Add(currentPanel);
Later, each time you want to switch, you re-assign the panels you need like this:
this.Controls.Remove(currentPanel);
currentPanel = panel2; // or whichever panel you want to show..
this.Controls.Add(currentPanel );
If your real panels are docked to fill the tabpage, as they should, the currentPanel will fill the form. You still have access to each panel and to each control by their names at any time but you see no overhead of tabs and your form never changes, except for the full content.
I am working on a WinForms project, and I have a form where I have a DataGrid, a TextBox, and 2 button controls (btnNew and btnSearch). The click event of a btnSearch is supposed to perform a search on the DataGrid.
I deleted the event handler for the search button and have saved my work. It now appears that all other controls have been deleted and the form is back to the default state. The application works fine though when run, with some errors. I have resolved the error but the designer view is still in the default state. How do I go about reinstating my form's design view?
i had your problem and solved it in this way:
just make another form with another name(NewForm.cs) and copy the InitializeComponent() content from YourFirstForm.designer.cs and paste it into
NewForm.designer.cs InitializeComponent() function. but be careful when copy and paste the function content change all YourFirstForm keywords to NewForm . finally Remove YourFirstForm and just work with your NewForm....
With the control selected in the properties window.
Right click on an empty space on the form,
Click cut
Right click / then paste into a cell somewhere in a form control.
I've run into errors like this before where simply closing all the form's files (code view + design view) re-open the code view and then Shift-F7 to reload the design view would fix it.
If that doesn't work perhaps fixing the error you mentioned in the designer view caused something to get out of whack. Try comparing the structure in the YourForm.Designer.cs file with a new form to see if an inadvertent edit was made.
i had your problem in Visual Studio 2017 and solved it in this way:
-select controls by Properties
-view Location is -
-change the Location
-drag the control to another location
Make sure all errors related to form are removed and your code compiles successfully.
If Form has been copied from an existing project make sure Form.Designer.cs and Form.resx are under same parent Form.cs and not separate. Check Form tree in Solution explorer. If they are not under the same tee, load the designer.cs and .resx file again in the project.
I got this code working!
I have a button inside a my UserControl dropped at design time in a my Form. All worked well and when in a button's event I called this.ParentForm it correctly returned to me the (only) parent Form.
After a refactoring, I moved the UserControl with the button to another NameSpace and the same piece of code no longer works. this.ParentForm now is NULL!
I read the MSDN site and it says that only when the control is hosted in IE or another context this.ParentForm returns null. But I moved only the namespace!
Anyone has an idea?
I cannot use a different constructor to pass it the parent form because at design time Visual Studio wouldn't render the Form.
See if you're accessing the control's parent form before the control has been added to the form.
Your code my be triggering upon the user control class initialization, but the control hasn't been added to the Parent form.
Try putting this code in the Control Load event.
Look at the code-behind file that contains the designer-generated code. During the renaming, you may have confused the Visual Studio designer and it may have "orphaned" an instance of your control in the Form.designer.cs file.
In particular, look at the code in the InitializeComponent method and see if you can spot any code that creates an instance of your UserControl but does not add it to a container, or adds it to a container that is not added to the form.