0

I am currently working on a Editor, which lets the user design his own WinForm overlay, at least to a certain point. Therefore I want the user to decide, which AnchorStyles the current selected Control should have. I would like it to be handled by checkboxes. Here's how I had it in mind:

enter image description here

As you can see, the user has currently selected a dynamically added Panel, called Grid. Handled by the CheckBoxes to the right, he should now be able to set the selected Controls AnchorStyles. Here's my problem: I can't seem to find a usable solution, to dynamically add a specific AnchorStyle to the already existing ones, or the opposite, remove the AnchorStyle, but keep the other ones as they are.

I was trying to get it to work with...

SelectedControl.Anchor += AnchorStyles.Top;

which doesen't work at all. So i thought of this...

SelectedControl.Anchor = SelectedControl.Anchor | AnchorStyles.Top

which I imagine could work, but I haven't even tested it, since I wouldn't know how to remove ones unchecked AnchorStyle. Building a gigantic if(){} else if(){}... doesen't seem to be a good Idea :)

I'm open for any ideas / solutions. Thanks in advance!

arvenyon
  • 153
  • 1
  • 8
  • You should be handling the `Checked` event for each checkbox, then you can remove it with `SelectedControl.Anchor &= ~AnchorStyles.Top;` for example when the box is cleared, or add it with the box selected. – Ron Beyer Jan 13 '20 at 20:04
  • Since you're using a CheckBox, you could set `SelectedControl.Anchor ^= AnchorStyles.Top` etc. (starting with `AnchorStyles.None`, of course) – Jimi Jan 13 '20 at 20:17
  • A suggestion, in case you need to restore the layout when the application is restarted: use a class object to store the Controls' properties that define their layout, so you can then easily serialize a list of these object and deserialize it to replicate the layout, re-applying the original properties to the corresponding Controls (so, store the Type, too). – Jimi Jan 13 '20 at 20:25
  • In general I'd prefer to show a subset of properties and use a `PropertyGrid` to edit properties. You can easily achieve it using a custom class containing desired properties which acts as a wrapper around the selected control and each property gets or sets the corresponding property in the selected control. You can also use [custom type descriptor](https://stackoverflow.com/a/51618536/3110834) and make some properties browsable. – Reza Aghaei Jan 13 '20 at 20:43
  • @RonBeyer I'm new to this, so sorry for dumb questions - but which way would I add the AnchorStyle to the existing ones, given that your provided Solution removes it from the existing ones – arvenyon Jan 13 '20 at 20:49
  • 1
    See what Reza Aghaei posted. Use the same event handler for all CheckBox controls: that method simplifies all the logic. – Jimi Jan 13 '20 at 20:54

1 Answers1

2

Assuming you have four check box controls named top, bottom, left and right, you can handle CheckedChange event of them using a single method and set the anchor of the desired control based on the checked value of the controls. For example:

private void checkBox_CheckedChanged(object sender, EventArgs e)
{
    var values = new[] { top.Checked, bottom.Checked, left.Checked, right.Checked };
    byte[] data = new byte[1];
    new BitArray(values).CopyTo(data, 0);

    selectedControl.Anchor = (AnchorStyles)data[0];
}

Note: AnchorStyles is a flag enum having top=1, bottom=2, left=4 and right=8. Using above code I've mixed those flags to create the AnchorStyles and have assigned to the Anchor property of control.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
  • I believe the post answers the question. Let me know if you have any problem in applying/verifying the solution :) – Reza Aghaei Jan 30 '20 at 17:25