I have 2 Forms in my Windows Form application. Form1 creates .txtfiles.
I open Form2 from Form1 and can see the .textfiles that is created in a comboBox3.

If I open the application from the beginning and go to Form2 I will see all .txt files that exists in a folder in this comoBox3. While the application is open I close Form2 and is then on Form1 where I now will create a new .text file.
I open Form2 and see if this is in the comboBox3 and it isn´t ?
If I close the whole application, open it again and go to Form2 then it will appear in the comboBox3.
Why do I have to close the application and open it again to receive the changes.

I load in the .txt files to the comboBox3 with this code under Form2(void):

Form2(void)
{
	InitializeComponent();
	//
	//TODO: Add the constructor code here
	//

System::String ^ Files10 = "C:\\Globejennife\\Groupjennif\\"; 
array<System::String ^> ^ files4 = System::IO::Directory::GetFiles(Files10,"*.txt");

array<Char>^ chars = {'.'};
array<System::String ^> ^ splits;
			
    for (int count1 = 0; count1 < files4->Length; count1++)
   {
    splits = files4[count1]->Split(chars);	
    comboBox3->Items->Add(splits[0]->ToString()->Substring(29));

   }
}

Recommended Answers

All 14 Replies

Typically, your window isn't actually destroyed, it is only hidden. Hence, when you ask to show it again nothing has changed. You will need to add an OnShow event handler to reload your combobox whenever the form is displayed.

Hope this helps.

I am trying something like this but are not sure at all if this is the right approach to it.
I have tried Both OnShow and OnShown and none of them works anyway here.

private: System::Void Form2::OnShow(System::Object^ sender, System::EventArgs^ e)
		 {

                                  // Reload the comboBox1

                                  }

I have looked at msdn at OnShow() but there is no good examples how this is done.
Is there on any special place in the code I have to write this and I am not sure about how the syntax goes for this really.

Typically, your window isn't actually destroyed, it is only hidden. Hence, when you ask to show it again nothing has changed. You will need to add an OnShow event handler to reload your combobox whenever the form is displayed.

Hope this helps.

I am trying a test. When open Form2 from Form1. A messageBox will appear but this is not working. I dont know what is wrong with this code.

private: System::Void Form1::Form2::OnShow(System::Object^ sender, System::EventArgs^ e)
    {    

    MessageBox:: Show("Window Reloaded");

                 }

Your procedure should be named something like FormShow.

Then, either in the IDE using the Object Inspector (or whatever M$ calls it) or in the FormCreate function set the OnShow property to the FormShow procedure.

I am not really sure still how to do this. If I will use FormShow instead of OnShow where will I write this in the form and How does the code look like for this. I cant find any examples of this in msdn and I cant find any alternative to OnShow is the list where you choose.
I beleive I am really lost on this one.

Though I have a feeling that any code should be set inside this in Form2:

public:
	Form2(void)
{
	InitializeComponent();
		
	//
	//TODO: Add the constructor code here
	//
			
}

If you put this code like this under void InitializeComponent(void) in Form2, close the application and open the whole application from the beginning. The messageBox will appear before Form1 wich is the first Form that appears when the application opens but when open Form2 the MessageBox doesn´t appear so it seems that all forms loads into memory one time and something else is needed here to do.

void InitializeComponent(void)

{
MessageBox::Show(this, "Window Reloaded");
}

OnShow is a property. (Essentially it is a function pointer.) Your function should be named something like void Form1::FormShow( TObject *Sender ); and then you set OnShow = FormShow.

For each form, the OnCreate handler (which it looks like you have named "InitializeComponent"?) should only fire once. Your form is created at the beginning of the program, and no matter how many times you show or hide it, it is only destroyed when the program ends.

It is possible to do things differently, but I wouldn't worry about that for now...

If this doesn't help then please post some complete code that demonstrates your problem...

Thank you for your answer. I am not sure if FormShow will be used in VC++ 2008 Express Edition. I think that is Borland but am not sure.

As you said my: InitializeComponent(); is only executed once when the application is opened.

I cant find FormShow as a member of class Form1 or Form2. However "OnShow" and "OnShown" I can find.
The only code and solution I can think of is to go inside the code of Form2 and write this in the same area where all button and comboxcontrols are when you doubleclick them:
(To mention, the code compils anyway.)

(I use a messagebox just to see if it works but this messagebox doesn´t appear when open Form2 from Form1.)

private: System::Void Form1::Form2::OnShow(System::Object ^sender, System::EventArgs ^e)
{
 MessageBox::Show(this, "Window Reloaded");
}

If you just double click the OnShow property in the Object Inspector (sorry to keep using Borland terminology) the IDE should auto-create an event procedure for you. Borland would name it "FormShow".

It doesn´t happen anything when I doubleclick on "OnShow" on this code:

private: Void Form1::Form3::OnShow(System::Object ^sender, System::EventArgs ^e)
{
        MessageBox::Show(this, "Window Reloaded");
}

But if I just put the MouseCursor over OnShow on the code above, there is a message that tells:
void Form1::Form3::OnShow(System::Object ^sender, System::EventArgs ^e)

So I have tried all these 4 examples without any success so I am really lost of what to do:

void Form1::Form3::OnShow(System::Object ^sender, System::EventArgs ^e)

private: void Form1::Form3::OnShow(System::Object ^sender, System::EventArgs ^e)

public: void Form1::Form3::OnShow(System::Object ^sender, System::EventArgs ^e)

protected: void Form1::Form3::OnShow(System::Object ^sender, System::EventArgs ^e)

If you just double click the OnShow property in the Object Inspector (sorry to keep using Borland terminology) the IDE should auto-create an event procedure for you. Borland would name it "FormShow".

I have 2 Forms in my Windows Form application. Form1 creates .txtfiles.
I open Form2 from Form1 and can see the .textfiles that is created in a comboBox3.

If I open the application from the beginning and go to Form2 I will see all .txt files that exists in a folder in this comoBox3. While the application is open I close Form2 and is then on Form1 where I now will create a new .text file.
I open Form2 and see if this is in the comboBox3 and it isn´t ?
If I close the whole application, open it again and go to Form2 then it will appear in the comboBox3.
Why do I have to close the application and open it again to receive the changes.

I load in the .txt files to the comboBox3 with this code under Form2(void):

Form2(void)
{
	InitializeComponent();
	//
	//TODO: Add the constructor code here
	//

System::String ^ Files10 = "C:\\Globejennife\\Groupjennif\\"; 
array<System::String ^> ^ files4 = System::IO::Directory::GetFiles(Files10,"*.txt");

array<Char>^ chars = {'.'};
array<System::String ^> ^ splits;
			
    for (int count1 = 0; count1 < files4->Length; count1++)
   {
    splits = files4[count1]->Split(chars);	
    comboBox3->Items->Add(splits[0]->ToString()->Substring(29));

   }
}

override the virtual function OnActivated in Form2. protected: virtual void OnActivated( EventArgs^ e ) ; move the code in the constructor for populating comboBox3 to this function. (and also call the base class OnActivated.) http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.onactivated.aspx

I have done this test with the MessageBox like this. I beleive it should be something like this.
There is no examples on msdn on how to do this really. I have put this code inside Form2(void) like this. I have also tried with protected without ":" and also virtual instead of override.
When running this in the compiler, the comiler says:
error C2143: syntax error : missing ';' before 'protected'

But this should not be the problem because if I take the protected: etc {} away it do compiles.

public:
      Form2(void)
{
      InitializeComponent();
     
			
       protected: override void Form1::Form2::OnActivated(EventArgs^ e );
       {
              MessageBox::Show(this, "Window Reloaded");
       }
}

override the virtual function OnActivated in Form2. protected: virtual void OnActivated( EventArgs^ e ) ; move the code in the constructor for populating comboBox3 to this function. (and also call the base class OnActivated.) http://msdn2.microsoft.com/en-us/library/system.windows.forms.form.onactivated.aspx

// ...
public:
  Form2(void)
  {
      InitializeComponent();
  }
  // ..
protected: 
  virtual void OnActivated( EventArgs^ e ) override
  {
    // clear all strings in combobox
    // reload fresh strings into combobox   
  }
// ...

looks like you need a refresher on basic c++ (and c++/cli) syntax.

Thank you for this help. I am just 2 months with C++ so I am still learning the basics parallelly with a little more advanced.
This worked great. On the examples I found that they did put the override instead of virtual.
So overriding meens to put it after the EventArgs.. etc... if I understand right.

// ...
public:
  Form2(void)
  {
      InitializeComponent();
  }
  // ..
protected: 
  virtual void OnActivated( EventArgs^ e ) override
  {
    // clear all strings in combobox
    // reload fresh strings into combobox   
  }
// ...

looks like you need a refresher on basic c++ (and c++/cli) syntax.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.