Solved

Sequential tasks

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…

724 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