none
showing another form from main form? RRS feed

  • Question

  • Hi, i am trying to access another form from Form1 but i am running in to issues.  I have 2 forms, (Form1 and AddItem)  I  want to be access controls on form 1 via AddItem so  this is what i did.

    in AddItem form code

    include"From1.h"

    near initializecomponents()
    Add_Form = gcnew Form1


    public: Form1^ Add_Form

    button1()
    {

     AddItem ^Add_Form = gcnew AddItem;
    add_Form-> what ever controls are on form 1 etc

    }

    This works fine but when i try to add include"AddItem.h" to Form 1 to access controls from there, i get like 23 errors(Without adding anything else, just that 1 line of code) . I Dont know how to access controls between the two forms at the same time, its either i access controls on form1 via AddItem or access controls on AddItem via Form1.

    Can someone help me out? Sorry its a bit confusing, ill post some code when i arrive  home.
    • Moved by Kira Qian Monday, February 15, 2010 6:21 AM (From:Windows Forms General)
    Monday, February 8, 2010 1:19 AM

All replies

  • Hi cruisx,

    Your code confused me.
    Add_Form = gcnew Form1
    public: Form1^ Add_Form
    So Add_Form is Form1

    AddItem ^Add_Form = gcnew AddItem;
    Add_Form is AddItem.

    > when i try to add include"AddItem.h" to Form 1 to access controls from there

    The code you show me is the AddItem form code. How about the code of Form1? Could you please show both code and avoid using confuse name for class instance?

    > add_Form-> what ever controls are on form 1 etc

    If you want to access to a control, you need to mark it public first. I don’t know what error raised on your side. Could you please post them?

    Sincerely,
    Kira Qian
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Monday, February 8, 2010 7:26 AM
  • Sorry, ill post some code.

    This is the code for Form 2(called AddItem) I can access form 1 controls from here and make form 1 visible etc.

    #pragma once
    #include "AddItem.h"
    namespace multifrm {
    
    	using namespace System;
    	using namespace System::ComponentModel;
    	using namespace System::Collections;
    	using namespace System::Windows::Forms;
    	using namespace System::Data;
    	using namespace System::Drawing;
    	using namespace System::IO;
    
    
    	/// <summary>
    	/// Summary for Form1
    	///
    	/// WARNING: If you change the name of this class, you will need to change the
    	///          'Resource File Name' property for the managed resource compiler tool
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    
    	public ref class Form1 : public System::Windows::Forms::Form
    	{
    	public:
    		Form1(void)
    		{
    			InitializeComponent();
    			CreateMyListView();
    //frm2 is AddItem form			
    frm2 = gcnew AddItem;
    
    
    			//
    			//TODO: Add the constructor code here
    			//
    		}
    
    	protected:
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		~Form1()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    	private: System::Windows::Forms::ListView^  listView1;
    	private: System::Windows::Forms::Button^  button1;
    	private: System::Windows::Forms::Button^  button2;
    	private: System::Windows::Forms::Button^  button3;
    	private: System::Windows::Forms::TextBox^  textBox1;
    
    	protected: 
    
    	protected: 
    
    	public:
    		AddItem^ frm2;
    
    
    
    
    
    	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->listView1 = (gcnew System::Windows::Forms::ListView());
    			this->button1 = (gcnew System::Windows::Forms::Button());
    			this->button2 = (gcnew System::Windows::Forms::Button());
    			this->button3 = (gcnew System::Windows::Forms::Button());
    			this->textBox1 = (gcnew System::Windows::Forms::TextBox());
    			this->SuspendLayout();
    			// 
    			// listView1
    			// 
    			this->listView1->Location = System::Drawing::Point(14, 12);
    			this->listView1->Name = L"listView1";
    			this->listView1->Size = System::Drawing::Size(312, 267);
    			this->listView1->TabIndex = 0;
    			this->listView1->UseCompatibleStateImageBehavior = false;
    			// 
    			// button1
    			// 
    			this->button1->Location = System::Drawing::Point(36, 326);
    			this->button1->Name = L"button1";
    			this->button1->Size = System::Drawing::Size(59, 41);
    			this->button1->TabIndex = 1;
    			this->button1->Text = L"Add Task";
    			this->button1->UseVisualStyleBackColor = true;
    			this->button1->Click += gcnew System::EventHandler(this, &Form1::button1_Click);
    			// 
    			// button2
    			// 
    			this->button2->Location = System::Drawing::Point(141, 326);
    			this->button2->Name = L"button2";
    			this->button2->Size = System::Drawing::Size(59, 41);
    			this->button2->TabIndex = 2;
    			this->button2->Text = L"Remove Task";
    			this->button2->UseVisualStyleBackColor = true;
    			this->button2->Click += gcnew System::EventHandler(this, &Form1::button2_Click);
    			// 
    			// button3
    			// 
    			this->button3->Location = System::Drawing::Point(245, 326);
    			this->button3->Name = L"button3";
    			this->button3->Size = System::Drawing::Size(59, 41);
    			this->button3->TabIndex = 3;
    			this->button3->Text = L"button3";
    			this->button3->UseVisualStyleBackColor = true;
    			this->button3->Click += gcnew System::EventHandler(this, &Form1::button3_Click);
    			// 
    			// textBox1
    			// 
    			this->textBox1->Location = System::Drawing::Point(120, 297);
    			this->textBox1->Name = L"textBox1";
    			this->textBox1->Size = System::Drawing::Size(100, 20);
    			this->textBox1->TabIndex = 4;
    			// 
    			// Form1
    			// 
    			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->ClientSize = System::Drawing::Size(338, 379);
    			this->Controls->Add(this->textBox1);
    			this->Controls->Add(this->button3);
    			this->Controls->Add(this->button2);
    			this->Controls->Add(this->button1);
    			this->Controls->Add(this->listView1);
    			this->Name = L"Form1";
    			this->Text = L"Form1";
    			this->ResumeLayout(false);
    			this->PerformLayout();
    
    		}
    #pragma endregion
    
    
    		void CreateMyListView()
    		{
    			// Create a new ListView control.
    			//      ListView^ listView1 = gcnew ListView;
    			//    listView1->Bounds = Rectangle(Point(10,10),System::Drawing::Size( 300, 200 ));
    			//disable the remove button on start up. 
    			if(listView1->Items->Count <= 0)
    			{
    				// listView1->Items->Remove(temp1);
    				button2->Enabled = false;
    
    			}
    
    			// Set the view to show details.
    			listView1->View = View::Details;
    
    			// Allow the user to edit item text.
    			listView1->LabelEdit = true;
    
    			// Allow the user to rearrange columns.
    			listView1->AllowColumnReorder = true;
    
    			// Display check boxes.
    			listView1->CheckBoxes = true;
    
    			// Select the item and subitems when selection is made.
    			listView1->FullRowSelect = true;
    
    			// Display grid lines.
    			listView1->GridLines = true;
    
    			// Sort the items in the list in ascending order.
    			listView1->Sorting = SortOrder::None;//:Ascending;
    			/*
    			// Create three items and three sets of subitems for each item.
    			ListViewItem^ item1 = gcnew ListViewItem( "item1",0 );
    
    			// Place a check mark next to the item.
    			item1->Checked = true;
    			item1->SubItems->Add( "1" );
    			//item1->SubItems->Add( "2" );
    			//item1->SubItems->Add( "3" );
    			ListViewItem^ item2 = gcnew ListViewItem( "item2",1 );
    			item2->SubItems->Add( "4" );
    			//item2->SubItems->Add( "5" );
    			//item2->SubItems->Add( "6" );
    			ListViewItem^ item3 = gcnew ListViewItem( "item3",0 );
    
    
    			// Place a check mark next to the item.
    			item3->Checked = true;
    			item3->SubItems->Add( "7" );
    			item3->SubItems->Add( "8" );
    			item3->SubItems->Add( "9" );
    
    			ListViewItem^ item4 = gcnew ListViewItem( "item4",0 );
    
    			ProgressBar ^ pb = gcnew ProgressBar();
    			pb->Value = 40;
    			pb->BringToFront();
    			item4->SubItems->Add(pb->ToString());
    
    			*/
    			// Create columns for the items and subitems.
    			listView1->Columns->Add("col one test",-2,System::Windows::Forms::HorizontalAlignment::Left);
    
    
    			// listView1->Columns->Add( "Todo List2", -2, HorizontalAlignment::);
    			// listView1->Columns->Add( "Column 2", -2, HorizontalAlignment::Left );
    			//listView1->Columns->Add( "Column 3", -2, HorizontalAlignment::Left );
    			//listView1->Columns->Add( "Column 4", -2, HorizontalAlignment::Center );
    
    
    			//Add the items to the ListView.
    			/*	 		array<ListViewItem^>^temp1 = {item1,item2,item3,item4};
    			listView1->Items->AddRange( temp1 );
    			*/
    			// Create two ImageList objects.
    			ImageList^ imageListSmall = gcnew ImageList;
    			ImageList^ imageListLarge = gcnew ImageList;
    
    
    			//Assign the ImageList objects to the ListView.
    			//    listView1->LargeImageList = imageListLarge;
    			//  listView1->SmallImageList = imageListSmall;
    
    
    			//image to a list view
    
    
    
    
    
    
    
    			//listView1->BackgroundImage = Image::FromFile("backtest.jpg");
    
    
    			// Add the ListView to the control collection.
    			this->Controls->Add( listView1 );
    		}
    
    
    
    
    
    
    
    
    
    
    
    	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
    				 //Form1 ^Add_Form = gcnew Form2;
    				 //Add_Form->Show();
    				 //Add_Form->Show();
    				 array<String^>^ListItems = {textBox1->Text};
    				 if (  !textBox1->Text->Equals( "" ) )
    				 {
    					 //if there is a valid entry, enable the remove item button
    					 button2->Enabled = true;
    
    
    
    					 ListViewItem ^itm = gcnew ListViewItem(textBox1->Text);//->SelectedItems[0]; 
    					 textBox1->Text = itm->Text;
    					 listView1->Items->Add(itm);//->BackColor = Color::Pink;
    
    
    					 listView1->Items[0]->BackColor = Color::LightGreen;
    
    
    				 }
    			 }
    	private: System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) {
    
    
    
    
    				 array<ListViewItem^>^temp1 = {listView1->Items[0]};
    				 // listView1->Items[0]->Remove();
    
    
    				 for (int i = listView1->SelectedItems->Count - 1; i >= 0; i--)
    				 {
    					 listView1->SelectedItems[i]->Remove();
    
    				 }
    				 listView1->Refresh();
    
    
    				 //This is where the button is disabled if there is nothing in list
    				 if(listView1->Items->Count <= 0)
    				 {
    					 // listView1->Items->Remove(temp1);
    					 button2->Enabled = false;
    
    				 }
    
    
    
    
    
    				 //for (int i = listView1->SelectedItems->Count - 1; i >= 0; i--)
    				 //{
    				 // for each(ListViewItem ^i in listView1->SelectedItems)
    
    
    
    				 //ListViewItem^ li = listView1->SelectedItems[i];
    				 //listView1->Items->Remove(li);
    
    				 // listView1->Items(listView1->Items->remo
    
    				 //}
    
    
    
    
    			 }
    
    
    
    	private: System::Void button3_Click(System::Object^  sender, System::EventArgs^  e) {
    
    				 AddItem ^frm2 = gcnew AddIdem();
    
    				 frm2->Show();
    
    
    
    
    
    			 }
    	};
    }
    
    
    

    This is the Form1 code. See when i put in Additem.h I start to get errors right away,i dont see the issue, the code used to display the form/access controls is the exact same as the one above. Any idea whats up.

    #pragma once
    
    #include "Form1.h"
    
    
    
    namespace multifrm {
    	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 AddItem
    	///
    	/// WARNING: If you change the name of this class, you will need to change the
    	///          'Resource File Name' property for the managed resource compiler tool
    	///          associated with all .resx files this class depends on.  Otherwise,
    	///          the designers will not be able to interact properly with localized
    	///          resources associated with this form.
    	/// </summary>
    	public ref class AddItem : public System::Windows::Forms::Form
    	{
    	public:
    		AddItem(void)
    		{
    			InitializeComponent();
    
    //Add_Form is form1 Add_Form = gcnew Form1; // //TODO: Add the constructor code here // } protected: /// <summary> /// Clean up any resources being used. /// </summary> ~AddItem() { if (components) { delete components; } } public: System::Windows::Forms::TextBox^ textBox1; protected: public: Form1^ Add_Form; private: System::Windows::Forms::Label^ label1; public: System::Windows::Forms::ComboBox^ comboBox1; public: System::Windows::Forms::Button^ button1; 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->textBox1 = (gcnew System::Windows::Forms::TextBox()); this->label1 = (gcnew System::Windows::Forms::Label()); this->comboBox1 = (gcnew System::Windows::Forms::ComboBox()); this->button1 = (gcnew System::Windows::Forms::Button()); this->SuspendLayout(); // // textBox1 // this->textBox1->Location = System::Drawing::Point(80, 26); this->textBox1->Name = L"textBox1"; this->textBox1->Size = System::Drawing::Size(155, 20); this->textBox1->TabIndex = 0; // // label1 // this->label1->AutoSize = true; this->label1->Location = System::Drawing::Point(37, 29); this->label1->Name = L"label1"; this->label1->Size = System::Drawing::Size(40, 13); this->label1->TabIndex = 1; this->label1->Text = L"To Do:"; // // comboBox1 // this->comboBox1->FormattingEnabled = true; this->comboBox1->Location = System::Drawing::Point(241, 26); this->comboBox1->Name = L"comboBox1"; this->comboBox1->Size = System::Drawing::Size(44, 21); this->comboBox1->TabIndex = 2; // // button1 // this->button1->Location = System::Drawing::Point(109, 103); this->button1->Name = L"button1"; this->button1->Size = System::Drawing::Size(107, 34); this->button1->TabIndex = 3; this->button1->Text = L"Add To List"; this->button1->UseVisualStyleBackColor = true; this->button1->Click += gcnew System::EventHandler(this, &AddItem::button1_Click); // // AddItem // this->AutoScaleDimensions = System::Drawing::SizeF(6, 13); this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font; this->ClientSize = System::Drawing::Size(331, 149); this->Controls->Add(this->button1); this->Controls->Add(this->comboBox1); this->Controls->Add(this->label1); this->Controls->Add(this->textBox1); this->Name = L"AddItem"; this->Text = L"AddItem"; this->ResumeLayout(false); this->PerformLayout(); } #pragma endregion private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) { Form1 ^Add_Form = gcnew Form1; Add_Form->Show(); } }; }


    And these are the errors i am getting. Maybe i am doing it wrong but this is how i was told to display/access other forms. The weird thing is that intelliSense sees that frm2->show() is valid but i still get errors.

    Error	2	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\adil\documents\visual studio 2008\projects\multifrm\multifrm\Form1.h	62
    Error	3	error C4430: missing type specifier - int assumed. Note: C++ does not support default-int	c:\users\adil\documents\visual studio 2008\projects\multifrm\multifrm\Form1.h	62
    Error	10	error C2227: left of '->Show' must point to class/struct/union/generic type	c:\users\adil\documents\visual studio 2008\projects\multifrm\multifrm\Form1.h	341
    Error	1	error C2143: syntax error : missing ';' before '^'	c:\users\adil\documents\visual studio 2008\projects\multifrm\multifrm\Form1.h	62
    Error	4	error C2065: 'frm2' : undeclared identifier	c:\users\adil\documents\visual studio 2008\projects\multifrm\multifrm\Form1.h	32
    Error	7	error C2065: 'frm2' : undeclared identifier	c:\users\adil\documents\visual studio 2008\projects\multifrm\multifrm\Form1.h	339
    Error	9	error C2065: 'frm2' : undeclared identifier	c:\users\adil\documents\visual studio 2008\projects\multifrm\multifrm\Form1.h	341
    Error	6	error C2065: 'AddItem' : undeclared identifier	c:\users\adil\documents\visual studio 2008\projects\multifrm\multifrm\Form1.h	339
    Error	5	error C2061: syntax error : identifier 'AddItem'	c:\users\adil\documents\visual studio 2008\projects\multifrm\multifrm\Form1.h	32
    Error	8	error C2061: syntax error : identifier 'AddIdem'	c:\users\adil\documents\visual studio 2008\projects\multifrm\multifrm\Form1.h	339
    
    Monday, February 8, 2010 11:02 PM
  • Hi cruisx,

    From your code I know you want to show Form1 when the button1 on Form2 is clicked. The code seems to be ok. Some error shows your miss type in the code. For example, syntax error : missing ';' before '^'. You can double click the error line to view the error point in the code.

    “error C2065: 'frm2' : undeclared identifier”. That seems strange. I saw you have defined the form correctly.

    “error C4430: missing type specifier - int assumed.” This is also strange to me. I suggest you to post it on VC++ forum. It is much closer to language problem. The C++ compiler doesn’t work on my computer either.

    VC++ forum
    http://social.msdn.microsoft.com/Forums/en/vclanguage/threads


    Sincerely,
    Kira Qian
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework!
    Tuesday, February 9, 2010 2:55 AM