9 Replies - 882 Views - Last Post: 17 May 2017 - 02:28 PM

#1 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Referencing Windows Form Controls on Seperate Forms

Posted 26 January 2017 - 03:16 PM

Hello,
Lets say I want to make a checkBox1 on "Form1.h" (UI Header file) in a CRC/Windows Forms application and have it control checkBox2 on "Form2.h" ... so that they are synchronized: Does anyone know how to link together two controls that are in two different forms when using CLI/C++ with .NET extensions?
Is This A Good Question/Topic? 0
  • +

Replies To: Referencing Windows Form Controls on Seperate Forms

#2 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13493
  • View blog
  • Posts: 53,897
  • Joined: 12-June 08

Re: Referencing Windows Form Controls on Seperate Forms

Posted 26 January 2017 - 03:21 PM

Did form2 get spawned by form1?

I would suggest investigating events.
https://msdn.microso...y/58cwt3zh.aspx
Was This Post Helpful? 0
  • +
  • -

#3 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Referencing Windows Form Controls on Seperate Forms

Posted 26 January 2017 - 03:37 PM

View Postmodi123_1, on 26 January 2017 - 03:21 PM, said:

Did form2 get spawned by form1?

I would suggest investigating events.
https://msdn.microso...y/58cwt3zh.aspx


Well I could make one spawn off the other; does that change the process for linking two controls?

BTW/// I DID search google for this... i've been searching for the past week and I can't find a good answer. Lots of great responses in C# but all of the C++ solutions refuse to recognize the control that isn't in the header file I'm coding in. So i can reference controls in whatever header im working with but if I try and go to a control that's created in another header file the IDE acts like there's no control with that name.
Was This Post Helpful? 0
  • +
  • -

#4 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13493
  • View blog
  • Posts: 53,897
  • Joined: 12-June 08

Re: Referencing Windows Form Controls on Seperate Forms

Posted 26 January 2017 - 03:40 PM

Events are the common methodology for GUI forms and I have plenty of child forms created/displayed from the parent... one object shoots up events, the other hears it.. that or when the child is closed the parent raids the properties to do gui updates or what not. Though I have no idea what you are doing so your mileage may vary.
Was This Post Helpful? 0
  • +
  • -

#5 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Referencing Windows Form Controls on Seperate Forms

Posted 26 January 2017 - 04:15 PM

Ok yeah I'll look into that... see I was expecting some simple solution to this because usually CLI is pretty easy to adapt to what I'm trying to do. I'd really just like something like this...
#include "Form2.h"
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
	{
if (checkBox1->Checked = true)
{
         checkBox2->Checked = true;
}
	}




except in two different forms... I tried this and it didn't work:
#include "Form2.h"
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
	{
if (Form1::checkBox1->Checked = true)
{
         Form2::checkBox2->Checked = true;
}
	}



and if I try to just use checkBox2 without the Form2 part it just says I haven't declared checkBox2 even though the IDE knows it's an object in the project
Was This Post Helpful? 0
  • +
  • -

#6 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13493
  • View blog
  • Posts: 53,897
  • Joined: 12-June 08

Re: Referencing Windows Form Controls on Seperate Forms

Posted 26 January 2017 - 04:15 PM

You would need a handle to the actual instance of the form in memory.

Though I am not sure why you would have two gui objects doing the same action in two forms. Seems hinky.
Was This Post Helpful? 1
  • +
  • -

#7 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Referencing Windows Form Controls on Seperate Forms

Posted 26 January 2017 - 04:57 PM

Yeah that's actually not what I'm using this for but I figured it would be an easy way to describe what I'm trying to do. What I'm really attempting to do is take a value from a trackbar position and translate that into a variable for Red green and blue in the background of another form... I know that if I can find the referencing syntax for interform control interaction I'll be able to adapt it to any form; because that's how I've been teaching myself to use CLI for months... not through tutorials but from small examples which I adapt into my own applications.

I also toyed around with the idea of using global variables but I can't get that to work either.

I guess it's back to the drawing board. I'll post my solution once I figure this out, as I have been doing.
Was This Post Helpful? 0
  • +
  • -

#8 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Referencing Windows Form Controls on Seperate Forms

Posted 14 March 2017 - 08:21 AM

I never did figure this out. It was weird I can get that example with the textboxes to work but I can't get it to work with the other variables. Perhaps I need to look into classes and events today... not knowing how to do this is becoming problematic lol. And yes.. :stupid: still using Winforms C++/.NET for one of my older ongoing projects
Was This Post Helpful? 0
  • +
  • -

#9 Sam Hobbs  Icon User is offline

  • D.I.C Head

Reputation: 11
  • View blog
  • Posts: 50
  • Joined: 01-April 17

Re: Referencing Windows Form Controls on Seperate Forms

Posted 06 April 2017 - 12:40 PM

The first issue to decide is how to show the two forms. One way is to show both of them from the main function and the other is to show the second form from the first. Since this is an old question I will be vague and I am not sure which of those that you are using or if you are trying something else.

In main there is a line that runs the message loop for the main form. We can use a different class (I think ApplicationContext) to run multiple forms for the application. If you do that then you will need code in whatever class the main function is using.

If you show the second form from the main form then you will do something like "Form2 f2 = new Form2();" and then you can use f2 to refer to the form. What you should do is to make a "property" in Form2 that the main form uses.
Was This Post Helpful? 1
  • +
  • -

#10 hexagod  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 242
  • Joined: 29-October 16

Re: Referencing Windows Form Controls on Seperate Forms

Posted 17 May 2017 - 02:28 PM

View PostSam Hobbs, on 06 April 2017 - 12:40 PM, said:

The first issue to decide is how to show the two forms. One way is to show both of them from the main function and the other is to show the second form from the first. Since this is an old question I will be vague and I am not sure which of those that you are using or if you are trying something else.

In main there is a line that runs the message loop for the main form. We can use a different class (I think ApplicationContext) to run multiple forms for the application. If you do that then you will need code in whatever class the main function is using.

If you show the second form from the main form then you will do something like "Form2 f2 = new Form2();" and then you can use f2 to refer to the form. What you should do is to make a "property" in Form2 that the main form uses.


Interesting!! Thanks for the info! I need to revisit this because I never figured out how to transfer variables from form to form. So basically what I need to do is assign classes in the main.h file or in the main.cpp? Should I set the classes in the header or in the .cpp file? If I set a class then will that class be available in any form spawned from the form that has the class information specified?

Thanks!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1