WinForms - Dynamically Add Multiline Textboxes to Panel with Horizontal Scrollbars - winforms

I have been given a design that I have to reproduce in Winforms. My attempt is shown below...
At the moment I have 5 static labels and 5 static multiline text boxes to hold my lists of "items"...
But, I need to be able to cope with more than 5 columns...
Not being massively experienced in Winforms, what would be the best way to be able to add additional columns with a horizontal scrollbar to enable the user to scroll from left to right?
I was thinking that maybe a panel, with scrollbars set to Auto, and then dynamically add the titles and text boxes at calculated positions?
Is that possible? Is that the best approach?
Any help would be greatly appreciated.
Thanks
Trev

yes, this looks the way to do it...
this code worked as a test...
int number = 0;
int LocationX = 169;
while (number < 10)
{
// create a new multiline text box
TextBox trev = new TextBox();
trev.Size = txtParent1.Size;
trev.Location = new Point() { X = txtParent1.Location.X + LocationX, Y = txtParent1.Location.Y };
trev.Name = number.ToString();
panelSummary.Controls.Add(trev);
number = number + 1;
LocationX += 169;
}

You could create a view that includes a label and the text box and in the main view you could use a TableLayoutPanel, for every view create a new column and add the view inside the column with Dock set to Fill. This way the resizing and the scrolling is handled by the TableLayoutPanel.
TableLayoutPanel

Related

How To Highlight a Column in a WinForm StackedColumn Chart

I'd like to highlight a single column in a WinForm StackedColumn Chart. As examples, I'm seeing how to put borders around the individual DataPoints in each Series displayed in the column and put an ArrowAnnotation pointing at one of the DataPoints, but I don’t see a way to highlight the column as a whole. For example, it would be great to have the column expand to say twice the width of the other columns and/or have a different backcolor (including the empty areas above and below the DataPoints). Is it possible to do what I want and, if so, how? C# examples are preferable but not necessary.
Thanks. Steve
You could dim every other column, using BackHatchStyle = ChartHatchStyle.Percent50 and BackSecondaryColor = Color.Black. This will make your chosen column appear brighter/highlighted.
Here's an example:
int highlightColumnIndex = 0; // Set the highlighted column here!
foreach (Series cs in chart1.Series) {
foreach (DataPoint dp in cs.Points) {
dp.BackSecondaryColor = Color.Black;
dp.BackHatchStyle = ChartHatchStyle.Percent50;
}
cs.Points[highlightColumnIndex].BackHatchStyle = ChartHatchStyle.None;
}
Change chart1 to your chart's name, and change highlightColumnIndex to match the index of the column you want to highlight.
Hope this helps :)

How to force a WinForms control to fill up FlowLayoutPanel that's in a multi-column-spanning cell in a TableLayoutPanel?

I have a TextBox that's in a FlowLayoutPanel that's inside a cell of a TableLayoutPanel. The FlowLayoutPanel spans 5 columns of the TableLayoutPanel and fills up the entire width of the 5 columns. However, the TextBox doesn't fill up the entire width of the FlowLayoutPanel, as you can see here (the black border is the FlowLayoutPanel):
How can I get the TextBox to span the entire width of the FlowLayoutPanel?
Code to produce this example:
// fsi --exec Test.fsx
open System
open System.Windows.Forms
let frmMain () =
let f = new Form(Text = "Test table layout panel")
let tlp =
new TableLayoutPanel
( ColumnCount = 5,
RowCount = 1,
AutoSize = true,
Parent = f )
let flp =
new FlowLayoutPanel
( AutoSize = true,
BorderStyle = BorderStyle.FixedSingle )
let tb = new TextBox(Parent = flp)
tlp.Controls.Add(flp, 0, 0)
tlp.SetColumnSpan(flp, 5)
flp.Dock <- DockStyle.Fill
tb.Dock <- DockStyle.Fill
f
[<STAThread>]
do
Application.EnableVisualStyles()
Application.Run(frmMain ())
Docking doesn't work inside a FlowLayoutPanel since it wants to layout the controls in a flowing order. Since you want to dock-fill the TextBox control, try using a simple Panel control instead.
Also, set the Multiline property of the TextBox to true.
You can't really Dock inside a flow layout panel, it makes no sense.
A flow layout panel is used to contain a collection of controls that flow together (left to right, top to bottom), having one control fill it will negate the usefulness of the flow layout.
If you want to fill the columns with a text box, just use a Textbox with a Dock Fill and Multiline set to true

Greek character are not displayed in winform combobox

I am working on a winform application, where I have grid with column displaying certain length measurement units. I have defined a column as below.
var unitColumn = new DataGridViewComboBoxColumn {
Name = "UnitColumn",
HeaderText = "UnitColumnHeader",
Width = 80,
DataSource = new[] { "nm", "mm", "μm" },
};
_calibGrid.Columns.Add(unitColumn);
As you can see the second item in the combobox suppose to display μm, but it displays the m. After I choose the item text in the cell displayed correctly. I am quite new to winform development, can any one suggest the fix/solution?
Try - Console.WriteLine("\u00b5");
I came across this same problem (but with some other characters). Don't know why it does this.
It is quite simple to implement the DrawItem event and draw the text correctly. We use a method similar to this:
https://stackoverflow.com/a/857232/417721

Odd DataGridView vertical scrolling behavior

I have a windows forms DataGridView. That I fill and update using the code below, all pretty straightforward and it is all done on the UI thread.
For some strange reason sometimes the size of the vertical scrollbar (wich I set to be only visible when needed) does not reflect the amount rows available. If I scroll all the way down, I still cannot see the very last rows. I can tell by selecting the lines below (and bringing them into view) by using the arrow down key.
What could possibly be a reason for this. Do I need some sort of BeginUdate or SuspendLayout or something? The control is embedded through interop in an MFC application.
Andy idea how to track down this problem? Is this a known bug? Google doesn't think so, it seems.
Here is the code I use.
adding or inserting a row:
int newRowIndex = insertAt;
if (insertAt < 0 || insertAt > this.dataGridView.Rows.Count)
{
newRowIndex = this.dataGridView.Rows.Add();
}
else
{
this.dataGridView.Rows.Insert(insertAt, 1);
}
removing a row:
this.dataGridView.Rows.Remove(index);
clearing:
this.dataGridView.Rows.Clear();
updating a row:
this.dataGrid[0, rowIndex].Value = someString;
this.dataGrid[1, rowIndex].Value = someBool;
this.dataGrid[2, rowIndex].Value = someInt;
I had the same problem and found that when I set the DataGridView's scrollbar property in code as opposed to the designer, it worked just fine. So I just had something along these lines:
foreach(Listitem item in list)
{
//populate grid
}
dataGridView.ScrollBars = ScrollBars.Both;
Have no idea why it worked however :)

Programatically bringing a Datagrid column into view (horizontal scroll)

I would like to bring a column into view programatically. I have a lot of columns and when they are off screen i would like to select a cell and bring that cell into view, i can select a cell that is off screen and when i horizontal scroll to bring the cell visible the cell is selected.
i know you can do this with the rows, i.e ScrollIntoView but how about columns?
anyone had any luck doing this? and if so how :) (of course)
Answer : use the datagrid method.
public void ScrollIntoView(object item, DataGridColumn column);
which takes a column. simple.
aran, suppose you want to go to 30 th column, try setting the horizontal offset of the scroll viewer.
like,
columnCount = 30;
offset = columCount * Columnwidth
scrollViewer.ScrollToHorizontalOffset(offest);
best,
Quafin

Resources