Growing user control not updating - c#

I am developing in C# and .Net 2.0. I have a user control that draws cells (columnar) depending upon the maximum number of cells. There are some drawing routines that generate the necessary cells. There is a property NumberOfCells that adjust the height of this control; CELLHEIGHT_CONSTANT * NumberOfCells. The OnPaint() method is overridden (code that draws the Number of cells).
There is another user control that contains a panel which contains the userControl1 from above. There is a property NumberCells that changes userControl1's NumberOfCells.
UserControl2 is then placed on a windows form. On that form there is a NumericUpDown control (only increments from 1). When the user increments by 1, I adjust the VerticalScroll.Maximum by 1 as well.
Everything works well and good BUT when I increment once, the panel updates fine (inserts a vertical scrolll when necessary) but cells are not being added! I've tried Invalidating on userControl2 AND on the form but nothing seems to draw the newly added cells.
Any assistance is appreciated. Thank you in advance.
Lawrence

Try debugging and check that the NumberOfCells property is properly being set. If it is and the cells still do not show after the window has invalidated (say, by minimizing then restoring the window), you have a problem with your cell-drawing method.
If after minimizing/restoring the cells show up, you need to call Invalidate(CellRegion) in the setter for NumberOfCells

Are you resizing the controls when you add columns? If they are not big enough to show their entire contents, then they will simply clip them and you may not see any new column(s) that have been added.

Related

Activereports shrink to fit issue- textboxes

In Activereports ver 7.0,
I have placed 2 textboxes one after another in vertical manner. I assigned particular width and height for both and then programmatically assigning text contents to both textboxes.
CanShrink property is set to true for both textboxes,so the textboxes can shrink to fit based on its contents.
My problem is once the first textbox shrink, I want to move the second one closely to the first one [To remove the extra space generated by shrinking], but it doesn't happen. Why is that?
Please check the image below
Because, the controls only move down, not up during report run. This is by design. In order to accomplish what you are doing, make the textbox1 size very small to begin with, so in essence it will always grow. In case if shrinks with not enough text you can use api to move the textbox2 up (use the location of that control in section format/before print event.

User Control Spacing Blows Up When Placed In Main Form

I have a C# WinForms application I am developing with Visual Studio 2010. My application has a custom control (User Control). When I am designing the custom control here is what it looks like:
Note the very narrow space between the image, label, and combo box.
When I add the custom control to my main form here is what it looks like:
Notice how much more space there is between the controls as compared to the spacing in the first image.
I have no idea where this extra spacing is coming from. It's as if the margin/padding between the controls is getting multiplied by some scale factor of which I have no idea how it's determined.
I have tried changing the padding/margins on both the custom control and the main form but nothing seems to work.
Does anyone know why this extra space shows up?
Thank you.
I can see only two reasons for such behavior - either you have wrong Dock / Anchor settings (issue is reproduced when you have Dock set to None and Anchor to Top only), or your image, label and combobox are sitting in cells of some other control, like TableLayoutPanel which adjusts width of cells. Thus you don't have any additional container, then check very carefully Dock and Anchor settings of each of the controls (image, label and combobox). Also check if you change these properties at runtime (also check Margin, Location and Padding).

Changing textbox text causes parent windows to resize incorrecly

I've tried looking many places for an answer to an issue I'm having and so far I've found nothing.
What I currently have is a c# windows form with user controls inside it. Some user controls have other controls inside them. What happens when I change the text in a textbox, is its parent windows will no longer resize like they should when changing the window size. i.e. A horizontal scrollbar will appear even though horizontal scrollbars are disabled in that specific window. Its almost as if changing the text changes the parent window's styling.
In case this is too vague, I have a textbox inside a panel with a docking property set to fill. The panel has a padding of 10 in order to allow the textbox to have some white space for aesthetic purposes. This control resides within a parent control (we'll call it parent 1), which in turn resides within another control as well (we'll call it parent 2). So when I change the textbox's text (at all, even adding a space), will then make parent 2 have a horizontal scrollbar flicker and sometimes even remain when resizing the form window manually.
You should make sure that not only the TextBox in the UserControl is docked to fill but also that the user control itself and its parent (and its parent) are Docked correctly or have anchor set so that they resize with the Form.
Do you execute any special code when the user enters a character? (KeyPressed event etc.). If yes you should try disabling the events temporarily to see if they cause the problem.
If you post a sample of your code it would be easier to help. Without this we can only guess, like I tried...
I found out my issue! When using autoScrollBars and double buffering, it caused the horizontal scrollbar to show when it shouldn't have (at least in my case) when resizing the window. The answer was simple, forget the autoScrollBars, and implement my own vertical scroll bar!
I was actually getting some code to post up on here for you guys to look at, but when looking at it, I decided to forget the autoscroll, and lo and behold it worked!
I'm actually curious as to why that is though. My friend heard that .net has some issues with autoScroll but I didn't think it would be to this degree.

Using anchor property with dynamically added controls

I'm adding some textboxes to a form dynamically at runtime. Everything works fine i.e. the textboxes are aligned, anchored and automatically resizes until the form is maximized. On maximizing the form, the textboxes are added to the same location while the form was not maximized. This causes a misalignment of the textboxes.
How can I ensure that all the textboxes are at the same location and of the same size both while the windowstate is normal as well as maximized?
EDIT:
Btw I'm using C#
EDIT:
Would a flowlayoutpanel be useful here?
It's a quite old question, but maybe i'm able to answer it.
After reading all your comments, i think i can summarize your problem to this:
You have a form at a specific size and add some controls at runtime at a specific location with anchor set to Top | Right.
If you just display the form and let the controls appear everything works fine
If you maximize your form (or change the size of it) your controls won't be appear anymore at the correct location you want.
To get rid of this problem you can try some different approaches:
Use a FlowLayoutPanel, take care for the FlowDirection and maybe just create all your needed controls beforehand and just toggle the visible state.
Use correct values for the location of your newly created controls.
The second point is the error you have (i think). You found someway to calculate the location of your control if your form has it's original size. To get the correct position if the size of the form has changed (e.g. maximized) you have to consider several factors.
The delta values from your default size to your current size.
The Anchor(s) you wish to set on your control.
In your case you'd like to put a control which is anchored Top | Right, but the location is set by Top | Left. In that case you have to calculate the difference between the control.location.x and the form.width in it's default size. Then you take this difference and subtract it from the form current width. Now you can place your control at this position (cause Top never changes through a resizing). If you have a Anchor at Bottom | Right you have to calculate the same with control.location.y and form.height.
The behaviour and calculation if no anchor, for Top | Bottom or Left | Right are set is left as an exercise to the reader. ;-)
Last but not least there is also another hacky way to get your control at the right position:
If you like to place a new control somewhere change the Form.Visible to false
Save the form state, size and location
Change them to your default values
Add the controls you want
Restore the formerly saved values
Make the form visible again.
The Anchor property specifies which borders the controls should ensure they're always the same distance from. It can get pretty confusing, which is why you're seeing things shift around when anchored to the right border.
If you just want to ensure that the textbox display stays consistent relative to itself, I'd suggest putting down a Panel, with anchoring on the Panel, and then adding textboxes to the Panel. The X and Y coordinates on your text boxes become relative to the Panel, so it's a lot easier to do layout especially when the location of the Panel suddenly changes.

Best Layout of a WinForms UserControl with both Static and Dynamic Content?

I have a user control that has:
a) a buttons panel at the top (it always has to be visible)
b) a panel with controls that are dynamically added and re-sized at run-time. The controls can be many, so the panel has to be scrollable.
This user control will be hosted in a form, with the following requirements:
a) The initial size of the form will try to fit in maximum part of the dynamic content.
b) On changing the form size, the control has to be re-sized accordingly.
I had played with various anchoring, docking, and auto-sizing and I don't quite get it working in the way I want to. Sometimes, it is the scrolling that messes up, sometimes it is something else.
What combination of anchoring, docking, and auto-sizing of the panels, usercontrol, form should work best to achieve the desired outcome?
I succeeded to meet the requirements. Here is my solution:
The dynamic panel is anchored to the top and the bottom of the control. It does not AutoSize, it manually changes its MaximumSize and PreferredSize after change in the contents.
The form hosts the form using:
cntrl.AutoSize = true;
cntrl.AutoSizeMode = System.Windows.Forms.AutoSizeMode.GrowAndShrink;
cntrl.Dock = System.Windows.Forms.DockStyle.Fill;
The form subscribes to a custom control's event that notifies for the preferredHeight and it changes its own Height accordingly.
I'd go with a table layout panel. You can specify two rows by one column with the exact size for the buttons at the top and fill the rest with the bottom. Then put put either a normal panel or a flowlayoutpanel for the dynamic content in that area.
Without knowing the specifics of your problem I find multiple fill docked split containers with one fixed panel and/or a fixed slider usually creates a really handy resizing experience. You can also collapse panels very effectively too.

Categories

Resources