How to make a link to a structure in C #

In my application, I have a LineShape control and a custom control (essentially a PictureBox with a label).

I want LineShape to change one of its point coordinates, according to the location of the custom control.

I had the idea of โ€‹โ€‹linking to a LineShape point inside a custom control and adding a location change event handler that changes the coordinates of the anchor points.

However, an inline point is a structure that is a type of value, so this will not work. Does anyone have an idea how to link to a structure or maybe someone knows a workaround for my problem?

I tried the solution regarding using a NULL type, but it still does not work. Here I define a field in my custom control (DeviceControl):

private Point? mConnectionPoint; 

And the implementation of the location change event handler:

 private void DeviceControl_LocationChanged(object sender, EventArgs e) { if (mConnectionPoint != null) { DeviceControl control = (DeviceControl)sender; Point centerPoint= new Point(); centerPoint.X = control.Location.X + control.Width / 2; centerPoint.Y = control.Location.Y + control.Height / 2; mConnectionPoint = centerPoint; } } 
+8
reference c # struct
source share
2 answers

You can pass value types by reference by adding the 'ref' before this when passing in the method.

like this:

 void method(ref MyStruct param) { } 
+7
source share

Your method really does not require access to the mConnectionPoint element reference; You can assign location values โ€‹โ€‹to the directly referenced point as a member of your class:

 private void DeviceControl_LocationChanged(object sender, EventArgs e) { if (mConnectionPoint != null) { DeviceControl control = (DeviceControl)sender; mConnectionPoint.X = control.Location.X + control.Width / 2; mConnectionPoint.Y = control.Location.Y + control.Height / 2; } } 

However, if the reason for this code is to move the LineShape control, you must go straight to the source. The best way to change the properties of a control is to simply change the properties of the control:

  DeviceControl control = (DeviceControl)sender; line1.StartPoint = [calculate point1 coordinates]; line1.EndPoint = [calculate point2 coordinates]; 
0
source share

All Articles