Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

Sequential tasks

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
judico
Asked:
judico
  • 3
  • 2
1 Solution
 
AndyAinscowCommented:
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
 
judicoAuthor Commented:
Can you suggest a concrete way to do that?
0
 
AndyAinscowCommented:
roughly

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

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

}
0
 
judicoAuthor Commented:
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
 
AndyAinscowCommented:
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

Independent Software Vendors: 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!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now