I have a loop that iterates through each date between two selected dates.
It creates a panel for each product on that date and then moves on.
That works fine, but if I run the loop again, it produces a blank space between the new panels and the old panels. Here is a example:
(small cause of my small screen)
The black lines indicate what I'm speaking about. The short one is the normal spacing between each panel and the long one indicates when I run the loop again.
The reason I'm worried is because if you want to add products after your initial add, it will be the second or third or fourth time the loop runs and leave that gap which just creates an unpleasant view.
Can someone please tell me what is causing this gap, here is the code for this section(i know about the SQL injections, i do send parameters to a DAL but for the sake of this question I'm using a SQL query):
for (DateTime date = dtpSDate.Value; date.Date <= dtpCDate.Value; date = date.AddDays(1))
{
pnlReport.Controls.Add(new Label { Text = date.ToString(), Height = 20, Width = 150, Name = date.ToString(), Location = new Point(20, globalY), Font = new Font("Arial", 10, FontStyle.Bold) });
globalY += 30;
foreach (DataRowView lstvItem in chkListProducts.CheckedItems)
{
DataTable ProdTmp2 = new DataTable();
string sqlP2 = "SELECT * FROM Product WHERE ProdID = '" + lstvItem[this.chkListProducts.ValueMember] + "'";
ProdTmp2 = db.GetDataTable(sqlP2);
if (Side == "Left")
{
Panel pnltmp = new Panel();
pnltmp.Size = new Size(472, 120);
pnltmp.Location = new Point(5, globalY);
pnltmp.BackColor = Color.Green;
pnltmp.Name = ProdTmp2.Rows[0][1].ToString();
Label l = new Label();
l.Text = ProdTmp2.Rows[0][1].ToString();
l.Location = new Point(5, 5);
pnltmp.Controls.Add(l);
pnlReport.Controls.Add(pnltmp);
Side = "Right";
last = ProdTmp2.Rows[0][1].ToString();
}
else
{
Panel pnltmp = new Panel();
pnltmp.Size = new Size(472, 120);
pnltmp.Location = new Point(487, globalY);
pnltmp.BackColor = Color.Red;
pnltmp.Name = ProdTmp2.Rows[0][1].ToString();
Label l = new Label();
l.Text = ProdTmp2.Rows[0][1].ToString();
l.Location = new Point(5, 5);
pnltmp.Controls.Add(l);
pnlReport.Controls.Add(pnltmp);
Side = "Left";
globalY += 140;
last = ProdTmp2.Rows[0][1].ToString();
}
}
if (Side == "Right")
{
Side = "Left";
globalY += 170;
}
}
Extra Info: The bigger the date gap. The larger the blank space gap.
So the end Code looks like this and the space is now non existent.
Thanx to all.
private void AddProducts()
{
foreach (Panel p in pnlReport.Controls.OfType<Panel>())
{
if (p.Name == last)
{
globalY = p.Location.Y + 140;
}
}
for (DateTime date = dtpSDate.Value; date.Date <= dtpCDate.Value; date = date.AddDays(1))
{
pnlReport.Controls.Add(new Label { Text = date.ToString(), Height = 20, Width = 150, Name = date.ToString(), Location = new Point(20, globalY), Font = new Font("Arial", 10, FontStyle.Bold) });
globalY += 30;
foreach (DataRowView lstvItem in chkListProducts.CheckedItems)
{
string table = "";
string select = "";
if (Regex.IsMatch(lstvItem[this.chkListProducts.DisplayMember].ToString(), #"^[a-hA-H]"))
{
table = "ProductHistoryAH";
}
else if (Regex.IsMatch(lstvItem[this.chkListProducts.DisplayMember].ToString(), #"^[i-qI-Q]"))
{
table = "ProductHistoryIQ";
}
else if (Regex.IsMatch(lstvItem[this.chkListProducts.DisplayMember].ToString(), #"^[r-zR-Z]"))
{
table = "ProductHistoryRZ";
}
DataTable ProdTmp1 = new DataTable();
string sqlP1 = "SELECT * FROM " + table + " WHERE ProdID = '" + lstvItem[this.chkListProducts.ValueMember] + "' AND ProdHDate = '" + date.ToString(#"yyyy\/MM\/dd") + "'";
ProdTmp1 = db.GetDataTable(sqlP1);
DataTable ProdTmp2 = new DataTable();
string sqlP2 = "SELECT * FROM Product WHERE ProdID = '" + lstvItem[this.chkListProducts.ValueMember] + "'";
ProdTmp2 = db.GetDataTable(sqlP2);
if (Side == "Left")
{
Panel pnltmp = new Panel();
pnltmp.Size = new Size(472, 120);
pnltmp.Location = new Point(5, globalY);
pnltmp.BackColor = Color.Green;
pnltmp.Name = ProdTmp2.Rows[0][1].ToString() + count.ToString();
//Name
Label lblName = new Label();
lblName.Text = lstvItem[this.chkListProducts.DisplayMember].ToString();
lblName.Location = new Point(5, 5);
pnltmp.Controls.Add(lblName);
int Location = 0;
//Count
if (chkPCount.Checked)
{
Label lblCount = new Label();
if(ProdTmp1.Rows.Count>0)
{
lblCount.Text = "Count: " + ProdTmp1.Rows[0][4].ToString();
}
else
{
lblCount.Text = "Count: 0";
}
lblCount.Location = new Point(17, 32);
pnltmp.Controls.Add(lblCount);
}
pnlReport.Controls.Add(pnltmp);
Side = "Right";
}
else if(Side == "Right")
{
Panel pnltmp = new Panel();
pnltmp.Size = new Size(472, 120);
pnltmp.Location = new Point(487, globalY);
pnltmp.BackColor = Color.Red;
pnltmp.Name = ProdTmp2.Rows[0][1].ToString() + count.ToString();
//Name
Label lblName = new Label();
lblName.Text = lstvItem[this.chkListProducts.DisplayMember].ToString();
lblName.Location = new Point(5, 5);
pnltmp.Controls.Add(lblName);
//Count
if (chkPCount.Checked)
{
Label lblCount = new Label();
if (ProdTmp1.Rows.Count > 0)
{
lblCount.Text = "Count: " + ProdTmp1.Rows[0][4].ToString();
}
else
{
lblCount.Text = "Count: 0";
}
lblCount.Location = new Point(17, 32);
pnltmp.Controls.Add(lblCount);
}
pnlReport.Controls.Add(pnltmp);
Side = "Left";
globalY += 140;
}
last = ProdTmp2.Rows[0][1].ToString() + count.ToString();
}
if (Side == "Right")
{
Side = "Left";
globalY += 140;
}
}
count++;
}
and the output:
Related
My goal is to create a chart that will sit inside of a panel restricting it's size.
I managed to achieve this some time ago but today I noticed that the chart was growing inside of the panel, not allowing the data to be seen.
I have attached a picture bellow which should help understand the issue.
UPDATE
I noticed that if I rmeove 'bottom' from the Anchor property of the panel the chart does not exceed the parent panel but it does not increase with the change of the form which is what I'm looking for.
I also noticed that there was also another chart on the form that was exceeding the parent form, this time the chart would extend to the right not allowing to see the data.
This is the code that generates this second chart and places is inside of the parent panel.
panel_chart.Controls.Clear();
chart1 = new Chart();
chart1.MouseMove += chart1_MouseMove;
chart1.ChartAreas.Add(new ChartArea("chartArea1"));
chart1.Series.Clear();
chart1.Titles.Clear();
var serieOEE = new Series("OEE");
serieOEE.ChartType = SeriesChartType.Line;
serieOEE.XValueType = ChartValueType.String;
var serieProd = new Series("Prod");
serieProd.ChartType = SeriesChartType.Column;
serieProd.XValueType = ChartValueType.String;
var serieDisp = new Series("Disp");
serieDisp.ChartType = SeriesChartType.Column;
serieDisp.XValueType = ChartValueType.String;
var serieQual = new Series("Qual");
serieQual.ChartType = SeriesChartType.Column;
serieQual.XValueType = ChartValueType.String;
DateTime DataReg = DateTime.MinValue;
List<AreaOEE> listaChart = new List<AreaOEE>();
foreach (var item in ListaGrafico) //listaOEE
{
if (item.Designacao == DesignacaoLista)
{
listaChart.Add(item);
}
}
listaChart = listaChart.OrderBy(a => a.IDReg).ToList();
DateTime DataUltimoReg = DateTime.MinValue;
int j = 0;
foreach (var item in listaChart)
{
string HoraGraf = Convert.ToDateTime(item.Hora).ToString("HH:mm");
if (j == 0 || j == listaChart.Count - 1 ||
Math.Abs(Convert.ToDateTime(item.Hora).Subtract(DataUltimoReg).TotalMinutes) >= 30)
{
serieOEE.Points.AddXY(HoraGraf, item.OEE);
serieProd.Points.AddXY(HoraGraf, item.Produtividade);
serieQual.Points.AddXY(HoraGraf, item.Qualidade);
serieDisp.Points.AddXY(HoraGraf, item.Disponibilidade);
DataUltimoReg = Convert.ToDateTime(item.Hora);
if (j == listaChart.Count - 2)
{
break;
}
}
j++;
}
//Adicionar o ultimo
foreach (var item in listaOEE)
{
if (item.Designacao == DesignacaoLista)
{
string sHora = "";
try
{
sHora = item.Hora.Substring(1, 5);
}
catch (Exception ex)
{
string sEx = ex.Message;
}
foreach (var itemOee in serieOEE.Points)
{
if (itemOee.AxisLabel == sHora)
{
itemOee.YValues[0] = item.OEE;
}
}
foreach (var itemP in serieProd.Points)
{
if (itemP.AxisLabel == sHora)
itemP.YValues[0] = item.Produtividade;
}
foreach (var itemD in serieDisp.Points)
{
if (itemD.AxisLabel == sHora)
itemD.YValues[0] = item.Disponibilidade;
}
foreach (var itemQ in serieQual.Points)
{
if (itemQ.AxisLabel == sHora)
itemQ.YValues[0] = item.Qualidade;
}
}
}
chart1.Series.Add(serieProd);
chart1.Series.Add(serieQual);
chart1.Series.Add(serieDisp);
chart1.Series.Add(serieOEE);
serieOEE.BorderWidth = 4;
chart1.ChartAreas[0].AxisX.LabelStyle.Angle = 90;
chart1.ChartAreas[0].AxisX.Interval = 1;
chart1.ChartAreas[0].AxisY.Minimum = 0;
chart1.ChartAreas[0].AxisY.Maximum = 140;
chart1.Legends.Clear();
chart1.Legends.Add(serieOEE.Legend);
chart1.Titles.Add(DesignacaoLista + " " + DataTitulo.ToString("dd-MM HH:mm"));
chart1.Titles[0].Font = new Font("Arial", 13, FontStyle.Bold);
chart1.Visible = true;
chart1.Dock = DockStyle.Fill;
panel_chart.Controls.Add(chart1);
you can change the size of Chart with Chart.Size:
Chart1.Size = new Size(1000, 200); //1000px * 200px
I have problem displaying the columns correctly,
some columns have some gap in between
I wanted to attach a picture but I need 10 reputation points to post image
See picture link below!!
Here is my code:
private void UpdateProducts()
{
MyPharmacyDataContext db = new MyPharmacyDataContext();
int tabid = Convert.ToInt32(radRibbonBar1.SelectedCommandTab.Name);
var list = from g in db.OutOfStocks
where g.ListID == tabid
select new { g.ID, g.Product.CName, g.Required, g.Product.Price, g.Product.Disscount, g.Product.BuyPrice, g.TotalBuy, Stock = g.Product.Available + "," + g.Product.AvailableMid + "," + g.Product.AvailableSmall,g.Product.Supplier.Title };
if (list.Count() > 0)
{
radGridView1.SummaryRowsBottom.Clear();
radGridView1.Columns.Clear();
radGridView1.Visible = true;
radGridView1.TableElement.TableHeaderHeight = 60;
GridViewTextBoxColumn num = new GridViewTextBoxColumn();
num.MaxWidth = 50;
num.Name = "num";
num.HeaderText = "م";
num.DataType = typeof(int);
radGridView1.Columns.Add(num);
radGridView1.DataSource = list;
radGridView1.Columns[1].IsVisible = false;
radGridView1.Columns[2].Width = 350;
radGridView1.Columns[2].HeaderText = "اسم المنتج";
radGridView1.Columns[2].Name = "Name";
radGridView1.Columns[2].TextAlignment = ContentAlignment.MiddleCenter;
radGridView1.Columns[3].MaxWidth = 60;
radGridView1.Columns[3].HeaderText = "عدد";
radGridView1.Columns[3].TextAlignment = ContentAlignment.MiddleCenter;
radGridView1.Columns[3].Name = "Q";
radGridView1.Columns[4].MaxWidth = 90;
radGridView1.Columns[4].HeaderText = "السعر";
radGridView1.Columns[4].Name = "Price";
radGridView1.Columns[4].TextAlignment = ContentAlignment.MiddleCenter;
radGridView1.Columns[4].FormatString = "{0:G29}";
radGridView1.Columns[5].MaxWidth = 80;
radGridView1.Columns[5].HeaderText = "الخصم";
radGridView1.Columns[5].TextAlignment = ContentAlignment.MiddleCenter;
radGridView1.Columns[5].FormatString = "{0:G29}";
radGridView1.Columns[6].MaxWidth = 100;
radGridView1.Columns[6].HeaderText = "سعر\nالشراء";
radGridView1.Columns[6].Name = "BuyPice";
radGridView1.Columns[6].TextAlignment = ContentAlignment.MiddleCenter;
radGridView1.Columns[6].FormatString = "{0:G29}";
radGridView1.Columns[7].MaxWidth = 100;
radGridView1.Columns[7].HeaderText = "اجمالي\nالشراء";
radGridView1.Columns[7].Name = "TotalBuyPice";
radGridView1.Columns[7].TextAlignment = ContentAlignment.MiddleCenter;
radGridView1.Columns[7].FormatString = "{0:G29}";
radGridView1.Columns[8].MaxWidth = 80;
radGridView1.Columns[8].MinWidth = 80;
radGridView1.Columns[8].Name = "Stock";
radGridView1.Columns[8].HeaderText = "الرصيد";
radGridView1.Columns[8].TextAlignment = ContentAlignment.MiddleCenter;
radGridView1.Columns[9].MaxWidth = 100;
radGridView1.Columns[9].MinWidth = 100;
radGridView1.Columns[9].Name = "Supplier";
radGridView1.Columns[9].HeaderText = "افضل\nمورد";
radGridView1.Columns[9].TextAlignment = ContentAlignment.MiddleCenter;
GridViewCheckBoxColumn checkbox = new GridViewCheckBoxColumn();
checkbox.MaxWidth = 30;
checkbox.HeaderText = "";
checkbox.DataType = typeof(bool);
checkbox.Name = "Ordered";
radGridView1.Columns.Add(checkbox);
var allpro = from g in db.OutOfStocks
where g.ListID == tabid
select g;
foreach (OutOfStock p in allpro)
{
foreach (GridViewRowInfo row in radGridView1.Rows)
{
if ((int)row.Cells[1].Value == p.ID)
{
if (p.Ordered == 'y')
row.Cells["Ordered"].Value = true;
else
row.Cells["Ordered"].Value = false;
}
}
}
GridViewSummaryItem total = new GridViewSummaryItem("Name", "الاجمالي", GridAggregateFunction.Avg);
GridViewSummaryItem price = new GridViewSummaryItem();
price.Name = "Price";
price.AggregateExpression = "(Sum(Q*Price))";
price.FormatString = "{0:G29}";
// GridViewSummaryItem price = new GridViewSummaryItem("Price", "{0}", GridAggregateFunction.Sum);
GridViewSummaryItem buypice = new GridViewSummaryItem("TotalBuyPice", "{0:G29}", GridAggregateFunction.Sum);
GridViewSummaryRowItem summaryRowItem = new GridViewSummaryRowItem(new GridViewSummaryItem[] { total, price, buypice });
radGridView1.SummaryRowsBottom.Add(summaryRowItem);
radGridView1.Focus();
}
else
{
radGridView1.Visible = false;
}
if (radGridView1.Rows.Count() > 0)
radGridView1.ChildRows.Last().IsCurrent = true;
}
Here is the pic
https://fbcdn-sphotos-h-a.akamaihd.net/hphotos-ak-xpa1/v/t34.0-12/10893575_742987025776952_423788280_n.jpg?efg=eyJpIjoidCJ9&oh=c69f42a8f47d6529f2b9d89ab2782711&oe=54A71C8A&gda=1420210517_66e1b08ccf9c00fd3dcff31e1854b3cc
for (int i = 0; i < 5; i += 1)
{
ShowReports(0);
}
private void ShowReports(int ComboID)
{
Graph.Series["Series1"].ChartType = SeriesChartType.Line;
Graph.Series["Series1"].BorderWidth = 2;
Graph.Series["Series1"].MarkerStyle = MarkerStyle.Circle;
Graph.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "dd-MMM-yyy";
Graph.ChartAreas["ChartArea1"].AxisX.Title = "Date";
Graph.ChartAreas["ChartArea1"].AxisY.Title = "Average Score (%) ";
Graph.ChartAreas["ChartArea1"].AxisY.Minimum = 0;
Graph.ChartAreas["ChartArea1"].AxisY.Maximum = 100;
Graph.ChartAreas["ChartArea1"].AxisY.Interval = 10;
Graph.Series["Series1"].ToolTip = "Date :#VALX Avg Score(%) :#VALY";
Graph.Titles.Add(dtReportDetails.Rows[0].ItemArray[1].ToString());
Graph.Titles.Add(SetGraphTitile());
Graph.Titles[0].Font = new System.Drawing.Font("Arial", 20);
Graph.Titles[0].ForeColor = System.Drawing.Color.Black;
Graph.Titles[1].Font = new System.Drawing.Font("Arial", 13);
Graph.Titles[1].ForeColor = System.Drawing.Color.Black;
Graph.Titles[1].Visible = false;
// Graph.Series[0].Points.AddXY(DateTime.Parse(dtReportDetails.Rows[0].ItemArray[4].ToString()), dtReportDetails.Rows[0].ItemArray[5].ToString());
Graph.Series[0].XValueMember = dtReportDetails.Rows[0].ItemArray[4].ToString();
Graph.Series[0].YValueMembers = dtReportDetails.Rows[0].ItemArray[5].ToString();
Graph.Series[0].MarkerStyle = MarkerStyle.Circle;
Graph.Legends.Add("Legend1");
Graph.Legends[0].Enabled = false;
Graph.Legends[0].Docking = Docking.Bottom;
Graph.Legends[0].Alignment = System.Drawing.StringAlignment.Center;
Graph.DataSource = dv;
Graph.DataBind();
}
else if (dtReportDetails.Rows[0].ItemArray[7].ToString() == "Bar")
{
Graph.Series["Series1"].ChartType = SeriesChartType.Column;
Graph.Series["Series1"].BorderWidth = 2;
//Graph.Series["Series1"].MarkerStyle = MarkerStyle.Circle;
//Graph.ChartAreas["ChartArea1"].AxisX.LabelStyle.Format = "dd-MMM-yyy";
//Graph.ChartAreas["ChartArea1"].AxisX.Title = "Date";
//Graph.ChartAreas["ChartArea1"].AxisY.Title = "Average Score (%) ";
Graph.Series["Series1"].ToolTip = "(#VALX,#VALY)";
Graph.Titles.Add(dtReportDetails.Rows[0].ItemArray[1].ToString());
Graph.Titles.Add(SetGraphTitile());
Graph.Titles[0].Font = new System.Drawing.Font("Arial", 20);
Graph.Titles[0].ForeColor = System.Drawing.Color.Black;
Graph.Titles[1].Font = new System.Drawing.Font("Arial", 13);
Graph.Titles[1].ForeColor = System.Drawing.Color.Black;
Graph.Titles[1].Visible = false;
Graph.ChartAreas["ChartArea1"].AxisX.Title = "Learning Domains";
Graph.ChartAreas["ChartArea1"].AxisY.Title = "Covered";
// Graph.Series[0].Points.AddXY(DateTime.Parse(dtReportDetails.Rows[0].ItemArray[4].ToString()), dtReportDetails.Rows[0].ItemArray[5].ToString());
Graph.Series[0].XValueMember = dtReportDetails.Rows[0].ItemArray[4].ToString();
Graph.Series[0].YValueMembers = dtReportDetails.Rows[0].ItemArray[5].ToString();
// Graph.Series[0].MarkerStyle = MarkerStyle.Circle;
Graph.Legends.Add("Legend1");
Graph.Legends[0].Enabled = false;
Graph.Legends[0].Docking = Docking.Bottom;
Graph.Legends[0].Alignment = System.Drawing.StringAlignment.Center;
Graph.DataSource = dv;
Graph.DataBind();
Random random = new Random();
foreach (var item in Graph.Series[0].Points)
{
System.Drawing.Color c = System.Drawing.Color.FromArgb(random.Next(0, 255), random.Next(0, 255), random.Next(0, 255));
item.Color = c;
}
Graph.Series[0]["PointWidth"] = "0.2";
Graph.Series[0]["BarLabelStyle"] = "Center";
Graph.Series[0]["PixelPointDepth"] = "99";
Graph.Series[0]["DrawingStyle"] = "Cylinder";
}
This is my code for creating graph,for creating 1 graph i have no problem ,but creating more than one i will get error:A chart element with the name 'Legend1' already exists in the 'LegendCollection'.because i am creating same legends each time.So can you help to get rid of this problem.
Getting around the issue of the legend object name should be as simple as setting it via a string that takes your ComboID parameter into account.
string myLegendTitle = "Legend" + ComboID;
Graph.Legends.Add(myLegendTitle);
In order to create multiple charts - on the assumption you're using Microsoft Chart controls would look something like
for (int i = 0; i < 5; i += 1)
{
ShowReports(0);
}
private void ShowReports(int ComboID)
{
var myNewGraph = new System.Windows.Forms.DataVisualization.Charting.Chart();
myNewGraph .Series["Series1"].ChartType = SeriesChartType.Line;
myNewGraph .Series["Series1"].BorderWidth = 2;
myParentControl.Controls.Add(myNewGraph);
}
The key points being to instantiate a new Chart in your ShowReports method, then set all the properties specifically as before BUT on the new graph / chart object. Then ensure you add the control to the appropriate container if it's not already there.
If you know you will have, for example, 5 charts, then another approach is to build them at design time instead, and use a switch approach to set your variable, eg.
private void ShowReports(int ComboID)
{
Chart myNewGraph;
switch (ComboID)
{
case 1:
myNewGraph = Graph1;
break;
}
myNewGraph .Series["Series1"].ChartType = SeriesChartType.Line;
myNewGraph .Series["Series1"].BorderWidth = 2;
}
Is the code for create 5 graphs..
for(int i=0;i< 6;i++)
{
Chart1.Series.Add("Series1" + t.ToString());
Chart1.ChartAreas.Add("ChartArea1" + t.ToString());
Chart1.Legends.Add("Legend1" + t.ToString());
Chart1.Series[t].ChartArea = "ChartArea1" + t.ToString();
Chart1.Series[t].ChartType = SeriesChartType.Column;
Chart1.Series[t].BorderWidth = 2;
Chart1.Series[t].ToolTip = "(#VALX,#VALY)";
Chart1.ChartAreas["ChartArea1" + t.ToString()].AxisX.Title = "Learning Domains";
Chart1.ChartAreas["ChartArea1" + t.ToString()].AxisY.Title = "Covered";
}
I have the below code
public void panel_item_collections_Click(object sender, EventArgs e)
{
TextBox[] textbox_item_array = new TextBox[5];
item_textbox.Add(textbox_item_array);
textbox_item_array[0] = new TextBox();
textbox_item_array[0].Width = label_item_code.Width;
textbox_item_array[0].Height = 26;
textbox_item_array[0].Font = print_font_default;
textbox_item_array[0].Location = new Point(label_item_code.Location.X, 45 + (20 * row_count));
textbox_item_array[0].Name = string.Concat("item_code", row_count.ToString());
panel_item_collections.Controls.Add(textbox_item_array[0]);
textbox_item_array[0].Leave += new EventHandler(dynamic_text_item_code_Leave);
textbox_item_array[1] = new TextBox();
textbox_item_array[1].Width = label_item_descrition.Width;
textbox_item_array[1].Font = textbox_item_array[0].Font;
textbox_item_array[1].Location = new Point(label_item_descrition.Location.X, textbox_item_array[0].Location.Y);
textbox_item_array[1].Name = string.Concat("item_description", row_count.ToString());
panel_item_collections.Controls.Add(textbox_item_array[1]);
textbox_item_array[2] = new TextBox();
textbox_item_array[2].Width = label_item_price.Width;
textbox_item_array[2].Font = textbox_item_array[0].Font;
textbox_item_array[2].Location = new Point(label_item_price.Location.X, textbox_item_array[0].Location.Y);
textbox_item_array[2].Name = string.Concat("item_price", row_count.ToString());
panel_item_collections.Controls.Add(textbox_item_array[2]);
textbox_item_array[3] = new TextBox();
textbox_item_array[3].Width = label_item_quantity.Width;
textbox_item_array[3].Font = textbox_item_array[0].Font;
textbox_item_array[3].Location = new Point(label_item_quantity.Location.X, textbox_item_array[0].Location.Y);
textbox_item_array[3].Name = string.Concat("item_quantity", row_count.ToString());
panel_item_collections.Controls.Add(textbox_item_array[3]);
textbox_item_array[4] = new TextBox();
textbox_item_array[4].Width = label_item_total.Width;
textbox_item_array[4].Font = textbox_item_array[0].Font;
textbox_item_array[4].Location = new Point(label_item_total.Location.X, textbox_item_array[0].Location.Y);
textbox_item_array[4].Name = string.Concat("item_total", row_count.ToString());
panel_item_collections.Controls.Add(textbox_item_array[4]);
row_count++;
}
Now, here is the leave event handler:
void dynamic_text_item_code_Leave(object sender, EventArgs e)
{
//MessageBox.Show(((Control)sender).Name.Substring(((Control)sender).Name.Length - 1, 1));
int i;
string name_textbox = ((Control)sender).Name;
i = System.Convert.ToInt32(name_textbox.Substring(name_textbox.Length - 1, 1));
//MessageBox.Show(i.ToString());
//i--;
TextBox[] textbox_item_array = new TextBox[5];
textbox_item_array = (TextBox[])(item_textbox[i]);
double item_total;
Item item = new Item();
if (long.TryParse(textbox_item_array[0].Text, out item.item_code) == true)
{
if (item.get_item() == 0)
{
textbox_item_array[1].Text = item.item_details;
textbox_item_array[2].Text = item.sell_price.ToString();
textbox_item_array[3].Text = "1";
item_total = System.Convert.ToInt32(textbox_item_array[3].Text) * item.sell_price;
textbox_item_array[4].Text = item_total.ToString();
}
}
else
{
//TextBox[] textbox_item_array = new TextBox[5];
textbox_item_array = (TextBox[])(item_textbox[item_textbox.Count - 1]);
panel_item_collections.Controls.Remove(textbox_item_array[0]);
panel_item_collections.Controls.Remove(textbox_item_array[1]);
panel_item_collections.Controls.Remove(textbox_item_array[2]);
panel_item_collections.Controls.Remove(textbox_item_array[3]);
panel_item_collections.Controls.Remove(textbox_item_array[4]);
item_textbox.RemoveAt((item_textbox.Count - 1));
row_count--;
}
}
Now, the problem is like this:
If the user leave the textbox blank, the row will be deleted. The strange problem is:
If press tab, it will execute the leave event handler twice. It means it will try to delete the same textbox twice and this will create problem. Can any one help me how to avoid this double calling?
Thanks
I want to add more: here is exactly what is happening:
Now, I will give exactly how it is executed:
void dynamic_text_item_code_Leave(object sender, EventArgs e)
{
//MessageBox.Show(((Control)sender).Name.Substring(((Control)sender).Name.Length - 1, 1));
int i;
string name_textbox = ((Control)sender).Name;
i = System.Convert.ToInt32(name_textbox.Substring(name_textbox.Length - 1, 1));
//MessageBox.Show(i.ToString());
//i--;
TextBox[] textbox_item_array = new TextBox[5];
textbox_item_array = (TextBox[])(item_textbox[i]);
double item_total;
Item item = new Item();
if (long.TryParse(textbox_item_array[0].Text, out item.item_code) == true)
{
if (item.get_item() == 0)
{
textbox_item_array[1].Text = item.item_details;
textbox_item_array[2].Text = item.sell_price.ToString();
textbox_item_array[3].Text = "1";
item_total = System.Convert.ToInt32(textbox_item_array[3].Text) * item.sell_price;
textbox_item_array[4].Text = item_total.ToString();
}
}
else
{
//TextBox[] textbox_item_array = new TextBox[5];
textbox_item_array = (TextBox[])(item_textbox[item_textbox.Count - 1]);
panel_item_collections.Controls.Remove(textbox_item_array[0]);
After that, it calls the same function
void dynamic_text_item_code_Leave(object sender, EventArgs e)
{
//MessageBox.Show(((Control)sender).Name.Substring(((Control)sender).Name.Length - 1, 1));
int i;
string name_textbox = ((Control)sender).Name;
i = System.Convert.ToInt32(name_textbox.Substring(name_textbox.Length - 1, 1));
//MessageBox.Show(i.ToString());
//i--;
TextBox[] textbox_item_array = new TextBox[5];
textbox_item_array = (TextBox[])(item_textbox[i]);
double item_total;
Item item = new Item();
if (long.TryParse(textbox_item_array[0].Text, out item.item_code) == true)
{
if (item.get_item() == 0)
{
textbox_item_array[1].Text = item.item_details;
textbox_item_array[2].Text = item.sell_price.ToString();
textbox_item_array[3].Text = "1";
item_total = System.Convert.ToInt32(textbox_item_array[3].Text) * item.sell_price;
textbox_item_array[4].Text = item_total.ToString();
}
}
else
{
//TextBox[] textbox_item_array = new TextBox[5];
textbox_item_array = (TextBox[])(item_textbox[item_textbox.Count - 1]);
panel_item_collections.Controls.Remove(textbox_item_array[0]);
panel_item_collections.Controls.Remove(textbox_item_array[1]);
panel_item_collections.Controls.Remove(textbox_item_array[2]);
panel_item_collections.Controls.Remove(textbox_item_array[3]);
panel_item_collections.Controls.Remove(textbox_item_array[4]);
item_textbox.RemoveAt((item_textbox.Count - 1));
row_count--;
}
}
Then it continues here
panel_item_collections.Controls.Remove(textbox_item_array[1]);
panel_item_collections.Controls.Remove(textbox_item_array[2]);
panel_item_collections.Controls.Remove(textbox_item_array[3]);
panel_item_collections.Controls.Remove(textbox_item_array[4]);
item_textbox.RemoveAt((item_textbox.Count - 1));
row_count--;
}
}
So, why it executes once it get to the remove
Are you sure, you don't use this line twice in another part of code?
textbox_item_array[0].Leave += new EventHandler(dynamic_text_item_code_Leave);
If not I suppose you hooks dynamic_text_item_code_Leave to one another control.
EDIT
You can also to protect by adding this code:
if (textbox_item_array[0].Leave != null)
textbox_item_array[0].Leave += new EventHandler(dynamic_text_item_code_Leave);
I am working on creating a couple of charts and I cannot figure out why there is so much empty space on the left and right side of the chart. I have a Winforms Chart, ChartArea, and Series and there is always a good inch to the left and right side of the chart that seems like wasted space. What setting do I need to change to reduce the size of this empty space? I have included a screenshot of the chart with empty space with this post as well as the code that I use to create it. Thanks in advance.
if (listBoxCharts.SelectedItems.Count == 1)
{
switch (listBoxCharts.SelectedItem.ToString().Trim())
{
case "Incomplete Work Orders By Status":
ChartArea chartArea = new ChartArea();
Series series = new Series();
Title title = new Title();
chartArea.Area3DStyle.Enable3D = true;
chartArea.Area3DStyle.LightStyle = LightStyle.Realistic;
chartArea.Area3DStyle.Rotation = 10;
chartArea.Area3DStyle.WallWidth = 3;
chartArea.BorderColor = Color.Transparent;
chartArea.Name = "IncompleteWorkOrdersByStatus_ChartArea";
// Fix this hack
ChartContainer.ChartAreas.Clear();
this.ChartContainer.ChartAreas.Add(chartArea);
this.ChartContainer.Dock = DockStyle.Fill;
this.ChartContainer.Location = new Point(2, 21);
this.ChartContainer.Name = "Charts";
this.ChartContainer.PaletteCustomColors = new Color[] { Color.LemonChiffon };
series.ChartArea = "IncompleteWorkOrdersByStatus_ChartArea";
series.ChartType = SeriesChartType.StackedColumn;
series.CustomProperties = "DrawingStyle=Cylinder";
series.EmptyPointStyle.BorderDashStyle = ChartDashStyle.NotSet;
series.IsXValueIndexed = true;
series.Name = "IncompleteWorkOrdersByStatus_Series";
List<WorkOrder> incompleteWorkOrders = woDao.getIncompleteWorkOrders();
var uniqueStatuses = (
from statuses in incompleteWorkOrders
select statuses.fkOrderStatus).Distinct().ToList();
int xVal = 1;
foreach (var status in uniqueStatuses)
{
var count = (
from wo in incompleteWorkOrders
where wo.fkOrderStatus == status
select wo).Count();
DataPoint dPoint = new DataPoint(xVal, count);
if (status == null)
{
dPoint.AxisLabel = "No Status";
}
else
{
dPoint.AxisLabel = status.codedesc;
}
dPoint.ToolTip = count + " " + dPoint.AxisLabel;
series.Points.Add(dPoint);
xVal++;
}
this.ChartContainer.Series.Clear();
this.ChartContainer.Series.Add(series);
title.DockedToChartArea = "IncompleteWorkOrdersByStatus_ChartArea";
title.IsDockedInsideChartArea = false;
title.Name = "IncompleteWorkOrdersByStatus_Title";
title.Text = "Incomplete Work Orders By Status";
this.ChartContainer.Titles.Clear();
this.ChartContainer.Titles.Add(title);
break;
default:
break;
}
}
Try playing with the InnerPlotPosition settings:
chart1.ChartAreas[0].InnerPlotPosition.X = 0;