Solved

Updating a list control

Posted on 2003-12-04
4
375 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
repeatEnd java challenge 42 84
Excel Use VBA to get user's Mac Address for their computer 5 159
format the code in java 6 93
zeroMAx challenge 20 100
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

808 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