Hottest Forum Q&A on CodeGuru - February 22, 2004

Tuesday Mar 2nd 2004 by Sonu Kapoor

This week's topics include setting the focus of multiple items in a list control when a dialog is open, why a DOS application runs satisfactorily in Win98 but improperly in Win2000, why rand() always returns the same number, and resolving MFC Class conflicts.


Lots of hot topics are covered in the Discussion Forums on CodeGuru. If you missed the forums this week, you missed some interesting ways to solve a problem. Some of the hot topics this week include:

How do I set the focus of multiple items in a list control when a dialog is open? (top)


wow9999 is working in an application in which he needs to set the focus of several list control items.

I created a dialog and added a List control which is an icon list
control. I can select several items if I press Ctrl or Shift. Now,
I want several items to be seleted and set focus when a user opens
this dialog. This means if items 1, 2, and 3 are seleted, and the
user closes the dialog, they should be selected automatically and
the focus should be set when the dialog is opened the next time.

I just want to know how to set several items' focus when a user
opens the dialog.

You should be able to use CListCtrl::SetItemState to set mutiple items as selected (you would have to call it once for each item you want to select). As for the focus, only one item can have the focus, but you can use CListCtrl::SetItemState to do that as well. You also should make sure that your list control doesn't have the LVS_SINGLESEL style set and make sure your listctrl is set to "Always Show Selection," and it should work. Here is an example code:

CString sLabel;

// add 10 items to list
for(int i = 0;i < 9; i++)
   sLabel.Format("Item%d", i);
   m_List.InsertItem(i, sLabel);

// select 1st 5 items
for(int i = 0;i < 5; i++)
   m_List.SetItemState(i, LVIS_SELECTED, LVIS_SELECTED );

// set focus to first item
m_List.SetItemState(0, LVIS_FOCUSED , LVIS_FOCUSED  );

Why does my DOS application run satisfactorily in Win98 but improperly in Win2000? (top)


acselli has a DOS application that works properly in Win98SE, but in Win2K it also works, but not properly. Let's see what acselli says.

I have a DOS application that communicates through the RS232 with an
Intel ICE (in circut emulator). With W98SE no problem but with
W2000, the application runs but doesn't communicate with the ICE.

How I can solve this problem?
Is it possible to write some code to run first my DOS application?

Well, you always have to consider that if you are dealing with hardware-related problems, don't expect a program that works on one OS to work on another without any changes. Those changes can be anything from changing an argument to a function to rewriting the whole application from scratch. That is why, without the source code, you may be out of luck.

Windows 98 is essentially DOS 7.0 with a GUI interface. Windows 2000 is not DOS (the command line in Win2000 is not DOS—it is the 32-bit command line), and many things that you can do in DOS will not work in Windows 2000.

Why does rand() always return the same number? (top)


Amit Sebiz is using the rand() function in a for loop but unfortunately rand always returns the same number. Do you know why?

I am using the following code to generate a random string. But 
each time I get the same string output. Can anyone help. Here is
the code:
char myarray[22];
char* CGeneratorApp::GenerateString()
   for(int k=0;k<21;k++){
       myarray[k] = (char)(((int)rand()%25)+65);

   char first[2];





   return myarray;

You need to initialize the random seed using srand() before calling rand(). For example,


Besides that, take a look at the following thread, which contains some more information. Also, take a look at the following VC++ FAQs:

How can I resolve MFC Class conflicts? (top)


Kibble is working on a application that loads a DLL. The DLL has some classes called CObject and CArchive. Now, he gets a name conflict in the DLL header and in the MFC header.

I have been coding a game engine for a while now, the core part 
of it is in a DLL file. I have started coding the editor for it,
and I'm trying to use MFC. The problem is, I have classes named
CObject and CArchive. The first thing I tried to do to fix this is
include MFC into a namespace, but I seriously doubted it would work:
namespace MFC
   // MFC includes here, just the classwizard-generated ones
Then use fully qualified MFC names for everything. but, surprise
surprise, this didn't work. Many hundreds of include file errors,
lots of 'name' undefined (particularly time_t), and 'name' is not
a member of 'Global namespace' (resulting from things like
::GetWindowTextA in the MFC headers).

I could put my engine in a namespace, but that would be a LOT of
work (300 files +), so this is my last resort. What are my options

Either change your names or place your code in a namespace. As a matter of fact, it would be beneficial if you did place your names in a namespace and change the names. Changing the names to CGameObject, or CGameArchive is a much better choice than CObject or CArchive. If you have many source files, usage of a good editor (one that has multiple file "search and replace") makes changing the names of classes very easy.

Placing MFC names in a namespace will not work at link-time. The linker will be looking for "CObject" for the MFC names, but will only find "MFC::CObject". Then, you will get "unresolved external" errors. In other words, you're wasting your time by placing MFC in a namespace unless you want to rebuild the entire MFC library with the MFC namespace.

As an alternative to using another editor, also have a look at the articles section; there are add-ins and macros such as this one that adds search & replace across multiple files to VisualStudio.

Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved