Solved

Updating a list control

Posted on 2003-12-04
4
334 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
  • 3
4 Comments
 

Accepted Solution

by:
JeffT earned 125 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

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

Suggested Solutions

Title # Comments Views Activity
Generic progress indicator 6 110
if loop in java 3 114
sum28 challenge 31 96
has77  challenge 9 68
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 (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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

746 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

13 Experts available now in Live!

Get 1:1 Help Now