Solved

text in tabcontrols

Posted on 2000-03-18
9
576 Views
Last Modified: 2013-11-20
MFC. Microsoft Visual Studio 5.0.

How can I change the text in the tabs of a tabcontrol? (Code, please)

The whole problem is: I have an MFC dialog. I have an input-text field associated with the member variable m_Name (CString).
When the user types in this field, I want the text in a tab of a tabcontrol to be written at the same time with the user input. Actually, the user is typing the name of the tab. The name of the tab should be written at the same time, as if the user were typing direct on the tab.
I have tried this, but it doesn't work at all:

void CFhgDlg::OnChangeNameInput()
//the user types in the input field m_Name
{      
      int tab=m_CTab.GetCurSel();
      PSTR text=m_Name.GetBuffer(0);
      TC_ITEM* tcItem;
      if (m_CTab.GetItem(tab,tcItem2)){
            tcItem->pszText=text;
            m_CTab.SetItem(tab,tcItem2);
            UpdateData(TRUE);
      }


Thanks in advance.

    Javier Gil.
0
Comment
Question by:javigil1
  • 4
  • 3
  • 2
9 Comments
 
LVL 30

Expert Comment

by:SteveGTR
ID: 2632720
Try this:

void CFhgDlg::OnChangeNameInput()
{
  UpdateData(TRUE);
 
  int tab = m_CTab.GetCurSel();

  if (tab < 0)
    tab = 0;

  TC_ITEM tcItem;

  tcItem.mask = TCIF_TEXT;

  if (m_CTab.GetItem(tab, tcItem))
    {
    tcItem.pszText = m_Name.GetBuffer(0);
    m_CTab.SetItem(tab, tcItem);
    }
}

You have to flush the edit box value down to m_Name first via the UpdateData(TRUE) call. I added code to default to the first tab if no tab is current (optional). If you want to manipulate the text portion of the tab, set the mask equal to TCIF_TEXT prior to calling GetItem() and SetItem().

Good Luck,
Steve



0
 

Author Comment

by:javigil1
ID: 2633971
It doesn't work at all. First of all, tcItem is a pointer, so it isnt:
    TC_ITEM tcItem;
but:
    TC_ITEM* tcItem;

Anyway, I have added the reference to the mask, and I have now this:

void CFhgDlg::OnChangeNameInput()
                   //the user types in the input field m_Name
                   {
                   int tab=m_CTab.GetCurSel();
                   PSTR text=m_Name.GetBuffer(0);
                   TC_ITEM* tcItem;
                  tcItem2->mask = TCIF_TEXT;    //new!!
                   if (m_CTab.GetItem(tab,tcItem2)){
                   tcItem->pszText=text;
                   m_CTab.SetItem(tab,tcItem2);
                   UpdateData(TRUE);
                   }

There's no problem by compiling, linking... I can run the programm, but when i try to write in the field m_Name, the programm terminates and I get an error. You know that funny window with 3 options: Close/Debug/Details.

So, so far it doesn't work at all.
0
 

Author Comment

by:javigil1
ID: 2633973
in the previous code, there is a spelling mistake:

     tcItem2 = tcItem  (just take the "2" away)

so, I'm not using two variables! just one: tcItem...  the problem has nothing to do with this "2".
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 5

Expert Comment

by:vachooho
ID: 2634379
void CFhgDlg::OnChangeNameInput()
//the user types in the input field m_Name
{
int tab=m_CTab.GetCurSel();
PSTR text=(PSTR)(LPCTSTR)m_Name;
TC_ITEM tcItem;
::ZeroMemory((PVOID)&tcItem, sizeof(tcItem));
// you can try this too
//tcItem.mask = TCIF_TEXT;
// tcItem.pszText = new TCHAR[100];
// ZeroMemory((PVOID)tcItem.pszText, 100);
if(m_CTab.GetItem(tab,tcItem))
{
//
// delete [] tcItem.pszText;
//
tcItemp.szText=text;
m_CTab.SetItem(tab,tcItem);
}


You've got protection fault in GetItem() call
because you do not make space for item text  you are retrieveing.
Actualy you not need even to call getItem if you not need old tab text.


Here is the code I will wrote for setting tab ctrl's text

int tab = m_CTab.GetCurSel();
if (tab >= 0)
{
PSTR text=(PSTR)(LPCTSTR)m_Name;
TC_ITEM tcItem;
::ZeroMemory((PVOID)&tcItem, sizeof(tcItem));
tcItem.mask = TCIF_TEXT;
tcItemp.szText=text;
m_CTab.SetItem(tab,tcItem);
}

that's all
Hope this helps
0
 
LVL 5

Expert Comment

by:vachooho
ID: 2634383
ups
m_CTab.SetItem(tab,tcItem);
should be
m_CTab.SetItem(tab,&tcItem); //<<- this is correct line
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 2634496
Sorry about the typo...
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 100 total points
ID: 2634500
It should have read:

if (m_CTab.GetItem(tab, &tcItem))
  {
  tcItem.pszText = m_Name.GetBuffer(0);
  m_CTab.SetItem(tab, &tcItem);
  }
   
0
 

Author Comment

by:javigil1
ID: 2635659
Hey vachooho !!

We did you post your answer as a comment and not as an "answer"?. I cannot rate you and give you my points if you post your answer as a comment.
Anyway, your answer is right (with slight modifications).
This is the final version, which is working fine:

UpdateData(TRUE);
TC_ITEM tcItem;
int tab = m_CTab.GetCurSel();
        if (tab >= 0)
        {
        PSTR text=(PSTR)(LPCTSTR)m_Name;
        TC_ITEM tcItem;
        ::ZeroMemory((PVOID)&tcItem, sizeof(tcItem));
        tcItem.mask = TCIF_TEXT;
        tcItem.pszText=text;
        m_CTab.SetItem(tab,&tcItem);
        UpdateData(TRUE);
         }

// Thanks!

If you post an answer soon, I'll give you the points. Otherwise I'll give them to SteveGTR, whose answer is right as well.
0
 
LVL 30

Expert Comment

by:SteveGTR
ID: 2636448
javigil1 my original code works just fine... It did however have two typo's. As you might not have noticed, I have answered the question again with the corrected code. vachooho can't respond in with an answer because I have. I really don't care whom you give the points to.
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
ffmpeg - "rtsp://...... Operation not permitted" 4 86
Window placement 17 85
FizzBuzz challenge 9 82
matchUp  challenge 9 115
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question