I am trying to programmatically organize from top to down a pair of label-textbox using a flowlayoutpanel. What I am trying to get is similar to the following image:
modscan example
so I have implemented below code (I need to create 254 label-textbox pair):
Dim lbl As Label
Dim txt As TextBox
Dim flowLayout As FlowLayoutPanel
For i As Integer = 0 To 253
lbl = New Label
lbl.Text = i.ToString("000") + ":"
lbl.Padding = New Padding(0)
lbl.Margin = New Padding(0)
txt = New TextBox
txt.Text = "<" + i.ToString.PadLeft(3, " ") + ">"
txt.MaxLength = 5
txt.Margin = New Padding(0)
txt.Size = New Size(39, 20)
flowLayout = New FlowLayoutPanel
flowLayout.FlowDirection = FlowDirection.LeftToRight
flowLayout.Controls.Add(lbl)
flowLayout.Controls.Add(txt)
flowLayout.Padding = New Padding(0)
flowLayout.Margin = New Padding(0)
Me.FlowLayoutPnl.Controls.Add(flowLayout)
Next
but using above code I am getting below:
my flowlayoutpanel
Any ideas?
If I understand what you're looking for, this code should give you expected result.
Dim flowLayout As New FlowLayoutPanel
flowLayout.AutoScroll = True
For i = 0 To 253
Dim label As New Label
label.AutoSize = True
label.Padding = New Padding(10, 5, 5, 10)
label.Text = i.ToString("000 ") + ":"
Dim txt As New TextBox
txt.Text = "Input " + i.ToString
txt.MaxLength = 5
flowLayout.Controls.Add(label)
flowLayout.Controls.Add(txt)
Next
Controls.Add(flowLayout)
flowLayout.Dock= DockStyle.Fill
This is what I get when I run this code:
By the way, the desired picture you posted shows a pair of labels, not label/TextBox. About the code, you have to create the main container (FlowLayoutPanel) first, then while you make itterationm you have to add each element to your container. Finally, you need to add the FlowLayoutPanel to your form controls to be shown on the form.
I have solved it by using below code:
Private Sub PopupForm_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Me.Load
Dim lbl As Label
Dim txt As TextBox
Dim flowLayout As FlowLayoutPanel
Dim g As Graphics
For i As Integer = 0 To 253
lbl = New Label
lbl.Text = i.ToString("000") + ":"
lbl.Anchor = AnchorStyles.None
lbl.AutoSize = True
txt = New TextBox
txt.Text = "<" + i.ToString.PadLeft(3, " ") + ">"
txt.MaxLength = 5
txt.Anchor = AnchorStyles.None
txt.ReadOnly = True
g = txt.CreateGraphics
txt.Width = g.MeasureString(txt.Text, txt.Font).Width + 5
g.Dispose()
flowLayout = New FlowLayoutPanel
flowLayout.FlowDirection = FlowDirection.LeftToRight
flowLayout.AutoSize = True
flowLayout.Anchor = AnchorStyles.None
flowLayout.Margin = New Padding(0)
flowLayout.Padding = New Padding(0)
flowLayout.Controls.Add(lbl)
flowLayout.Controls.Add(txt)
Me.FlowLayoutPnl.Controls.Add(flowLayout)
Next
End Sub
and the result is this
Notes:
My FlowLayoutPnl is created in design time with below properties (others to default):
AutoSize to true
AutoScroll to true
Dock to fill
FlowDirection to TopDown
Related
I have panels and each of them has 1 label. Everything works fine except 1 thing:
I can't fit the Panel Height to the Label Height...
I'm using this code:
Point location = new Point(0, 0);
ColorConverter cc = new ColorConverter();
foreach (var item in temp)
{
Panel pan = new Panel();
pan.AutoSize = false;
pan.Width = this.Width-75;
pan.Location = location;
pan.BackColor = (Color)cc.ConvertFromString("#" + item.Item3);
Label lbl = new Label();
lbl.Font = new Font("Arial", 12);
lbl.ForeColor = Color.White;
lbl.Text = item.Item2;
lbl.AutoSize = true;
lbl.MaximumSize = new Size(pan.Width - 5, 0);
lbl.Width = pan.Width - 10;
lbl.Location = new Point(lbl.Location.X + 5, lbl.Location.Y + 5);
//pan.Height = lbl.Height + 5;
pan.Controls.Add(lbl);
flowLayoutPanel1.Controls.Add(pan);
location = new Point(location.X - pan.Height, location.Y);
}
I tried doing this:
pan.Height = lbl.Height + 5;
But it the panel is then way too small...
It seems to me, that you are using a panel in order to get a margin around the label within the FlowLayoutPanel. If this is the case, set the Label's margin instead and don't use a Panel:
lbl.Margin = new Padding(5, 5, 80, 5);
or
lbl.Margin = new Padding(5); // If all margins are equal
the constructors are declared like this
public Padding(int left, int top, int right, int bottom)
public Padding(int all)
You could try docking the label in the panel, set the panel AutoSize to true and set AutoSizeMode to GrowAndShrink. Then you can set the panel padding to 5. That way you won't have to worry about the label size or location
foreach (var item in temp)
{
Panel pan = new Panel();
pan.Padding = new Padding(5);
pan.AutoSize = true;
pan.AutoSizeMode = AutoSizeMode.GrowAndShrink;
pan.BackColor = (Color)cc.ConvertFromString("#" + item.Item3);
Label lbl = new Label();
lbl.Dock = DockStyle.Fill;
lbl.Font = new Font("Arial", 12);
lbl.ForeColor = Color.White;
lbl.Text = item.Item2;
lbl.AutoSize = true;
lbl.MaximumSize = new Size(pan.Width - 5, 0);
pan.Controls.Add(lbl);
flowLayoutPanel1.Controls.Add(pan);
location = new Point(location.X - pan.Height, location.Y);
}
Edit : forgot the padding.
I have dynamic Labels and TextBox's on one panel.
I can delete the Panel. No Problem but then I also don't know how to delete the Textboxes etc
and i hoped that i can refresh or clear the panel so that all labels and textboxes will deleted..
Label makeLabelC = new Label();
makeLabelC.Width = 100;
makeLabelC.Font = new Font(makeLabelC.Font.Name, 8, FontStyle.Bold | FontStyle.Underline);
makeLabelC.Location = new Point(400, 100);
makeLabelC.Name = e.Node.Text;
makeLabelC.Text = e.Node.Text;
this.Controls.Add(makeLabelC);
this.Controls.Add(panel1);
TextBox textboxC = new TextBox();
textboxC.Width = 100;
textboxC.Location = new Point(500, 100 );
textboxC.Name = e.Node.Text + "lbl";
textboxC.Text = "enter here";
this.Controls.Add(textboxC);
this.Controls.Add(panel1);
for (int z = 0; z < n; z++)
{
Label makeLabel = new Label();
makeLabel.Width = 100;
makeLabel.Location = new Point(400, 150 + 2 * z * makeLabel.Height);
makeLabel.Name = e.Node.Text;
makeLabel.Text = e.Node.Nodes[z].Text;
this.Controls.Add(makeLabel);
this.Controls.Add(panel1);
TextBox textbox = new TextBox();
textbox.Width = 100;
textbox.Location = new Point(500, 150 + 2 * z * textbox.Height);
textbox.Name = e.Node.Text + "lbl";
textbox.Text = "enter here";
this.Controls.Add(textbox);
this.Controls.Add(panel1);
}
}
is there a way with panel how to do this or an other solution?
I thought that the Panel can help me there...
thanks Janik
You are adding the controls to the form instead of the panel - which you also add multiple times
this.Controls.Add(panel1); // do this once
panel1.Controls.Add(textbox); // add the controls to the panel
Once you have done this, when you remove the panel, you will also remove its child controls.
Im trying to create event with different index for dynamically created GroupBox. With my actual code event for every groupbox is that same. How can i make event with different index for every groupbox? My Code:
public void LoadGry()
{
// GroupBox groupbox = new GroupBox();
Label nazwagry = new Label();
for(int i = 0; i < myCollection.Count; i++)
{
GroupBox groupbox = new GroupBox();
groupbox.Text = myCollection[i];
groupbox.Size = new Size(290, 131);
groupbox.Location = new Point(6, 150 * (myCollection.Count - i - 1));
groupbox.ForeColor = Color.White;
Label label1 = new Label();
label1.Text = groupbox.Text;
label1.AutoSize = true;
label1.Location = new Point(groupbox.Location.X + 80, groupbox.Location.Y + 20);
groupbox.Controls.Add(label1);
Gry.Controls.Add(label1);
PictureBox picturebox = new PictureBox();
picturebox.Location = new Point(groupbox.Location.X + 5, groupbox.Location.Y + 20);
picturebox.Size = new Size(75, 75);
picturebox.SizeMode = PictureBoxSizeMode.StretchImage;
picturebox.LoadAsync(myCollection3[i]);
groupbox.Click += new EventHandler(delegate {groupboxclick(groupbox, picturebox, i);});
Label label2 = new Label();
label2.Text = "Status: " + "Aktualny";
label2.ForeColor = Color.Green;
label2.AutoSize = true;
label2.Location = new Point(label1.Location.X, label1.Location.Y + 20);
Gry.Controls.Add(label2);
Label zapiszopis = new Label();
zapiszopis.Text = myCollection4[i];
zapiszopis.Visible = false;
Gry.Controls.Add((Control)groupbox);
//MessageBox.Show("pokaz mi wysokosc");
}
}
private void groupboxclick(GroupBox groupbox, PictureBox picturebox, int itest)
{
groupbox.ForeColor = Color.Aqua;
this.pictureBox1.BackgroundImage = picturebox.BackgroundImage;
opishacka.Text = myCollection4[itest];
}
The problem is that the event setup is using the variable K value. For use the number instead you probably needs to create an expression manually to use the current value in each case.
BUT
You can easily do what you want using the following properties to attach values to controls.
1-) Tag in WinForms & WPF:
// Setup
pictureBox.Tag = i;
// Event
int i = (int) pictureBox.Tag;
2-) ViewState in WebForms
// Setup
ViewState[pictureBox.UniqueID] = i;
// Event
int i = (int) ViewState[pictureBox.UniqueID];
You can use many other techniques. I only post one for each popular framework. I guest that you are in a WinFors project.
Hope this help!
I am trying to add content in the detail section of ActiveReport. But the section height is limited to 2 inches. It is taking only (2/0.2 = )10 items. I want the section to increase its height as the contents increase, so that it can adopt all item. It seems like .CanGrow is not working. The code I am using is as below.
Dim lObjSecRpt As New GrapeCity.ActiveReports.SectionReport()
Dim lObjLbl As New GrapeCity.ActiveReports.SectionReportModel.Label()
Dim c As Single = 0.2F
Try
lObjSecRpt.Sections.InsertPageHF()
lObjSecRpt.Sections(0).BackColor = Color.WhiteSmoke
lObjSecRpt.Sections(0).Height = 0.0F
lObjSecRpt.Sections.Insert(1, New GrapeCity.ActiveReports.SectionReportModel.Detail())
lObjSecRpt.Sections(1).BackColor = Color.WhiteSmoke
lObjSecRpt.Sections(1).CanGrow = True
For Each dr As DataRow In mObjDtReport.Rows
lObjLbl = New GrapeCity.ActiveReports.SectionReportModel.Label()
lObjLbl.Alignment = GrapeCity.ActiveReports.Document.Section.TextAlignment.Left
lObjLbl.Font = New System.Drawing.Font("Arial", 10, FontStyle.Regular)
lObjLbl.Location = New PointF(0.0F, c)
lObjLbl.Height = 0.2F
lObjLbl.Width = 1.0F
lObjLbl.Text = CStr(dr("RptObjNam"))
lObjSecRpt.Sections(1).Controls.Add(lObjLbl)
c += c
Next
Me.rptViewer.LoadDocument(lObjSecRpt)
Ammar,
What you are trying to do in the code is creating sections and adding controls to the sections on the fly. So this is like creating a report layout at runtime. Since you are simply adding controls to the detail section, the format event for the detail section will not fire for each control as it is not bound to any data. Rather you are just adding controls to it. You can check and example of creating reports on the fly here.
If you want that the detail section should grow to show all the added controls, then you will need to increment its height on the basis of the total height of the controls inside it. For example check the sample code below which demonstrates how this can be done. You can simply add this code to the Form_Load event to verify it.
Dim lObjLbl As New GrapeCity.ActiveReports.SectionReportModel.Label()
Dim c As Single = 0.2F
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
Dim rpt As New GrapeCity.ActiveReports.SectionReport
rpt.Sections.InsertPageHF()
rpt.Sections(0).BackColor = Color.Yellow
rpt.Sections(0).Height = 1.0F
rpt.Sections.Insert(1, New GrapeCity.ActiveReports.SectionReportModel.Detail())
rpt.Sections(1).Name = "Detail"
rpt.Sections("Detail").BackColor = Color.Gainsboro
rpt.Sections("Detail").CanGrow = True
Dim i As Integer
For i = 0 To 20
Dim lObjLbl As New GrapeCity.ActiveReports.SectionReportModel.Label()
lObjLbl.Alignment = GrapeCity.ActiveReports.Document.Section.TextAlignment.Left
lObjLbl.Font = New System.Drawing.Font("Arial", 10, FontStyle.Regular)
lObjLbl.Location = New PointF(0.0F, c)
lObjLbl.Size = New SizeF(1.0F, 0.2F)
lObjLbl.Text = "Record: " + i.ToString()
lObjLbl.BackColor = Color.Aqua
rpt.Sections("Detail").Controls.Add(lObjLbl)
c += 0.2
Next
Dim height As Double = 0
For Each control As GrapeCity.ActiveReports.SectionReportModel.ARControl In rpt.Sections("Detail").Controls
height = height + control.Height
Next
rpt.Sections("Detail").Height = height
Viewer1.LoadDocument(rpt)
End Sub
I hope this helps.
I need to set a voltage in an application. I'm used to using sliders in Labview, and would like to replicate that using a C# program.
I've figured out that the track bar only does integer values, so rather than have the range go from -5 to 5 using a double, I need to have the track bar go from -50 to +50 with tick marks every 10 steps to get 0.1v resolution.
How do I label the track bar minimum and maximum values?
|-----|-----|-----|-----|-----|-----|-----|-----|-----|-----|
-5.0 0.0 5.0
I'm using C# with .net 3.5 with Visual Studio.
The easiest way would be to add to 2-3 labels, using a combination of height/width/top/left of the trackbar and labels you should be able to position them relatively without too much hassle?
EDIT: Also, it seems this post shows a custom implementation with the same requirements as you've described?
TrackBar Scale Image Link
What I've done is modify a chart and used its X axis scale as the scale for the TrackBar (See link for image [I would have embedded image but I couldn't get it to work]). The top half of the image is the slider and the bottom half is a regular chart object with it properties changed so the only thing visible is its X axis.
Here's the VS2010 Form Designer code for the TrackBar and Chart. This example will give you a scale of 0 to 100 which can easily be resized to line up with your trackbar. You could even put both controls in a panel so they can be easily resized as one (Code is VB rather than C#):
Dim ChartArea2 As System.Windows.Forms.DataVisualization.Charting.ChartArea = New System.Windows.Forms.DataVisualization.Charting.ChartArea()
Dim Legend2 As System.Windows.Forms.DataVisualization.Charting.Legend = New System.Windows.Forms.DataVisualization.Charting.Legend()
Dim Series2 As System.Windows.Forms.DataVisualization.Charting.Series = New System.Windows.Forms.DataVisualization.Charting.Series()
Dim DataPoint2 As System.Windows.Forms.DataVisualization.Charting.DataPoint = New System.Windows.Forms.DataVisualization.Charting.DataPoint(0.0R, 0.0R)
Me.chart_slider = New System.Windows.Forms.DataVisualization.Charting.Chart()
Me.TrackBar1 = New System.Windows.Forms.TrackBar()
CType(Me.chart_slider, System.ComponentModel.ISupportInitialize).BeginInit()
CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'chart_slider
'
Me.chart_slider.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.chart_slider.BackColor = System.Drawing.Color.Transparent
ChartArea2.AxisX.LabelAutoFitMaxFontSize = 8
ChartArea2.AxisX.LabelAutoFitMinFontSize = 8
ChartArea2.AxisX.LineColor = System.Drawing.Color.Transparent
ChartArea2.AxisX.MajorGrid.Enabled = False
ChartArea2.AxisX.Maximum = 100.0R
ChartArea2.AxisX.Minimum = 0.0R
ChartArea2.AxisY.Interval = 1.0R
ChartArea2.AxisY.LineWidth = 0
ChartArea2.AxisY.MajorGrid.Enabled = False
ChartArea2.AxisY.MajorTickMark.Enabled = False
ChartArea2.AxisY.Maximum = 0.0R
ChartArea2.AxisY.Minimum = 0.0R
ChartArea2.AxisY.TitleForeColor = System.Drawing.Color.Transparent
ChartArea2.BackColor = System.Drawing.Color.Transparent
ChartArea2.InnerPlotPosition.Auto = False
ChartArea2.InnerPlotPosition.Height = 5.0!
ChartArea2.InnerPlotPosition.Width = 100.0!
ChartArea2.Name = "ChartArea1"
Me.chart_slider.ChartAreas.Add(ChartArea2)
Legend2.Enabled = False
Legend2.Name = "Legend1"
Me.chart_slider.Legends.Add(Legend2)
Me.chart_slider.Location = New System.Drawing.Point(186, 426)
Me.chart_slider.Name = "chart_slider"
Series2.ChartArea = "ChartArea1"
Series2.Legend = "Legend1"
Series2.Name = "Series1"
Series2.Points.Add(DataPoint2)
Me.chart_slider.Series.Add(Series2)
Me.chart_slider.Size = New System.Drawing.Size(441, 31)
Me.chart_slider.TabIndex = 160
Me.chart_slider.Text = "Chart1"
'
'TrackBar1
'
Me.TrackBar1.Anchor = CType((((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
Or System.Windows.Forms.AnchorStyles.Left) _
Or System.Windows.Forms.AnchorStyles.Right), System.Windows.Forms.AnchorStyles)
Me.TrackBar1.Location = New System.Drawing.Point(191, 398)
Me.TrackBar1.Margin = New System.Windows.Forms.Padding(3, 3, 3, 0)
Me.TrackBar1.Name = "TrackBar1"
Me.TrackBar1.RightToLeftLayout = True
Me.TrackBar1.Size = New System.Drawing.Size(423, 45)
Me.TrackBar1.TabIndex = 1
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(690, 498)
Me.Controls.Add(Me.TrackBar1)
Me.Controls.Add(Me.chart_slider)
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.chart_slider, System.ComponentModel.ISupportInitialize).EndInit()
CType(Me.TrackBar1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
Me.PerformLayout()