Hey,
I have the code:

Form1(void)
{
	array<Label ^>^ labels = gcnew array<Label ^>(itemCount);
	InitializeComponent();
	for (int i = 0; i < itemCount; i++)
	{
		labels[i] = (gcnew Label());
		labels[i]->Tag = (int)i;
		labels[i]->Location =
			Point(1, (i + 1) * 50);
		labels[i]->Size =
			System::Drawing::Size(250, 50);
		this->Controls->Add(labels[i]);
	}
}

This works perfectly. I want the labels to highlight, which I will do in the form-mouse-move event, as I will be placing controls over the labels etc.

private: System::Void Form1_MouseMove(System::Object^  sender, System::Windows::Forms::MouseEventArgs^  e)
{
	Point^ y = e->Location;
	if (itemCurrent != -1)
	{
		this->labels[itemCurrent]->BackColor =
			System::Drawing::Color::DimGray;
	}
}

Firstly, I want the Y value to be an int; I can't remember how to get the Y value of a co-ordinate.
But more importantly, I cannot change the label colour, as it will not register that its existence.

Recommended Answers

All 12 Replies

To get the Y value, int y = e->Location.Y; Do you need to know the location and change the label simultaneously? I think it's much easier to handle the color change with a mouse_enter and mouse_leave event of the label rather than the form. However, in doing so the eventargs of the mouse_enter does not have position information any longer.

To get the Y value, int y = e->Location.Y; Do you need to know the location and change the label simultaneously? I think it's much easier to handle the color change with a mouse_enter and mouse_leave event of the label rather than the form. However, in doing so the eventargs of the mouse_enter does not have position information any longer.

Well, I'll be having picture boxes and buttons, which will tie to the labels if you will. So this looks like the easiest option.

Where is itemCurrent coming from in your code above? Seems to me you could match your sender to the desired control and run another static method when that is true.

Where is itemCurrent coming from in your code above? Seems to me you could match your sender to the desired control and run another static method when that is true.

itemCurrent is a global variable; it's just the selected item, as I will be referencing it in other functions.

int itemHover = (e->Location.Y - 1) / 50;
if (itemCurrent != itemHover)
{
	this->labels[itemCurrent]->BackColor =
		System::Drawing::Color::Black;
	itemCurrent = itemHover;
	if (itemCurrent > 0)
	{
		this->labels[itemCurrent]->BackColor =
			System::Drawing::Color::DimGrey;
	}
}

That's what I'm trying to do.

I still think it might be easier to do it label by label (you would only need one event handler). If you have the Tag free (it looked like you were using it for something before) you could tag the label with its position in the array and use that to compare to the sender. That way you wouldn't need the position information.
I think it could work your way I'm just not coming up with it, though.

I still think it might be easier to do it label by label (you would only need one event handler). If you have the Tag free (it looked like you were using it for something before) you could tag the label with its position in the array and use that to compare to the sender. That way you wouldn't need the position information.
I think it could work your way I'm just not coming up with it, though.

I don't have a fixed amount of labels.
I have done this in C# (I'm actually just rewriting an existing program I have).

What's not transferring over from the C# properly? In doing this I realize it would be nice for C++/CLI to have an "is" keyword.

What's not transferring over from the C# properly? In doing this I realize it would be nice for C++/CLI to have an "is" keyword.

My problem is that it it won't register the label array.

What do you mean by register?

How is it declared?

What do you mean by register?

How is it declared?

The code in my first post shows how it is declared.

Yes, you are declaring the labels array locally within the scope of the constructor. Place the array<Label ^>^ labels; with the other private variables of the form, that way it's local to the whole class.
I didn't understand that was a missing piece I was focusing on the logic of your method.

Solved:

private: array<Label^>^ labels;
public:
Form1(void)
{
labels = gcnew array<Label^>(itemCount);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.