1,105,578 Community Members

Having problem on listbox with an argument

Member Avatar
dr_iton
Light Poster
33 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.

Member Avatar
deceptikon
Eternally Awesome
4,693 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 689 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

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);
}
Member Avatar
dr_iton
Light Poster
33 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
deceptikon
Eternally Awesome
4,693 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 689 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

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);
Member Avatar
dr_iton
Light Poster
33 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
deceptikon
Eternally Awesome
4,693 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 689 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

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.

Member Avatar
dr_iton
Light Poster
33 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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).

Member Avatar
deceptikon
Eternally Awesome
4,693 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 689 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

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".

Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 403 [?]
Skill Endorsements: 36 [?]
 
0
 

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

Member Avatar
dr_iton
Light Poster
33 posts since Dec 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
-1
 

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.

Member Avatar
tinstaafl
Postaholic
2,012 posts since Jun 2010
Reputation Points: 559 [?]
Q&As Helped to Solve: 403 [?]
Skill Endorsements: 36 [?]
 
0
 

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");.

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: