Solved

text in tabcontrols

Posted on 2000-03-18
9
572 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
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Expand to include initial dialog with two choices. 9 78
sumNumber challenge 16 122
sum28 challenge 31 112
x-bar in Google Sheets 2 54
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: 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…
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.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

773 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