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);
}
deceptikon
Challenge Accepted
3,445 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56
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);
deceptikon
Challenge Accepted
3,445 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56
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.
deceptikon
Challenge Accepted
3,445 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56
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".
deceptikon
Challenge Accepted
3,445 posts since Jan 2012
Reputation Points: 822
Solved Threads: 473
Skill Endorsements: 56
You could use Math.Sign(value) if the return does not equal 1 make the value 0.
tinstaafl
Nearly a Posting Virtuoso
1,326 posts since Jun 2010
Reputation Points: 355
Solved Threads: 229
Skill Endorsements: 14
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");.
tinstaafl
Nearly a Posting Virtuoso
1,326 posts since Jun 2010
Reputation Points: 355
Solved Threads: 229
Skill Endorsements: 14