Solved

Sequential tasks

Posted on 2004-08-04
5
217 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

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

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

744 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

10 Experts available now in Live!

Get 1:1 Help Now