I am making a program where the user can enter values in textboxes on one form and then the data will be carried over to the other form. I think the best way of doing this is to store the user data in several arrays and then carry those arrays over but at the moment I am having real trouble doing it. The textboxes are also created once the user has entered how many they require so the textboxes don't exist on the page initially
string q = combobox1.SelectedItem.ToString();
int g = Convert.ToInt32(q);
MessageBox.Show("I have added " +(g-1) +" Films to the list");
TextBox[] FilmTitle1 = new TextBox[int.Parse(q)];
TextBox[] FilmBudget1 = new TextBox[int.Parse(q)];
TextBox[] FilmBoxOffice1 = new TextBox[int.Parse(q)];
TextBox[] FilmDirector1 = new TextBox[int.Parse(q)];
TextBox[] FilmRtScore1 = new TextBox[int.Parse(q)];
TextBox[] FilmGenre1 = new TextBox[int.Parse(q)];
int y = 500;
for (int i = 0; i < g; i++)
{
FilmTitle1[i] = new TextBox();
FilmTitle1[i].Text = "Film Title";
FilmTitle1[i].Size = new Size(162, 20);
FilmTitle1[i].Location = new Point(106, y);
FilmTitle1[i].Tag = 0;
this.Controls.Add(FilmTitle1[i]);
y= y + 40;
private void Createbar_Click(object sender, EventArgs e)
{
BarGraphCreation frm = new BarGraphCreation(FilmTitle.Text, FilmBudget.Text, FilmBoxOffice.Text, FilmDirector.Text, FilmGenre.Text, ft1, FilmBudget1.Text, FilmBoxOffice1.Text, FilmDirector1.Text, FilmGenre1.Text);
frm.Show();
}
But when I do it this way it says that there is no construct for textboxes despite the fact that it allows it everywhere else in the program.
Any type of help is extremely appreciated
public class Form
{
// your code ...
string q = combobox1.SelectedItem.ToString();
int g = Convert.ToInt32(q);
MessageBox.Show("I have added " +(g-1) +" Films to the list");
public TextBox[] FilmTitle1 = new TextBox[int.Parse(q)];
public TextBox[] FilmBudget1 = new TextBox[int.Parse(q)];
public TextBox[] FilmBoxOffice1 = new TextBox[int.Parse(q)];
public TextBox[] FilmDirector1 = new TextBox[int.Parse(q)];
public TextBox[] FilmRtScore1 = new TextBox[int.Parse(q)];
public TextBox[] FilmGenre1 = new TextBox[int.Parse(q)];
int y = 500;
for (int i = 0; i < g; i++)
{
FilmTitle1[i] = new TextBox();
FilmTitle1[i].Text = "Film Title";
FilmTitle1[i].Size = new Size(162, 20);
FilmTitle1[i].Location = new Point(106, y);
FilmTitle1[i].Tag = 0;
this.Controls.Add(FilmTitle1[i]);
y= y + 40;
private void Createbar_Click(object sender, EventArgs e)
{
BarGraphCreation frm = new BarGraphCreation(this);
frm.Show();
}
}
public class BarGraphCreation
{
// your code...
Form form;
public BarGraphCreation(Form form)
{
// ...
this.form = form;
}
private void function()
{
// here you can work with your data like
// form.FilmTitle1 and so on
}
}
You should avoid passing whole objects like TextBox over to some other controls. General idea is You wan't to pass only data (information), always.
As the Form layout is created dynamically, the information about it's Controls should be held somewhere, where can be accessed again easily.
Example (including both array and list as an example):
public class Form1 : Form
{
private TextBox[] FilmTitles;
private List<TextBox> FilmBudget = new List<Textbox>();
//code removed for brevity
private void Button1_Click( /***/ )
{
FilmBudget.Clear();
int count = Convert.ToInt32(q);
FilmTitles = new TextBox[count];
for (int i = 0; i < count; i++)
{
FilmTitles[i] = new TextBox()
{
Text = "Programmer in one day",
Size = new Size(162, 20)
// all other definitions
};
FilmBudget.Add(new TextBox()
{
Text = "1225",
Size = new Size(162, 20)
// all other definitions
};
this.Controls.Add(FilmTitles[i]);
this.Controls.Add(FilmBudget[i]);
//Now you are holding all the TB & text in global variables (arrays/lists)
}
}
private void Createbar_Click(object sender, EventArgs e)
{
BarGraphCreation frm = new BarGraphCreation(
FilmTitles.Select( a => a.Text).ToArray(),
FilmBudget.Select( a => a.Text).ToArray());
frm.Show();
}
}
Where:
public class BarGraphCreation : Form
{
public BarGraphCreation(string[] Titles, string[] Budgets)
{ }
}
Note that this part is using LINQ to select exact properties (values) into 2 arrays:
BarGraphCreation frm = new BarGraphCreation(
FilmTitles.Select( a => a.Text).ToArray(),
FilmBudget.Select( a => a.Text).ToArray());
Related
Ok, so with the help of awesome guys here on Stackoverflow I have created my array like this.
public partial class Form1 : Form
{
string[] Brands = new string[10];
int brandNo;}
public Form1()
{
InitializeComponent();
Brands[0] = "Yamaha"; //ok
Brands[1] = "Suzuki"; //ok
Brands[2] = "Harley"; //ok
Brands[3] = "Kawasaki"; //ok
brandNo = 4;
}
private void buttonAddbrand_Click(object sender, EventArgs e)
{
if (brandNo >= 10)
return; //cannot add more brand
Brands[brandNo++] = textBoxMerk.Text;
var Merk = Brands
listBoxMotoren.Items.Clear();
listBoxMotoren.Items.AddRange(Merk);
With this code I want to display the filled values of the array in my listbox. But I get the following error:
value can not be null.
Help will be much appreciated.
Firstly, be careful of where you put your curly bracket as to ensure that the code can be compiled.
public partial class Form1 : Form
{
string[] Brands = new string[10];
int brandNo;} //<- off-placed
Secondly, since you start with making the array as your DataSource (and not using Items.Add or Items.AddRange to your listBoxMotoren, it may be consistent if you doing so on addition or removal of your item in the listBoxMotoren.
private void buttonAddbrand_Click(object sender, EventArgs e) {
if (brandNo >= 10)
return; //cannot add more brand
Brands[brandNo++] = textBoxMerk.Text;
listBoxMotoren.DataSource = null; //the cheapest and dirtiest trick to do this
listBoxMotoren.DataSource = Brands; //Maintaining the style, use `DataSource` to accommodate new data
}
Lastly, if you want to remove the brand item at will, you might need another Control in your Form1 as an input for which item in the brand you want to get optionally delete. But beware that this may "destroy" the sequence of your items and thus you may need to "re-sequencing" your item.
Now, suppose you use NumericUpDown to delete and trigger the deletion using buttonDeletebrand then you should do something like this
private void buttonDeletebrand_Click(object sender, EventArgs e) {
int indexToDelete = (int)numericUpDownMotorenNumberDeleted.Value; //note the casting to (int)
if (indexToDelete < 0 || indexToDelete >= brandNo || brandNo <= 0) //can only delete index no [0] to [brandNo-1], and if the brand no > 0
return; //invalid index
for (int i = indexToDelete; i < brandNo - 1; ++i)
Brands[indexToDelete] = Brands[indexToDelete + 1]; //resequencing
Brands[brandNo - 1] = string.Empty; //removes the last element after resequencing
listBoxMotoren.DataSource = null; //remember the cheapest and dirtiest trick?
listBoxMotoren.DataSource = Brands;
--brandNo; //reduce the brandNo by 1
}
In total, you need all of them combined:
public partial class Form1 : Form {
string[] Brands = new string[10];
int brandNo;
public Form1() {
InitializeComponent();
Brands[0] = "Yamaha";
Brands[1] = "Suzuki";
Brands[2] = "Harley";
Brands[3] = "Kawasaki";
brandNo = 4;
listBoxMotoren.DataSource = Brands;
}
private void buttonAddbrand_Click(object sender, EventArgs e) {
if (brandNo >= 10)
return;
Brands[brandNo++] = textBoxMerk.Text;
listBoxMotoren.DataSource = null;
listBoxMotoren.DataSource = Brands;
}
private void buttonDeletebrand_Click(object sender, EventArgs e) {
int indexToDelete = (int)numericUpDownMotorenNumberDeleted.Value;
if (indexToDelete < 0 || indexToDelete >= brandNo || brandNo <= 0)
return;
for (int i = indexToDelete; i < brandNo - 1; ++i)
Brands[indexToDelete] = Brands[indexToDelete + 1];
Brands[brandNo - 1] = string.Empty;
listBoxMotoren.DataSource = null;
listBoxMotoren.DataSource = Brands;
--brandNo;
}
}
i'm pretty sure this code do not even compile....
int brandNo;} <- "}" ???
var Merk = Brands <- ";" missing and (var Merk) not needed
locking at this code i guess the problem is in
check
if (listBoxMotoren != null && listBoxMotoren.Items.Any())
listBoxMotoren.Items.Clear();
and of cource
if (listBoxMotoren != null && Brands != null)
listBoxMotoren.Items.AddRange(Brands);
if listboxMotoren is null the form is not initialized yet as long listBoxMotoren is a form Control
public partial class Form1 : Form
{
string[] Brands = new string[10];
int brandNo;
public Form1()
{
InitializeComponent();
Brands[0] = "Yamaha"; //ok
Brands[1] = "Suzuki"; //ok
Brands[2] = "Harley"; //ok
Brands[3] = "Kawasaki"; //ok
brandNo = 4;
listBoxMotoren.DataSource=Brands;//asssiign the current list to //listbox
}
private void buttonAddbrand_Click(object sender, EventArgs e)
{
if (brandNo >= 10)
return; //cannot add more brand
Brands[brandNo++] = textBoxMerk.Text;
listBoxMotoren.DataSource = null; //make the list empty
listBoxMotoren.DataSource = Brands;// assgin it new list
}
I'm trying to get a list of strings from the database.
For each string in the list i want to add a label & textbox to the page.
On button submit I want to collect the textbox value as well as the corresponding label value then save it to the database.
I need help retrieving the values from the textboxes.
What I have so far:
Panel1 is on the aspx page
protected List<string> items = MyClass.GetItems();
protected void Page_Load(object sender, EventArgs e)
{
GenerateItemsTable();
}
private void GenerateItemsTable()
{
Table table = new Table();
table.ID = "Table1";
//PlaceHolder1.Controls.Add(table);
Panel1.Controls.Add(table);
foreach (var x in items)
{
TableRow row = new TableRow();
for (int y = 0; y < 1; y++)
{
TableCell labelCell = new TableCell();
labelCell.Controls.Add(CreateLabel(x));
labelCell.CssClass = "tdLabel";
row.Cells.Add(labelCell);
TableCell txbCell = new TableCell();
txbCell.Controls.Add(CreateRadNumericTextBox(x));
txbCell.Width = 30;
row.Cells.Add(txbCell);
TableCell dataTypeCell = new TableCell();
dataTypeCell.Text = "<span style='font-size: 10px; color: #777'>(student count)</span>";
dataTypeCell.Width = 100;
row.Cells.Add(dataTypeCell);
TableCell fourthCell = new TableCell();
if (x == items[items.Count - 1])
{
RadButton rb = new RadButton();
rb.ID = "submit";
rb.Text = "Submit Guidance";
rb.Skin = "Forest";
rb.Click += new EventHandler(submit_Click);
rb.AutoPostBack = true;
fourthCell.Controls.Add(rb);
row.Cells.Add(fourthCell);
}
else
{
row.Cells.Add(fourthCell);
}
}
table.Rows.Add(row);
}
}
private RadNumericTextBox CreateRadNumericTextBox(string x)
{
RadNumericTextBox rntb = new RadNumericTextBox();
rntb.ID = x;
rntb.Width = 40;
return rntb;
}
private Label CreateLabel(string x)
{
Label l = new Label();
l.ID = "label_" + x;
l.Text = "<label>" + x + "</label>";
return l;
}
protected void submit_Click(object sender, EventArgs e)
{
foreach (Control x in FindControl("Panel1").FindControl("Table1").Controls)
{
if (x is RadNumericTextBox)
{
//how to get the data??????/
}
}
}
(thanks to those that actually read the whole post)
-----------------updated solution--------------------------------------------
I decided to change it and store the list from the db at page_load. With the list stored i loop through the list and use FindControl() to access the textboxes. Something like this..
//a couple containers
protected class ItemVal
{
public int Value { get; set; }
public string Name { get; set; }
}
protected List<ItemVal> items = new List<ItemVal>();
//get the list from that database
protected void GetItems()
{
foreach (var x in MyClass.GetItems())
{
ItemVal i = new ItemVal();
i.Name = x;
items.Add(i);
}
}
//submit
protected void submit_Click(object sender, EventArgs e)
{
foreach (var x in items)
{
RadNumericTextBox rntb = FindControl(x.Name) as RadNumericTextBox;
x.Value = (int)rntb.Value;
}
}
You need to cast x to a RadNumericTextBox and then pull out the property values you want, like this:
RadNumericTextBox theRadNumericTextBox = x as RadNumericTextBox;
string val = theRadNumericTextBox.Text;
Then for the other controls you want, you will need to put if conditions for their types, like this:
if (x is Label)
{
Label theLabel = x as Label;
string valLabel = theLabel.Text;
}
Here is the full code for the method:
protected void submit_Click(object sender, EventArgs e)
{
foreach (Control x in FindControl("Panel1").FindControl("Table1").Controls)
{
Label theLabel;
RadNumericTextBox theRadNumericTextBox;
if (x is RadNumericTextBox)
{
RadNumericTextBox theRadNumericTextBox = x as RadNumericTextBox;
string val = theRadNumericTextBox.Text;
}
if (x is Label)
{
Label theLabel = x as Label;
string valLabel = theLabel.Text;
}
// Either store up in a list or save to the database on each loop; it is recommended to store a list and send all the changes at once for a database save, but that is your choice
}
}
I'm trying to declare the array Scores as an array of textboxes. It doesn't have a size. I also need to declare it as an instance variable, and instantiate it in the method, CreateTextBoxes. I keep getting an error, "Scores is a field but is used like a type."
namespace AverageCalculator
{
public partial class AverageCalculator : Form
{
private TextBox[] Scores;
public AverageCalculator()
{
InitializeComponent();
}
private void AverageCalculator_Load(object sender, EventArgs e)
{
btnCalculate.Visible = false;
}
private void btnOK_Click(object sender, EventArgs e)
{
int intNumTextBoxes;
intNumTextBoxes = Convert.ToInt32(txtNumScores.Text);
this.Height = 500;
btnCalculate.Visible = true;
btnOK.Enabled = false;
}
private void CreateTextBoxes(int number)
{
Scores[number] = new Scores[number];
int intTop = 150;
for (int i = 0; i < 150; i++)
{
}
}
}
}
your CreateTextBoxes should probably be something like this:
private void CreateTextBoxes(int number)
{
Scores = new TextBox[number];
for (int i = 0; i < number; i++)
{
Scores[i] = new TextBox();
}
}
As Adil suggested, a List<TextBox> is probably better in this case.
You need to instantiate TextBox but number should be constant You can read more about the array creation expression here. Its better to use List instead of array if you want variable size.
Scores = new TextBox[number];
Using List
List<TextBox> Scores= new List<TextBox>();
Your code should read:
Scores = new TextBox[number];
// do things with this array
The problem is in
private void CreateTextBoxes(int number)
{
Scores[number] = new Scores[number];
int intTop = 150;
for (int i = 0; i < 150; i++)
{
}
}
When you are trying to initialize the array, you are using the name of the field as they type and are including an index to the field name. Just change the new type to TextBox and remove the index accessor like this:
private void CreateTextBoxes(int number)
{
Scores = new TextBox[number];
int intTop = 150;
for (int i = 0; i < 150; i++)
{
}
}
replace line 1 with line 2
Scores[number] = new Scores[number];
Scores[number] = new TextBox();
You can't do this.
Scores[number] = new Scores[number];
Use a list of TextBox.
In my code I need to make some textboxes[] that will get some information from the database however its working for 50%
public static Table tableinfo = new Table();
public static TableRow rowinfo = new TableRow();
public static TextBox[] information = new TextBox[1000];
public static Label[] information1 = new Label[1000];
public static Label[] information2 = new Label[1000];
public static Label[] information3 = new Label[1000];
public static Label[] information4 = new Label[1000];
public static string[] gettext = new string[1000];
public static int textboxes;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
textboxes = 1;
for (int i = 0; i < 1000; i++)
{
Array.Clear(gettext, 0, gettext.Length);
Array.Clear(information, 0, information.Length);
}
}
else
{
}
tableinfo.Controls.Clear();
tableinfo.BorderWidth = 1;
addtextboxes();
placeholder1.Controls.Add(tableinfo);
makeandfilltextboxes();
}
public static void makeandfilltextboxes()
{
for (int i = 0; i < textboxes; i++)
{
gettext[i] = "";
}
for (int i = 0; i < textboxes; i++)
{
gettext[i] = information[i].Text;
//go to database and fetch information
information1[i].text = databaseinformation1[i];
information2[i].text = databaseinformation2[i];
information3[i].text = databaseinformation3[i];
information4[i].text = databaseinformation4[i];
}
}
static void TestForm_TextChanged(object sender, EventArgs e)
{
makeandfilltextboxes();
tableinfo.Controls.Clear();
tableinfo.BorderWidth = 1;
textboxes++;
addtextboxes();
}
public static void addtextboxes()
{
for (int i = 0; i < textboxes; i++)
{
rowinfo = new TableRow();
TableCell cellinfo = new TableCell();
information[i] = new TextBox();
information[i].AutoPostBack = true;
information[i].CausesValidation = false;
information[i].EnableViewState = true;
information[i].Text = gettext[i];
information[i].TextChanged += new EventHandler(TestForm_TextChanged);
rowinfo.Cells.Add(cellinfo);
cellinfo.Controls.Add(information[i]);
tableinfo.Controls.Add(rowinfo);
TableCell cellinfo1 = new TableCell();
information1[i] = new Label();
information1[i].EnableViewState = true;
information1[i].Text = databaseinformation1[i];
rowinfo.Cells.Add(cellinfo1);
cellinfo.Controls.Add(information1[i]);
tableinfo.Controls.Add(rowinfo);
TableCell cellinfo2 = new TableCell();
information2[i] = new Label();
information2[i].EnableViewState = true;
information2[i].Text = databaseinformation2[i];
rowinfo.Cells.Add(cellinfo2);
cellinfo.Controls.Add(information2[i]);
tableinfo.Controls.Add(rowinfo);
TableCell cellinfo3 = new TableCell();
information3[i] = new Label();
information3[i].EnableViewState = true;
information3[i].Text = datbaseinformation3[i];
rowinfo.Cells.Add(cellinfo3);
cellinfo.Controls.Add(information3[i]);
tableinfo.Controls.Add(rowinfo);
TableCell cellinfo4 = new TableCell();
information4[i] = new Label();
information4[i].EnableViewState = true;
information4[i].Text = databaseinformation4[i];
rowinfo.Cells.Add(cellinfo4);
cellinfo.Controls.Add(information4[i]);
tableinfo.Controls.Add(rowinfo);
}
}
So as you can see, I'm filling the array's with the value you filled in the information[i]
on the value in there i'm looking in the database if i can find something.
this is all working perfectly.
the only problem is that the information[i].TextChanged += new EventHandler(TestForm_TextChanged); is called in a strange manner.
the 1st time you enter a textbox is fires
the 2nd time it isn't fired
the 3rd time it fires
the 4th time it isn't fired
and so on
If I can fire the TestForm_TextChanged everytime the problem will be fixed.
I also have some checkboxes who are having the eventhandler of checkedchanged who are showing the information on the hand of there are checked or not is this causing some problems
im working in a updatepanel and have a scriptmanager on my aspx desinger.
what is causing this problem?
With Google chrome I can use the ENTER and it will work all the time.
But the enter doesn't work in Internet Explorer.
NOTE: THE CODE THAT I HAVE SEND HERE IS JUST A REPLICA NOT THE REAL CODE.
THIS IS DUE THE POLICY OF MY INTERNSHIP.
If you need more information about the code be free to ask ill try to be as much detailed as can.
I think the problem is that all of your textboxes and labels are static. Make them part of your Page instance.
I fixed the problem apparently the problem was that there wasn't a Id
so i added information[i].ID = "information" + i;
and that fixed the problem
I was wondering how am I supposed to assign an eventHandler for each array element in my label array. I understand that it's not possible to create a method for each of eventHandlers, so what could be the solution? Thank you!
for(int i = 0, i < 10; i++)
{
lbs[i] = new Label();
lbs[i].Location = new System.Drawing.Point(76 + f, 164);
lbs[i].Size = new System.Drawing.Size(49, 17);
//able to perform this, but wont able to create a method for this
lbs[i].Click += new System.EventHandler(lbs[i]_Click);
}
//can't do this, what is alternative?
public void lbs[i]_Click(object sender, EventArgs e)
{
}
Your function name is invalid (you can't have [] in a function name) try changing lbs[i]_Click to lbs_Click.
for(int i = 0; i < 10; i++)
{
lbs[i] = new Label();
lbs[i].Location = new System.Drawing.Point(76 + f, 164);
lbs[i].Size = new System.Drawing.Size(49, 17);
lbs[i].Name = "label" + i;
//able to perform this, but wont able to create a method for this
lbs[i].Click += new System.EventHandler(lbsi_Click);
}
public void lbsi_Click(object sender, EventArgs e)
{
var label = sender as Label;
if(label != null && label.Name == "label1"){
//event was raised from label1
}
}