void BOARD_Unselect_All(System::Windows::Forms::Form^ Frm)
{
  Frm.a1->BackgroundImage = Image::FromFile("elephant_pla.gif");
}

a1 is a picturebox from the form. How do you use the buttons, picturebox, etc from the form being passed in?

Did you set up event handling functions for the controls that are on the form being passed in? If not then you need to do that because that is what makes the controls do things.

yeah, the click event handler is taken care of. This is just setting the background image which shouldn't have to be done in the event handler. I just need to know how to access that picturebox so I can change it's image, this must be done through a parameter like this because of the situation I'm in with the program.

Edited 6 Years Ago by homeryansta: n/a

that is the correct syntax for vb.net which I know pretty well since I interned over the summer for a company that utilizes it. From my understanding, the keyword "this" works in the form itself through the event handlers and other functions, so it will not work when passed into a function of another class. And believe me, I've tried searching and searching, but cannot find anything.

However, from the form, this is how the item passed in looks like.

BOARD.BOARD_Unselect_All(this);

where board is the object name and "this" referring to the form itself since it is being passed into BOARD from there.

>>that is the correct syntax for vb.net
The example is for C#, but vb.net and c++/CLR Forms is identical.

just replace "this" with the name of the form -- in the example you posted it would be "frm".

The unfortunate thing is that the controls are declared private to the window class, therefore you will not be allowed to change the picture from another class. But I suppose you could change it from private to public, or write a public method that wraps the private controls.

Edited 6 Years Ago by Ancient Dragon: n/a

the syntax for C++ is different from vb.net and c# it looks like. As you can see from my example above, it is not the same as what we thought it was.

note that if I was passing in a picturebox instead of a form then this is the correct syntax in C++. However, I need a form to be passed in so I can extract all the picturebox from it. Let's just say that there are 42 different picture boxes that I need to bring in. As you can tell... that is alot of parameter if I were to bring in all of them. Hence the reason why I need the form to be passed in instead.

void BOARD_Unselect_All(System::Windows::Forms::PictureBox^ a1)

{

   a1->BackgroundImage = Image::FromFile("elephant_pla.gif");

}

Edited 6 Years Ago by homeryansta: n/a

Doesn't this work? It worked for me after I made PictureBox1 public instead of private.

#include "Form`.h"
...
<snip>
        void BOARD_Unselect_All(FormsTest::Form1^ Frm)
        {
            Frm->pictureBox1->BackgroundImage = Image::FromFile("elephant_pla.gif");
        }

Hi
From reading the theads to your post, I understand that you want access to a control on a form outside of the class the form is created in.
If this is what you are trying to do then there two ways to do it.
One way (the prefered and accepted way)is to create a delegate (callback) to the code that accesses the control you want to change.
Or you can "include" the header file (the form that has the control and code you want to access)in the the class that you want to assess the code/control from. This method is not always practicle because it can create circular referencing.
It makes no difference if the code is private or public in a class, you can not access this code outside the class it is in.

Hope this will help you get started
Milton

>>makes no difference if the code is private or public in a class, you can not access this code outside the class it is in.


Wrong. See the code snippet I posted -- which I tested before I posted it. The objects in Frm had to be made public in order for that to work.

Edited 6 Years Ago by Ancient Dragon: n/a

Hello AD

Yes I see that, maybe you miss understood my post.
<< Or you can "include" the header file (the form that has the control and code you want to access)in the the class that you want to assess the code/control from. This method is not always practicle because it can create circular referencing.>>

I see in your snippet that you included the "Form.h" and this is what I was stating. If you did not include this header you can not access Form.h objects/code regardless of private or public.

Milton

Edited 6 Years Ago by Milton Neal: update

Just because it "could" create circular references doesn't prevent the coder from including the header file when you know that it won't.


>>If you did not include this header you can not access Form.h objects/code regardless of private or public.

Of course not -- that's just normal C/C++. Actually, you wouldn't be able to use the form at all for anything if you don't include the header file.

I don't actually like the idea of changing the access type from private to public, but then I'm not the one writing the program. IMO it would be better design to write a public method in Form1 that updates all its image controls, then call that public method BOARD_Unselect_All(). That way the data in Form1 can remain private.

Edited 6 Years Ago by Ancient Dragon: n/a

I'm still having issues with this passing of a form. It is not recognizing a1 as a picture box.

void BOARD::BOARD_CPU_TURN(VAR &objr_VAR, System::Windows::Forms::Form^ Frm)
{
	//
	//	CPU's move
	//

	int intl_level = 0;	//	initial level is set to 0 for root(current state)
	int intl_depth = 3;	//	plies we want to look ahead
	int intl_alpha = -999999;	//	initial value of alpha
	int intl_beta = 999999;	//	initial value of beta
	String^ stwl_move = "";
	int intl_move;

	MessageBox::Show("in cpu move mode");

	intl_move = BOARD_min_alpha_beta(objr_VAR, intl_alpha, intl_beta, intl_level, intl_depth);
	stwl_move += intl_move;
	MessageBox::Show(stwl_move);

	// cpu does random move towards opponent's den
	if(intl_move == 0)
	{
		srand(time(NULL));

		intl_move = rand() % 4;

		if(intl_move == 0)
		{
			Frm->a1->BackgroundImage = Image::FromFile("elephant_cpu_select.gif");
		}
		else if(intl_move == 1)
		{
			//Frm->BackgroundImage = Image::FromFile("elephant_cpu_select.gif");
		}
		else if(intl_move == 2)
		{
			//Frm->BackgroundImage = Image::FromFile("elephant_cpu_select.gif");
		}
		else
		{
			//Frm->BackgroundImage = Image::FromFile("elephant_cpu_select.gif");
		}
	}
}

Here is a snapshot of what's in Form1.h just to show that a1 does indeed exist and can be used if I'm passing in a1 as parameter instead of the whole form itself.

.
.
.
.
	private: System::Windows::Forms::Label^  lbl_d;
	private: System::Windows::Forms::Label^  lbl_c;
	private: System::Windows::Forms::Label^  lbl_b;
	private: System::Windows::Forms::Label^  lbl_a;
	private: System::Windows::Forms::ToolStripMenuItem^  exitToolStripMenuItem;
	private: System::Windows::Forms::MenuStrip^  menuStrip1;
	private: System::Windows::Forms::ToolStripMenuItem^  fileToolStripMenuItem;
	private: System::Windows::Forms::ToolStripMenuItem^  resetToolStripMenuItem;
	private: System::Windows::Forms::PictureBox^  a1;
	private: System::Windows::Forms::PictureBox^  a2;
	private: System::Windows::Forms::PictureBox^  a3;
	private: System::Windows::Forms::PictureBox^  a4;
	private: System::Windows::Forms::PictureBox^  a5;
	private: System::Windows::Forms::PictureBox^  a6;
	private: System::Windows::Forms::PictureBox^  a7;
	private: System::Windows::Forms::PictureBox^  b7;
	private: System::Windows::Forms::PictureBox^  b6;
	private: System::Windows::Forms::PictureBox^  b5;
	private: System::Windows::Forms::PictureBox^  b4;
	private: System::Windows::Forms::PictureBox^  b3;
.
.
.
.

Edited 6 Years Ago by homeryansta: n/a

hi
I think the best way to over come your from is by using delegates.
I mentioned this in an earlier posting.
I will show you the event method delegate.

To start, declare the delegate in your BOARD class

public delegate void delegate_DrawBackground(String^ filename);

public ref class BOARD
{
public:
	event delegate_DrawBackground^ BackgroundDraw;
.
.
.
}

Include the BOARD class in your Frm1 class
#include "BOARD.h"
Next declare the method(function) we will use as the delegate.
This is done in your InitializeComponent of your Frm1 class.

public ref class Frm1 : public System::Windows::Forms::Form
	{
	public:
		Form1(void)
		{
			InitializeComponent();
			//
			//TODO: Add the constructor code here
			//
			BOARD->BackgroundDraw += gcnew delegate_DrawBackground(this, &Frm1::PaintPicBox);
                            .
                            .
                            .
		}
         .
         .
         .

Next create the method in the Frm1 class to paint the pic's background image.
Note: To avoid a cross thead exception a local copy of the delegate is used.

private: void PaintPicBox(String^ filename)
		{
			if (this->a1->InvokeRequired)
			{
				delegate_DrawBackground^ dbi = 
					gcnew delegate_DrawBackground(this, &Frm1::PaintPicBox);
				this->Invoke(dbi, gcnew array<Object^> {filename});
			}
			else
			{
				Frm->a1->BackgroundImage =(Image::FromFile   (filename); 
			}
		}

In your code (line 29) use:

//Frm->a1->BackgroundImage = Image::FromFile("elephant_cpu_select.gif");
BackgroundDraw("elephant_cpu_select.gif");

And that's about it.
Let me know how you get on.
Milton

This article has been dead for over six months. Start a new discussion instead.