User control keeps stretching once dropped on form - c#

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 ;)

Related

Winform User Control size switch back to initial size after localized

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.

Change parent of a control in a size trigger in UWP

I'm creating a Windows 10 Universal app, there are two panels in my main page.
In tablet/desktop mode, both are visible at the same time (just two simple Grids, one fills the left side and the other one fills the right side)
However, in phone mode, there's a pivot and user can swipe between panels.
(I could use SplitView and hamburger menus which work easily in both Tablet/Desktop and Phone modes, but a pivot makes more sense in my case)
So, What I currently did is, I have two instances of each panel. PhonePanel1 and PhonePanel2 are two grids inside two pages of my pivot control, and TabletPanel1 and TabletPanel2 are two grids which can be shown side by side. and I use AdaptiveTriggers to detect page width and switch between these two view methods.
However, having two instances of each panel is not a good approach, because those are showing basically the same content.
Is there any better approach to do this? Maybe changing Grid's parent when triggers are changing view mode? or something better?
The only way to change the parent that I can think of is to write some code behind that will render them in the new location. A suitable solution may be to defer the loading of the controls until they are needed.
x:DeferLoadStrategy=”Lazy“
If you add this property to your controls that are duplicated, they will only be loaded when required. So on a phone the PC/Tablet view should never be loaded, unless the user is running in Continuum on an external display.
On a PC you may still end up with both loaded (If the user resizes the window) - On a PC there are usually more resources so it's less of a problem there in most cases.
I looked into the code behind approach to move the controls to a different parent and I think honestly it's premature to do that when it might not even improve performance.
I have gone for the Defer Load Strategy property and found performance to be fine, and it requires very little code. My duplicate controls are sharing the same ViewModel so I pretty much just added the property to ensure resources are only used when essential.
There is nothing wrong in your approach. To reduce memory usage, when adaptive trigger happens for example on the phone, set TabletPanel1 and TabletPanel2 visibility to collapsed.
If all panels use the same datacontext then leave it as is, if not set datacontext for collapsed controls to null.
You could create each of your panels as user controls then you've only need to define the contents once but each control could be included in each of the layouts you want.

ObjectListView Not Showing Selection Color For Selected Item

I have an ObjectListView which is essentially a wrapper around the standard .NET ListView. My problem is that I cannot figure out the correct sequence of method calls to add a new object to the control, scroll the control to ensure that the object is visible, and select the object. Below, is my code to achieve this. Everything works but, for some reason the background color for the selected item/object does not show up until I click on or re-size one of the columns. I'm not sure if the control is not being focused or what.
// objectListViewItems is of type BrightIdeasSoftware.ObjectListViewItems
objectListViewItems.AddObject(e.InsertedItem);
objectListViewItems.Refresh();
objectListViewItems.Focus();
objectListViewItems.EnsureModelVisible(e.InsertedItem);
objectListViewItems.SelectedObject = e.InsertedItem;
objectListViewItems.Focus();
The code below updates an item in the ObjectListView and works just fine. Not sure what I'm doing wrong above...
objectListViewItems.RefreshObject(itemToEdit);
objectListViewItems.Focus();
objectListViewItems.SelectObject(itemToEdit);
This should work like you proposed (I did this on several occasions). However, calling Refresh() and the second Focus() is unnecessary. Also I would rather use SelectObject() than the SelectedObject property.
Like this:
objectListView.AddObject(newItem);
objectListView.Focus();
objectListView.EnsureModelVisible(newItem);
objectListView.SelectObject(newItem);
Also, make sure that there is no code executed afterwards, that may cause another control to get the focus.
To narrow down what's happening, you could try setting
objectListView.HideSelection = false;
As for the normal ListView, this ensures that the current selection stays visible (but "grayed" out), even if the control loses focus.
Please post the complete OLV configuration (from InitializeComponent()) if you used the designer. Maybe there is some weird constellation causing this.

c# winForms open forms inside mainform

I have programmed c# application i will post screenshot. In this main form is 3 buttons which opens different forms. Now i decided to modify this application I want to Make one main form with strip menu which will open this forms. I used this code but i don't like or i'm doing something wrong. I don't like because there is child controls(minimize, maximize, close) in parent (please see second picture ):
Please advice me something. Is MDI good for such job? Thanks!
Sell sell = new Sell();
sell.MdiParent = this;
sell.Dock = DockStyle.Fill;
sell.Show();`
So my problem is that parent form is not filling when i open child form this is creen how to make that it parent form was filled with child form
Seeing your latest edit, I assume the reason that your child form's content doesn't fill the screen even when it's maximized is because your content/layout is not flexible.
Wherever you've placed the controls during Design Mode is where they're going to end up at run time, regardless of how big or small you make the window. If the window is too small to contain all of them, they'll either be covered up or you will see scrollbars. Alternatively, if the window is made larger than necessary, you'll see a lot of empty space.
The way around this is either to set the Dock and Anchor properties of your controls, which causes them to expand and compress to fit the layout of their containing form. You could also place your controls inside a TableLayoutPanel or FlowLayoutPanel control to help manage their layout.
As far as the question you appeared to be asking originally, I still can't tell if you're opposed to the way an MDI application looks, or if you simply don't understand how to correctly implement it. The clarification comment you offered actually makes things less clear to me—you posted a code snippet, but didn't explain what it means. As I wrote in a comment, there's no (non-hackish) way to show a form that doesn't have minimize, maximize, and close buttons (setting the FormBorderStyle property to "None" does this, but I think this is a silly solution that simply allows you to use the wrong control for the job—it won't behave like a form, the user won't be able to move it around like a form, etc. so why use a form?).
If you truly want to have a single application window with changing content in the center, you should create a series of UserControls. You can lay out each user control with the necessary child controls, just like you would with a form (using the fluid layout techniques I discussed above), add each user control to your main form, set each control's Dock property to "Fill" (so that they fill the entire viewing area), and then write code to simply swap out the currently visible user control in your main form's viewing area. The advantage of using a UserControl versus something like a Panel is that you consolidate all of your code into a single control, much like you would with a Form. You could use a tab control, but if you don't want to show any indication that there are multiple forms (which is what your aim appears to be), this would also be the wrong control for the job.
If you literally want to open child forms inside your main form, as your question title indicates, you should indeed be using MDI. If you don't understand how to do this, you'll need to clarify your question further.
Set MDI Container property to true for your parent form. It will help.
Set
FormBorderStyle = None
for your child forms

Where does a user-control go when the visibility of it is set to false?

Curiosity is kicking out again. I've been using the user control .Visible and .Hide() method for a long time. So where does a user-control go when the visibility of it is set to false?
It doesn't go anywhere - it stays in memory. It's just that its not visible. Being visible really means that it has to paint its area when requested and it can receive focus and user actions.
When you set the Visible property on a control it essentially tells the web server not to return the HTML markup for the control in the response. It does, however, keep the control's information in the viewstate so you can keep working with it in your code.
Oops - sorry. I thought you were talking about ASP.NET and not WinForms. My bad.
I'd agree with VinayC (+1 btw).
Also, even though the control doesn't paint itself or respond to user events through the UI you can still exercise it programmatically or indeed use its data.
For example you could have an invisible window that logs posted message data.
You actually use this functionality quite a lot without thinking about it, take your Form control for example. For a modal dialog box you create it, set property values whilst it is hidden then make it visible with the ShowDialog() method.

Categories

Resources