Textbox array textchanged doesn't fire properly - c#

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

Related

C# Dynamicaaly Add and Delete buttons

From the below code, I dynamically create a list of buttons based on client names provided by a TCP connection from the clientNames[i] list.
private void updateClientListUI()
{
if (this.InvokeRequired)
{
this.Invoke(new MethodInvoker(this.updateClientListUI));
}
else
{
//Debug.WriteLine(clientNames[0]);
int basex = subPanelClient.Location.X;
int basey = subPanelClient.Location.Y;
for (int i = 0; i < clientNames.Count; i++)
{
Button b = new Button();
b.Left = basex;
b.Top = basey;
b.Size = new Size(25, 25); // <== add this line
b.Dock = DockStyle.Top;
b.ForeColor= Color.Gainsboro;
b.FlatStyle= FlatStyle.Flat;
b.FlatAppearance.BorderSize = 0;
b.Padding= new Padding(35, 0, 0, 0);
b.TextAlign = ContentAlignment.MiddleLeft;
basey += 25;
b.Name = clientNames[i];
b.Text = clientNames[i];
subPanelClient.Controls.Add(b);
buttonsAdded.Insert(i, b);
}
}
}
What I am trying to figure out, is how to delete a button (i). What I attempted is the following:
private void removingButtons(int i)
{
if (buttonsAdded.Count > 0)
{
Button buttonToRemove = buttonsAdded[i];
subPanelClient.Controls.Remove(buttonToRemove);
buttonsAdded.Remove(buttonToRemove);
}
}
Or if anyone would know how to update the current list of buttons from updateClientListUI() to the current latest list from clientNames[i] instead of a list being added onto another list.
The issue that Im getting is that obviously every time there is a connection or disconnection the list just keeps getting added instead of refreshing to the current list.
One way to do it would be to clear the old buttons on each update.
foreach (Button b in buttonsAdded)
{
subPanelClient.Controls.Add(b);
}
buttonsAdded.Clear();
for (int i = 0; i < clientNames.Count; i++)
{
Button b = new Button();
...
...
If the subPanelClient only contains those buttons, you could just use
subPanelClient.Controls.Clear();
Last remark: Your removingButtonk function seems to use this.Controls.Remove instead of subPanelClient.Controls.Remove.

Application Freezes when managing datagridview cells values

I´m trying to hide a Datagridview password column value by changing cells values to asterisks but it freezes when trying to get cells values, I read that it may be because of the Thread, why?
Early it was working fine with this same code but I erased the datagridview (because I modified its properties and didnt liked) and added a new one.. problems started when I did that.
public partial class FormUsers : Form
{
public FormUsers()
{
InitializeComponent();
}
private void FormUsers_Load(object sender, EventArgs e)
{
LoadDataGrid();
}
private void LoadDataGrid()
{
dtgUsers.DataSource = UserHelper.ListUsers();
if (dtgUsers.Rows.Count > 0)
{
dtgUsers.Columns[0].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dtgUsers.Columns[1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dtgUsers.Columns[2].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dtgUsers.Columns[3].AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
dtgUsers.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dtgUsers.Columns[5].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dtgUsers.Columns[6].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dtgUsers.Columns[7].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dtgUsers.Columns[8].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
dtgUsers.Columns[0].HeaderText = "ID";
dtgUsers.Columns[1].HeaderText = "Nombre";
dtgUsers.Columns[2].HeaderText = "Apellidos";
dtgUsers.Columns[3].HeaderText = "Genero";
dtgUsers.Columns[4].HeaderText = "Direccion";
dtgUsers.Columns[5].HeaderText = "Correo Electronico";
dtgUsers.Columns[6].HeaderText = "Puesto";
dtgUsers.Columns[7].HeaderText = "Nombre de Usuario";
dtgUsers.Columns[8].HeaderText = "Contraseña";
string pass = "";
for (int i = 0; i < dtgUsers.Rows.Count; i++)
{
pass = "";
for (int j = 0; j < dtgUsers.Rows[i].Cells[8].Value.ToString().Length; j++) //APPLICATION FREEZES HERE
{
pass += "*";
}
dtgUsers.Rows[i].Cells[8].Value = pass;
}
}
}
}
Yes, your datagridview is freezing because you are handling the change on the same thread that runs the UI, and also because for every field you are. Although my recommendation would be to set to * the passwords before they even got the chance to be in the datagrid, also saving passwords as a plain text is not a very good idea.
what you should do is replace this: dtgUsers.DataSource = UserHelper.ListUsers();
with this:
var userList = UserHelper.ListUsers();
userList.ToList().ForEach(x =>
{
x.Password = new string('*', x.Password.Length);
});
dtgUsers.DataSource = userList;
Password might be another property you have

Can I concatenat a control such as linklable

I read from a text file, then save the contain of a file to an array
string[] SingleArray = new string[] {};
SingleArray[i] = File.ReadAllLines(path);
So now I would like to perform something like:
LnkmyValues_+Convert.ToString(i)+".Text" = singleArray[i];
Or If the above can't be done can I have an example on how to putting dynamic linklabel of array on each row or col to a tablelayoutpanel
Assuming that you are using Windows Forms with a Form called Form1 that contains a TableLayoutPanel called tableLayoutPanel1, here is a working example of how you could achieve what you want:
private void Form1_Load(object sender, EventArgs e)
{
string[] singleArray = File.ReadAllLines("path to your file");
// alternatively, for testing:
//string[] singleArray = {"http://www.google.com", "http://www.microsoft.com", "http://www.stackoverflow.com"};
tableLayoutPanel1.ColumnCount = 1;
tableLayoutPanel1.RowCount = singleArray.Length;
for (int i = 0; i < singleArray.Length; i++)
{
// create a new link label and set some properties
LinkLabel linkLabel = new LinkLabel();
linkLabel.Text = "I am link label #" + i;
LinkLabel.Link linkLabelLink = new LinkLabel.Link();
linkLabelLink.LinkData = singleArray[i];
// tell the label to open a browser upon a click
linkLabel.LinkClicked += (o, args) => System.Diagnostics.Process.Start(args.Link.LinkData as string);
linkLabel.Links.Add(linkLabelLink);
// add new link label to row i in colum 0
tableLayoutPanel1.Controls.Add(linkLabel, 0, i);
}
}

Carrying user inputs from a textbox from one form to another

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());

creat eventHandler for label array element

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
}
}

Categories

Resources