Solved

text in tabcontrols

Posted on 2000-03-18
9
567 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 5

Expert Comment

by:vachooho
Comment Utility
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 
LVL 5

Expert Comment

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

Expert Comment

by:SteveGTR
Comment Utility
Sorry about the typo...
0
 
LVL 30

Accepted Solution

by:
SteveGTR earned 100 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now