Dublowanie okien

0

Witam.
Mam następujący problem, gdy klikam na przycisk za każdym razem otwierają mi się dwa nowe okna.
I moje pytanie brzmi jak sprawdzić przy kliknięciu czy dane okna już istnieją czy nie ? tak aby nie otwierać ciągle nowych, tylko np odświeżać te stare ?

 private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			
			 TMap^ Map = gcnew TMap;
			TInfomation^ Information = gcnew TInfomation;
			
		
			
			
		 }
2

Zamiast zmiennej lokalnej użyj pola w klasie.

0
#pragma once
#include "TInfomation.h"
#include "TMap.h"

namespace Mateusz_Pietron {

	using namespace System;
	using namespace System::ComponentModel;
	using namespace System::Collections;
	using namespace System::Windows::Forms;
	using namespace System::Data;
	using namespace System::Drawing;
	
	/// <summary>
	/// Summary for TSearch
	/// </summary>
	public ref class TSearch : public System::Windows::Forms::Form
	{
	public:
		 TMap^ Map;
		 TInfomation^ Information;
		TSearch(void)
		{
			InitializeComponent();
			
		}

	protected:
		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		~TSearch()
		{
			if (components)
			{
				delete components;
			}
		}

	private: System::Windows::Forms::TextBox^  tbx_From;
	private: System::Windows::Forms::TextBox^  tbx_To;
	protected: 

	protected: 

	private: System::Windows::Forms::Label^  label1;
	private: System::Windows::Forms::Label^  label2;
	private: System::Windows::Forms::DateTimePicker^  date_Control;



	private: System::Windows::Forms::Button^  button1;

	protected: 

	private:

		/// <summary>
		/// Required designer variable.
		/// </summary>
		System::ComponentModel::Container ^components;

#pragma region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>

		void InitializeComponent(void)
		{
			this->tbx_From = (gcnew System::Windows::Forms::TextBox());
			this->tbx_To = (gcnew System::Windows::Forms::TextBox());
			this->label1 = (gcnew System::Windows::Forms::Label());
			this->label2 = (gcnew System::Windows::Forms::Label());
			this->date_Control = (gcnew System::Windows::Forms::DateTimePicker());
			this->button1 = (gcnew System::Windows::Forms::Button());
			this->SuspendLayout();
			// 
			// tbx_From
			// 
			this->tbx_From->Location = System::Drawing::Point(28, 43);
			this->tbx_From->Name = L"tbx_From";
			this->tbx_From->Size = System::Drawing::Size(100, 20);
			this->tbx_From->TabIndex = 0;
			// 
			// tbx_To
			// 
			this->tbx_To->Location = System::Drawing::Point(147, 43);
			this->tbx_To->Name = L"tbx_To";
			this->tbx_To->Size = System::Drawing::Size(100, 20);
			this->tbx_To->TabIndex = 1;
			// 
			// label1
			// 
			this->label1->AutoSize = true;
			this->label1->Location = System::Drawing::Point(28, 24);
			this->label1->Name = L"label1";
			this->label1->Size = System::Drawing::Size(17, 13);
			this->label1->TabIndex = 2;
			this->label1->Text = L"Z ";
			// 
			// label2
			// 
			this->label2->AutoSize = true;
			this->label2->Location = System::Drawing::Point(144, 27);
			this->label2->Name = L"label2";
			this->label2->Size = System::Drawing::Size(21, 13);
			this->label2->TabIndex = 3;
			this->label2->Text = L"Do";
			// 
			// date_Control
			// 
			this->date_Control->Format = System::Windows::Forms::DateTimePickerFormat::Time;
			this->date_Control->Location = System::Drawing::Point(28, 108);
			this->date_Control->Name = L"date_Control";
			this->date_Control->Size = System::Drawing::Size(96, 20);
			this->date_Control->TabIndex = 4;
			this->date_Control->Value = System::DateTime(2014, 9, 27, 10, 58, 0, 0);
			// 
			// button1
			// 
			this->button1->Location = System::Drawing::Point(158, 108);
			this->button1->Name = L"button1";
			this->button1->Size = System::Drawing::Size(75, 23);
			this->button1->TabIndex = 5;
			this->button1->Text = L"Wyszukaj";
			this->button1->UseVisualStyleBackColor = true;
			this->button1->Click += gcnew System::EventHandler(this, &TSearch::button1_Click);
			// 
			// TSearch
			// 
			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
			this->ClientSize = System::Drawing::Size(266, 164);
			this->Controls->Add(this->button1);
			this->Controls->Add(this->date_Control);
			this->Controls->Add(this->label2);
			this->Controls->Add(this->label1);
			this->Controls->Add(this->tbx_To);
			this->Controls->Add(this->tbx_From);
			this->Name = L"TSearch";
			this->Text = L"TSearch";
			this->Load += gcnew System::EventHandler(this, &TSearch::TSearch_Load);
			this->ResumeLayout(false);
			this->PerformLayout();

		}
#pragma endregion

	private: System::Void TSearch_Load(System::Object^  sender, System::EventArgs^  e) {
				
			 }
			 

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
			 Map = gcnew TMap;
			 Information = gcnew TInfomation;
				
			Map->Show();
			Information->Show();
			
		
			
		
			
			
		 }
};
}
 

nadal niestety coś nie działa.

0
             Map = gcnew TMap;

A jak ma działać, skoro za każdym razem tworzysz nowe okno?

if (Map == nullptr)
{
    Map = gcnew TMap();
    Map->Show();
}
0

tak od razu się spytam. W jakiej książce lub czy może jest jakaś strona, gdzie będę miał rozpisane najlepiej w języku PL, możliwości danego typu obiektu ?
np." Map-> " wyskakuje mi wiele możliwości itd

0

Od tego właśnie jest dokumentacja. Tam znajdziesz odpowiedź na wszystkie nurtujące Cię pytania odnośnie jak coś jest zrobione, jak działa, co ma w środku itp. Musisz nauczyć się korzystać z dokumentacji :)

0

w dokumentacji .NET

0

Wrzucenie zmienne Map i Information rozwiązało problem i nie duplują mi się okna ale niestety pojawił się nowy problem.
Sytaucja wygląda tak Form search jest jakby "głównym", posiada on button1, który umożliwia otworzenie dwóch nowych form Map i Information. Obecnie niestety gdy zamykam jedno z tych dodatkowych form i klikam powtórnie na button ta forma się nie pojawia.

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

				
					 if (Map == nullptr) {
						 Map = gcnew TMap;	
						 Map->Show();
						 Map->Location =  Point(this->Location.X + this->Size.Width,this->Location.Y);
						 Map->Size = System::Drawing::Size( 500, 500 );

					 }
					 else
					 {
						 Map->Refresh();
					 }


					 if (Information == nullptr)  {
						 Information = gcnew TInfomation;
						 Information->Show();
						 Information->Location = Point(this->Location.X,this->Location.Y + this->Size.Height);
						 Information->Size = System::Drawing::Size(this->Size.Width,Map->Size.Height - this->Size.Height); 


					 }
					 else
					 {
						 Information->Refresh();
					 }


			 }


private: System::Void TSearch_FormClosing(System::Object^  sender, System::Windows::Forms::FormClosingEventArgs^  e) {
			 Map->Close();
			Information->Close();
		 }
};
0

Zamknięcie okna nie sprawi że zmienna przybierze wartość nullptr, więc wykona się Refresh() na zamkniętym oknie.

0

a więc co mam zrobić by po zamknięciu Map lub Information miało wartość nullprt ?

0
this->Map = gcnew TMap();
this->Map->FormClosed += gcnew FormClosedEventHandler(this, &Form1::Map_FormClosed);

...

void Map_FormClosed(Object ^sender, FormClosedEventArgs ^e)
{
    this->Map = nullptr;
}

1 użytkowników online, w tym zalogowanych: 0, gości: 1