User control Anchor property issue - c#

Context :
I created an User Control. For some reason, I want to use this control in different size. To keep the initial "Template" of my User Control when re-sizing, I use the property Anchor on my different element inside the control.
So when I create my control at design time, it is possible to me to hand re-size the control and keep the original "Template" of it.
When the control is created, it look like this :
And after re-size :
As you can see, the property Anchor work well.
The label and the picture stay in the middle.
The "?" stay to the left corner.
The problem :
The problem I have is, when the control is reloaded, created with a different size as the initial one, all the elements inside return to their initial position :
I don't know if this is the better way to do what I try to achieve. Keep in mind that I add and re-sizing the control's during the design time.
Thank you.
EDIT :
I think my problem is caused by the designer. Ex : I add my control in the designer, I re-size it, I run the solution. All is working good. But when I go to the code of the page, and then, return to the designer, the element inside the control returned to their initial position.
EDIT 2 :
Ok I have found a solution, I simply moved all the element of the User control inside a Panel. For some reason that I can't explain, it work perfectly. The control's stay at the same location.

The solution is ta add a Panel to the User Control and dock it to "Fill", then place the element inside of this panel. For some reason that I can't explain, the designer keep the location of the re-sized control's elements.

The anchoring, docking and auto-sizing of a UserControl seem to be terribly confusing. I found UserControl does not auto resize with the Form which suggests that you set the AutoSize property to False, which I did, and it still didn't correct my problem. But when I tried your solution, I also noticed there are two copies of the AutoSize property! I had set the AutoSize in the UserControl designer to False, but the Form designer where the UserControl instance was added also had an AutoSize on the instance, and that one had a different value (it was still True). When I set that to False also, then everything worked (with the panel in place). Then I removed the panel you suggested, and everything still worked. So I guess the trick is to make sure you check all the properties of the UserControl in the UserControl designer and in the form designer where the control is used. Then you shouldn't need a panel.

I've had similar problem in VS2015 project, and unfortunately - none of your answers helped. Clean and working solution was found here, in Jignesh Thakker answer.
For quicker navigation here it is how it was done in my project (c++/cli, not c#, but idea is the same):
System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e) {
/* some code */
myUserControl = gcnew MyUserControl();
myUserControl->Dock = DockStyle::Fill;
tabPage1->Dock = DockStyle::Fill;
tabPage1->Controls->Add(myUserControl);
/* some code */
}

Set the Localizable property of the parent form at VS designer to false. This solves the problem at design time. (Save, close and reopen the form after switching the property)
If you need a localized application switch the Localizable property to true after finish up working at the layout and don't care about the wired representation in the VS designer. At run time it's shown correctly.
Tested in VS2013

Related

Custom UserControls automatically resizes when hosted on a WinForm form

I've a problem with my WinForm application.
I'm using a Tab Control where i put a custom usercontrol in every tab panel (tab panel 1 contains a custom usercontrol, tab panel 2 an another one, etc).
My custom usercontrols have specified size, but when I add my custom usercontrol to an another usercontrol or when I run the application, them are bigger than in design mode...it seems that the hosting Form zooms hosted usercontrols, but I have no idea why this happens..
Another problem is that distances between items are corrupted.
Here is an example:
Three combos (hosted in a hosted usercontrol) in design mode:
And the same three combos when I'm running the program or when I simply add
my custom usercontrol to a window/form:
As you can see they are bigger and the distance between them changes...
This is happening on most of hosted controls (but not all), without a logic...
What can it be? I'm optimizing the application to run on specified-size screens but I can't do this with these problems...
Thank you!
I've found an another user with the same problem only now :
Sizing issues while adding a .Net UserControl to a TabPage
The problem is on a property not visible in design mode: you have to change the property AutoScaleMode to None, on usercontrol's designer:
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.None;
Very late reply, but hopefully this will help someone out there who's run into this same problem and couldn't find a solution, like I just did.
What worked for me was to set AutoScaleMode to Inherit in the user control's designer.
AutoSize was set to false, though I don't know if that had any effect.

TabStop is not working

We have a form and textboxes in it. All of control's tabindex are set correctly and TabStop = true.
When some textbox is focused and when I press tab, focus is not set on next textbox.
Note : Controls are in the panel and its TabStop = true
Set TabStop for the panel to false
Make sure that the tab indexes are like the following:
Remember that you have a nice tool to set the Tab indexes in Visual Studio:
My solution: Containers must have the tab numbers set correctly even though they are not stops. Finally working!
Old post and nothing here helped me.
Tried everything including to force .Select() e .Focus() on the controls.
For some reason taborder was behaving all in a funny order, even disabling/setting visible = false on one control other orders was wrong as if the tab index was working in reverse.
Only thing help was de Document Outline, you can open that box with Ctrl + W, U or by the menu on VS.
VIEW -> Other Windows -> Document Outline
It ill show your controls in a tree fashion.
Just drag the controls to the correct order, it ill affect the tab order.
Word of warning: I struggled a bit with it because dragging (in the Document Outline box) some controls inside a panel caused them to move to the top upper corner of the panel (in the Designer). Just dragged them back to the position in the Designer.
For some reason the order the controls are show in the Outline Document box affects tab behaviour in nasty ways.
Have you set the tab order correctly? Select the form in designer view and select View > Tab Order to set the correct order.
Set TabStop for individual controls. Remove it from panel.
Perhaps the TextBox that you can't tab from is a multiline TextBox with AcceptsTab set to true? This would cause the TextBox to consume the tab key itself.
I know this is an old post but I've just had a similar issue and thought I'd share my solution.
Check that you have set the TabIndex before adding the control to it's parents Control collection.
I noticed for the controls that were being skipped I was adding them to the parent before setting their TabIndex. Once I set the TabIndex and then added them to the collection they behaved as expected.
One thing to note is that Visual Studio showed the tab order I wanted but at run time it did not behave that way.

Inconsistent behaviour when attempting to highlight C# TextBox

I'm building a C# WinForms program, and my textboxes do not allow the user to highlight the text consistently throughout the program.
In some places, the highlighting works normally: you type something in the box, click and drag over some text, and it highlights where you dragged.
In other places, clicking and dragging does not select the text. The only way to do it is by double clicking on the text.
I haven't changed any default properties of these textboxes or messed with any event listeners. I placed brand new textboxes in different places, and they behave differently.
I'm wondering if it has something to do with the properties of the Form the TextBox is contained in, since it seems to appear that either all textboxes in a particular form work, or none do. However, as far as I can tell the properties look to be the same across the board, and I don't ever remember changing anything.
To me it seems like it's happening randomly. I can't find any information on the topic. Does anybody have any idea what I'm talking about?
EDIT: Ok, I figured out where the problem lies, but I still don't know how to fix it.
It happens only in forms which have been added to a SplitContainer in my main window like so:
myForm.TopLevel = false;
this.splitContainer.Panel2.Controls.Add(myForm);
myForm.Show();
EDIT 2: I now know that this is the same issue encountered here: Windows Forms: Unable to Click to Focus a MaskedTextBox in a Non TopLevel Form . The accepted answer isn't useful to me, and the other answers seem impractical, since I'd have to add event handlers to every single textbox...
I had the same problem today. I tried changing TopLevel as others have suggested. This didn't work. Somewhere along my search I saw a suggestion to create a click event for the text box and use it to force focus on the control. This made no difference either. There were no events that should intercept and block a click event. It was just an MDI child with a few controls on it stuffed inside a panel on a split container. I couldn't highlight text in textboxes or textbox-derived controls though.
Turns out the solution was to switch the order of childform.Show() and panel.Controls.Add(childform). If you add the child form before it is shown, you apparently cause this bug.
I'm a little perplexed at what you're trying to accomplish. I'm used to using a user control if I want to embed something on a SplitPanel, and using an MDI form if I want child forms.
Do either of these approaches work for you, and if not, can you explain why not/what you are trying to accomplish?
Thanks!
James
* Edit *
You can add a panel (regular panel, not a split panel) to an MDI parent form and dock it to the left. Add whatever you currently have in the left panel of the SplitContainer to this left-docked panel, instead. Now you can instantiate forms, set them as children to the main MDI parent, and have all the window functionality you're looking for... You can maximize them, and they will fill the right-side of the MDI parent; you can pick cascade or tile from the window menu, etc.
If you want to let the user dynamically resize the left panel, drop a splitter panel into the right-hand portion of the main MDI form container; it will dock left by default, and show up to the immediate right of the panel. Now when you run, you can drag the border of the panel to resize.
Remember, an MDI form is like any other form... you can add any control you want to its surface, and .NET is pretty smart about how it incorporates the child windows.
If you're still not sure of what I'm trying to describe, I'll try to find somewhere I can drop a sample project... because everything is really done in the designer, there's not really any code I can show you. Here's the code for creating a form as an MDI child (running from within the MDI parent):
MyForm frm = new MyForm();
frm.MdiParent = this;
frm.Show();
That's all there is to it.
HTH!
James

Why are controls within custom panel (C# winforms) disappearing in designer?

I have been able to create a custom C# winforms control that is basically a panel with a fixed banner (header/footer). I want to base other user controls on this "banner panel". I've gotten past the problem with the designer here. I can successfully add controls to the inner content panel. Everything looks fine while designing. However, when I recompile, the controls I added to the content panel disappear. They are still there (in code) but aren't displayed in the designer. Is there any thing that I need to do to set the drawing order of the controls?
Your controls are still nested correctly within the panel control, they have just lost their z-order. If you choose the controls from the property panel and right click on the control border that appears within the parent panel and select "Bring To Front" from the layout toolbar, your nested controls will re-appear. I don't know why it does this, but a workaround is to bring all child controls to the front during control initialization in the code.
There is really nothing to go on here without src. What I would do is to comment everything out including in the InitializeComponent function but a widget in the middle panel and run. Do whatever it takes to get that one widget to show. Inherit from UserControl instead of the banner panel.
Then comment in each piece until the widget no longer comes up. That is what is causing your problems. Once it all comes up properly, then you make sure the designer portion of the src works. It is going to potentially be a long process.

System.Windows.Forms.GroupBox text is missing

I am working with Visual Studio 2005 (C#) and in one of my windows forms I have a couple of textboxes. I have placed the textboxes inside instances of 'System.Windows.Forms.GroupBox' because I want to use the GroupBox member 'Text' to tell what the textboxes are for (it shows up as a label near the top left corner of the fine line that encircles the GroupBox).
My problem is that suddenly one of these labels just disappeared. It is present when I work with the form in the designer but when I run the program and the form appears the label is gone.
Thank you for any information on this issue!
Check to be sure you're not setting the .Text property of the GroupBox to an empty string.
If you do not need to access the groupbox in your code you can always set the property GenerateMember on the groupbox to false and what you see in the editor "should" always be what you get when you run the app.
This is useful with any controls that you do not wish to modify during runtime thus keeping your memory allocation down as well as mistakenly modifying one of it's properties as well.

Categories