Hello this is my c# project, so if a want to build another form called \"cola ty
ID: 3827850 • Letter: H
Question
Hello this is my c# project, so if a want to build another form called "cola type" which when i click drink>>cola it shows types of cola type button i wanted, now i can only do two layer of buttons and cant go futher more orwise i got error. i want three layers of buttons i want add a custom configuration Form that can click in and add items, such as toppings, can someone help me how to add another layer of button. following code is what i got so far.I can send you my database if you want
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace DynFormEx
{
// All non-fixed data for Forms are read from the DB
// frmMain is instantiated for all forms
public partial class frmMain : Form
{
public bool DEBUG = true; // DEBUG mode for testing
public Button btnOper; // Button on Form to close or perform other Ops
public Label lblTitle; // Title of Form
public Button[] btnArray; // Button array for Form
private FormConfigArr frmCfgArr;// Array of configs for all Forms in App
public int frmCfgArrIdx; // Index of current Form
public string frmType; // Type of Form
public string frmImage; // Path of background image on Form
// Form constructor for a typFrmButton Form
private frmMain(int instNum, string frmType, string frmImage, FormConfigArr frmCfgArr)
{
this.frmCfgArrIdx = instNum;
this.frmType = frmType;
this.frmImage = frmImage;
this.frmCfgArr = frmCfgArr;
InitializeComponent();
}
// Form constructor for a typFrmImage Form
private frmMain(int instNum, string frmType, FormConfigArr frmCfgArr)
{
this.frmCfgArrIdx = instNum;
this.frmType = frmType;
this.frmCfgArr = frmCfgArr;
InitializeComponent();
}
// Form constructor for first Form - called from Program.cs
public frmMain(int instNum, string frmType)
{
this.frmCfgArrIdx = instNum;
this.frmType = frmType;
InitializeComponent();
}
// btnOper event handler
public void btnOper_Click(object sender, EventArgs e)
{
if (btnOper.Text == "Exit")
{
// Dispose deletes Form
this.Dispose();
// Close would keep instance of Form
// This may lead to multiple unneeded copies in memory
//this.Close();
}
else if (btnOper.Text == "Settings")
{
// Dispose deletes Form
this.Dispose();
// Close would keep instance of Form
// This may lead to multiple unneeded copies in memory
//this.Close();
}
}
// Form close for typFrmImage Forms
public void frmMain_Click(object sender, EventArgs e)
{
// Dispose deletes Form
this.Dispose();
// Close would keep instance of Form
// This may lead to multiple unneeded copies in memory
//this.Close();
}
// btnArray[] event handler
public void btnArray_Click(object sender, EventArgs e)
{
// Cast sender as Button
Button b = (Button)sender;
// Cast Tag as ButtonConfig
ButtonConfig bc = (ButtonConfig)b.Tag;
// Declare reference to a frmMain Form
frmMain nf = null;
// Instantiate Form given type of Form
if (bc.btnCfgTarget == "typFrmButton")
nf = new frmMain(bc.frmOpenIdx, bc.btnCfgTarget, frmCfgArr);
else if (bc.btnCfgTarget == "typFrmItem")
nf = new frmMain(bc.frmOpenIdx, bc.btnCfgTarget, frmCfgArr);
else if(bc.btnCfgTarget == "typFrmImage")
nf = new frmMain(bc.frmOpenIdx, bc.btnCfgTarget, bc.btnCfgImage, frmCfgArr);
// Show and transfer flow of contril to new Form
nf.ShowDialog();
}
// Form load event handler - Set initial Form
// and handle subsequent Forms
private void frmMain_Load(object sender, EventArgs e)
{
// If initial Form
if (frmCfgArrIdx == 0)
{
// Instantiate and fill frmCfgArr
frmCfgArr = new FormConfigArr();
frmCfgArr.GetFormConfigs();
// Draw initial Form
if(frmCfgArr != null && frmCfgArr.Count > 0 && frmCfgArr[0] != null)
frmCfgArr[0].ConfigureButtonForm(this);
}
else // A subsequent Form
{
// If a Button Form
if (frmType == "typFrmButton")
{
frmCfgArr[frmCfgArrIdx].ConfigureButtonForm(this);
}
// If a Image Form
else if (frmType == "typFrmImage")
{
frmCfgArr[frmCfgArrIdx].ConfigureImageForm(this, frmImage);
}
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
namespace DynFormEx
{
// The FormConfig class contains data and methods to draw a Form
class FormConfig
{
// Form data read from DB
public int frmIdx; // The unique ID for the Form in app abd DB
private string frmTitle; // The title shown on the Form (none for none)
private string frmImage; // The path of the image to be displayed for background
public string frmType; // The type of Form (typFrmButton or TypeFrmImage fir this app)
private string imgDir; // The directory (folder) that contains the app images
// Button data
public ButtonConfig[] btnConfig; // Array fo Buttons for this Form
public int numButtons; // Number of Butons on Form
public int maxBtnRows = 3; // Max rows of Buttons on Form
public int maxBtnCols = 4; // Max cols of Buttons on Form
// Calculated measurements
private int frmWidth; // Maximized width of Form
private int frmHeight; // Maximized height of Form
private int btnWidth; // Calculated width of a Button wrt the Form
private int btnHeight; // Calculated height of a Button wrt the Form
private int btnStartPosX; // Starting X position on Form of first Button
private int btnStartPosY; // Starting Y position on Form of first Button
private int btnOffsetX; // Spacing X between Buttons
private int btnOffsetY; // Spacing Y between Buttons
// Constructor for FormConfig
public FormConfig(int frmIdx, string frmTitle, string frmImage, string frmType, string imgDir)
{
this.frmIdx = frmIdx;
this.frmTitle = frmTitle;
this.frmImage = frmImage;
this.frmType = frmType;
this.imgDir = imgDir;
}
// Draw a Form to expand an image
public void ConfigureImageForm(frmMain fm, string path)
{
// Set Form Properties
// Maximize the Form
fm.WindowState = FormWindowState.Maximized;
// Remove the Form control box
fm.ControlBox = false;
// Remove border around Form
fm.FormBorderStyle = FormBorderStyle.None;
// Read image from file
Image image1 = Image.FromFile(path, true);
// Set Image as background
fm.BackgroundImage = image1;
// Click event wiring
fm.Click += new System.EventHandler(fm.frmMain_Click);
// Stretch image to fit background size
fm.BackgroundImageLayout = ImageLayout.Stretch;
// Get width and height of maximized Form
frmWidth = fm.Size.Width;
frmHeight = fm.Size.Height;
}
public void ConfigureButtonForm(frmMain fm)
{
// Instantiate Form Button and Label
fm.btnOper = new Button();
fm.lblTitle = new Label();
// Set Form Properties - These are fixed
// Maximize the Form
fm.WindowState = FormWindowState.Maximized;
// Remove the Form control box
fm.ControlBox = false;
// Remove border around Form
fm.FormBorderStyle = FormBorderStyle.None;
string path = imgDir + frmImage;
// Read image from file
Image image1 = Image.FromFile(path, true);
// Set Image as background
fm.BackgroundImage = image1;
// Stretch image to fit background size
fm.BackgroundImageLayout = ImageLayout.Stretch;
// Get width and height of maximized Form
frmWidth = fm.Size.Width;
frmHeight = fm.Size.Height;
// Get sizes and offsets - These are calculated from Form width and height
btnWidth = (int)(frmWidth / 6.5);
btnOffsetX = (int)(btnWidth * 0.5);
btnStartPosX = btnOffsetX;
btnHeight = (int)(frmHeight / 7.0);
btnOffsetY = (int)(btnHeight * 1.75);
btnStartPosY = btnOffsetY;
// Create exit Button if in DEBUG mode
if (fm.DEBUG)
{
// Set Text property
fm.btnOper.Text = "Exit";
// Set location of Button on Form
fm.btnOper.Location = new System.Drawing.Point(frmWidth - 80, frmHeight - 30);
// Event handler wiring
fm.btnOper.Click += new System.EventHandler(fm.btnOper_Click);
// Add Button to Form
fm.Controls.Add(fm.btnOper);
}
// Else put Admin Button on Form
else
{
// Set Text property
fm.btnOper.Text = "Settings";
// Set location of Button on Form
fm.btnOper.Location = new System.Drawing.Point(frmWidth - 80, frmHeight - 30);
// Event handler wiring
fm.btnOper.Click += new System.EventHandler(fm.btnOper_Click);
// Add Button to Form
fm.Controls.Add(fm.btnOper);
}
// Put title Label on Form
// Set position of Label
// Note that (0, 0) is the upper left of Form
int thisLblPosX = frmWidth / 2 - 250;
int thisLblPosY = 75;
// Set Label Name
fm.lblTitle.Name = "lblTitle";
// Set Label Text
fm.lblTitle.Text = frmTitle;
// Set Label Font color
fm.lblTitle.ForeColor = Color.AntiqueWhite;
// Set Label background color
fm.lblTitle.BackColor = Color.Black;
// Set Label size
fm.lblTitle.Size = new System.Drawing.Size(500, 50);
// Set position of text in Label
fm.lblTitle.TextAlign = ContentAlignment.MiddleCenter;
// St Font family, point and style
fm.lblTitle.Font = new Font("Arial", 24, FontStyle.Bold);
// Set Label location
fm.lblTitle.Location = new System.Drawing.Point(thisLblPosX, thisLblPosY);
// Add Label to Form
fm.Controls.Add(fm.lblTitle);
// Put Buttons on form
// Init Button count
int btnCount = 0;
// Position of current Button on Form
int thisBtnPosX = btnStartPosX;
int thisBtnPosY = btnStartPosY;
// Instantiate the array of Buttons for this Form
fm.btnArray = new Button[maxBtnRows * maxBtnCols];
// Loop through rows of Buttons
for (int i = 0; i < maxBtnRows; i++)
{
// Get start position for row
thisBtnPosX = btnStartPosX;
// Loop through cols
for (int j = 0; j < maxBtnCols; j++)
{
// Get DB data until all Buttons are added
if (btnCount < numButtons)
{
// Instantiate a Button
fm.btnArray[btnCount] = new Button();
// Set Button Name
fm.btnArray[btnCount].Name = "btnChoice" + btnCount;
// Set Size of Button
fm.btnArray[btnCount].Size = new System.Drawing.Size(btnWidth, btnHeight);
// Set Location of Button
fm.btnArray[btnCount].Location = new System.Drawing.Point(thisBtnPosX, thisBtnPosY);
// Set Text for Button
fm.btnArray[btnCount].Text = fm.btnArray[btnCount].Name;
// Set Font color of Button
fm.btnArray[btnCount].ForeColor = Color.AntiqueWhite;
// Read image from file
image1 = Image.FromFile(btnConfig[btnCount].btnCfgImage, true);
// Set Image as background image
fm.btnArray[btnCount].BackgroundImage = image1;
// Stretch image to fit background size
fm.btnArray[btnCount].BackgroundImageLayout = ImageLayout.Stretch;
// Copy btnConfig to this Buttons Tag for future reference
fm.btnArray[btnCount].Tag = btnConfig[btnCount];
// Event handler wiring
fm.btnArray[btnCount].Click += new System.EventHandler(fm.btnArray_Click);
// Add Button to Form
fm.Controls.Add(fm.btnArray[btnCount]);
// Increment count
btnCount++;
}
// Advance to next Button position in X
thisBtnPosX += btnOffsetX + btnWidth;
}
// Advance to next Button position in Y
thisBtnPosY += btnOffsetY;
}
}
// ToString to print Form data
public override string ToString()
{
string s = frmIdx + " ";
s += frmTitle + " ";
s += frmImage + " ";
s += frmType + " ";
s += imgDir + " ";
s += numButtons;
return s;
}
} // End class
} // End namespace
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.OleDb;
//using System.Windows.Forms;
using System.IO;
namespace DynFormEx
{
// Array of FormConfig for project
class FormConfigArr : List<FormConfig>
{
private static string imgDir;
// Constructor for FormConfigArr
public FormConfigArr()
{
// Get Image dir from DB
GetImgDir();
}
// Get Image dir from DB
public static void GetImgDir()
{
// DB objects
OleDbConnection conn = null;
OleDbCommand comm = null;
string sqlStr;
// Query image dir from DB
try
{
conn = GetConnection();
conn.Open();
sqlStr = "SELECT fldImgDir " +
"FROM tblApplication " +
"WHERE fldID = 0";
comm = new OleDbCommand(sqlStr, conn);
OleDbDataReader reader = comm.ExecuteReader(
System.Data.CommandBehavior.SingleRow);
if (reader.Read())
{
// Note that Access uses index of data given its
// location in forst line of sqlStr
imgDir = reader.GetString(0);
}
}
catch (Exception ex)
{
//MessageBox.Show("Exception: " + ex.ToString());
}
finally
{
if (conn != null)
conn.Close();
}
}
// Get Form config data from DB
// This reads all the Form config data from DB
public void GetFormConfigs()
{
// Number of Buttons on current Form
int numButtons;
// DB objects
OleDbConnection conn = null;
OleDbCommand comm = null;
string sqlStr;
// Read all Forms from DB
try
{
conn = GetConnection();
conn.Open();
sqlStr = "SELECT fldID, fldFrmTitle, fldFrmImage, fldFrmType " +
"FROM tblForms";
comm = new OleDbCommand(sqlStr, conn);
OleDbDataReader reader = comm.ExecuteReader(
System.Data.CommandBehavior.SingleResult);
while (reader.Read())
{
// Use FormConfig constructor to instantiate Form
FormConfig cf = new FormConfig(reader.GetInt32(0),
reader.GetString(1), reader.GetString(2),
reader.GetString(3), imgDir);
// Add form to this List
this.Add(cf);
}
}
catch (Exception ex)
{
//MessageBox.Show("Exception: " + ex.ToString());
}
finally
{
if (conn != null)
conn.Close();
}
// Add Bottons to Forms
for (int i = 0; i < this.Count; i++)
{
// Init num Buttons to zero
numButtons = 0;
// The first type of Form has Buttons that cause some action when clicked
if (this[i].frmType == "typFrmButton")
{
// Instantiate a ButtonConfig array with max rows x max cols elements
this[i].btnConfig = new ButtonConfig[this[i].maxBtnRows * this[i].maxBtnCols];
// Read Buttons for this Form from DB
try
{
conn = GetConnection();
conn.Open();
sqlStr = "SELECT fldBtnCfgImage, fldBtnCfgOper, fldBtnCfgTarget, fldFrmOpenIdx " +
"FROM tblButtons " +
"WHERE fldFormID = " + this[i].frmIdx;
//MessageBox.Show(sqlStr);
comm = new OleDbCommand(sqlStr, conn);
OleDbDataReader reader = comm.ExecuteReader(
System.Data.CommandBehavior.SingleResult);
while (reader.Read())
{
// Instantiate a Button with constructor
this[i].btnConfig[numButtons] = new ButtonConfig(imgDir + reader.GetString(0),
reader.GetString(1), reader.GetString(2), reader.GetInt32(3));
numButtons++;
}
// Record current Forms number of Buttons
this[i].numButtons = numButtons;
}
catch (Exception ex)
{
//MessageBox.Show("Exception: " + ex.ToString());
}
finally
{
if (conn != null)
conn.Close();
}
}
}
}
// Get a connection to DB
public static OleDbConnection GetConnection()
{
OleDbConnection conn = null;
string connStr = File.ReadAllText(@"....DBConnStr.txt", Encoding.UTF8);
conn = new OleDbConnection(connStr);
return conn;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.OleDb;
using System.IO;
namespace DynFormEx
{
class BikesDB
{
public static OleDbConnection GetConnection()
{
OleDbConnection conn = null;
string connStr = File.ReadAllText(@"....DBConnStr.txt", Encoding.UTF8);
conn = new OleDbConnection(connStr);
return conn;
}
public static string[] getFormData()
{
string[] sArr = null;
return sArr;
}
}
}
Explanation / Answer
You can create new custome form
bool isTopPanelDragged = false;
bool isLeftPanelDragged = false;
bool isRightPanelDragged = false;
bool isBottomPanelDragged = false;
bool isTopBorderPanelDragged = false;
bool isWindowMaximized = false;
Point offset;
Size _normalWindowSize;
Point _normalWindowLocation = Point.Empty;
//**********************************************************************
//top border panel
private void TopBorderPanel_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isTopBorderPanelDragged = true;
}
else
{
isTopBorderPanelDragged = false;
}
}
private void TopBorderPanel_MouseMove(object sender, MouseEventArgs e)
{
if (e.Y < this.Location.Y)
{
if (isTopBorderPanelDragged)
{
if (this.Height < 50)
{
this.Height = 50;
isTopBorderPanelDragged = false;
}
else
{
this.Location = new Point(this.Location.X, this.Location.Y + e.Y);
this.Height = this.Height - e.Y;
}
}
}
}
private void TopBorderPanel_MouseUp(object sender, MouseEventArgs e)
{
isTopBorderPanelDragged = false;
}
//**********************************************************************
//top panel
private void TopPanel_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isTopPanelDragged = true;
Point pointStartPosition = this.PointToScreen(new Point(e.X, e.Y));
offset = new Point();
offset.X = this.Location.X - pointStartPosition.X;
offset.Y = this.Location.Y - pointStartPosition.Y;
}
else
{
isTopPanelDragged = false;
}
if (e.Clicks == 2)
{
isTopPanelDragged = false;
_MaxButton_Click(sender, e);
}
}
private void TopPanel_MouseMove(object sender, MouseEventArgs e)
{
if (isTopPanelDragged)
{
Point newPoint = TopPanel.PointToScreen(new Point(e.X, e.Y));
newPoint.Offset(offset);
this.Location = newPoint;
if (this.Location.X > 2 || this.Location.Y > 2)
{
if (this.WindowState == FormWindowState.Maximized)
{
this.Location = _normalWindowLocation;
this.Size = _normalWindowSize;
toolTip1.SetToolTip(_MaxButton, "Maximize");
_MaxButton.CFormState = MinMaxButton.CustomFormState.Normal;
isWindowMaximized = false;
}
}
}
}
private void TopPanel_MouseUp(object sender, MouseEventArgs e)
{
isTopPanelDragged = false;
if (this.Location.Y <= 5)
{
if (!isWindowMaximized)
{
_normalWindowSize = this.Size;
_normalWindowLocation = this.Location;
Rectangle rect = Screen.PrimaryScreen.WorkingArea;
this.Location = new Point(0, 0);
this.Size = new System.Drawing.Size(rect.Width, rect.Height);
toolTip1.SetToolTip(_MaxButton, "Restore Down");
_MaxButton.CFormState = MinMaxButton.CustomFormState.Maximize;
isWindowMaximized = true;
}
}
}
//**********************************************************************
//left panel
private void LeftPanel_MouseDown(object sender, MouseEventArgs e)
{
if (this.Location.X <= 0 || e.X < 0)
{
isLeftPanelDragged = false;
this.Location = new Point(10, this.Location.Y);
}
else
{
if (e.Button == MouseButtons.Left)
{
isLeftPanelDragged = true;
}
else
{
isLeftPanelDragged = false;
}
}
}
private void LeftPanel_MouseMove(object sender, MouseEventArgs e)
{
if (e.X < this.Location.X)
{
if (isLeftPanelDragged)
{
if (this.Width < 100)
{
this.Width = 100;
isLeftPanelDragged = false;
}
else
{
this.Location = new Point(this.Location.X + e.X, this.Location.Y);
this.Width = this.Width - e.X;
}
}
}
}
private void LeftPanel_MouseUp(object sender, MouseEventArgs e)
{
isLeftPanelDragged = false;
}
//**********************************************************************
//right panel
private void RightPanel_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isRightPanelDragged = true;
}
else
{
isRightPanelDragged = false;
}
}
private void RightPanel_MouseMove(object sender, MouseEventArgs e)
{
if (isRightPanelDragged)
{
if (this.Width < 100)
{
this.Width = 100;
isRightPanelDragged = false;
}
else
{
this.Width = this.Width + e.X;
}
}
}
private void RightPanel_MouseUp(object sender, MouseEventArgs e)
{
isRightPanelDragged = false;
}
//**********************************************************************
//bottom panel
private void BottomPanel_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
isBottomPanelDragged = true;
}
else
{
isBottomPanelDragged = false;
}
}
private void BottomPanel_MouseMove(object sender, MouseEventArgs e)
{
if (isBottomPanelDragged)
{
if (this.Height < 50)
{
this.Height = 50;
isBottomPanelDragged = false;
}
else
{
this.Height = this.Height + e.Y;
}
}
}
private void BottomPanel_MouseUp(object sender, MouseEventArgs e)
{
isBottomPanelDragged = false;
}
//**********************************************************************
//actions for close,min,max buttons
private void _CloseButton_Click(object sender, EventArgs e)
{
this.Close();
}
private void _MaxButton_Click(object sender, EventArgs e)
{
if (isWindowMaximized)
{
this.Location = _normalWindowLocation;
this.Size = _normalWindowSize;
toolTip1.SetToolTip(_MaxButton, "Maximize");
_MaxButton.CFormState = MinMaxButton.CustomFormState.Normal;
isWindowMaximized = false;
}
else
{
_normalWindowSize = this.Size;
_normalWindowLocation = this.Location;
Rectangle rect = Screen.PrimaryScreen.WorkingArea;
this.Location = new Point(0, 0);
this.Size = new System.Drawing.Size(rect.Width, rect.Height);
toolTip1.SetToolTip(_MaxButton, "Restore Down");
_MaxButton.CFormState = MinMaxButton.CustomFormState.Maximize;
isWindowMaximized = true;
}
}
private void _MinButton_Click(object sender, EventArgs e)
{
this.WindowState = FormWindowState.Minimized;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.