tabctrl text
Hello,
I want to label my tab control,in a dialog box at run time.
i used the following code in the OnInitdialog() .The label is not getting displayed on the tab ctrl.
CRect rect;
rect.left = 0;
rect.top = 0;
rect.right =50;
rect.bottom = 30;
TCITEM item;
item.mask = TCIF_TEXT;
item.dwState = TCIS_BUTTONPRESSED;
item.iImage= -1;
item.pszText = "Processor";
item.cchTextMax = strlen(item.pszText);
tab.Create(TCS_OWNERDRAWFIXED|TCS_FIXEDWIDTH|WS_CHILD|WS_VISIBLE|WS_TABSTOP,rect,this,100);
tab.SetItem(0,&item);
can anyone pls help me.
thnx
swetha
# 1 Re: tabctrl text
A few things: Please use code tags when posting code. Note that you can initialize a CRect object like this:CRect rect(0, 0, 50, 30); Why are you setting dwState to TCIS_BUTTONPRESSED? This will set the text only for the clicked state of the tab.
# 2 Re: tabctrl text
Maybe you should first add the item using InsertItem:
. . .
tab.Create(. . .);
tab.InsertItem(0, &item);
Later, when this item exists, you can change its text using SetItem function.
Also be sure the CRect size of your tab control is big enough and the TCS_OWNERDRAWFIXED and TCS_FIXEDWIDTH styles are really needed in your program.
I hope this helps.
Viorel at 2007-11-10 23:18:45 >

# 3 Re: tabctrl text
Hello
Stercken , I will defenitely use code tags frm now on.
Viorel,Your suggestion did not work! If i insert item,i am getting a debug assertion failed error
any more suggestions?
thnx
swetha
# 4 Re: tabctrl text
It is not clear what is wrong with your code , because there is little info on the details.
What do you mean by label is not displayed ? What is appearing instead of "Processor" ?
Please post the whole code of OnInitDialog.
# 5 Re: tabctrl text
Hello,
Let me explain what i am trying to do. I am trying to add tabs to a dialog box at runtime. The label of my first tag(I am not sure if this can also be called as tab text) should read processor.
BOOL CJunkDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
CRect rect(0,0,100,30); // initializing the dimensions of the tab control.
// Creating a tab control
tab.Create(TCS_OWNERDRAWFIXED|WS_CHILD|WS_VISIBLE|WS_TABSTOP,rect,this,100);
TCITEM item;
item.mask = TCIF_TEXT;
item.iImage= -1;
item.pszText ="Processor";
item.cchTextMax = strlen(item.pszText);
tab.InsertItem(0,&item);
tab.SetItem(0,&item)
return TRUE; // return TRUE unless you set the focus to a control
}
can u suggest wht is wrong with my code, i am lost here.
thnx
swetha
# 6 Re: tabctrl text
Did you run the debug version through the debugger ? If you did, you would have noticed an assertion? And if you did see it, the following line appears when you hit retry which clearly indicates the source of the problem.
void CTabCtrl::DrawItem(LPDRAWITEMSTRUCT)
{
ASSERT(FALSE); // must override for self draw tab controls
}
Note the comment there. It appears that you are being asked to draw the control for some reason, and if you track it down in your code, you see that you mention OWNERDRAW style while creating the tab control. That is the cause and not the InsertItem or SetItem
# 7 Re: tabctrl text
As a suggestion, in the course of development, run the debug version. At least for MFC apps. If you are writing MFC apps too, make it a habit to put such useful macros in the code that you write too.
Also, when you develop make sure you heed any compiler warnings and examine them. These are somethings that one tends to overlook and get into issues.
# 8 Re: tabctrl text
Hello,
Thankyou very much. My problem is fixed.
I see in my code the setitem is redundant.
When do we use Setitem function?
thnx
swetha
# 9 Re: tabctrl text
Hello,
Thankyou very much. My problem is fixed.
You are welcome.
When do we use Setitem function?
To change an existing item, for short.
Anyways, for any Microsoft related questions, MSDN should be your first stop :)