Hey people,
happy easter and similar stuff… eggs and so on… yay?
Currently I try to use the components contextmenu to modify the behaviour of it. There are going to be a lot of different settings of different form and modi; so I tried to use WinForms, but I have some problems I do not understand at all… Putting basic controls, like radiobutton, into a container like Groupbox displays in weird way…
Can anybody tell me whats going on here with the containers (Panel / Groupbox).
What is needed to do to arrange the radiobuttons with a proper spacing, and fit the container to its childs?
I do not know a lot about WinForms. Normaly I do my UI’s with WPF or Eto…
I guess it would be the best choice to do all the stuff with the provided functions in DocumentObject.Menu_* but ya, there’nt a lot of them, so all the thing gets kind of messy.
I gues, Best would be to create some (nested) sub-menus… But ya, I havn’t found any method to do so… What is the way to go in this case?
Anyway.
I coded a simple class-definition to populate the contextmenu. If the basics are working I would like to add more/different combinations of controls.
public interface IMenuWatcher
{
string Name { get; }
void AppendMenu(ToolStripDropDown menu);
}
public sealed class MenuWatcher : IMenuWatcher
{
public event SelectionChanged Changed;
public delegate void SelectionChanged(MenuWatcher sender, int prevIndex);
private string[] _entries;
private int _selectedIndex;
public MenuWatcher(string name, IEnumerable<string> entries)
{
_entries = entries.ToArray();
Name = name;
}
public string SelectedEntry => this[SelectedIndex];
public string this[int i] => _entries[i];
public string Name { get; }
public int SelectedIndex
{
get { return _selectedIndex; }
set { Callback(this, new EventArgs(), value); }
}
public void AppendMenu(ToolStripDropDown menu)
{
GH_DocumentObject.Menu_AppendItem(menu, Name, (s, e) => { }, false);
int idx = 0;
foreach (string e in _entries)
{
int i = idx;
RadioButton rd = new RadioButton() {Text = e};
rd.CheckedChanged += (s, ev) => Callback(s, ev, i);
rd.BackColor = Color.Transparent;
GH_DocumentObject.Menu_AppendCustomItem(menu, rd);
idx++;
}
GH_DocumentObject.Menu_AppendSeparator(menu);
}
[Obsolete("fishy display")]
public void AppendMenu_Panel(ToolStripDropDown menu)
{
var grb = new Panel() { Text = Name };
int idx = 0;
foreach (string e in _entries)
{
int i = idx;
RadioButton rd = new RadioButton() { Text = e };
rd.CheckedChanged += (s, ev) => Callback(s, ev, i);
rd.BackColor = Color.Transparent;
grb.Controls.Add(rd);
idx++;
}
GH_DocumentObject.Menu_AppendCustomItem(menu, grb);
}
[Obsolete("even a more fishy display")]
public void AppendMenu_GroupBox(ToolStripDropDown menu)
{
var grb = new GroupBox() {Text = Name};
int idx = 0;
foreach (string e in _entries)
{
int i = idx;
RadioButton rd = new RadioButton() { Text = e };
rd.CheckedChanged += (s, ev) => Callback(s, ev, i);
rd.BackColor = Color.Transparent;
grb.Controls.Add(rd);
idx++;
}
GH_DocumentObject.Menu_AppendCustomItem(menu, grb);
}
private void Callback(object sender, EventArgs args, int index)
{
if (_selectedIndex != index)
{
int prevIndex = _selectedIndex;
_selectedIndex = index;
Changed?.Invoke(this, prevIndex);
}
}
}
USED LIKE on a component…