|
I would do it like this :
Outside your UserControl you have to check if all fields are filled (and perhaps if all information is valid) - because the Information is outside the UserControl.
Your UserControl gets a Property which give this (boolean) Information to it.
This Check could be done each time one of those fields is changed ... or with an Event which comes from the UserControl. But I prefer the 1st ...
|
|
|
|
|
Hi, Ralf. What to do so when the user clicks on the Save buttons it first verify the fields? I mean: the verification must ocur just when the user clicks on the Save buttons. You know how to do this?
Thanks.
|
|
|
|
|
Hi ...
in my opinion it would be not so good to generally allow the using of the Save-Button if the state of the data-field is not checked. In my applications I prefered allways the way of "check input when changes" and give a valid-information (and allow further action).
If you want to do it the other way round you should give an event to "outside" of your UserControl to do the check and the Check itself has to start (when all data is valid) perhaps a public method inside your UserControl which does the action (or raise another Event). What should happen if the Check isn't valid ? Who should give the Message to the User - the UserControl ? Or a Messagebox from the Ckeck-method ?
You see : there are serverall ways to do this ...
|
|
|
|
|
Hi, Ralf.
I used your idea and it's working fine. The only (minor) problem is that the system can't show the user a message to tell him what to correct. Anyway, as I said, is a minor problem. If you have any sugestion about this, please let me know. And thank you for your help.
|
|
|
|
|
Perhaps I have ...
Give me some more details about your project ...
Basicly the Textbox (or whereever you do the input) "knows" what kind of data is required. Here you can decide directly what is wrong ...
But perhaps you write some more ...
|
|
|
|
|

Hi, Ralf.
My system has a main form which is used as an MDI Parent. From it the user can access many other screens to control the system. Some of these screens make the registration of clients, suppliers, banks and others. In these registration screens, there are fields connected to columns in tables. When fields are mandatory, the system must verify what was written there. That’s my problem. At this moment this verification is working well, but it does not warn the user about a wrong entry or an empty field which should be filled. My User Control now has the following code:
private void SalvarButton_Click(object sender, EventArgs e)
{
if (PermSalvar)
{
ClickSalvar?.Invoke(this, e);
EmEdicao = false;
}
}
I created a variable (PermSalvar) which indicates whether the data can be saved. In the main program there is a routine named ValidaCampos which is called in the leave moment of the fields of interest, as you can see below.
private void ValidaCampos()
{
if (tb_Nome.Text == "")
botoes1.PermSalvar = false;
else
botoes1.PermSalvar = true;
if ((tb_Id.Text == "") || (!int.TryParse(tb_Id.Text, out int cp)))
botoes1.PermSalvar = false;
else
botoes1.PermSalvar = true;
}
private void tb_Nome_Leave(object sender, EventArgs e)
{
ValidaCampos();
}
private void tb_Id_Leave(object sender, EventArgs e)
{
ValidaCampos();
}
So, if you could give me any advice, it would be very welcome.
Thank you.
|
|
|
|
|
I question the validity of logic that flags an error (and continues) and then turns it off under another condition further on.
"Initialize" once.
if (tb_Nome.Text == "")
botoes1.PermSalvar = false;
else
botoes1.PermSalvar = true;
if ((tb_Id.Text == "") || (!int.TryParse(tb_Id.Text, out int cp)))
botoes1.PermSalvar = false;
else
botoes1.PermSalvar = true;
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it.
― Confucian Analects: Rules of Confucius about his food
|
|
|
|
|
I test al fields that must be filled. In one case in this form, the contents must be integer. If all fields are correct the variable PermSalvar receives a true value. If at least one of them is not correctly filled, that variable (which is inside the User Control and has false as its default value) remains false.
And the Save action only performs if the variable is true.
Do you have any sugestion? Plase tell me.
Thanks.
|
|
|
|
|
Some time ago I created an application with a similar behaviour (normally I create HMI-applications for machines).
I found that the users sometimes entered senseless data into a Textbox. So I created (with the TextChanged-Event) a method to those Textboxes which data has a special format (for example : it must have a legal date or an Integer value or a Phone-number or ...) and give a User-message (MessageBox) to correct this. In the same time I disable the Save-Button.
Perhaps this approach helps you ...
|
|
|
|
|
I tried your suggestion some time ago. It worked fine, but I could not prevent the buttons to change their enable status. That’s why I’m trying something different.
I continue accepting suggestions and thanks.
|
|
|
|
|
Why don't you can disable the Button ?
Please explain ...
|
|
|
|
|
Enable and disable the buttons depend on which buttons was pressed. When the user enters the screen, the Save and Cancel buttons are disabled. The other 3 are enabled. When user presses New or Edit buttons, the enable state changes. It's programmed in the User Control, which is generic.
|
|
|
|
|
There is no contradiction betwenn my suggestion and your realisation ...
If your UserControl has one (or more) Property, which gives this further information to it (for example "the entered data is not valid") you can add all the behaviour you want to have. If you want to work with an UserControl for those functionality it only depends on the "interface" to the rest of the application.
|
|
|
|
|
$(document).ready(function () {
$('.box-item').draggable({
cursor: 'move',
helper: "clone"
});
});
function GetActions() {
$.ajax({
url: "/Service.asmx/GetList",
type: "GET",
dataType: "json",
data: {
},
contentType: "application/Json; Charset= Utf-8",
success: function (data) {
var list = "";
$.each(data.d[0][0], function (index, item) {
list += "<div itemid='" + item.Id + "' class='card card-body
box-item'>" + item.Value + "</div>";
});
$("#container1").html(list);
},
error: function (response) {
alert(response);
}
});
}
the box item not using in my jquery function ? some help
|
|
|
|
|
And what does this have to do with C#?
Use the right forum, or ask here: Ask a question[^]
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
I want to add after adding, I had used the datagriedview which works well but for other reasons of redu I decided to use a listview but with this code it adds me that after the second addition, it is to say when I make a first addition, my textbox which receives the sum remains 0 and at the second addition it takes the value of the first and at the third it adds the first and the second, it does not add all the additions, and I do not that's not why. I need an explanation to review what is wrong in the code
for (int i = 0; i < listView1.Items.Count; i++)
{
total_achat += Convert.ToDecimal(listView1.Items[i].SubItems[4].Text);
}
Lbl_Affich_TotalCmd.Text = total_achat.ToString();
|
|
|
|
|
We can't tell: we don't have access to your code or data while it is running.
So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.
Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.
Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
ok thank you i'm getting started
|
|
|
|
|
You're welcome!
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
thank you sir I followed your advice and I saw that the problem came from the fact that at the first addition my variables total_achat and i always remain 0. Frankly I do not know why?.
private void BtnAjouter_Click(object sender, EventArgs e)
{
double total;
decimal total_achat;
if (Lbl_Affich_Designation.Text == "" || Lbl_Affich_PrixUnitaire.Text == "" || TxtQteCmd.Text == "" || Lbl_affich_TxtQteStock.Text == "" || CmbRef_Produit.Text == "")
{
MessageBox.Show("Rassurez vous que tous les champs ont bien été rempli.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (CmbRef_Produit.Text != "")
{
double a = 0;
double b = 0;
double c;
int nbprod;
double.TryParse(TxtQteCmd.Text.Trim(), out a);
double.TryParse(Lbl_affich_TxtQteStock.Text.Trim(), out b);
double.TryParse(Lbl_Affich_PrixUnitaire.Text.Trim(), out c);
if (a <= b)
{
total = a * c;
total_achat = 0;
nbprod = 0;
try
{
using (OleDbCommand cmd = d.sql_con.CreateCommand())
{
cmd.CommandText = "INSERT INTO Detail_temp (ref_det, qute_det, Designation, Prix_unitaire_HT, Prix_total_HT) VALUES (@ref_det,@qute_det,@Designation,@Prix_unitaire_HT,@Prix_total_HT)";
cmd.Parameters.AddWithValue("@ref_det", CmbRef_Produit.Text);
cmd.Parameters.AddWithValue("@qute_det", TxtQteCmd.Text);
cmd.Parameters.AddWithValue("@Designation", Lbl_Affich_Designation.Text);
cmd.Parameters.AddWithValue("@Prix_unitaire_HT", Lbl_Affich_PrixUnitaire.Text);
cmd.Parameters.AddWithValue("@Prix_total_HT", total);
d.sql_con.Open();
cmd.ExecuteNonQuery();
dataGridView1.DataSource = d.DT;
cmd.Dispose();
d.sql_con.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
try
{
LoadDB();
for (int i = 0; i < listView1.Items.Count; i++)
{
total_achat += Convert.ToDecimal(listView1.Items[i].SubItems[4].Text);
nbprod++;
LblNbProd.Text = nbprod.ToString();
}
Lbl_Affich_TotalCmd.Text = total_achat.ToString();
Ajouter(CmbRef_Produit.Text, TxtQteCmd.Text, Lbl_Affich_Designation.Text, Lbl_Affich_PrixUnitaire.Text, total.ToString());
Vider();
TxtQteCmd.Text = "1";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
d.sql_con.Close();
}
}
else
{
MessageBox.Show("Veuillez verifier le stock du produit!!!", "Problème avec le stock", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("Caractères non autorisés", "Problème de saisie", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
with the datagridview I have no problem.
private void BtnAjouter_Click(object sender, EventArgs e)
{
double total;
decimal total_achat;
if (Lbl_Affich_Designation.Text == "" || Lbl_Affich_PrixUnitaire.Text == "" || TxtQteCmd.Text == "" || Lbl_affich_TxtQteStock.Text == "" || CmbRef_Produit.Text == "")
{
MessageBox.Show("Rassurez vous que tous les champs ont bien été rempli.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (CmbRef_Produit.Text != "")
{
double a = 0;
double b = 0;
double c;
int nbprod;
double.TryParse(TxtQteCmd.Text.Trim(), out a);
double.TryParse(Lbl_affich_TxtQteStock.Text.Trim(), out b);
double.TryParse(Lbl_Affich_PrixUnitaire.Text.Trim(), out c);
if (a <= b)
{
total = a * c;
total_achat = 0;
nbprod = 0;
try
{
using (OleDbCommand cmd = d.sql_con.CreateCommand())
{
cmd.CommandText = "INSERT INTO Detail_temp (ref_det, qute_det, Designation, Prix_unitaire_HT, Prix_total_HT) VALUES (@ref_det,@qute_det,@Designation,@Prix_unitaire_HT,@Prix_total_HT)";
cmd.Parameters.AddWithValue("@ref_det", CmbRef_Produit.Text);
cmd.Parameters.AddWithValue("@qute_det", TxtQteCmd.Text);
cmd.Parameters.AddWithValue("@Designation", Lbl_Affich_Designation.Text);
cmd.Parameters.AddWithValue("@Prix_unitaire_HT", Lbl_Affich_PrixUnitaire.Text);
cmd.Parameters.AddWithValue("@Prix_total_HT", total);
d.sql_con.Open();
cmd.ExecuteNonQuery();
dataGridView1.DataSource = d.DT;
cmd.Dispose();
d.sql_con.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
try
{
LoadDB();
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
total_achat += Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);
nbprod++;
LblNbProd.Text = nbprod.ToString();
}
Lbl_Affich_TotalCmd.Text = total_achat.ToString();
Ajouter(CmbRef_Produit.Text, TxtQteCmd.Text, Lbl_Affich_Designation.Text, Lbl_Affich_PrixUnitaire.Text, total.ToString());
Vider();
TxtQteCmd.Text = "1";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
d.sql_con.Close();
}
}
else
{
MessageBox.Show("Veuillez verifier le stock du produit!!!", "Problème avec le stock", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("Caractères non autorisés", "Problème de saisie", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
|
|
|
|
|
So use the debugger to find out!
Start by breakpointing the line itself:
total_achat += Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value);
When it hits for the first time, the total and the loop index should both be zero because that is what your code told them to be:
total_achat = 0;
...
for (int i = 0; i < dataGridView1.Rows.Count; i++) After it, you will have added the cell value, so step over that line.
What's the value of the total now? If it's still zero, then it means that this code evaluates to zero:
Convert.ToDecimal(dataGridView1.Rows[i].Cells[4].Value) So look at why that is: what's the value of dataGridView1.Rows[i].Cells[4].Value - use the debugger to find out by firstly hovering over word "Value" and if that looks "right" then adding the calculation to teh Watch pane.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
when I put the breakpoint on total buy + = Convert.ToDecimal (dataGridView1.Rows [i] .Cells [4] .Value); total_achat displays 0.
|
|
|
|
|
Yes, because the line hasn't been executed yet. Breakpoints stop at the beginning of the line, not the end.
Step over the line, (as opposed to step into) and see if it changes.
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
"Common sense is so rare these days, it should be classified as a super power" - Random T-shirt
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
sir sorry and thank you for seeing my error after a review of my code.
I don't know how to explain it to you but I will illustrate it as a comment in my code.
|
|
|
|
|
private void Ajouter (String refProd, String quantite, String design, String prixU, String prixTtc)
{
String[] row = { refProd, quantite, design, prixU, prixTtc };
ListViewItem item = new ListViewItem(row);
listView1.Items.Add(item);
}
private void BtnAjouter_Click(object sender, EventArgs e)
{
double total;
decimal total_achat;
if (Lbl_Affich_Designation.Text == "" || Lbl_Affich_PrixUnitaire.Text == "" || TxtQteCmd.Text == "" || Lbl_affich_TxtQteStock.Text == "" || CmbRef_Produit.Text == "")
{
MessageBox.Show("Rassurez vous que tous les champs ont bien été rempli.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
if (CmbRef_Produit.Text != "")
{
double a = 0;
double b = 0;
double c;
int nbprod;
double.TryParse(TxtQteCmd.Text.Trim(), out a);
double.TryParse(Lbl_affich_TxtQteStock.Text.Trim(), out b);
double.TryParse(Lbl_Affich_PrixUnitaire.Text.Trim(), out c);
if (a <= b)
{
total = a * c;
total_achat = 0;
nbprod = 0;
try
{
using (OleDbCommand cmd = d.sql_con.CreateCommand())
{
cmd.CommandText = "INSERT INTO Detail_temp (ref_det, qute_det, Designation, Prix_unitaire_HT, Prix_total_HT) VALUES (@ref_det,@qute_det,@Designation,@Prix_unitaire_HT,@Prix_total_HT)";
cmd.Parameters.AddWithValue("@ref_det", CmbRef_Produit.Text);
cmd.Parameters.AddWithValue("@qute_det", TxtQteCmd.Text);
cmd.Parameters.AddWithValue("@Designation", Lbl_Affich_Designation.Text);
cmd.Parameters.AddWithValue("@Prix_unitaire_HT", Lbl_Affich_PrixUnitaire.Text);
cmd.Parameters.AddWithValue("@Prix_total_HT", total);
d.sql_con.Open();
cmd.ExecuteNonQuery();
dataGridView1.DataSource = d.DT;
Ajouter(CmbRef_Produit.Text, TxtQteCmd.Text, Lbl_Affich_Designation.Text, Lbl_Affich_PrixUnitaire.Text, total.ToString());
cmd.Dispose();
d.sql_con.Close();
}
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
try
{
LoadDB();
for (int i = 0; i < listView1.Items.Count; i++)
{
total_achat += Convert.ToDecimal(listView1.Items[i].SubItems[4].Text);
nbprod++;
LblNbProd.Text = nbprod.ToString();
}
Lbl_Affich_TotalCmd.Text = total_achat.ToString();
Vider();
TxtQteCmd.Text = "1";
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
d.sql_con.Close();
}
}
else
{
MessageBox.Show("Veuillez verifier le stock du produit!!!", "Problème avec le stock", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("Caractères non autorisés", "Problème de saisie", MessageBoxButtons.OK, MessageBoxIcon.Warning);
}
}
}
|
|
|
|
|