I am trying to figure out how to position my DataGridView correctly inside of the parent container, but despite trying widths/heights/docks/anchors/etc. I absolutely cannot figure this out.
I would like to have the table anchored to the left/right/bottom but have some padding to keep it from being flush against the sides/bottom.
I want to keep some extra space at the top as I plan to put a second container there with additional options.
Not sure if this is related, but I would like the rows to be a percentage of the DataGridView's width. Can this be done?
How can I achieve this? I currently have the following set, but as can be seen from the screenshot, it only seems to anchor to the left and for some reason stays near the top.
dgv.Width = parent.Width;
dgv.Anchor = (AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Bottom);
You can use following properties of DataGridView(dgv)to work
Anchor: Top,Left // dgv.Anchor = AnchorStyles.Top | AnchorStyles.Left
AutoSizeColumn: Fill // we can fill for all columns or else it is enough to fill the last column dgv..Columns[0].AutoSizeModeDataGridViewAutoSizeColumnMode.Fill;
Dock: Fill // dgv.Dock = DockStyle.Fill
Just see the link How to resize datagridview control when form resizes
Related
I'm trying to keep two labels centrally aligned, one above the other. Before the form runs, everything looks great, but when running they are all misaligned.
Before running:
After running:
Example code for one label:
Me.TemperatureLabel1.AutoSize = True
Me.TemperatureLabel1.BackColor = System.Drawing.Color.WhiteSmoke
Me.TemperatureLabel1.Font = New System.Drawing.Font("Bahnschrift", 25.0!)
Me.TemperatureLabel1.ForeColor = System.Drawing.Color.Gray
Me.TemperatureLabel1.Location = New System.Drawing.Point(278, 53)
Me.TemperatureLabel1.Name = "TemperatureLabel1"
Me.TemperatureLabel1.Size = New System.Drawing.Size(227, 41)
Me.TemperatureLabel1.TabIndex = 8
Me.TemperatureLabel1.Text = "TempLabelVal"
Me.TemperatureLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter
Me.TemperatureLabel1.Visible = False
So what I would like is to have the values populated, but centrally aligned above each other.
Set label's AutoSize property to false and TextAlign property to MiddleCenter.
As another option, you can use TableLayoutPanel having 4 columns and 2 rows. Then drop labels inside cells, set Anchor property of the Label controls to none and keep their AutoSize as true.
This way, the labels are always will be aligned in center of the cell.
It also allows you to have absolute, percent or auto-size mode for columns.
If I choose one of the WF which have C1flexGrid, and try to maximize that form, columns in my grid have the same width. What I am trying to do is have the columns auto fit to the width of the content, and have the last column fill the remaining space. Is this possible?
For anyone still looking for the answer:
You can use AutoSizeCols method and ExtendLastCol property
c1FlexGrid1.ExtendLastCol = true;
c1FlexGrid1.AutoSizeCols(c1FlexGrid1.Cols.Fixed, c1FlexGrid1.Cols.Count - 1)
Reference.
I have a line chart, which, after enough data points have been plotted to it, the data will exceed what is visible on screen (so that the chart is only showing the most recent data). When this occurs, I would like a scroll bar to be filled for the X axis, allowing the user to use the scroll bar to view such previous data.
How do I go about doing this? I don't want the user to be able to drag or zoom on the chart itself, just to solely use the scroll bar to navigate along the chart.
I've looked at this article: https://msdn.microsoft.com/en-us/library/dd456730.aspx but it doesn't help & the scrollbars do not appear.
Without seeing the relevant parts of your code it is hard to pin down your problems.
Here is one strange statement:
after enough data points have been plotted to it, the data will exceed
what is visible on screen (so that the chart is only showing the most recent data).
Now this can only happen after you have set the AxisX.Maximum because by default the chart control will squeeze the area more and more while you add points.
But when you have set a maximum of what can be shown, no scrollbar can work or even been shown. Sounds logical, right?
So either don't set it in the first place or clear it when the number of points exceeds what you want to show. To clear it use NaN :
chart1.ChartAreas[0].AxisX.Maximum = Double.NaN;
Or, of course, set it to the last point you want to be shown!
After looking at what you mustn't do let's see what you need to do to show the scrollbar:
First you enable it:
chart1.ChartAreas[0].AxisX.ScrollBar.Enabled = true;
Next you tell it to show only the scrolling handle and not the zoom-reset button:
chart1.ChartAreas[0].AxisX.ScrollBar.ButtonStyle = ScrollBarButtonStyles.SmallScroll;
See MSDN on ScrollBarButtonStyles for the various things the scrollbar can show/do!
And to make sure the user can't zoom set this:
chart1.ChartAreas[0].AxisX.ScaleView.Zoomable = false;
And finally set the current range to show:
chart1.ChartAreas[0].AxisX.ScaleView.Size = 111; // show 111 DataPoints
Now the scrollbar should show.
It is a good idea to study the AxisScaleView class as it has a couple of helpful properties..
Depending on the data type of your X-Values you may also need to set the ScaleView.MinSizeType to whatever suits your data:
chart1.ChartAreas[0].AxisX.ScaleView.MinSizeType = DateTimeIntervalType.Number;
Take a GroupBox, put let say Label inside and then set AutoSizeMode = GrowAndShrink and AutoSize = true.
Two problems will arise:
There is a huge gap between Label and bottom of GroupBox (almost enough to fit another Label lol);
AutoSize doesn't respect the GroupBox.Text property.
Question is how to make GroupBox.AutoSize working properly? Properly means: minimum Width should be enough to fit GroupBox.Text, there should be no gaps below for unknown reason (it's not Margin, nor Padding and it looks pretty ugly).
I've tried to measure string length in OnPaint and setting MinimumSize right there. It works, but I have doubts about this, as if I would want to actually set MinimumSize later - it will be lost after repaint.
Update, here is screenshot:
You can get rid of the unwanted yellow space at the bottom by deriving a new class from GroupBox that adjusts the bottom edge a bit. In VB something like ...
Public Class BetterGroupBox
Inherits GroupBox
Public Overrides Function GetPreferredSize(ByVal proposedSize As Size) As Size
Dim ns = MyBase.GetPreferredSize(proposedSize)
Return New Size(ns.Width, ns.Height - 15)
End Function
End Class
It's simple that the location of your Label is fixed at some point other than (0,0), try this:
label1.Location = Point.Empty;
You may also want to try setting the Padding of your GroupBox to 0 for all (default is 3):
groupBox1.Padding = new Padding(0);
It seems as though the GroupBox control has a predefined padding of sorts when growing the control if AutoSize = true. That is, once a control (inside the GroupBox) gets within 20 pixels or so of the bottom of the GroupBox, the GroupBox starts growing. This causes a 20 pixel or so padding from the bottom of the bottom-most control to the bottom of the GroupBox (as highlighted in yellow by #Sinatr's attached image).
Based on my observations, the padding seems to be less when growing the Width of the GroupBox.
At any rate, you can do something like the following "get around" the issue:
public void MyFunction()
{
groupBox1.AutoSize = true;
// Do stuff (e.g., add controls to GroupBox)...
// Once all controls have been added to the GroupBox...
groupBox1.AutoSize = false;
// Add optional padding here if desired.
groupBox1.Height = myBottomMostControl.Bottom;
}
Background:
I have a TableLayoutPanel placed in a UserControl, which then is placed in SplitContainer. Rows are added programmatically. TableLayoutPanel is anchored Top|Left|Right, so after rows are added, its height is recalculated and it expands downward.
Inside the TableLayoutPanel, there are two columns. The size of the first column is Absolute, the size of the second column is set to AutoSize.
In every cell, there is a Label. All labels in the second column are defined as follows:
Label vName = new Label();
vName.AutoSize = true;
vName.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom;
vName.Margin = new Padding(3);
vName.TextAlign = ContentAlignment.MiddleLeft;
vName.Name = "controlName";
vName.Text = "Some text here";
vName.DoubleClick += new EventHandler(vName_DoubleClick);
vName.Dock = DockStyle.None;
The problem:
Usually, everything works all right, labels resize and everything, except for one strange scenario:
The text of the label is something like "immoballizes the device (33.33%)", width of TableLauoutPanel's second column is set exactly, so all text is shown in one line.
Splitter distance changes by one pixel and the UserControl is resized: width decreases, so the label should resize, and the text in a label should wrap.
The label is not resized, and the second line of text isn't shown, it also probably doesn't wrap (there would be a change in the text location in the label if it did).
Splitter distance changes by one pixel again, and the UserControl is resized: the width decreases further.
The label resizes all right and all text is shown, wrapped.
The same thing happens when the TableLayoutPanel's width increases, but always only if there is a difference of one pixel (between wrapping/not wrapping text).
Also, changing Dock and/or Anchor and/or BorderStyle properties of labels doesn't work (I probably tried all possible combinations...)
This picture illustrates the issue a little:
Apparently it is a label issue: when autosizing, it wasn't measuring text correctly and sometimes there was a one pixel difference. I've found a strange workaround, though, if someone knows something better please enlighten me.
This way text in my labels wraps correctly every time and everything is autosized properly:
void tableLayoutPanel1_Resize(object sender, EventArgs e)
{
float fWidth = tableLayoutPanel1.GetColumnWidths()[1];
foreach (Control ctr in tableLayoutPanel1.Controls)
{
if (ctr is Label && ctr.Name.Contains("vName_"))
{
// -7 for margins
Size s = TextRenderer.MeasureText(ctr.Text, ctr.Font, new Size((int)fWidth - 7,1000),
TextFormatFlags.VerticalCenter
| TextFormatFlags.Left
| TextFormatFlags.NoPadding
| TextFormatFlags.WordBreak);
if(!ctr.MaximumSize.Equals(s))
ctr.MaximumSize = new Size(s.Width, s.Height);
}
}
}