Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Coloured records in list box

Status
Not open for further replies.

knighty03

Technical User
Aug 12, 2003
32
GB
I have a list box which contains a number of records and I want to be able to select a record in this list and change the colour of that item only. Is there any way to do this as I can only get it where it changes the entire list colour.
 
If you know how to subclass Windows messages you can control the painting durint WM_PAINT. I've never done it with VB but I have with C++.
Code:
LRESULT CSpyDlg::OnDrawItem(UINT uMsg, WPARAM wParam, 
							   LPARAM lParam, BOOL& bHandled)
{
	// LPARAM is a DRAWITEMSTRUCT, initilize a variable for it.
	LPDRAWITEMSTRUCT pdis = (LPDRAWITEMSTRUCT)lParam;

	// Set up listbox colors.
	COLORREF blue = (COLORREF)RGB(0, 0, 255);
	COLORREF red = (COLORREF)RGB(255, 0, 0);
	COLORREF wndcolor = (COLORREF)GetSysColor(COLOR_WINDOW);
	COLORREF hilite = (COLORREF)GetSysColor(COLOR_HIGHLIGHT);
	COLORREF hitext = (COLORREF)GetSysColor(COLOR_HIGHLIGHTTEXT);

	// Create variable to hold the URL.
	char buffer[HUGE_URL] = {0};
	
	// Brush for focus rect.
	HBRUSH hbr;

	// Put URL into buffer variable.
	::SendMessage(m_hwndSpyLst, LB_GETTEXT, 
				 (WPARAM)pdis->itemID, (LPARAM)&buffer );

	// Colors depend on violation status.
	if (m_pParent->CheckURL(buffer) == URL_VIOLATION)
	{
		if (pdis->itemState & ODS_SELECTED) 
		{
			hbr = CreateSolidBrush( hitext );
			SetBkColor( pdis->hDC, hitext );
			SetTextColor( pdis->hDC, hilite );
			
		}
		else
		{
			hbr = CreateSolidBrush(wndcolor); 
			SetBkColor(pdis->hDC, wndcolor);
			SetTextColor( pdis->hDC, red );
		}
	}
	else
	{

		if (pdis->itemState & ODS_SELECTED) 
		{
			hbr = CreateSolidBrush(hilite);
			SetBkColor(pdis->hDC, hilite);
			SetTextColor( pdis->hDC, hitext );
		}
		else
		{
			hbr = CreateSolidBrush(wndcolor); 
			SetBkColor(pdis->hDC, wndcolor);
			SetTextColor( pdis->hDC, blue );
		}

	}
	
	
	FillRect(pdis->hDC, (LPRECT)&pdis->rcItem, hbr);
	
	// Output the string.
	TextOut(pdis->hDC, pdis->rcItem.left, pdis->rcItem.top, 
			buffer, strlen (buffer) );
	
	// Forward the DRAWITEM message to Windows.
	::SendMessage(pdis->hwndItem, WM_DRAWITEM,
				 (WPARAM)pdis->CtlID,(LPARAM)pdis);
	
	return true;

}


VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
Check out
Stephen has a range of samples that monkey around with list boxes. Not many are easy to understand, but there are plenty of samples to work with.

hth

Ben

----------------------------------------------
Ben O'Hara

"Where are all the stupid people from...
...And how'd they get so dumb?"
NoFX-The Decline
----------------------------------------------
 
Interesting note - VB.Net exposes the OnDrawItem() event natively just like C++, so you can extend the built-in listbox to create your own custom control fairly easily.

Access? That's a whole different animal... [hammer]

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
I've not had chance to use VB.Net yet, what are your experiences of it? I've heard all sorts of reports from Spawn of Satan to Sent from the Gods!
What does it do for the 'average' programmer?

Ben

----------------------------------------------
Ben O'Hara

"Where are all the stupid people from...
...And how'd they get so dumb?"
NoFX-The Decline
----------------------------------------------
 
I've been using it mainly to migrate from ASP to ASP.Net, but I've attended Several .Net Summits here in Houston and I've been continuing to use VB.Net for my own continued studies. I love the .Net platform but agree that there will probably be many part-time or hobbyist programmers who never make the journey into using it. There are over 8000 classes available in the .Net framework - who can claim to master them all?

I started my formal schooling with C++ and Java, after having tinkered with VB for several years, so I welcomed the additional horsepower provided by VB.Net. I think the biggest hurdle for VB6 programmers is the amount of "wrapping" that's done to shield them from the underlying mechanics of their code. For example, to use an API function in VB you have to declare the function with its library and alias, make sure you have the necessary types and constants declared, and then call it and hope you don't crash VB. In C++, you just call it. Simple. And you don't have to do callbacks to access windows messages in C++, they're just available to you to do cool things with. In that regard, C++ can actually be easier to use than VB when it comes to doing things "outside the box." That's one reason a good VB coder is probably not given the credit they deserve since they have to jump through hoops to make VB do something that's native to C++.

That said, VB.Net is actually closer to Java than it is to VB6. And if you can master VB.Net, it's a very short jump to C#, which pays more, which is why the latest polls from programmers shows them to be moving to C# rather than learning VB.Net. Some actually fear the demise of VB due to the new complexity that comes with .Net. We have to remember that a large portion of VB programmers have no formal programming training and learned the language by hands-on experimentation and macro recording. I migrated to VB because of the huge business demand for it and its associated RAD turnaround time. I don't consider it "dumbing down" to use VB, I just find VB more enjoyable to work with because it allows me to focus on the business problem rather than spending half a day researching to find out how to call a function that has 8 pointers in its parameter list.

Another thing about VB.Net is the IDE is shared with the other .Net languages so switching between VB.Net and C#.Net is a no-brainer - it's really just a difference in syntax, with C# being less verbose, and a little more elegant to look at.

On the plus side, since Microsoft did so much work on the new classes in the .Net framework, you can do things with one line of code that took dozens of lines in VB6. The IDE is awesome for database work, and is almost like having SQL Server built right into it. I think that was the main goal of the design team anyway, since most apps are data-centric in nature. You don't see a lot of 3D-shooters written in VB do you?

What I'm waiting to see is what they're going to do with the huge base of VBA programmers out here. Are we headed toward VBA.Net? What then? Is the average Joe Macro out there going to be able to migrate to an XML-bloated VBA.Net environment. I think not. However, I also think that's a good thing for full-time programmers. There's a push right now to form a real structure in the IT hierarchy so when someone puts "Programmer / Analyst" on their resume they actually have the training and certifications to back it up. The Y2K bug brought thousands of people into the IT world who might not have been there otherwise. The result, a bloated IT workforce where it's hard to determine who is qualified to do what because there isn't a defined standard. Microsoft actually has a position I saw advertised called "Software Evangelist!" How do you become that? Is there a course at Carnegie Mellon for Evangelist Certification? In comparison, when someone becomes a lawyer, engineer or doctor, we know they have taken the necessary steps to get there.

I saw a recent article that predicted that 20% of today's progammers would have to switch careers by 2005, due to this re-structuring and the flow of jobs overseas, primarily in India. That's one reason to get out your thinking cap and get good with .Net as soon as possible. Microsoft has announced the end of VB6 certification testing after next year - you know that's a bad sign for VB6 people.

It seems to me that the trend now is to hire people with specific industry knowledge rather than people who know syntax rules. Anybody can read a reference manual and figure out a syntax rule. A programming language is no different than a spoken language. I learned Spanish in High School in much the same way I learned VB. I applied it as I had the need for it until I got comfortable with it. I think programmers tend to get haughty because they can program a certain language or two, but how valuable are you if you don't know anything about the business you work for? Today, you need to be a jack-of-all-trades, doing everything from analysis, to coding, to testing, to documentation and help systems, to graphics, to packaging, to tech support, to ... well you get the point. There are so many engineers and other professionals who moved into programming that a pure programmer is at a disadvantage in the business market. What needs to happen is for IT positions to get re-defined so a programmer is a programmer, an analyst is an analyst, an architect is an architect, and so on. It appears that programmers have become more of a blue-collar commodity these days, which means no money. Even 15 year old kids can program in VB, but what do they know about petroleum refinement, or banking, or medicine? The key to becoming valuable is learning an industry, not a language.

What was the question? [bugeyed]

Disclaimer
The opinions expressed here are the sole responsibility of the author and have no warranty implied or otherwise. Oh...(pause)...the political-correctness of it all...


VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
Maybe what drives the 'Joe Macros' out there who are in business is the knowledge of their industry first, and seeing the benefits of improved systems by way of eaily accessible databases with programming capabilities such as VBA.

Brad 'Joe Macro' Stevens
 
I've done most of my VB development work on projects that were the brain-children of industry professionals who knew their business, knew enough vba to create a prototype of some kind, and then looked for help in moving it to the next level. There are tons of "spread-bases" out there (spreadsheets being used as databases) that would have been implemented as databases from the start if the people developing them weren't learning as they go. That's where the problem is. The people who know the business rules should be doing analysis work - not coding. The technical people who know how to mix bits should be coding - not talking to secretaries about their job duties. Could this problem be attributed to the simplicity of VBA?

Interesting...

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
From my own experience, it was fairly easy to get up and running with an MS Access DB for my own business that has gradually expanded and improved as my own skills and understanding of a DB's usefulness improved.

Good for me, as the application is doing what I want it to do as well as having immediate flexibility, but has this done someone out of a job?

Would Microsoft be happy to hear this discussion about traditional boundaries being moved?
 
My observation has been that people are either fascinated with computers or they tolerate them. Before everyone had a desktop PC, our DP (data-processing) department was a small group of nerds who sat in a dark room listening to the IBM mainframes humming while they talked about Dungeons and Dragons. Nobody was interested in what they were doing. The bosses were bossing and the secretaries were making coffee and everything was groovy.

Then came the dumb terminals. Not much changed then. They wouldn't do anything other than what they were supposed to do: just business.

Then came smart terminals - big problem. Now everybody is too busy to do anything because they have their noses stuck to the monitor all day playing with everything from stock prices to sports scores to trying to become a part-time database guru. Is that what they were hired to do?

I worked with one guy who had a masters in business and spent most of his career in logistics, but got completely sidetracked when he started playing with Excel. Everybody called him "the spreadsheet guy." He spent 12 hours a day in his office playing with a spreadsheet he made for calculating incentive bonuses for order fillers and forklift drivers. This thing was 25 MB and had formulas so long they wouldn't even fit in the formula bar without wrapping 3 times. The sad thing is we could have built a small database and some active-x .dlls with very little effort that would have blown his spread-base away, and would have required virtually zero maintenance.

So, from a business perspective, was it really cost effective for the company to be paying this guy 100K per year to sit in his office and pretend to be a developer? Why not just define a business need, try to justify the cost of developing it, and forget it if you can't. This is the real issue. The cost of his project was hidden because it was all done under the table, without any input from the CIO.

This is another reason the IT forecasters are predicting a return to the dumb terminal in the business place, which automatically solves the majority of problems. If you had a box that wouldn't do anything but allow data entry, how eager would you be to turn it on every morning?



VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
I just realized how far off-topic this is. My apologies to knighty03 for the rambling. I assume this thread will be truncated at one point.

I'm not usually much of a talker, so congratulations to Ben for bringing me out of my shell. [worm]


End transmission...

VBSlammer
redinvader3walking.gif

[sleeping]Unemployed in Houston, Texas
 
Thanks VBSlammer, I found your insight very stimulating here.
 
No probs VBS!
It sounds like what you are saying is that .NET will start to weed out the bloat in the IT industry that happened with the dot com boom.
This can only be a good thing, but there will still be a place for the macro Joe's. These are the people who m$ depend on for bring Office into the office (!) to build business critical apps, which are then upgraded to .NET apps when it outgrows itself.

I'm interested to see what happens to VBA. My guess is that it will stay as it is, but allow easier access to .NET classes and routines, rather than having them built in directly, something along the lines of COM.NET!!

Anyway, just my thoughts.

Ben

----------------------------------------------
Ben O'Hara

"Where are all the stupid people from...
...And how'd they get so dumb?"
NoFX-The Decline
----------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top