Solved

text in tabcontrols

Posted on 2000-03-18
9
584 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

696 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