I have a somewhat complex UserControl, and Visual Studio 2008 is giving me a rather harmless annoyance when working with it. Every single time I open the control with the Designer, it decides to immediately change some of the harmless values set by the designer - namely the initialization of Size properties. If I save those changes, close, and reopen, it almost invariably ends up deciding another component of my control needs its initial size changed, ad infinitum. Luckily these changes are harmless since I'm using automatic sizing everywhere, but this is quite annoying to work with. I haven't the foggiest on where to start figuring out what's going wrong, my only thought right now is that the Designer is assigning the results of auto-sizing back into the initial size fields every time I open the control. Any ideas on causes/fixes?
Edit: Also, I am using Application Settings to save sizes of certain resizable child components across runs of the application, but I really hope the Designer is smart enough to understand that it should only ever be using the defaults.
Maybe it can help:
I noticed that FormDesigner (no WPF, no Web etc) has a strange behaviour if you insert one custom UserControl.
There is a random change of other controls (GroupBox, EditBox, ComboBox) size (to me happened with width).
The controls choosen to resize seems to be random, but across restarting of vs2010 it is always the same. If deleted and reinserted, the designer chooses a different control do randomly resize...
I changed the property AutoScaleMode of my UserControl from "Font" to "Inherit" and it did not happen again.
You're right, the designer often tries to add default values to properties.
Add this on top of the property declaration:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
That will tell the designer to ignore this property.
I have somewhat similar problem. I am using Infragistics GroupBox on a user control which I inherited and now want to change its look and feel in the derived class. I have made it protected in base class -- so it does allow me changing properties in derived class. But it does not save it. Every time I open it -- I get same old values of base class back.
Any idea?
Edit: I figured it out.
Trying various value for one of the above given answers.
Using [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] instead of [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] generates code for changed properties - and things work as desired.
Try overriding the DefaultSize property of your control.
From MSDN:
The DefaultSize property represents the Size of the control when it is initially created.
Related
I need to put many controls on form during design. Each time I must scroll in Properties in order to find and change control name name. Is it possible somehow change controls name in fast way during design?
Sorry, but currently native Visual Studio does not have the function you want.
You can press F4 on the control to quickly enter the property interface, but you need to find the Name property modification by yourself.
Using code modification is also an option if many controls need to be modified.
this is my first thread on this great website for programming :)
I have a problem with my UC. I'm currently developing an extended version of DataGridView, which added some functionality like paging support.
Here is the picture of the UC inside a form, after I make it bigger than the initial size:
And finally, I change the form's language to Indonesian. And then the designer suddenly repaint my UC. The size of the container of my UC is OK, means the designer remembers that I resized my UC before. But unfortunately the controls inside the UC's container gets switched back to their initial size, leave some blank spaces inside. This behaviour only happen in design time. The UC is fine in runtime mode, its size works well.
How could this happen guys? Although it's fine in runtime, but it's ugly in design time.
Sorry for my English, greeting from Indonesian :)
Thanks.
===SOLUTION===
After play a bit, finally I've found the solution. Before I place the controls inside to the container, I must first add a Panel to the container, set it to transparent, and set its Dock property to Fill. Then followed by DataGridView and BindingNavigator to the rest. Build and it works like a charm!
It seems that the Panel resolves the problem :)
Cheers
Maybe it will help to change the order of the factors. First change the Form language to Indonesian. Then save your project and close it. Open it again and check if it still on Indonesian then place the DataGridView control on the form and re-size it.
I am working on a winforms app with a DataGridView control on it, and I am experiencing some frustrating things.
First off, I want to turn off AutoColumnGeneration, but it's not listed in the properties. No problem, I can do that with a line of code...and this is where it gets weird:
In my code, the DataGridView is inaccessible. Its like it doesnt exist on the form. Looking into this, its because the designer is declaring it as part of the InitializeComponent() method instead of where it initializes all the other controls.
Because its in the designer, any change I make there gets reversed so I can't fix this.
Is there any way to stop visual studio from doing this? I found a hack around it by using one of the datagrid columns (which ARE accessible) to create a reference to the datagridview its associated with and access it that way. It works, but its ugly and not intuitive at all.
I think I found it:
In the designer, click on the DataGridView control, and change the property of GenerateMember to true. I'm guessing it is set to false.
That property is used to do just that: hide the control from the code windows. It's useful for Labels or ToolStripSeparators that you don't need to deal with in code.
I personally use the binding source as the datasource which can even be an object and then under columns it will list all of the available columns and you can pick and choose which ones are visible as well as a slew of other options including formatting.
Click the drop down on the datasource and Add a new data source and select the necessary object, in my case an order detail object. Here is some of my designer code which is created by VS2010
this.dgvOrderDetails.DataSource = this.orderDetailBindingSource;
this.orderDetailBindingSource.DataSource = typeof(OrderDetail);
And the binding source code that sets up the data to fill the datagridview (I coded this part)
orderDetailBindingSource.DataSource = orderDetList;
Then just click the ellipses on the Columns property of the datagridview and it will have all the columns listed that are available from the object and I set the ones I want visible, the order, format etc.
As for the 2nd issue I don't think you'll have that problem once you use the designer to make the changes I listed above.
In my case, I declared a private property in the Form's partial class (the file for my code, not the Designer's file) to return the control by navigating through the Controls hierarchy.
private DataGridView MyGrid
{
get { return (DataGridView)this.Controls[0].Controls[1].Controls[0].Controls[1].Controls[0]; }
}
I agree, there ought to be a better way, such as Visual Studio Designer declaring the control like it does most other controls on the form. In the meantime, this works.
Warning!
If the form's control hierarchy is ever changed, the property's definition will have to be manually updated.
Edit
I have a better solution - at least in Visual Studio 2012.
While in the form Designer, with the DataGridView selected, open its properties and look for the GenerateMember property (under the Design node) and ensure it is set to True. Once set to True, the Designer will declare a member variable for the DataGridView control.
The strange thing is that the default value appears to be True, so I'm curious how it was changed to False? Perhaps I mis-clicked when setting the name?
By the way #LarsTech's answer is the same as this update.
This may be a winforms newb mistake, but I've created a user control that has one component on it - a Telerik dropdownlist control. When I initially drop it on a form it looks correct. However, as soon as I run the form the control stretches off the form.
The dropdownlist on the user control has its anchor property set to top/left/right/bottom.
Before:
After:
What the heck am I doing wrong here?
EDIT: a bit more info: If I close the designer and open it back up, it immediately changes sizes. The code itself doesn't appear to cause the problem. In fact, I commented out all of the code (besides the InitializeComponent() call) and I still see the problem. The control itself still works - when I run the form it is populated with the data I'm expecting. It's just stretched.
I can "work around" the issue by setting the width of the dropdownlist about 35 pixels less than the size I actually want it to be, so when it resizes itself it's actually the correct width, but I really shouldn't have to.
You were probably on the right track. It looks as though it is anchoring to something that is way off the page. I would try removing the anchor right for starters to see what effect this has. If that doesn't work, perhaps try different combinations of the anchoring to see what effect it has on the control.
Make sure there are no docking settings enabled, as it would undo any anchoring you have on the control.
Check to see what the dropdownlist control is contained in. You said it was in a usercontrol, but is it within a grid in the UC or anything else like that? Also, ensure that the user control is sized properly (not outside the bounds of the page) -- would cause anchoring problem. Use a background color to test this.
Next, I would check the Maximum/Minimum size of the control (perhaps consider setting one?).
If all else fails, delete the dropdownlist and create a new one and see if you have the same problem. Sometimes the designer side of code gets glitchy and causes problems or you just accidentally change something you didn't mean to.
Finally, last resort, just use a normal drowndownlist ;)
I am creating huge amounts of dynamic controls, and for everything that must have an ID assigned, I assign it using a guaranteed unique variable(unless 64bits overflows from just controls).
Now, I have a problem though. I have a duplicate control somewhere and I can not discover where it is added or anything because it doesn't happen until after Page_Load, which means, it is out of my own code when an exception is thrown(I think at like Render or some other internal function).
The control name is "ctlXXX" where XXX is a number(right now, always 244) this control ID is not being made by me. It is being made by ASP.Net automatically(as none of my IDs are prefixed with ctl). So how do I correct this error that I can not see? Can anyone suggest ways of finding my error(I really hope this isn't a bug in ASP.Net)?
Also, I got the error just recently by using a Copy function which will do a Memberwise copy on a custom control, and then it will reset any controls to have either a unique ID or to be null as this part happens before controls are loaded into the custom control, which is a descendant of Panel)
Can anyone give me advice on how to find this bug?
I think you want to look at the INamingContainer interface. I think it will solve your problem but you will have to derive a new control class to do it.