J'ai des zones de texte Quantité, Prix et Total, toutes générées dynamiquement.
Je ne sais pas comment les calculer sur leur événement de changement de texte.
J'ai créé des événements TextChanged pour txtBox3 et txtBox4 qui sont respectivement la quantité et le prix. Je ne sais pas comment les capturer, les multiplier et les afficher dans une zone de texte totale générée dynamiquement.
TextBox txtBox3 = new TextBox();
txtBox3 = new TextBox();
txtBox3.Location = new Point(313, position);
txtBox3.Visible = true;
txtBox3.Name = "txt_QTY" + qtyTextbox;
txtBox3.TextChanged += txtBox3_TextChanged;
txtBox3.KeyPress += txtBox3_KeyPress;
qtyTextbox++;
TextBox txtBox4 = new TextBox();
txtBox4 = new TextBox();
txtBox4.Location = new Point(447, position);
txtBox4.Visible = true;
txtBox4.Name = "txt_Price" + priceTextbox;
txtBox4.TextChanged += txtBox4_TextChanged;
txtBox4.KeyPress += txtBox4_KeyPress;
priceTextbox++;
TextBox txtBox5 = new TextBox();
txtBox5 = new TextBox();
txtBox5.Location = new Point(556, position);
txtBox5.Visible = true;
txtBox5.Name = "txt_Total" + totalTextbox;
totalTextbox++;
panel1.Controls.Add(txtBox3);
panel1.Controls.Add(txtBox4);
panel1.Controls.Add(txtBox5);
private void txtBox4_TextChanged(object sender, EventArgs e)
{
}
private void txtBox3_TextChanged(object sender, EventArgs e)
{
}
Comment puis-je calculer la quantité à partir de la zone de texte dynamique * Prix de la zone de texte dynamique et l'afficher dans la zone de texte Total générée dynamiquement sur leur événement de changement de texte.
3 Réponses :
Tout d'abord, implémentez une fonction de contrôle de recherche, par exemple:
TextBox txtTotal = (TextBox) this.GetControlByName(this, "txt_Total" + totalTextbox);
TextBox txtPrice = (TextBox) this.GetControlByName(this, "txt_Price" + priceTextbox);
Ensuite, vous pouvez obtenir la zone de texte totale:
public Control GetControlByName(Control ParentCntl, string NameToSearch)
{
if (ParentCntl.Name == NameToSearch)
return ParentCntl;
foreach (Control ChildCntl in ParentCntl.Controls)
{
Control ResultCntl = GetControlByName(ChildCntl, NameToSearch);
if (ResultCntl != null)
return ResultCntl;
}
return null;
}
Merci pour votre réponse rapide à ce sujet. J'ai juste essayé ce code mais il ne renvoie jamais "ResultCntl". Fondamentalement, je veux afficher le total du prix qté * dans la zone de texte totale.
J'ai ajouté une nouvelle réponse!
Vous devez ajouter votre contrôle dynamique dans la collection de contrôles
int controlCounts = 0;
private void addControls_Click(object sender, EventArgs e)
{
controlCounts++;
TextBox txt_QTY = new TextBox();
txt_QTY.Location = new Point(100 * controlCounts, 100);
txt_QTY.Name = "txt_QTY" + controlCounts;
Controls.Add(txt_QTY);
txt_QTY.TextChanged += txt_QTY_TextChanged;
TextBox txt_Price = new TextBox();
txt_Price.Location = new Point(100 * controlCounts, 200);
txt_Price.Name = "txt_Price" + controlCounts;
Controls.Add(txt_Price);
txt_Price.TextChanged += txt_Price_TextChanged;
TextBox txt_Total = new TextBox();
txt_Total.Location = new Point(100 * controlCounts, 300);
txt_Total.Name = "txt_Total" + controlCounts;
Controls.Add(txt_Total);
}
private void txt_QTY_TextChanged(object sender, EventArgs e)
{
TextBox txt_QTY = (TextBox)sender;
string index = txt_QTY.Name.Substring("txt_QTY".Length);
updateTotal(index);
}
private void txt_Price_TextChanged(object sender, EventArgs e)
{
TextBox txt_Price = (TextBox)sender;
string index = txt_Price.Name.Substring("txt_Price".Length);
updateTotal(index);
}
private void updateTotal(string index)
{
TextBox txt_QTY = (TextBox)Controls["txt_QTY" + index];
TextBox txt_Price = (TextBox)Controls["txt_Price" + index];
TextBox txt_Total = (TextBox)Controls["txt_Total" + index];
if ((txt_QTY.Text != "") && (txt_Price.Text != ""))
{
txt_Total.Text = (Convert.ToInt32(txt_QTY.Text) * Convert.ToInt32(txt_Price.Text)).ToString();
}
}
oui je les ajoute à la collection de contrôles, j'ai oublié de le mentionner dans ma question. J'ai besoin de calculer le nombre de zones de texte et non pas une seule. lorsque je crée des zones de texte, je change leur valeur Name par txtBox4.Name = "txt_Price" + priceTextbox;
(TextBox) Les contrôles ["txt_QTY" + index] me donnent null sous updateTotal. il montre cependant la valeur d'index correcte.
désolé, vous devez mettre à jour son nom avant de l'ajouter à la collection de contrôles. J'ai mis à jour ma réponse
Voici une version complète:
//Add tag
String tag = "";
TextBox txtBox3 = new TextBox();
txtBox3 = new TextBox();
txtBox3.Location = new Point(313, position);
txtBox3.Visible = true;
txtBox3.Name = "txt_QTY" + qtyTextbox;
txtBox3.TextChanged += txtBox3_TextChanged;
txtBox3.KeyPress += txtBox3_KeyPress;
tag = "txt_QTY" + qtyTextbox;
qtyTextbox++;
TextBox txtBox4 = new TextBox();
txtBox4 = new TextBox();
txtBox4.Location = new Point(447, position);
txtBox4.Visible = true;
txtBox4.Name = "txt_Price" + priceTextbox;
txtBox4.TextChanged += txtBox4_TextChanged;
txtBox4.KeyPress += txtBox4_KeyPress;
tag += "," + "txt_Price" + priceTextbox;
priceTextbox++;
TextBox txtBox5 = new TextBox();
txtBox5 = new TextBox();
txtBox5.Location = new Point(556, position);
txtBox5.Visible = true;
txtBox5.Name = "txt_Total" + totalTextbox;
totalTextbox++;
tag += "," + "txt_Total" + totalTextbox;
//Set same tag into three 3 textbox
txtBox3.Tag = txtBox4.Tag = txtBox5.Tag = tag;
Add a parse function:
private TextBox[] getTextBoxFromTag(String Tag)
{
TextBox [] arrTextBox = new [3] TextBox();
String arrTag[] = Tag.Split(",");
//Harcode
arrTextBox[0] = GetControlByName(this, arrTag[0]);
arrTextBox[1] = GetControlByName(this, arrTag[1]);
arrTextBox[2] = GetControlByName(this, arrTag[2]);
return arrTextBox;
}
public Control GetControlByName(Control ParentCntl, string NameToSearch)
{
if (ParentCntl.Name == NameToSearch)
return ParentCntl;
foreach (Control ChildCntl in ParentCntl.Controls)
{
Control ResultCntl = GetControlByName(ChildCntl, NameToSearch);
if (ResultCntl != null)
return ResultCntl;
}
return null;
}
void updateTotal(object sender){
String tag = ((TextBox)sender).Tag;
TextBox [] txt = getTextBoxFromTag(tag);
txt[2].Text = Convert.ToInt32(txt[0].Text) * Convert.ToInt32(txt[1].Text);
}
private void txt_QTY_TextChanged(object sender, EventArgs e)
{
updateTotal(sender);
}
private void txt_Price_TextChanged(object sender, EventArgs e)
{
updateTotal(sender);
}
GetControlByName (arrTag [0]); me donne une erreur en disant qu'il a besoin de 2 arguments. que devrais-je faire?
Je viens de modifier le code, ajoutez "ceci" au premier paramètre.
n'imprime toujours pas la valeur dans la zone de texte totale. il revient avec une valeur mais une erreur de référence d'objet sur arrTextBox [0] = GetControlByName (this, arrTag [0]);
Avez-vous ajouté des contrôles au formulaire comme: this.controls.add (txtBox3); this.controls.add (txtBox4); this.controls.add (txtBox5)?
oui j'ajoute des contrôles et ils apparaissent lorsque je les génère
Avez-vous défini un point d'arrêt dans la fonction getTextBoxFromTag pour vous assurer que l'analyseur et GetControlByName fonctionnent correctement? PS: j'ai écrit le code sans tester;)
GetControlByName et getTextBoxFromTag obtiennent tous deux une valeur. J'ai accepté la réponse de Masoud car elle fonctionnait bien. Merci de votre aide.