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

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

Related

Add panels dynamically to form

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.

C# item Drag Fix

I move three controls using this code on MouseMove Event :
if (inHareket)
{
GelenResimcik.Left = e.X + GelenResimcik.Left -MouseDownLocation.X;
GelenResimcik.Top = e.Y + GelenResimcik.Top - MouseDownLocation.Y;
GelenResimcik.BackColor = Color.Transparent;
ResimHareket.Left = e.X + ResimHareket.Left - MouseDownLocation.X;
ResimHareket.Top = e.Y + ResimHareket.Top - MouseDownLocation.Y;
Yonlendir.Left = e.X + Yonlendir.Left - MouseDownLocation.X;
Yonlendir.Top = e.Y + Yonlendir.Top - MouseDownLocation.Y;
Aktar.Left = e.X + Aktar.Left - MouseDownLocation.X;
Aktar.Top = e.Y + Aktar.Top - MouseDownLocation.Y;
Vazgec.Left = e.X + Vazgec.Left - MouseDownLocation.X;
Vazgec.Top = e.Y + Vazgec.Top - MouseDownLocation.Y;
}
and if I try move them faster the result is :
Notice : When i stop moving, the glow is not visible, it is only moving faster.
What shall I do?
EDIT :
My Controls
this.Yonlendir = new System.Windows.Forms.PictureBox();
this.Vazgec = new System.Windows.Forms.PictureBox();
this.Aktar = new System.Windows.Forms.PictureBox();
this.ResimHareket = new System.Windows.Forms.PictureBox();
this.GelenResimcik = new System.Windows.Forms.PictureBox();
My Init :
((System.ComponentModel.ISupportInitialize)(this.Yonlendir)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.Vazgec)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.Aktar)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.ResimHareket)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.GelenResimcik)).BeginInit();
this.SuspendLayout();
//
// Yonlendir
//
this.Yonlendir.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
this.Yonlendir.Cursor = System.Windows.Forms.Cursors.Arrow;
this.Yonlendir.Image = global::Surface.Properties.Resources.rotate;
this.Yonlendir.Location = new System.Drawing.Point(26, 74);
this.Yonlendir.Name = "Yonlendir";
this.Yonlendir.Padding = new System.Windows.Forms.Padding(5);
this.Yonlendir.Size = new System.Drawing.Size(30, 30);
this.Yonlendir.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.Yonlendir.TabIndex = 14;
this.Yonlendir.TabStop = false;
this.Yonlendir.Click += new System.EventHandler(this.Yonlendir_Click);
//
// Vazgec
//
this.Vazgec.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
this.Vazgec.Cursor = System.Windows.Forms.Cursors.Arrow;
this.Vazgec.Image = global::Surface.Properties.Resources.iptal;
this.Vazgec.Location = new System.Drawing.Point(93, 8);
this.Vazgec.Name = "Vazgec";
this.Vazgec.Padding = new System.Windows.Forms.Padding(5);
this.Vazgec.Size = new System.Drawing.Size(30, 30);
this.Vazgec.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.Vazgec.TabIndex = 13;
this.Vazgec.TabStop = false;
this.Vazgec.Click += new System.EventHandler(this.buton1_Click);
//
// Aktar
//
this.Aktar.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
this.Aktar.Cursor = System.Windows.Forms.Cursors.Arrow;
this.Aktar.Image = global::Surface.Properties.Resources.kaydet;
this.Aktar.Location = new System.Drawing.Point(57, 8);
this.Aktar.Name = "Aktar";
this.Aktar.Padding = new System.Windows.Forms.Padding(5);
this.Aktar.Size = new System.Drawing.Size(30, 30);
this.Aktar.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.Aktar.TabIndex = 12;
this.Aktar.TabStop = false;
this.Aktar.Click += new System.EventHandler(this.Onayla_Click);
//
// ResimHareket
//
this.ResimHareket.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(40)))), ((int)(((byte)(40)))), ((int)(((byte)(40)))));
this.ResimHareket.Cursor = System.Windows.Forms.Cursors.Arrow;
this.ResimHareket.Image = global::Surface.Properties.Resources.hareket;
this.ResimHareket.Location = new System.Drawing.Point(26, 38);
this.ResimHareket.Name = "ResimHareket";
this.ResimHareket.Padding = new System.Windows.Forms.Padding(5);
this.ResimHareket.Size = new System.Drawing.Size(30, 30);
this.ResimHareket.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
this.ResimHareket.TabIndex = 11;
this.ResimHareket.TabStop = false;
this.ResimHareket.Click += new System.EventHandler(this.ResimHareket_Click);
this.ResimHareket.MouseDown += new System.Windows.Forms.MouseEventHandler(this.ResmiHareket_MouseDown);
this.ResimHareket.MouseMove += new System.Windows.Forms.MouseEventHandler(this.ResmiHareket_MouseMove);
this.ResimHareket.MouseUp += new System.Windows.Forms.MouseEventHandler(this.ResimHareket_MouseUp);
//
// GelenResimcik
//
this.GelenResimcik.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))), ((int)(((byte)(0)))));
this.GelenResimcik.BackgroundImageLayout = System.Windows.Forms.ImageLayout.None;
this.GelenResimcik.Location = new System.Drawing.Point(57, 39);
this.GelenResimcik.Name = "GelenResimcik";
this.GelenResimcik.Size = new System.Drawing.Size(438, 452);
this.GelenResimcik.SizeMode = System.Windows.Forms.PictureBoxSizeMode.AutoSize;
this.GelenResimcik.TabIndex = 2;
this.GelenResimcik.TabStop = false;
My Timer (Interval 100):
private void ResimKontrol_Tick(object sender, EventArgs e)
{
try
{
if (ontanimli.Height != 0)
{
GelenResimcik.Image = ontanimliGelen;
GelenResimcik.Parent = cizim;
entegreGoster();
}
}
catch
{
}
}
entegreGoster and entegreGizle
public void entegreGoster() { GelenResimcik.BringToFront();ResimHareket.BringToFront();Aktar.BringToFront();Vazgec.BringToFront(); Yonlendir.BringToFront(); }
public void entegreGizle() { GelenResimcik.SendToBack(); ResimHareket.SendToBack(); Aktar.SendToBack(); Vazgec.SendToBack(); Yonlendir.SendToBack(); }
My MouseDown event :
private void ResmiHareket_MouseDown(object sender, MouseEventArgs e)
{
MouseDownLocation = e.Location;
inHareket = true;
}
I start timer when Open new form
Ontanimlilar ontanimli = new Ontanimlilar();
ontanimli.Show();
ResimKontrol.Start();
ontanimliGelen (public static pictureBox)
ontanimli Class :
Ciz.ontanimliGelen = seciliRes.Image;
this.Close();

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.

Categories

Resources