Add panels dynamically to form - c#

I'm trying to add panels to a form according to the ids quantity that is received. At running, the panels are added to the form, but only the first one has the controls, and I don't get why.
This is my actual code:
private void loadItems(List<int> ids)
{
int id = 0,
panelX = 10,
panelY = -30,
itemslblX = 15,
itemslblY = -65,
IDtxtX = 360,
IDtxtY = -40,
nametxtX = 130,
nametxtY = -40;
int height = (80 * ids.Count) + 50;
this.Size = new Size(600,height);
foreach(int c in ids)
{
panelY = panelY + 80;
itemslblY = itemslblY + 80;
terrainMenuY = terrainMenuY + 80;
IDtxtY = IDtxtY + 80;
nametxtY = nametxtY + 80;
Panel panel = new Panel();
panel.Location = new Point(panelX, panelY);
panel.Dock = DockStyle.None;
panel.Height = 75;
panel.Width = 575;
panel.BackColor = Color.Gray;
panel.Name = ids[id]+"Panel".ToString();
Label itemslbl = new Label();
itemslbl.Location = new Point(itemslblX, itemslblY);
itemslbl.Text = "Imagen Nombre ID";
itemslbl.Height = 20;
itemslbl.Width = 550;
itemslbl.Name = ids[id] + "itemsLabel".ToString();
TextBox IDtxt = new TextBox();
IDtxt.Location = new Point(IDtxtX, IDtxtY);
IDtxt.Height = 27;
IDtxt.Width = 200;
IDtxt.Text = ids[id].ToString();
IDtxt.ReadOnly = true;
IDtxt.Name = ids[id] + "IDtext".ToString();
TextBox nametxt = new TextBox();
nametxt.Location = new Point(nametxtX, nametxtY);
nametxt.Height = 27;
nametxt.Width = 200;
nametxt.ReadOnly = true;
nametxt.Name = ids[id] + "nameText".ToString();
panel.Controls.Add(itemslbl);
panel.Controls.Add(nametxt);
panel.Controls.Add(IDtxt);
this.Controls.Add(panel);
id++;
}
}
Any advice, please, and thanks for your time.

Related

C#/ Visual studio 2019 || windows form autoscroll flickering

I am trying to make a Windows Form that shows display data from data a table. Every thing is working fine until I start scrolling in the Form using the auto scroll property.
I don't know why that's happening. I hope someone can help me.
private void allShowsToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
// Load All the Shows from the database
DataTable dt = accesse.LoadAllShows();
// Create the allShows From
Form allShows = new Form();
// Create the Controls for the AllShows form.
PictureBox[] pic = new PictureBox[dt.Rows.Count];
Label[] showName = new Label[dt.Rows.Count], season = new Label[dt.Rows.Count], eps = new Label[dt.Rows.Count], typ = new Label[dt.Rows.Count];
TextBox[] txtShowName = new TextBox[dt.Rows.Count], txtSeason = new TextBox[dt.Rows.Count], txtEps = new TextBox[dt.Rows.Count], txtTyp = new TextBox[dt.Rows.Count];
// AllShows Form properties
allShows.BackColor = Color.White;
allShows.Font = new Font("Calibri", 14f, FontStyle.Regular);
allShows.ForeColor = Color.Black;
allShows.Size = new Size(1050, 700);
allShows.StartPosition = FormStartPosition.CenterScreen;
allShows.AutoScroll = true;
allShows.AutoScrollMargin = new Size(0, 18);
// Variables
int y = 325; // the distens bettwen the controls on the Y and X.
bool xTurn = false; // the axies turn.
int yTurnNum = 0; // the y turn number.
for (int i = 0; i < dt.Rows.Count; i++)
{
// PictureBox Poster Properties
pic[i] = new PictureBox();
pic[i].BorderStyle = BorderStyle.FixedSingle;
pic[i].Size = new Size(162, 288);
pic[i].SizeMode = PictureBoxSizeMode.StretchImage;
pic[i].Image = Image.FromFile(dt.Rows[i][4].ToString());
// Label showName Properties
showName[i] = new Label();
showName[i].Text = "Show Name: " + dt.Rows[i][0];
showName[i].AutoSize = true;
// Label Season Properties
season[i] = new Label();
season[i].Text = "Season: " + dt.Rows[i][1];
season[i].AutoSize = true;
// Label Eps Properties
eps[i] = new Label();
eps[i].Text = "Episodes: " + dt.Rows[i][2];
eps[i].AutoSize = true;
// Label Typ Properties
typ[i] = new Label();
typ[i].Text = "Typ: " + dt.Rows[i][3];
typ[i].AutoSize = true;
if (xTurn)
{
// Sitting the location of the controls on the X turn
pic[i].Location = new Point(515, pic[i - 1].Location.Y);
showName[i].Location = new Point(687, showName[i - 1].Location.Y);
season[i].Location = new Point(687, season[i - 1].Location.Y);
eps[i].Location = new Point(687, eps[i - 1].Location.Y);
typ[i].Location = new Point(687, typ[i - 1].Location.Y);
xTurn = false;
}
else
{
// Sitting the location of the controls on the Y turn
pic[i].Location = new Point(15, 15 + (yTurnNum * y));
showName[i].Location = new Point(187, 20 + (yTurnNum * y));
season[i].Location = new Point(187, 55 + (yTurnNum * y));
eps[i].Location = new Point(187, 90 + (yTurnNum * y));
typ[i].Location = new Point(187, 125 + (yTurnNum * y));
yTurnNum += 1;
xTurn = true;
}
allShows.Controls.Add(pic[i]);
allShows.Controls.Add(showName[i]);
allShows.Controls.Add(season[i]);
allShows.Controls.Add(eps[i]);
allShows.Controls.Add(typ[i]);
}
allShows.ShowDialog();
}
catch (Exception ex)
{
tools.ShowErrorMessageBox(ex.Message, "Error");
}
}
I hope this gif will help you to understand what is happening.
enter image description here

How to place button at the end of form, but when AutoScroll = true

I have multiple GroupBox's, and that's why I set AutoScroll to true. I create all controls in Form_Load. How to place one button after all GroupBox'es?
The code, where I create GroupBoxes:
for (int i = 0; i < 10; i++)
{
GroupBox gb = new GroupBox();
gb.Name = "GroupBox" + (i + 1);
gb.Size = new Size(500, 200);
gb.Location = new Point(40, loc);
gb.BackColor = System.Drawing.Color.Aquamarine;
Label q_text = new Label(); // текст питання
q_text.Name = "label" + (i + 1);
q_text.Text = "Питання" + (i + 1);
q_text.Font = new Font("Aria", 10, FontStyle.Bold);
q_text.Location = new Point(10, 10);
gb.Controls.Add(q_text);
int iter = q_text.Location.Y + 30;
if (i <= 5)
{
foreach (string key in questions[i].answers.Keys)
{
RadioButton rb = new RadioButton();
rb.Text = key;
rb.Size = new Size(120, 25);
rb.Location = new Point(q_text.Location.X + 10, iter);
iter += 30;
gb.Controls.Add(rb);
}
}else
if (i > 5)
{
foreach (string key in questions[i].answers.Keys)
{
CheckBox rb = new CheckBox();
rb.Text = key;
rb.Size = new Size(120, 25);
rb.Location = new Point(q_text.Location.X + 10, iter);
iter += 30;
gb.Controls.Add(rb);
}
}
this.Controls.Add(gb);
loc += 200;
Place all the scrollable group boxes in one panel which is set to AutoScroll = true. Below this panel is another one containing the fixed button. This panel is docked to the bottom.
Since you are using a loc variable, you can do:
btnMyButton.Locaction= new Point(40, loc);
Anyway, if you want to find the position of the last group box dynamically, try this:
double leftPos=0,topPos=0;
foreach(Control c in Forms.Controls)
{
if(c.Name=="GroupBox")
{
if(c.Left>leftPos)
leftPos=c.Left;
if(c.Top>topPos)
topPos=c.Top;
}
}

Drawing dynamic panels C#

Good afternoon.
There was a problem with the rendering of dynamically created panel, all panels initially were 100px and I their renders (y + = 100), but now the production has changed a bit, these panels can be of different sizes, and the distance between them actually remains unchanged 100rh ...
Pliz tell me how to make, that they are somehow drawn at equal (10px) distances from each other. Read that you can somehow make a method SetBounds, but did not understand.
http://pastebin.com/TnSuFTti
for (int i = data_list.Count - 1; i >= 0; i--)
{
Panel panel = new Panel(); //создание блока сообщения и наложение картинки
panel.Name = i.ToString();
panel.MouseEnter += new EventHandler(panel_MouseEnter);
Label textBox_name = new Label();
Label textBox_date = new Label();
Label textBox_msg = new Label();
panel.Width = 308;
Bitmap btm_msg = new Bitmap(
Properties.Resources.NotificationCenterWindow_msg_box);
panel.BackgroundImage = btm_msg;
panel.BackgroundImageLayout = ImageLayout.Stretch;
panel.Location = new Point(5, 5);
panel.Controls.Add(textBox_date);
textBox_date.Name = "textBox_date" + i.ToString();
textBox_date.Location = new Point(232, 8);
textBox_date.Size = new System.Drawing.Size(70, 15);
textBox_date.BorderStyle = BorderStyle.None;
textBox_date.MinimumSize = new System.Drawing.Size(72, 15);
textBox_date.TextAlign = ContentAlignment.MiddleRight;
textBox_date.BackColor = Color.DarkGray;
textBox_date.Anchor = AnchorStyles.Right | AnchorStyles.Top;
textBox_date.ForeColor = SystemColors.ScrollBar;
panel.Controls.Add(textBox_name);
textBox_name.Size = new System.Drawing.Size(100, 15);
textBox_name.MinimumSize = new System.Drawing.Size(100, 15);
textBox_name.Location = new Point(5, 8);
textBox_name.BorderStyle = BorderStyle.None;
textBox_name.BackColor = Color.DarkGray;
textBox_name.ForeColor = SystemColors.ScrollBar;
panel.Height = textBox_name.Height + 19;
panel1.Controls.Add(panel);
panel.Anchor = AnchorStyles.Left | AnchorStyles.Right | AnchorStyles.Top;
y += 100;
}
panel.Location = new Point(5, y);
//.....
y = panel.Bottom + 10;
Just set the position based on the previous panel...
// This will put the panel to the right of an existing one
panel_b.Left = panel_a.Left + panel_a.Width + 10;
.
// This will put the panel below an existing one
panel_b.Top= panel_a.Top+ panel_a.Height+ 10;

The graph resizes and becomes smaller on every function call

I have a function that I need to call every once in a while. But, every time I do, the graph (a pie chart) resizes itself and becomes smaller than the previously obtained piechart.
void LoadPieChart(DateTime lower, DateTime higher)
{
splitContainer1.Panel1.Controls.Remove(pieChart);
pieChart.Series.Clear();
pieChart.Palette = ChartColorPalette.Fire;
pieChart.BackColor = Color.Black;
pieChart.Titles.Add("LOST OPPORTUNITY");
pieChart.ChartAreas[0].BackColor = Color.Transparent;
Series series1 = new Series
{
Name = "series1",
IsVisibleInLegend = true,
Color = System.Drawing.Color.White,
ChartType = SeriesChartType.Pie
};
pieChart.Series.Add(series1);
int num = A.CountLO();
List<string>[] lista = new List<string>[7];
lista = A.SelectLO();
float counter_manpower = 0;
float counter_spares = 0;
float counter_tools = 0;
float counter_other = 0;
string[] reason = lista[6].ToArray();
string[] low = lista[4].ToArray();
string[] up = lista[5].ToArray();
string[] collection = new string[366];
for (int j = num-LO; j < num; j++)
{
DateTime x = DateTime.Parse(low[j]);
DateTime y = DateTime.Parse(up[j]);
if (x.Date>=lower.Date && y.Date<=higher.Date)
{
if (reason[j].Equals("LACK OF MANPOWER"))
counter_manpower++;
if (reason[j].Equals("LACK OF SPARES"))
counter_spares++;
if (reason[j].Equals("LACK OF TOOLS"))
counter_tools++;
if (!reason[j].Equals("LACK OF MANPOWER") && !reason[j].Equals("LACK OF SPARES") && !reason[j].Equals("LACK OF TOOLS"))
{
counter_other++;
}
}
}
float a = ((counter_manpower/(counter_manpower+counter_spares+counter_tools+counter_other))*100);
float b = ((counter_spares / (counter_manpower + counter_spares + counter_tools+counter_other)) * 100);
float c = ((counter_tools / (counter_manpower + counter_spares + counter_tools+counter_other)) * 100);
float d = ((counter_other / (counter_manpower + counter_spares + counter_tools + counter_other)) * 100);
double aa = Math.Truncate(100 * a) / 100;
double bb = Math.Truncate(100 * b) / 100;
double cc = Math.Truncate(100 * c) / 100;
double dd = Math.Truncate(100 * d) / 100;
series1.Points.Add(counter_manpower);
var p1 = series1.Points[0];
Math.Round(a, 1);
Math.Round(b, 1);
Math.Round(c, 1);
if (counter_manpower!=0)
p1.AxisLabel = (aa.ToString() + "%");
p1.LegendText = "LACK OF MANPOWER";
p1.Color = Color.Red;
series1.Points.Add(counter_spares);
p1 = series1.Points[1];
if (counter_spares!=0)
p1.AxisLabel = (bb.ToString() + "%");
p1.LegendText = "LACK OF SPARES";
p1.Color = Color.Yellow;
series1.Points.Add(counter_tools);
p1 = series1.Points[2];
if(counter_tools!=0)
p1.AxisLabel = (cc.ToString() + "%");
p1.LegendText = "LACK OF TOOLS";
p1.Color = Color.Orange;
series1.Points.Add(counter_other);
p1 = series1.Points[3];
p1.AxisLabel = (dd.ToString() + "%");
p1.LegendText = "OTHER";
p1.Color = Color.Maroon;
//pieChart.Invalidate();
splitContainer1.Panel1.Controls.Add(pieChart);
}
I can't seem to find out why, any suggestions?
I use the following function to initalize the graph:
private void InitializeChart()
{
this.components = new System.ComponentModel.Container();
ChartArea chartArea1 = new ChartArea();
Legend legend1 = new Legend() { BackColor = Color.White, ForeColor = Color.Black, Title = "CAUSE" };
pieChart = new Chart();
((ISupportInitialize)(pieChart)).BeginInit();
SuspendLayout();
//===Pie chart
chartArea1.Name = "PieChartArea";
pieChart.ChartAreas.Add(chartArea1);
pieChart.Dock = System.Windows.Forms.DockStyle.Fill;
legend1.Name = "Legend1";
pieChart.Legends.Add(legend1);
pieChart.Location = new System.Drawing.Point(0, 50);
//====Bar Chart
AutoScaleDimensions = new System.Drawing.Size(284,262);
AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
//this.ClientSize = new System.Drawing.Size(284, 262);
this.Load += new EventHandler(Form1_Load);
((ISupportInitialize)(this.pieChart)).EndInit();
this.ResumeLayout(false);
}
I'm not sure where pieChart is defined... a private field?? If so, try creating a new PieChart after you have removed the previous one from the container:
splitContainer1.Panel1.Controls.Remove(pieChart);
pieChart = new PieChart();
pieChart.Series.Clear();
I'm guessing somewhere in your code you are accumulating a value instead of assigning it, so it is getting progressively smaller as you call the function on the same PieChart. Clearing out the pieChart should fix this problem.

only the first listpicker opens windows phone

I am using Windows Phone Toolkit - Nov 2011 (7.1 SDK) and I want to display multiple listpickers each
within a grid and each grid in a listbox. The problem is, only the first listpicker pops open and the rest don't. secondly how do I customize the full mode page of the listpicker to be displayed exclusively in Landscape orientation and set the page's "shell:SystemTray.IsVisible" to false.
Sorry I couldn't post a screenshot. error "Earn more than 10 reputation to post images".
Thanks
public MainPage()
{
InitializeComponent();
for (int g = 0; g < 10; g++)
{
// Define the margins template dor elements
Thickness elemThick = new Thickness();
// Create the Grid that will hold all the elements of a single entry
Grid entryGrd = new Grid();
entryGrd.VerticalAlignment = VerticalAlignment.Top;
entryGrd.HorizontalAlignment = HorizontalAlignment.Left;
elemThick.Left = 0;
elemThick.Bottom = 0;
elemThick.Right = 0;
elemThick.Top = 0;
entryGrd.Margin = elemThick;
entryGrd.Tag = lstbxPH.Items.Count;
// Setup the backgound value of the letBoder element
LinearGradientBrush elemLGB = new LinearGradientBrush();
elemLGB.EndPoint = new Point(0.5, 1);
elemLGB.StartPoint = new Point(0.5, 0);
GradientStop AquamarineGS = new GradientStop();
AquamarineGS.Color = Color.FromArgb(255, 127, 255, 212);
AquamarineGS.Offset = 0;
GradientStop greenLikeGS = new GradientStop();
greenLikeGS.Color = Color.FromArgb(255, 101, 250, 193);
greenLikeGS.Offset = 0.988;
elemLGB.GradientStops.Add(AquamarineGS);
elemLGB.GradientStops.Add(greenLikeGS);
// Draw the letter
for (int x = 0; x < 9; x++)
{
Border letBoder = new Border();
letBoder.Width = 53;
letBoder.Height = 51;
letBoder.VerticalAlignment = VerticalAlignment.Top;
letBoder.HorizontalAlignment = HorizontalAlignment.Left;
elemThick.Left = x * 60 + 71;
elemThick.Top = lstbxPH.Items.Count * 1 + 20;
letBoder.Margin = elemThick;
letBoder.Background = elemLGB;
// The Texblock
TextBlock let = new TextBlock();
let.VerticalAlignment = VerticalAlignment.Center;
let.HorizontalAlignment = HorizontalAlignment.Center;
let.FontSize = 25;
let.FontWeight = FontWeights.Bold;
let.Foreground = new SolidColorBrush(Color.FromArgb(200, 255, 255, 255));
let.Text = x.ToString();
let.Tag = x;
letBoder.Child = let;
entryGrd.Children.Add(letBoder);
}
// Draw the List picker element for the draw types
ListPicker DType = new ListPicker();
DType.Width = 48;
DType.VerticalAlignment = VerticalAlignment.Top;
DType.HorizontalAlignment = HorizontalAlignment.Left;
DType.Background = new SolidColorBrush(Color.FromArgb(200, 255, 255, 255));
DType.BorderBrush = new SolidColorBrush(Color.FromArgb(200, 255, 255, 255));
elemThick.Left = 17;
elemThick.Top = lstbxPH.Items.Count * 1 + 10;
DType.Margin = elemThick;
DType.FontSize = 18;
ListPickerItem item1 = new ListPickerItem() { Content = "A" };
ListPickerItem item2 = new ListPickerItem() { Content = "B" };
ListPickerItem item3 = new ListPickerItem() { Content = "C" };
DType.Items.Add(item1);
DType.Items.Add(item2);
DType.Items.Add(item3);
entryGrd.Children.Add(DType);
if (lstbxPH.Items.Count != 0)
{
// The delete button and related image
Button btnDel = new Button();
btnDel.Height = 65;
btnDel.Width = 60;
btnDel.Tag = lstbxPH.Items.Count;
btnDel.VerticalAlignment = VerticalAlignment.Top;
btnDel.HorizontalAlignment = HorizontalAlignment.Left;
btnDel.VerticalContentAlignment = VerticalAlignment.Top;
btnDel.HorizontalContentAlignment = HorizontalAlignment.Left;
elemThick.Left = 600;
elemThick.Top = lstbxPH.Items.Count + 13;
btnDel.Margin = elemThick;
elemThick.Left = 0;
elemThick.Bottom = 0;
elemThick.Right = 0;
elemThick.Top = 0;
btnDel.Name = "btnDel";
btnDel.Padding = elemThick;
btnDel.BorderBrush = new SolidColorBrush(Color.FromArgb(0, 0, 0, 0));
Image imgDel = new Image();
imgDel.Source = new BitmapImage(new Uri("/appbar.delete.rest.png", UriKind.RelativeOrAbsolute));
imgDel.VerticalAlignment = VerticalAlignment.Top;
imgDel.HorizontalAlignment = HorizontalAlignment.Left;
imgDel.Height = 35;
imgDel.Width = 30;
elemThick.Left = 0;
elemThick.Bottom = 0;
elemThick.Right = 0;
elemThick.Top = 0;
imgDel.Margin = elemThick;
imgDel.Stretch = Stretch.UniformToFill;
btnDel.Content = imgDel;
entryGrd.Children.Add(btnDel);
}
// Add the grid with to the list box
this.lstbxPH.Items.Add(entryGrd);
}
}
This issue was actually solved by the Windows Phone Toolkit - Nov 2011 (7.1 SDK).
Here is how I worked it out.
Download the November release of the toolkit http://silverlight.codeplex.com/releases/view/75888
if you had a previous release of the toolkit installed, please uninstall it and unreferenced the tool kit on you project
Install the November release of the toolkit
Reference the toolkit on your project, debug and boom! it woks.

Categories

Resources