?
Solved

text in tabcontrols

Posted on 2000-03-18
9
Medium Priority
?
590 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
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit 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 400 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

771 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