private void CreateMenuFile() { FileMenu = new ThemedToolStripMenuHeader() { Name = "FileMenuItem", // Add spaces to give some extra room to the menu. Size = CorrectedMeasureText(" File ", BaseUI.BaseFont), Text = "File", }; ThemedToolStripMenuItem OpenMenu = new ThemedToolStripMenuItem() { Text = "Open" }; OpenMenu.Click += (s, a) => { OpenFileDialog OpenFile = new OpenFileDialog(); // This will set OpenFile.FileName to the chosen path. OpenFile.ShowDialog(); // "" is returned when the user closes the DialogBox with alt f4 or the X icon. It was annoying when // the error message showed up after doing this. if (OpenFile.FileName != "") { // This should not really be handled here, rather in the main class file. FlashFromFile(OpenFile.FileName); } }; ThemedToolStripMenuItem OpenClipboardMenu = new ThemedToolStripMenuItem() { Text = "Open from clipboard" }; OpenClipboardMenu.Click += (s, a) => { // Parse the clip board as text. IO.TXT ParsedClipboard = IO.TXT.Parse(System.Text.Encoding.UTF8.GetBytes(Clipboard.GetText(TextDataFormat.UnicodeText))); // If the output is null, there was an error somewhere, but this is already handled in the TXT class. // Regardless, the VM will be unchanged in this case. if (ParsedClipboard != null) { // This should not really be handled here, rather in the main class file. FlashProcedure(ParsedClipboard.Instructions); } }; // It is important this is called after both of these are initialised. FileMenu.DropDownItems.AddRange(new[] { OpenMenu, OpenClipboardMenu }); }
private void CreateMenuDebug() { // A work around for SelectDebugMenuItem.Dropdown opening when 's' is used in search, this is selected instead, which does nothing. // A work around for a very annoying windows form feature. When pressing a key such as "a" or "b", if there is a control whose name // begins with that letter, it will have OnClick called. However it was extremely annnoying when using the testcase search, as if // you tried to search for a testcase with an 'a' in its name, suddenly every testcase would run as All.OnClick was called(Same would happen with Select). // I could find very little about this online or any kind of fix, so this is the best I could come up with. As ToolStripMenuItem is no button, when its // corresponding key is pressed, nothing will happen. I assume windows forms recognises there could be some ambiguity here with multiple controls and corrects itself. // The workaround is to create these two dummy buttons to artificially create this effect. They are sized as 0 to prevent them being seen, however setting $Visible // to false would null the effect. ToolStripMenuItem FillerDebugItemSelect = new ToolStripMenuItem() { Text = "S", Size = new Size(0, 0), AutoSize = false }; ToolStripMenuItem FillerDebugItemAll = new ToolStripMenuItem() { Text = "A", Size = new Size(0, 0), AutoSize = false }; // Create a constant size for all items initially. Size ItemSize = new Size(70, 20); SelectDebugMenu = new ThemedToolStripMenuItem() { Size = ItemSize, Text = "Select" }; // Create a new menu item for each testcase available and add them as subitems to the dropdown of the Select menu. string[] Testcases = Hypervisor.TestHandler.GetTestcases(); for (int i = 0; i < Testcases.Length; i++) { ThemedToolStripMenuItem ToAdd = new ThemedToolStripMenuItem() { // Set the text to the name of the testcase. Text = Testcases[i], Size = ItemSize }; // When the button is clicked, call the testcase it corresponds to. ToAdd.Click += (s, a) => OnTestcaseSelected(ToAdd.Text); SelectDebugMenu.DropDownItems.Add(ToAdd); } AllDebugMenu = new ThemedToolStripMenuItem() { Size = ItemSize, Text = "All" }; AllDebugMenu.Click += (s, a) => OnTestcaseSelected("all"); // See SearchTextBox for information on this constructor. SearchDebugMenu = new SearchTextBox( () => Hypervisor.TestHandler.GetTestcases(), Layer.Background, Emphasis.Medium) { Size = ItemSize }; SearchDebugMenu.OnResultClicked += OnTestcaseSelected; // Create the header DebugMenu = new ThemedToolStripMenuHeader() { Name = "DebugMenuItem", // Add some extra padding either side Size = CorrectedMeasureText(" Debug ", BaseUI.BaseFont), Text = "Debug", }; // Call keypressed on the search menu when a key is pressed in the dropdown. This means that the user does not specifically have to // hover over the search button to search; only the dropdown has to have focus. DebugMenu.DropDown.PreviewKeyDown += SearchDebugMenu.KeyPressed; // This must be called after all controls have finished initialising. DebugMenu.DropDownItems.AddRange(new ToolStripItem[] { FillerDebugItemAll, FillerDebugItemSelect, AllDebugMenu, SelectDebugMenu, SearchDebugMenu }); }