Solved

Sequential tasks

Posted on 2004-08-04
5
218 Views
Last Modified: 2013-11-20
One recurrent problem I’m having is making the program do sequential tasks. In VB.NET the only way I found to stop the program in the middle of a subroutine in Form1 is to call Form2 through Form2.ShowDialog(), have the program do whatever necessary and then have it return back to Form1. Whatever values the variables acquire in Form2 are passed to Form1 through dimming the variables on a Modular level. This modal dialog seems to work well.

Here in Visual C++ .NET it seems the problems in this respect are not lesser. Suppose the following situation. First, I need to press a button which will cause opening of OpenFileDialog, so I write something like this:

private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)
       {

<code to open a text file and place its contents into two arrays>              
 
          }

Then, only after finishing the job of choosing the text file and placing data contained in that text file in arrays, I need to have the data in these arrays plotted on the form. Thus, as a next step I need to invoke the following:

void OnPaint( PaintEventArgs *paintEvent )      
          {

     <some code plotting the data on the screen>
     
          }

Unfortunately, when I have both functions in the same form Form1.h, both of them are executed at the same time. My impression was that one function can call another function but in this case obviously the method OnPaint seems to be common also with the operations that depict the buttons themselves – all buttons appear at the same time, so does the plotting of whatever is to be plotted by calling the OnPaint function.

What can I do to make clicking and opening the text file occur prior to plotting the data from that file?
0
Comment
Question by:judico
  • 3
  • 2
5 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 11726646
What you can do is to invalidate the dialog AFTER the read from file has finished and use a bool flag to determine if the paint should try to display the array contents.
0
 

Author Comment

by:judico
ID: 11726913
Can you suggest a concrete way to do that?
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 11727163
roughly

CMyForm::OnButton1Click()
{
m_bArraysOK = false;
read file, fill array
m_bArraysOK = true;
Invalidate();
}

CMyForm::OnPaint()
{
if(m_bArraysOK)
  {
  Draw arrays to screen
  }

}
0
 

Author Comment

by:judico
ID: 11729025
I need to understand this. Suppose I have the following code:




private: System::Void button1_Click(System::Object *  sender, System::EventArgs *  e)
                    {
listBox1->Items->Clear();

try  
{    

OpenFileDialog *fileChooser = new OpenFileDialog();
Windows::Forms::DialogResult result = fileChooser->ShowDialog();

// name of file containing data
String *fileName;

// exit event handler if user clicked Cancel                      
if ( result == DialogResult::Cancel )                            
     return;                                                      
                                                                 
// get specified file name                                        
fileName = fileChooser->FileName;                                
                                                                 
// show error if user specifies invalid data                      
if ( ( fileName->Equals( S"" ) ) )                                
MessageBox::Show( S"Invalid File Name", S"Error", MessageBoxButtons::OK, MessageBoxIcon::Error );  

else
     {
            String* textFile = String::Concat( fileName );
       }

      StreamReader *reader = new  StreamReader(fileName);
      do
      {
            listBox1->Items->Add(reader->ReadLine());
      }  
            while(reader->Peek() != -1);
    }


catch(FileNotFoundException *ex)                          
{                                                        
      listBox1->Items->Add(ex);                            
}                                                        
                                                         
catch (System::Exception *e)                              
{                                                        
      listBox1->Items->Add(e);                              
}                                                        
             
                   }





protected:

                   void OnPaint( PaintEventArgs *paintEvent )      
        {
 __super::OnPaint( paintEvent ); //      call base OnPaint method

             //      get      graphics object
             Graphics *graphicsObject =      paintEvent->Graphics;
             SolidBrush      *brush = new SolidBrush( Color::Blue );
             Pen *pen =      new      Pen( Color::Black );
            
             for ( int count = 1; count      <= 100;      count++      )
              {
                  graphicsObject->DrawEllipse( pen, count      * 5, Math::Sin(      count )*100, 7,      7 );
              }

        }         




How would the idea from your last posting be implemented in this case?
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 250 total points
ID: 11793166
Sorry for not getting back for so long.
All I can do is repeat the bit of MFC code I posted earlier.

CMyForm::OnButton1Click()
{
m_bArraysOK = false;
read file, fill array
m_bArraysOK = true;
Invalidate();
}

CMyForm::OnPaint()
{
if(m_bArraysOK)
  {
  Draw arrays to screen
  }

}

I can't think of any simpler way to explain the logic.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 48
Question regarding Copy/Paste 16 91
set up trigger (or something) to rename files (starting with ATL) in a linux directory 6 53
deburging in oracle form 12 88
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. …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

914 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

17 Experts available now in Live!

Get 1:1 Help Now