Click here to Skip to main content
15,028,402 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When i try to show messagebox on a condition , message box also looping on each data adding on listbox . I also gave messagebox outside the loop . still it's the same .
i want to show messagebox only once if condition is true

What I have tried:

C#
var dataReader = sqlDataReader1;
while (dataReader.Read())
{
    string q = listBox1.Items.Count.ToString();
    string a = Settings.Default["outercode"].ToString();

    label4.Text = "(" + q + "/" + a + ")";
    listBox1.Items.Add(dataReader[0]);
    if (Convert.ToInt32(q) < Convert.ToInt32(a))
    {
        //MessageBox.Show("No sufficient Unit codes");
    }
}
Posted
Updated 30-May-21 7:06am
v2
Comments
BillWoodruff 31-May-21 2:54am
   
Until you clarify what you want to achieve, how can we help you ? What works, what doesn't work; what errors, what error messages and, where do errors or unexpected results. What do you expect the runb-time user to do ?

1 solution

You can for example use a boolean variable to indicate that the message is already shown. Something like
C#
var dataReader = sqlDataReader1;
bool messageShown = false;
while (dataReader.Read())
{
   string q = listBox1.Items.Count.ToString();
   string a = Settings.Default["outercode"].ToString();

   label4.Text = "(" + q + "/" + a + ")";
   listBox1.Items.Add(dataReader[0]);
   if (!messageShown && Convert.ToInt32(q) < Convert.ToInt32(a))
   {
      MessageBox.Show("No sufficient Unit codes");
      messageShown = true;
   }
} 


EDIT

Looking at the solution you've posted, it looks like you would like to fill the listbox and if the amount of items is less than your setting then you'd like to show a message box. If that is correct, then the code could look something like

C#
var dataReader = sqlDataReader1;
int requiredAmount = (int)Settings.Default["outercode"];
while (dataReader.Read())
{
   label4.Text = $"({listBox1.Items.Count}/{requiredAmount})";
   listBox1.Items.Add(dataReader[0]);
} 
if (listBox1.Items.Count < requiredAmount) 
{
   MessageBox.Show("No sufficient Unit codes");
}

Note that the code above will cause an exception if the setting is missing or cannot be converted to int so you may want to use Int32.TryParse Method (System) | Microsoft Docs[^] and in general add proper error handling using try-catch - C# Reference | Microsoft Docs[^]

To better understand how your code works, refer to Tutorial: Debug C# code - Visual Studio | Microsoft Docs[^] to better understanding for debugging the code.
   
v3
Comments
Member 15212425 30-May-21 13:14pm
   
but when im giving if condition inside the lopp, im no still gettng the desired solution . if list box count is 2 ,itz only showing one and if p=q , still messagebox working
Wendelius 30-May-21 13:19pm
   
I can't quite understand why you convert number to strings and then back to numbers. Can you explain in detail what is the desired solution you're referring to?
Member 15212425 30-May-21 13:33pm
   
check if condition true and popup messagebox only once . my program showing messagebox each time when data is adding from datareader to lstbox .
i am storing count of listbox to q and another in r . to check integer im converting to numbers. on each adding of elements ,its checking , after full adding i want to check if condition .
i am new in c# programming
Wendelius 30-May-21 13:34pm
   
See the updated answer
Gerry Schmitz 30-May-21 16:17pm
   
If you want to break out of a while loop, use "break". If you don't want to break, use / test a "one time only" (bool) variable / member.
BillWoodruff 31-May-21 2:51am
   
Another long post in response to a very confused OP who has been unable to articulate what his goal is. I guess you just enjoy writing code, rather than helping the OP clarify what their goal is.
Wendelius 1-Jun-21 0:49am
   
Thanks for the comment, it was well-grounded.
BillWoodruff 1-Jun-21 2:49am
   
Kong Fu Zi (Confucius): "I do not enlighten those who are not eager to learn, nor arouse those who are not anxious to give an explanation themselves. If I have presented one corner of the square and they cannot come back to me with the other three, I should not go over the points again.6

The case is like that of someone raising a mound. If he stops working, the fact that it perhaps needed only one more basketful makes no difference; I stay where I am. Whereas even if he has not got beyond leveling the ground, but is still at work, the fact that he has only tilted one basketful of earth makes no difference. I go to help him."

:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900