14 Replies - 899 Views - Last Post: 04 October 2012 - 08:15 AM Rate Topic: -----

#1 czarmaiyne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-October 12

how to change backcolor when the value is negative.

Posted 04 October 2012 - 05:07 AM

hello there, can i ask help from you?

so, here my code. i want the item to be highlighted when its value is negative simultaneously when the value is added to the listbox.

what i have done is i add a button to highlight the item coz i dont know what codes i will use when i want to highlight it automatically after the value has been added.

how could this be done?

thank you! :)

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: how to change backcolor when the value is negative.

#2 czarmaiyne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-October 12

Re: how to change backcolor when the value is negative.

Posted 04 October 2012 - 05:13 AM

consider the second code:

disregard the first one.

Attached image(s)

  • Attached Image

Was This Post Helpful? 0
  • +
  • -

#3 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,462
  • Joined: 29-May 08

Re: how to change backcolor when the value is negative.

Posted 04 October 2012 - 06:33 AM

If you use some simple Debugging Skills and step through the code. You should spot a simple error.
Was This Post Helpful? 0
  • +
  • -

#4 czarmaiyne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-October 12

Re: how to change backcolor when the value is negative.

Posted 04 October 2012 - 06:40 AM

error?

that code works. but the logic isn't sufficient to meet what output is suppose to..
Was This Post Helpful? 0
  • +
  • -

#5 PeterH  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 47
  • Joined: 03-September 09

Re: how to change backcolor when the value is negative.

Posted 04 October 2012 - 06:43 AM

Yes the code will run but I think Adam was pointing you to a specific problem in that you are checking against a value which has been previously modified by means of .clear().
Was This Post Helpful? 1
  • +
  • -

#6 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,462
  • Joined: 29-May 08

Re: how to change backcolor when the value is negative.

Posted 04 October 2012 - 06:44 AM

Do like I suggested and step through the code examining the value of each variable (controls included). Do they match what you expect they to contain?
Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: how to change backcolor when the value is negative.

Posted 04 October 2012 - 06:45 AM

The code works but there is a logic error in your code. What do you think would happen if you had more than one item in the list that is < 0 would it be red?
Was This Post Helpful? 0
  • +
  • -

#8 czarmaiyne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-October 12

Re: how to change backcolor when the value is negative.

Posted 04 October 2012 - 06:52 AM

yeah. charlie, that's what i like to happen. all the items being add that is less than 0 would be red.
Was This Post Helpful? 0
  • +
  • -

#9 czarmaiyne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-October 12

Re: how to change backcolor when the value is negative.

Posted 04 October 2012 - 07:01 AM

my first attemp was i only put this code in a button:

lstDisplay.FocusedItems(0).SubItems(0).Backcolor = Color.Red

but then, i have to click first the buttom before the item gets red, which is obviously a wrong logic.

*button
Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: how to change backcolor when the value is negative.

Posted 04 October 2012 - 07:29 AM

OK first, there is a known bug in the listview control where if you change the color of a subitem, it will change the entire row to that color. Now that being said, you can still produce a color based on a value much like this:Attached Image

If that is what you're wanting then you need to build your listviewitem a little differently

Dim c as New ListViewItem
c.Text = txtCode.Text
c.SubItems.Add(txtValue.Text)
If val(txtValue.Text) < 0 then
c.SubItems(0).BackColor = Color.Red
End If


At this point you have created a listviewitem which contains a text column and a subitem column and you specified the background color based on a value

Now all you have to do is add it to the listview
lstDisplay.Items.Add(c)


Edit:
If anyone is interested, here is the Bug Description

This post has been edited by CharlieMay: 04 October 2012 - 07:38 AM

Was This Post Helpful? 0
  • +
  • -

#11 czarmaiyne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-October 12

Re: how to change backcolor when the value is negative.

Posted 04 October 2012 - 07:55 AM

am i going to loop this? coz i want every value being added when its negative it will automaticaly becomes red..
Was This Post Helpful? 0
  • +
  • -

#12 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: how to change backcolor when the value is negative.

Posted 04 October 2012 - 08:02 AM

?
Not sure what you mean.

When you press the button, you add the value
If it's < 0 then the entry is red
then it waits for the next button click to add the next value

Not sure where looping comes in to play here.
Was This Post Helpful? 0
  • +
  • -

#13 czarmaiyne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-October 12

Re: how to change backcolor when the value is negative.

Posted 04 October 2012 - 08:10 AM

oh.. i see now! hehe been confused lately, sorry!
thank you charlie! :)

so i'll just add the whole to the add button, right??

so i'll just add the whole thing to the add button, right??
Was This Post Helpful? 0
  • +
  • -

#14 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: how to change backcolor when the value is negative.

Posted 04 October 2012 - 08:12 AM

You can, and then clear the appropriate textboxes for the next entry.
Was This Post Helpful? 0
  • +
  • -

#15 czarmaiyne  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-October 12

Re: how to change backcolor when the value is negative.

Posted 04 October 2012 - 08:15 AM

copy that charlie, thank you! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1