CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Mar 2009
    Posts
    20

    Question change the control value from cpp file

    hey any one can tell me how to change the value of any control say a comboBox in a win32 forms project from the source cpp file as the form is created in a .h file and control is added in the .h file and i want to write the code in .cpp file and acces control from cpp file can anyone tell me how.actually i am new to vc++ gui programming ..

    Code:
    #pragma once
    
    
    namespace diskinfo {
    
    	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 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();
    			//
    			//TODO: Add the constructor code here
    			//
    		}
    
    	protected:
    		/// <summary>
    		/// Clean up any resources being used.
    		/// </summary>
    		~Form1()
    		{
    			if (components)
    			{
    				delete components;
    			}
    		}
    	public: System::Windows::Forms::ComboBox^  comboBox1;
    	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->comboBox1 = (gcnew System::Windows::Forms::ComboBox());
    			this->SuspendLayout();
    			// 
    			// comboBox1
    			// 
    			this->comboBox1->FormattingEnabled = true;
    			this->comboBox1->Location = System::Drawing::Point(79, 52);
    			this->comboBox1->Name = L"comboBox1";
    			this->comboBox1->Size = System::Drawing::Size(121, 21);
    			this->comboBox1->TabIndex = 0;
    			this->comboBox1->SelectedIndexChanged += gcnew System::EventHandler(this, &Form1::comboBox1_SelectedIndexChanged);
    			// 
    			// Form1
    			// 
    			this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
    			this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
    			this->ClientSize = System::Drawing::Size(292, 266);
    			this->Controls->Add(this->comboBox1);
    			this->Name = L"Form1";
    			this->Text = L"Form1";
    			this->ResumeLayout(false);
    
    		}
    #pragma endregion
    	
    	private: System::Void comboBox1_SelectedIndexChanged(System::Object^  sender, System::EventArgs^  e) {
    			 }
    	};
    }

    this is the .h file i had created of the form it has a combo box added i just want is to hcange or acces this control from the cpp file shown next


    Code:
    
    // diskinfo.cpp : main project file.
    
    #include "stdafx.h"
    #include "Form1.h"
    #include"windows.h"
    #include"stdio.h"
    using namespace diskinfo;
    #define BUFSIZE 512
    [STAThreadAttribute]
    int main(array<System::String ^> ^args)
    {
    	char szTemp[512];
    	//szTemp[0] = '\0';
    	::GetLogicalDriveStringsA(BUFSIZE-1,szTemp);
    
    
    	char *pch = szTemp;
    while (*pch) {
    printf("%s\n", pch);
    pch = &pch[strlen(pch) + 1];
    }
    	
    	
    
       ::Form1.comboBox1.Visible=false; this is where iam trying to access the combobox
    
    	
    	// Enabling Windows XP visual effects before any controls are created
    	Application::EnableVisualStyles();
    	Application::SetCompatibleTextRenderingDefault(false); 
    
    	// Create the main window and run it
    	Application::Run(gcnew Form1());
    	return 0;
    
    
    
    }

  2. #2
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,234

    Re: change the control value from cpp file

    [ Moved thread ]
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: change the control value from cpp file

    To a conventional C++/Win32/MFC programmer there are quite a few puzzling aspects to your code.

    Firstly, the declaration of combobox1 (public: System::Windows::Forms::ComboBox^ comboBox1;) isn't a valid C++ declaration.

    Secondly, your header file assumes that combobox1 is a pointer but in your cpp file, you're treating it like an instantiated object.

    Thirdly, the declaration of class Form1 (public ref class Form1) suggests that you're writing managed code (which is covered in its own forum).

    Fourthly, the presence of a function called 'main' suggests that you're writing a console application, so you wouldn't be able to display a combo box anyway.

    My guess is that you might have posted your question in the wrong forum. Try the Managed C++ forum and hopefully, they'll understand your problem.


    [Edit...] Oops - thread got moved to the right forum while I was typing!!
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    Join Date
    Jan 2002
    Location
    Scaro, UK
    Posts
    5,940

    Re: change the control value from cpp file

    The best piece of advice I can give you is go and buy a book.

    That way you'll learn about headers and cpp files, where code goes, what a class is and how its member functions are defined.

    All these are fundamental concepts of the language which you are missing and not something I can describe in a forum response.

    Or if you want to save yourself a huge amount of trouble, go and learn C# instead. I personally wouldn't ever use C++/CLI to write UI because I like my sanity too much. And I've lost enough hair over programming already.

    Darwen.
    www.pinvoker.com - PInvoker - the .NET PInvoke Interface Exporter for C++ Dlls.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured