Click here to Skip to main content
15,887,256 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.
 
Share this answer
 
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
[no name] 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.

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