Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sequential tasks

Posted on 2004-08-04
5
Medium Priority
?
226 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
[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
  • 2
5 Comments
 
LVL 45

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 45

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 45

Accepted Solution

by:
AndyAinscow earned 1000 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

604 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