private void GUI_FormClosing(object sender, FormClosingEventArgs e) { //Save buttons for (int i = 0; i < allButtons.Count; i++) { ButtonsClient.AddButtonToAllButtons(Mapper.mapDragButtonToTransferButton(allButtons[i])); } }
//Mouse clicked inside GUI panel, create dragButton object private void MouseClicked(object sender, MouseEventArgs e) { if (ButtonControl.enableAdd) { int btnId; if (allButtons.Count != 0) { btnId = allButtons.Keys.Max() > collectionButtons.Keys.Max() ? allButtons.Keys.Max() + 1 : collectionButtons.Keys.Max() + 1; } else { btnId = collectionButtons.Keys.Max() + 1; } //Creating StorageCollection button if (cmbAddType.Text == "Storage collection") { //Create collection button DragButton dragButton = new DragButton(Enums.ButtonTypes.Collection); dragButton.Text = tBoxAddName.Text; dragButton.Name = "btnCollection_" + btnId.ToString(); dragButton.Tag = btnId; dragButton.Location = new System.Drawing.Point(PointToClient(Cursor.Position).X, PointToClient(Cursor.Position).Y); dragButton.MouseDown += collectionPressed; //When button is clicked //Add button to panel [display it] panelGUI.Controls.Add(dragButton); dragButton.BringToFront(); //Add button to it's collection [to all btns] collectionButtons[buttonsStack.Peek()].Add(dragButton); //Create this button collection [this collection] collectionButtons.Add(btnId, new List <DragButton>()); //Add to all buttns [database] try{ ButtonsClient.AddButtonToAllButtons(Mapper.mapDragButtonToTransferButton(dragButton)); } catch (Exception) { MessageBox.Show("Failed to add to database"); } //Add to all buttons [holder] allButtons.Add(btnId, dragButton); //Create new connnection ButtonConnection connection = new ButtonConnection { ConnectionID = ButtonConnectionHolder.connections.Count == 0 ? 0 : ButtonConnectionHolder.connections.Max(elem => elem.ConnectionID) + 1, CollectionButtonId = buttonsStack.Peek(), UnitButtonID = btnId }; //Add connection to database, so we know in which layer it is [database] try { ButtonsClient.AddButtonConnection(connection); } catch (Exception) { MessageBox.Show("Failed to add to database"); } //Add connection to connection holder ButtonConnectionHolder.connections.Add(connection); //Push on stack [stack] buttonsStack.Push(btnId); panelGUI.Controls.Clear(); //make backbtn visible btnBackFromChild.Visible = true; } else { //Create Unit button DragButton dragButton = new DragButton(Enums.ButtonTypes.Unit); dragButton.Text = tBoxAddName.Text; dragButton.Name = "btnUnit_" + btnId.ToString(); dragButton.Tag = btnId; dragButton.Location = new System.Drawing.Point(PointToClient(Cursor.Position).X, PointToClient(Cursor.Position).Y); dragButton.MouseDown += unitPressed; //When button is clicked //Add button to panel [display it] panelGUI.Controls.Add(dragButton); dragButton.BringToFront(); //Add UnitButton to it's collectionButton [list] collectionButtons[buttonsStack.Peek()].Add(dragButton); //Add button to allButtons [list] allButtons.Add(btnId, dragButton); //Add button to database, all buttons table [database] try { ButtonsClient.AddButtonToAllButtons(Mapper.mapDragButtonToTransferButton(dragButton)); } catch (Exception) { MessageBox.Show("Failed to add to database"); } //Create new connnection ButtonConnection connection = new ButtonConnection { ConnectionID = ButtonConnectionHolder.connections.Count == 0 ? 0 : ButtonConnectionHolder.connections.Max(elem => elem.ConnectionID) + 1, CollectionButtonId = buttonsStack.Peek(), UnitButtonID = btnId }; try { //Add connection to database, so we know in which layer it is [database] ButtonsClient.AddButtonConnection(connection); } catch (Exception) { MessageBox.Show("Failed to add to database"); } //Add connection to connection holder [Holder] ButtonConnectionHolder.connections.Add(connection); } //btn.Image = System.Drawing.Image.FromFile(@"C:\Users\Alen\Desktop\faks\0.ZAVRSNI\images\container.png"); ButtonControl.enableAdd = false; } }