How to increment values when add dynamic controls? - c#

I am adding dynamic controls and I want to increment the values in Label when adding controls dynamically
Code:
private int controlCount
{
get
{
int val = 0;
try
{
val = (int)ViewState["ControlCount"];
}
catch (Exception e)
{
// handle exception, if required.
}
return val;
}
set { ViewState["ControlCount"] = value; }
}
protected void addnewtext_Click(object sender, EventArgs e)
{
int i = controlCount++;
for (int j = 0; j <= i; j++)
{
AddVisaControl ac = (AddVisaControl)Page.LoadControl("AddVisaControl.ascx");
Label lb = new Label();
string z = Convert.ToString(i + 1);
lb.Text = "Visa " + z;
rpt1.Controls.Add(lb);
lb.Attributes.Add("class", "style8");
rpt1.Controls.Add(ac);
rpt1.Controls.Add(new LiteralControl("<BR>"));
}
}
In the below image I'm getting label values i.e(Visa 3) are overwriting
Any ideas?

You want
string z = Convert.ToString(j + 1);
rather than
string z = Convert.ToString(i + 1);

Related

check the condition and remove dynamic controls accordingly c#

I have a combobox which has values 4-9, and according to that value I want generate runtime labels and textboxes. When I click on 6 then the code can generate 6 labels and textboxes as required, but when I click on 5 again one label and textbox should disappear or if I click on 4 again 2 labels and textboxes should disappear....which is not happening. I have this code in c#. What changes should I make in this code? Is there any other way that I can do this code?
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.Text == "4")
{
checkBox1.Visible = true;
for (int i = 0; i < 4; i++)
{
addlabel(i);
}
for (int i1 = 0; i1 < 4; i1++)
{
addlabel1(i1);
}
}
if (comboBox1.Text == "5")
{
checkBox1.Visible = true;
for (int i = 0; i < 5; i++)
{
addlabel(i);
}
for (int i1 = 0; i1 < 5; i1++)
{
addlabel1(i1);
}
}
if (comboBox1.Text == "6")
{
checkBox1.Visible = true;
for (int i = 0; i < 6; i++)
{
addlabel(i);
}
for (int i1 = 0; i1 < 6; i1++)
{
addlabel1(i1);
}
}
}
void addlabel(int i)
{
int left = 70;
int top = 100;
int step_x = 80;
int step_y = 30;
new Label()
{
Name = $"label{i}",
Text = "Enter Subject:",
Location = new Point(left, top + step_y * i),
Parent = this,
};
left += step_x;
int left1 = 357;
int top1 = 100;
int step_x1 = 80;
int step_y1 = 30;
new Label()
{
Name = $"label{i}",
Text = "Total Marks:",
Location = new Point(left1, top1 + step_y1 * i),
Parent = this,
};
left1 += step_x1;
}
void addlabel1(int i1)
{
int left = 200;
int top = 100;
int step_x = 80;
int step_y = 30;
new TextBox()
{
Name = $"textbox{i1}",
Text = "",
Size = new Size(122, 20),
Location = new Point(left, top + step_y * i1),
Parent = this,
};
left += step_x;
int left1 = 480;
int top1 = 100;
int step_x1 = 80;
int step_y1 = 30;
new TextBox()
{
Name = $"textbox{i1}",
Text = "",
Size = new Size(122, 20),
Location = new Point(left1, top1 + step_y1 * i1),
Parent = this,
};
left1 += step_x1;
}
Any Suggestions? Help me out.
Try the following code:
public partial class Form1 : Form
{
private int prev = 0;
private Point lblLocation = new Point(70, 100);
private Point tbLocation = new Point(170, 100);
public Form1()
{
InitializeComponent();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int cur = Convert.ToInt32(comboBox1.SelectedItem);
int tmp = cur - prev;
if (tmp > 0)
{
// add new controls
for (int i = 1; i <= tmp; i++)
{
AddLabel(prev + i);
AddTextBox(prev + i);
lblLocation.Y += 30;
tbLocation.Y += 30;
}
prev = cur;
}
else
{
// remove controls
tmp = Math.Abs(tmp);
for(int i= 0; i < tmp; i++)
{
RemoveControl($"lbl{prev}");
RemoveControl($"tb{prev}");
lblLocation.Y -= 30;
tbLocation.Y -= 30;
prev--;
}
}
}
private void AddLabel(int i)
{
new Label()
{
Name = $"lbl{i}",
Text = $"lbl{i}",
Location = lblLocation,
Parent = this
};
}
private void AddTextBox(int i)
{
new TextBox()
{
Name = $"tb{i}",
Text = $"tb{i}",
Location = tbLocation,
Parent = this
};
}
private void RemoveControl(string name)
{
foreach (Control item in Controls.OfType<Control>())
{
if (item.Name == name)
{
Controls.Remove(item);
}
}
}
}

how to use sender arg for access to i&j in 2D Array in C#?

i have a code like this:
Label[,] Cell = new Label[8, 8];
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Cell[i, j] = new Label();
Cell[i, j].Text = (i + 1) + "" + (j + 1);
Cell[i, j].Location = new Point(j * 50 + 25, i * 50 + 25);
this.Controls.Add(Cell[i, j]);
Cell[i, j].Click += new System.EventHandler(lbl_click);
}
}
public void lbl_click(object sender, EventArgs e)
{
//I want having i & j here and work with them.
}
How can I access i and j variables from within the click event handler?
Using the Tag property
One option could be to Tag the label with the data you need to use.
For example, create a class to hold the data...
class TagData
{
public int I { get; set; }
public int J { get; set; }
}
In your loop...
Cell[i, j].Tag = new TagData() { I = i, J = j };
In the event handler...
public void lbl_click(object sender, EventArgs e)
{
Label label = sender as Label;
TagData tagData = label.Tag as TagData;
// Do something with tagData.I and tagData.J
}
Parsing the label Text
If you can assume that neither i or j would be more than a single digit each, then you could simply parse the Text. Like so:
public void lbl_click(object sender, EventArgs e)
{
Label label = sender as Label;
int i = int.Parse(label.Text[0]) - 1;
int j = int.Parse(label.Text[1]) - 1;
}
NOTE: The danger with more than a single digit for each is that without a separator you could not know if "123" was i = 1 or i = 12. You could of course work around this by using a separator, for example "12,3" but I wouldn't suggest having code that relies on specific UI design/formatting.
Ok thanks to musefan here is the solution with storing the coordinates in the Tag of the label. Winforms controls got a Property called Tag where you can store related Information.
Label[,] Cell = new Label[8, 8];
for (int i = 0; i < 8; i++)
{
for (int j = 0; j < 8; j++)
{
Label tempLabel = new Label();
Cell[i,j] = tempLabel;
tempLabel.Text = (i + 1) + "" + (j + 1);
tempLabel.Location = new Point(j * 50 + 25, i * 50 + 25);
tempLabel.Click += new System.EventHandler(lbl_click);
tempLabel.Tag = new Tuple<int, int>(i, j);
this.Controls.Add(tempLabel);
}
}
public void lbl_click(object sender, EventArgs e)
{
Label label = sender as Label;
Tuple<int, int> position = label.Tag as Tuple<int, int>;
if(positon != null)
{
int i = position.Item1;
int j = position.Item2;
//do whatever with the coordinates
}
}

How to interact with variables (just created in the program) that are in another method?

In the first method I'm creating a matrix of textboxes and in another (Button_click) method. I need to take the values of textboxes from the Button_Click method and then do something with them. I don't know how interact with just created values(the names are also new). But I know that I can't use t[j].
private void vsematrici_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int selectedIndex = vsematrici.SelectedIndex+2;
StackPanel[] v = new StackPanel[selectedIndex];
for (int i = 0; i < selectedIndex; i++)
{
v[i] = new StackPanel();
v[i].Name = "matrixpanel" + i;
v[i].Orientation = Orientation.Horizontal;
TextBox[] t = new TextBox[selectedIndex];
for (int j = 0; j < selectedIndex; j++)
{
t[j] = new TextBox();
t[j].Name = "a" + (i + 1) + (j + 1);
t[j].Text = "a" + (i + 1) + (j + 1);
v[i].Children.Add(t[j]);
Thickness m = t[j].Margin;
m.Left = 1;
m.Bottom = 1;
t[j].Margin = m;
InputScope scope = new InputScope();
InputScopeName name = new InputScopeName();
name.NameValue = InputScopeNameValue.TelephoneNumber;
scope.Names.Add(name);
t[j].InputScope = scope;
}
mainpanel.Children.Add(v[i]);
}
Button button1 = new Button();
button1.Content = "Найти определитель";
button1.Click += Button_Click;
mainpanel.Children.Add(button1);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
// Double sa11v = Convert.ToDouble(t[j].Text);
}
Sorry for my English, I'm from Russia :)
you could create a member variable that is used outside the event in which you have created TextBox Array-
TextBox[] _t = null;
private void vsematrici_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int selectedIndex = vsematrici.SelectedIndex + 2;
StackPanel[] v = new StackPanel[selectedIndex];
for (int i = 0; i < selectedIndex; i++)
{
v[i] = new StackPanel();
v[i].Name = "matrixpanel" + i;
v[i].Orientation = Orientation.Horizontal;
_t = new TextBox[selectedIndex];
for (int j = 0; j < selectedIndex; j++)
{
_t[j] = new TextBox();
_t[j].Name = "a" + (i + 1) + (j + 1);
_t[j].Text = "a" + (i + 1) + (j + 1);
v[i].Children.Add(_t[j]);
Thickness m = t[j].Margin;
m.Left = 1;
m.Bottom = 1;
_t[j].Margin = m;
InputScope scope = new InputScope();
InputScopeName name = new InputScopeName();
name.NameValue = InputScopeNameValue.TelephoneNumber;
scope.Names.Add(name);
_t[j].InputScope = scope;
}
mainpanel.Children.Add(v[i]);
}
Button button1 = new Button();
button1.Content = "Найти определитель";
button1.Click += Button_Click;
mainpanel.Children.Add(button1);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (_t != null)
{
//Do your work here
// Double sa11v = Convert.ToDouble(t[j].Text);
}
}
but for a broader aspect, you can use dictionary,
Dictionary<string, TextBox> _dicTextBoxes;
private void vsematrici_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int selectedIndex = vsematrici.SelectedIndex + 2;
StackPanel[] v = new StackPanel[selectedIndex];
for (int i = 0; i < selectedIndex; i++)
{
v[i] = new StackPanel();
v[i].Name = "matrixpanel" + i;
v[i].Orientation = Orientation.Horizontal;
_dicTextBoxes = new Dictionary<string, TextBox>();
for (int j = 0; j < selectedIndex; j++)
{
TextBox txtBox = new TextBox();
txtBox = new TextBox();
txtBox.Name = "a" + (i + 1) + (j + 1);
txtBox.Text = "a" + (i + 1) + (j + 1);
v[i].Children.Add(txtBox);
Thickness m = txtBox.Margin;
m.Left = 1;
m.Bottom = 1;
txtBox.Margin = m;
InputScope scope = new InputScope();
InputScopeName name = new InputScopeName();
name.NameValue = InputScopeNameValue.TelephoneNumber;
scope.Names.Add(name);
txtBox.InputScope = scope;
_dicTextBoxes.Add(txtBox.Name, txtBox);
}
mainpanel.Children.Add(v[i]);
}
Button button1 = new Button();
button1.Content = "Найти определитель";
button1.Click += Button_Click;
mainpanel.Children.Add(button1);
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (_dicTextBoxes != null)
{
// a23 is the name of textbox, 'a' is prefixe, '2' is the 2nd stackpanel you have added
// '3' is the 3rd textbox you have added in stackpanel
if (_dicTextBoxes.ContainsKey("a23"))
{
//Do your work here
Double sa11v = Convert.ToDouble(_dicTextBoxes["a23"].Text);
}
}
}
Loop through the Children of mainpanel and check each control if it is a TextBox. Something like:
foreach (UIElement ctrl in mainpanel.Children)
{
if (ctrl.GetType() == typeof(TextBox))
{
TextBox oneOfYourTextBoxes = ((TextBox)ctrl);
// do your thing
}
}
You cannot change an arrays size after you created it. Hence your size is known only at runtime after firing your event you cannot set its size within the declaration.
class MyClass
{
Textbox[] myTextBoxes;
private void vsematrici_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
// ...
myTextBoxes = new TextBoxes[selectedIndex];
}
private void Button_Click(object sender, RoutedEventArgs e)
{
int myIndex = // your selected item here
if (this.myTextBoxes != null && myIndex < this.myTextBoxes.Length)
{
Console.WriteLine(this.myTextBoxes[myIndex].Text);
}
}
}
This will also work:
button1.Click += (sender, args) => DoSomethingToTextboxes(_t);

get the value from the dictionary

I am working with Round Robin (RR). I am building random Round-Robin value. I get the time for the RR randomly. In the first run it shows me the current values that i have in the list. but when it goes to run for the second time it just say the index is not presented in the dictionary key. I am trying to fix this problem I have add the time and the quantum tim that the algorithm reduce the value from the dictionary and return back to the dictionary it the value is > 0.
Dictionary<int, int> waytosave = new Dictionary<int, int>();
List<int> randomListxCoor = new List<int>();
List<int> randomListyCoor = new List<int>();
int xCoor;
int yCoor;
Random coor = new Random();
Random setTimeRandom = new Random();
int randomTimeSetting;
int NodeIndex = 0;
Graphics graphDraw;
Graphics drawLine;
int TimeToProcess = 0;
string NodeName = "";
int QuantumTime = 1;
int TotalRouterTime = 0;
int NextNodeToProcess = 0;
public Form1()
{
InitializeComponent();
this.graphDraw = this.panel1.CreateGraphics();
this.drawLine = this.panel1.CreateGraphics();
}
private void btnRun_Click(object sender, EventArgs e)
{
int nodesNumber = (int)this.numNodes.Value;
SolidBrush getNodesDrawn = new SolidBrush(Color.LightYellow);
for (int x = 1; x <= nodesNumber; x++)
{
xCoor = coor.Next(0, 700);
yCoor = coor.Next(0, 730);
if (!randomListxCoor.Contains(xCoor))
{
randomListxCoor.Add(xCoor);
}
if (!randomListyCoor.Contains(xCoor))
{
randomListyCoor.Add(yCoor);
}
randomTimeSetting = setTimeRandom.Next(1, 10);
//Draw the line from main Node to the other Nodes
Pen linePen = new Pen(Color.DarkMagenta, 2);
drawLine.DrawLine(linePen, 350, 360, xCoor, yCoor);
drawLine = this.panel1.CreateGraphics();
graphDraw.FillEllipse(getNodesDrawn, xCoor - 5, yCoor - 5, 15, 12);
//Add the values t the Dictionaries and the List.
waytosave.Add(x, randomTimeSetting);
}
}
private void btnRunTIme_Click(object sender, EventArgs e)
{
this.QuantumTime = (int)this.numQuanrum.Value;
this.ComputeTotalNodeTime();
this.timerRoundRobin.Enabled = true;
}
private void timerRoundRobin_Tick(object sender, EventArgs e)
{
this.NodeIndex = this.GetNextNodeToProcess();
if (this.NodeIndex >= 0)
{
this.waytosave[this.NodeIndex] -= this.QuantumTime;
here at this point it getting crashed. I tried to fixed the number of iteration but if i change the number of iterations it will not go through all the values in the dictionary.
if (this.waytosave[this.NodeIndex] < 0)
{
this.waytosave[this.NodeIndex] = 0;
}
this.NodeName = this.NodeIndex.ToString();
this.TimeToProcess = this.waytosave[this.NodeIndex];
this.txtOutput.Text += "\r\r\n" + " Time Remaining: "
+ this.TimeToProcess
+ "Router name: "
+ this.NodeName;
}
else
{
this.timerRoundRobin.Enabled = false;
this.txtOutput.Text += "\r\r\n" + " Ends x " + TotalRouterTime.ToString();
return;
}
}
private int GetNextNodeToProcess()
{
int NextNodeIndex = -1;
if (NextNodeToProcess >= this.waytosave.Count)
{ NextNodeToProcess = 0; }
for (int i = NextNodeToProcess; i < this.waytosave.Count; i++)
{
if (this.waytosave[i] > 0)
{
NextNodeIndex = i;
break;
}
}
NextNodeToProcess++;
return NextNodeIndex;
}
private void ComputeTotalNodeTime()
{
this.TotalRouterTime = 0;
foreach (KeyValuePair<int, int> item in this.waytosave)
{
this.TotalRouterTime += item.Value;
this.txtOutput.Text += "\r\r\n" + " Time Remaining: "
+ item.Value
+ "Router name: "
+ item.Key;
}
}
I am trying to fix this value from days but i have no success. the problem that in cannot read the first value from the dictionary. How can I solve this problem. Can you please give me some Hence.

Runtime textbox creation creates issue

This code works fine for first two times,i.e ,when i click on "Add more solution" it creates a new row and controls respectively but when i click on "Add more solution" for the third time and "add step" button respective to that,,it creates problem.
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < TotalNumberAdded; i++)
{
AddControls((i + 1), plcSolution);
}
for (int i = 0; i < TotalNumberSolAdded; i++)
{
AddMoreControls((i + 1), plcAddMoreSolution, 1);
Button b = (Button)plcAddMoreSolution.FindControl("btn" + (i+1));
for (int j = 0; j < TotalNumberSolStepAdded; j++)
{
AddMoreStepControls((j + 1), plcAddMoreSolution);
}
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
TotalNumberAdded++;
AddControls(TotalNumberAdded, plcSolution);
}
private void AddControls(int controlNumber, PlaceHolder plc)
{
TextBox txtBoxSolution = new TextBox();
Label lblSolution = new Label();
txtBoxSolution.ID = "txtBoxSolution" + controlNumber;
txtBoxSolution.TextMode = TextBoxMode.MultiLine;
txtBoxSolution.Width = 470;
txtBoxSolution.Height = 50;
lblSolution.ID = "lblSolution" + controlNumber;
lblSolution.Text = "Step " + (controlNumber + 1) + ": ";
lblSolution.Width = 200;
plc.Controls.Add(lblSolution);
plc.Controls.Add(txtBoxSolution);
}
protected int TotalNumberAdded
{
get { return (int)(ViewState["TotalNumberAdded"] ?? 0); }
set { ViewState["TotalNumberAdded"] = value; }
}
protected void btnAlternate_Click(object sender, EventArgs e)
{
lblSol.Text = "Solution 1";
string str = (string)ViewState["btnId"];
if (str != null)
{
btn = (Button)plcAddMoreSolution.FindControl(str);
btn.Visible = false;
}
btnAdd.Visible = false;
TotalNumberSolAdded++;
AddMoreControls(TotalNumberSolAdded, plcAddMoreSolution,1);
}
protected int TotalNumberSolAdded
{
get { return (int)(ViewState["TotalNumberSolAdded"] ?? 0); }
set { ViewState["TotalNumberSolAdded"] = value; }
}
private void AddMoreControls(int controlNumber, PlaceHolder plc,int n)
{
TextBox txtBoxMoreSolution = new TextBox();
Label lblMoreSolution = new Label();
btn = new Button();
btn.Text = "add step";
btn.ID = "btn" + controlNumber;
btn.Click += new EventHandler(btn_Click);
ViewState["btnId"] = btn.ID;
Label lbl = new Label();
lbl.Text = "soltuion" + (controlNumber + 1);
lbl.ID = "moreSolution" + (controlNumber + 1);
lbl.Font.Size = 20;
lbl.Font.Underline = true;
txtBoxMoreSolution.ID = "txtBoxMoreSolution" + controlNumber;
txtBoxMoreSolution.TextMode = TextBoxMode.MultiLine;
txtBoxMoreSolution.Width = 470;
txtBoxMoreSolution.Height = 50;
lblMoreSolution.ID = "lblMoreSolution" + controlNumber;
lblMoreSolution.Text = "Step " + n + ": ";
lblMoreSolution.Width = 200;
plc.Controls.Add(lbl);
plc.Controls.Add(lblMoreSolution);
plc.Controls.Add(txtBoxMoreSolution);
plc.Controls.Add(btn);
}
private void btn_Click(object sender, EventArgs e)
{
TotalNumberSolStepAdded++;
AddMoreStepControls(TotalNumberSolStepAdded, plcAddMoreSolution);
}
protected int TotalNumberSolStepAdded
{
get { return (int)(ViewState["TotalNumberSolStepAdded"] ?? 0); }
set { ViewState["TotalNumberSolStepAdded"] = value; }
}
private void AddMoreStepControls(int controlNumber, PlaceHolder plc)
{
TextBox txtBoxMoreStepSolution = new TextBox();
Label lblMoreStepSolution = new Label();
txtBoxMoreStepSolution.ID = "txtBoxMoreStepSolution" + controlNumber;
txtBoxMoreStepSolution.TextMode = TextBoxMode.MultiLine;
txtBoxMoreStepSolution.Width = 470;
txtBoxMoreStepSolution.Height = 50;
lblMoreStepSolution.ID = "lblMoreStepSolution" + controlNumber;
lblMoreStepSolution.Text = "Step " + (controlNumber +1) + ": ";
lblMoreStepSolution.Width = 200;
plc.Controls.Add(lblMoreStepSolution);
plc.Controls.Add(txtBoxMoreStepSolution);
}
this code will help you.. it works fine...
protected void Page_Load(object sender, EventArgs e)
{
for (int i = 0; i < TotalNumberAdded; i++)
{
AddControls(i + 1);
}
for (int i = 0; i < TotalNumberSolAdded; i++)
{
AddMoreControls((i + 1), 1);
DataTable dt = (DataTable)ViewState["t"];
if (dt != null)
{
if ((TotalNumberSolAdded - dt.Rows.Count == 1) && (counter1 == dt.Rows.Count))
{
break;
}
if (i != 0)
{
for (int k = 0; k <= i; k++)
{
int a1 = int.Parse(dt.Rows[k][0].ToString());
b = a1 + b;
}
start = b - int.Parse(dt.Rows[i][0].ToString());
}
else
{
b = int.Parse(dt.Rows[i][0].ToString());
start = 0;
}
for (int j = start; j < b; j++)
{
counter++;
AddMoreStepControls((j + 1), counter);
}
b = 0;
counter = 0;
counter1++;
}
}
}
private void AddControls(int controlNumber)
{
TextBox txtBoxSolution = new TextBox();
Label lblSolution = new Label();
txtBoxSolution.ID = "txtBoxSolution" + controlNumber;
txtBoxSolution.TextMode = TextBoxMode.MultiLine;
txtBoxSolution.Width = 470;
txtBoxSolution.Height = 50;
lblSolution.ID = "lblSolution" + controlNumber;
lblSolution.Text = "Step " + (controlNumber + 1) + ": ";
lblSolution.Width = 200;
plcSolution.Controls.Add(lblSolution);
plcSolution.Controls.Add(txtBoxSolution);
}
protected int TotalNumberAdded
{
get { return (int)(ViewState["TotalNumberAdded"] ?? 0); }
set { ViewState["TotalNumberAdded"] = value; }
}
protected void btnAlternate_Click(object sender, EventArgs e)
{
Number = 0;
lblSol.Text = "Solution 1";
string str = (string)ViewState["btnId"];
if (str != null)
{
btn = (Button)plcAddMoreSolution.FindControl(str);
btn.Visible = false;
}
btnAdd.Visible = false;
TotalNumberSolAdded++;
AddMoreControls(TotalNumberSolAdded, 1);
}
protected int TotalNumberSolAdded
{
get { return (int)(ViewState["TotalNumberSolAdded"] ?? 0); }
set { ViewState["TotalNumberSolAdded"] = value; }
}
private void AddMoreControls(int controlNumber, int n)
{
TextBox txtBoxMoreSolution = new TextBox();
Label lblMoreSolution = new Label();
btn = new Button();
btn.Text = "add step";
btn.ID = "btn" + controlNumber;
btn.Click += new EventHandler(btn_Click);
ViewState["btnId"] = btn.ID;
Label lbl = new Label();
lbl.Text = "Soltuion" + (controlNumber + 1);
lbl.ID = "moreSolution" + (controlNumber + 1);
lbl.Font.Size = 20;
lbl.Font.Underline = true;
lbl.Font.Bold = true;
txtBoxMoreSolution.ID = "txtBoxMoreSolution" + controlNumber;
txtBoxMoreSolution.TextMode = TextBoxMode.MultiLine;
txtBoxMoreSolution.Width = 470;
txtBoxMoreSolution.Height = 50;
lblMoreSolution.ID = "lblMoreSolution" + controlNumber;
lblMoreSolution.Text = "Step " + n + ": ";
lblMoreSolution.Width = 200;
plcAddMoreSolution.Controls.Add(lbl);
plcAddMoreSolution.Controls.Add(lblMoreSolution);
plcAddMoreSolution.Controls.Add(txtBoxMoreSolution);
plcAddMoreSolution.Controls.Add(btn);
}
private void btn_Click(object sender, EventArgs e)
{
TotalNumberSolStepAdded++;
Number++;
string str = (string)ViewState["btnId"];
if (str != null)
{
string resultString = Regex.Match(str, #"\d+").Value;
a = int.Parse(resultString);
table = (DataTable)ViewState["t"];
if (table == null)
{
DataTable table1 = new DataTable();
table1.Columns.Add("count", typeof(int));
dr = table1.NewRow();
table1.Rows.Add(dr);
table1.Rows[a - 1]["count"] = Number;
table1.AcceptChanges();
ViewState["t"] = table1;
}
else
{
if (a != table.Rows.Count)
{
dr = table.NewRow();
table.Rows.Add(dr);
}
table.Rows[a - 1]["count"] = Number;
table.AcceptChanges();
ViewState["t"] = table;
}
}
AddMoreStepControls(TotalNumberSolStepAdded, Number);
}
protected int TotalNumberSolStepAdded
{
get { return (int)(ViewState["TotalNumberSolStepAdded"] ?? 0); }
set { ViewState["TotalNumberSolStepAdded"] = value; }
}
protected int Number
{
get { return (int)(ViewState["Number"] ?? 0); }
set { ViewState["Number"] = value; }
}
private void AddMoreStepControls(int controlNumber, int counter)
{
TextBox txtBoxMoreStepSolution = new TextBox();
Label lblMoreStepSolution = new Label();
txtBoxMoreStepSolution.ID = "txtBoxMoreStepSolution" + controlNumber;
txtBoxMoreStepSolution.TextMode = TextBoxMode.MultiLine;
txtBoxMoreStepSolution.Width = 470;
txtBoxMoreStepSolution.Height = 50;
lblMoreStepSolution.ID = "lblMoreStepSolution" + controlNumber;
lblMoreStepSolution.Text = "Step " + (counter + 1) + ": ";
lblMoreStepSolution.Width = 200;
plcAddMoreSolution.Controls.Add(lblMoreStepSolution);
plcAddMoreSolution.Controls.Add(txtBoxMoreStepSolution);
}

Categories

Resources