?
Solved

Updating a list control

Posted on 2003-12-04
4
Medium Priority
?
395 Views
Last Modified: 2013-11-20
Hi,

   I'll give an overview of my program.  I'm using MFC and an Access database. In my FormView class the user clicks the "Find Trade" button.  This generates a "FindTrade" dialog box which has a list control populated with the contents of a database as well as an "Edit" button.

  The user selects an item in the list control and then clicks the "Edit" button.  This generates an "Edit Trade" dialog box with edit controls populated with the field contents of the record selected in the list control.  Once the user edits any data in the  edit controls the "EditTrade" button is clicked.  This updates the record in the database just fine.
 
  What I would like to is have a function in my CTransactionSet class called UpDateItem() that will update the list control in "FindTrade" dialog with the record that was edited. The problem I'm having is trying to figure out how to pass the address of the list control in my "Find Trade" dialog box to a function, UpdateItem() in my Recordset class.    

  I thought of passing it the address in the DoModal() for CEditTradeDialog.  To be honest I'm not completely how to pass the address of the list control.
 


Below is the code for OnButtonEdit() from FindTrade dialog box
[code]

void CFindDialog::OnButtonEdit()
{
      //AfxMessageBox("You clicked the Edit Button");

      CTransactionSet TmpSet;
      CListCtrl* pList;
      pList = &m_lcTradeList;
      TmpSet.Open();
      CEditTradeDialog TmpEdTrdDlg;

      int nTransID;

      this->nLcReturnValue = m_lcTradeList.GetNextItem(-1,LVNI_SELECTED);
      int nItemIndex = this->nLcReturnValue;

      if(this->nLcReturnValue == -1)
      {
            AfxMessageBox("You must select and entry");
      }
      else

      {

            CString y;
            y.Format("The unique ID is %d" ,nItemIndex);
            AfxMessageBox(y);

            
            
            
            
            nTransID = m_lcTradeList.GetItemData(this->nLcReturnValue);
            CString z;
            z.Format("The TransactionID is %d",nTransID);
            AfxMessageBox(z);


            CString sel;
            sel.Format("the TransactionID at %d is %d ", nItemIndex, nTransID);
            AfxMessageBox(sel);



            

      }


                //I thought of passing the address in the Domodal()
      TmpEdTrdDlg.DoModal(nTransID, pList);

      
}

[/code]

This is the code for OnEditTrade() from the EditTrade dialog box


[code]

void CEditTradeDialog::OnEditTrade()
{

      UpdateData(TRUE);

      CTransactionSet TmpTransSet;

      
      if(!TmpTransSet.IsOpen())
            TmpTransSet.Open();

      TmpTransSet.Edit();

      TmpTransSet.m_Buyer = m_strBuyer;
      TmpTransSet.m_Seller = m_strSeller;

      TmpTransSet.m_BuyerBrokerNumber = atoi(m_strBuyerBrokerNumber);
      TmpTransSet.m_BuyerBrokerCommission = atof(m_strBuyerBrokerComm);

      TmpTransSet.m_SellerBrokerNumber = atoi(m_strSellerBrokerNumber);
      TmpTransSet.m_SellerBrokerCommision = atof(m_strBuyerBrokerComm);



      TmpTransSet.m_TradeDate = m_strTradeDate;
      TmpTransSet.m_SettlementDate = m_strSettlementDate;
      TmpTransSet.m_MaturityDate = m_strMaturityDate;

      TmpTransSet.m_Amount = m_strAmount;
      TmpTransSet.m_Rate = atof(m_strRate);

      TmpTransSet.m_NumberOfDays = atoi(m_strNumberOfDays);


      TmpTransSet.m_CollateralDescription = m_strSecurityDescription;




      TmpTransSet.Update();

      

      this->ClearContents();

      //TmpTransSet.UpdateItem(this->mpList);

      CDialog::OnCancel();

      





      
}

[/code]

Below is the code I've started for UpdateItem() from CTransactionSet

[code]

void CTransactionSet::UpdateItem(CListCtrl *pList, int Item)
{

      AfxMessageBox("In UpdateItem()");

      int nTrade = pList->GetItemData(Item);

      this->LocateTrade(nTrade);

      pList->SetItemText(Item,0,this->m_Buyer);
      pList->SetItemText(Item,1,this->m_Seller);

   





}

[/code]

0
Comment
Question by:soutine
[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
  • 3
4 Comments
 

Accepted Solution

by:
JeffT earned 500 total points
ID: 9876971
In your CTransactionSet class add a member function called:

SetListCtrl(CListCtrl* list)

This implementation of this method simply stores the pointer as a member variable, ie:

void CTransactionSet::SetListCtrl(CListCtrl* list)
{
     m_listCtrl = list;
}

In your OnButtonEdit() method, just call this before calling DoModel. For example....

TmpEdTrdDlg.SetListCtrl(pList);
TmpEdTrdDlg.DoModal(nTransID);

Finally, in your CEditTradeDialog::OnEditTrade() dialog, you can now just use the member variable you set, m_listCtrl.

Hope this helps.

Jeff
0
 

Expert Comment

by:JeffT
ID: 9876983
Sorry, I meant in you CEditTradeDialog class add the member function, not the CTransactionSet class. It should be:

void CEditTradeDialog::SetListCtrl(CListCtrl* list)
{
     m_listCtrl = list;
}
0
 

Author Comment

by:soutine
ID: 9899521
Hi Jeff,

  I see what is going on.  In a sense you're passing the address of the list control on to the class that will be using it.  I'm curious to know if this is the way thing are normally done to access a controls.  That is "pass the address along"

  In my case I also needed the item number for the list control so I added this to my SetListCtrl().  Here is my revised code.  In my CEditTradeDialog I added two variables: nTradeID and pList. Thanks for the help.  I really appreciate it.


[code]

void CFindDialog::OnButtonEdit()
{
      

      CTransactionSet TmpSet;
      CListCtrl* pList;
      pList = &m_lcTradeList;
      TmpSet.Open();
      CEditTradeDialog TmpEdTrdDlg;

      int nTransID;

      

      this->nLcReturnValue = m_lcTradeList.GetNextItem(-1,LVNI_SELECTED);
      int nItemIndex = this->nLcReturnValue;

      if(this->nLcReturnValue == -1)
      {
            AfxMessageBox("You must select and entry");
      }
      else

      {

            nTransID = m_lcTradeList.GetItemData(this->nLcReturnValue);
                        

      }
   
      c_DeleteTrade.EnableWindow(false);

      TmpEdTrdDlg.SetListCtrl(pList, this->nLcReturnValue);
      TmpEdTrdDlg.DoModal(nTransID);
      
}

[/code]


[code]


void CEditTradeDialog::OnEditTrade()
{

      UpdateData(TRUE);

      CTransactionSet TmpTransSet;

      
      if(!TmpTransSet.IsOpen())
            TmpTransSet.Open();

      TmpTransSet.Edit();

      TmpTransSet.m_Buyer = m_strBuyer;
      TmpTransSet.m_Seller = m_strSeller;

      TmpTransSet.m_BuyerBrokerNumber = atoi(m_strBuyerBrokerNumber);
      TmpTransSet.m_BuyerBrokerCommission = atof(m_strBuyerBrokerComm);

      TmpTransSet.m_SellerBrokerNumber = atoi(m_strSellerBrokerNumber);
      TmpTransSet.m_SellerBrokerCommision = atof(m_strBuyerBrokerComm);



      TmpTransSet.m_TradeDate = m_strTradeDate;
      TmpTransSet.m_SettlementDate = m_strSettlementDate;
      TmpTransSet.m_MaturityDate = m_strMaturityDate;

      TmpTransSet.m_Amount = m_strAmount;
      TmpTransSet.m_Rate = atof(m_strRate);

      TmpTransSet.m_NumberOfDays = atoi(m_strNumberOfDays);


      TmpTransSet.m_CollateralDescription = m_strSecurityDescription;



      TmpTransSet.Update();

      TmpTransSet.UpdateItem(this->pList, this->nTradeID);

      

      this->ClearContents();

      

      
      CDialog::OnCancel();

}

[/code]



[code]

void CEditTradeDialog::SetListCtrl(CListCtrl *pList, int nTransID)
{



      this->nTradeID = nTransID;

      this->pList = pList;
      
      
      

}


[/code]
0
 

Expert Comment

by:JeffT
ID: 9900160
There is no "normal" way of accessing a control. Basically, you should do it however you need to that fits within a good design of your system. Microsoft for example, tends to suggest that you should access your controls through public member variables of the dialog class. This is obvious the way that class wizard adds control member variables as public by default. It is perfectly acceptable to then pass a pointer to your entire dialog class to another class or dialog and access the controls through that pointer. I personally prefer a cleaner way of doing things. By making the member variables protected and then writing specific accessor functions to only the members you want to expose, you save yourself from getting in trouble down the road.

There is one thing that you need to be careful of. Since the second dialog has a pointer to a control in the first dialog, you could run into problems if the first dialog gets destroyed before the second. If this occurs, you will have an invalid pointer in the second dialog class. This can be prevented by checking to make sure the hWnd of the control is valid before using it for anything. Since the second dialog is Model in your case, you shouldn't have to worry about this though.

Glad I could help.

Jeff
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: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses

762 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