I created a software in Windows Form Application that includes two Forms.
In the form1 there are some textboxes and a listbox.
One of textboxes i populated in a listbox with followin method.
the name of textbox is xhirollogariaBox

             Random rasti = new Random();
            int numri = rasti.Next(10000000, 99999999);
            xhiro = "1301" + numri;
            xhirollogariaBox.Text = Convert.ToString(xhiro); 

The problem is that i don't know how to make a method or a solution to prevent the future number not to be thw same as they are in thelistbox.
So how to make a solution to prevent next entries (numbers) to be writen if the same number already exists in the listbox.

Also one other problem is that I don't know how to prevent another textbox to include numbers, probably that is something like regular expressions or whatever.

Thank you in advance for your reply.

Well, an obvious first attempt is to loop through the items in the listbox and not update your textbox if the random number already exists:

xhiro = "1301" + numri;

bool exists = false;

foreach (var item in listbox.Items)
{
    if (item == xhiro)
    {
        exists = true;
        break;
    }
}

if (!exists)
    xhirollogariaBox.Text = Convert.ToString(xhiro);

Also one other problem is that I don't know how to prevent another textbox to include numbers, probably that is something like regular expressions or whatever.

If you only want to allow numbers then a numericupdown control makes more sense. But a quick and dirty solution for regular textboxes is simply ignoring non-numeric digits in the KeyPress event:

private void textBox_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = !char.IsDigit(e.KeyChar);
}

Thank you very much for your reply, honestly i dod fixed the problem, but I can't fix the following one:

 public void bilanci()
        {
            double vlera = 0;

            foreach (string stringu in GjendjaListBox.Items)
            {
                vlera = vlera + Convert.ToDouble(stringu.Split('\t')[4]);
            }
            this.GjendjaButon.Text = "Gjendja: " + vlera;
        }

How to make this to not accept values less than zero. With other words if I press a button to draw a value that this "vlera" amount goes to minus values.
If I press Add buton I can increase the value of "vlera", if a press draw button I can decrease the value of "vlera".
How to make this to not draw values less than zero.
Cheers

Well, the ideal solution would be to disallow negative values in the first place, but a quick fix would be to take the absolute value of vlera so that it's never negative:

GjendjaButon.Text = "Gjendja: " + Math.Abs(vlera);

I'm writing the text in a text file using following method:

 string data = Convert.ToString(DateTime.Now);
                StreamWriter swGjendja = new StreamWriter(caku, true);
                swGjendja.WriteLine(data + "\t" + emriBox.Text + "\t"
                                    + mbiemriBox.Text + "\t"
                                    + xhirollogariaBox.Text + "\t-"
                                    + vleraBox.Text + "\t"
                                    + "Terheqje");
                swGjendja.Close();
                LexoGjendjenFajll();
                bilanci();

Also I'm reading from file with this method:

public void LexoGjendjenFajll()
         {
            GjendjaListBox.Items.Clear();
            List<string> linja = new List<string>();

            string strLinja = "";
            StreamReader srGjendjen = new StreamReader(caku, true);
            try
            {
                while ((strLinja = srGjendjen.ReadLine()) != null)
                {
                    //Paraqitja e shenimeve për personin e caktuar
                    if (strLinja.Split('\t')[1] == this.emriBox.Text && strLinja.Split('\t')[2] == 
                        this.mbiemriBox.Text && strLinja.Split('\t')[3] == this.xhirollogariaBox.Text)
                    {
                        GjendjaListBox.Items.Add(strLinja);
                    }
                }
                srGjendjen.Close();
            }
            catch
            { }
        }

I think that the solution must be different as you gave me.
Cheers

I think that the solution must be different as you gave me.

Sorry dude, but you keep showing me different code as an example of your problem. At this point I have no clue what you want.

All I want to say is if you want to draw an amount from "vlera" that the result is a negative value, you could not be able to draw that amount.
The Math.abs is not a solution, because the negative values converts to positive ones. So is there a way to say that if the result is less than zero, to recieve a message that you can not draw that amount because you dont have credits (just like in prepaid SIM cards).

So is there a way to say that if the result is less than zero, to recieve a message that you can not draw that amount because you dont have credits (just like in prepaid SIM cards).

Yes. Check the value and put up a message if someone tries to draw more from an account than the current balance. This is what I meant originally by saying "the ideal solution would be to disallow negative values in the first place".

You could use Math.Sign(value) if the return does not equal 1 make the value 0.

I still haven't found a solution with folloving:

  public void bilanci()
    {
        double vlera = 0;

            foreach (string stringu in GjendjaListBox.Items)
            {

            vlera = vlera + Convert.ToDouble(stringu.Split('\t')[4]);
            }
        this.GjendjaButon.Text = "Gjendja: " + vlera;
    }

Please give me a code.

this might do it:

     public void bilanci()
    {
        double vlera = 0;
        foreach (string stringu in GjendjaListBox.Items)
        {
            vlera = vlera + Convert.ToDouble(stringu.Split('\t')[4]);
        }
        if(Math.Sign(vlera)<> 1)
        {
            vlera=0;
        }
        this.GjendjaButon.Text = "Gjendja: " + vlera;
    }

If vlera is a negative value or 0 then you can set it to 0, or you can show a messagebox with a message(MessageBox.Show("Not Enough Credit");.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.