Thunder_scream
asked on
how do I make my app such that it implements a custom form object?
how do I make my app such that it implements a custom form object?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So I have followed your example, creating a small program with a button that when pressed changes a rectangle drawing to a circle.
but the compiler complains that the initcomp does not exist...could you perhaps tell me what might be wrong? do I need to create a constructor for the customForm aswell? and put initcomp inside?
here is the code
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace CFproject
{
public class myCustomForm : Form
{
// Required designer variable.
private System.ComponentModel.ICon tainer components = null;
//Clean up any resources being used.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitComp()
{
this.btn1 = new System.Windows.Forms.Butto n();
this.groupBox1 = new System.Windows.Forms.Group Box();
this.txtBxShapeInfo = new System.Windows.Forms.TextB ox();
this.groupBox1.SuspendLayo ut();
this.SuspendLayout();
//
// btn1
//
this.btn1.Location = new System.Drawing.Point(161, 19);
this.btn1.Name = "btn1";
this.btn1.Size = new System.Drawing.Size(92, 23);
this.btn1.TabIndex = 0;
this.btn1.Text = "Change shape?";
this.btn1.UseVisualStyleBa ckColor = true;
//this.btn1.Click += new System.EventHandler(this.b tn1_Click) ;
//
// groupBox1
//
this.groupBox1.Controls.Ad d(this.txt BxShapeInf o);
this.groupBox1.Controls.Ad d(this.btn 1);
this.groupBox1.Location = new System.Drawing.Point(12, 202);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(268, 52);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "ToolBox";
//
// txtBxShapeInfo
//
this.txtBxShapeInfo.Locati on = new System.Drawing.Point(27, 21);
this.txtBxShapeInfo.Name = "txtBxShapeInfo";
this.txtBxShapeInfo.ReadOn ly = true;
this.txtBxShapeInfo.Size = new System.Drawing.Size(100, 20);
this.txtBxShapeInfo.TabInd ex = 1;
this.txtBxShapeInfo.Text = "Rectangle";
this.txtBxShapeInfo.TextAl ign = System.Windows.Forms.Horiz ontalAlign ment.Cente r;
//
// CustomForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoS caleMode.F ont;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.gro upBox1);
this.Name = "Form1";
this.Text = "Exercise One";
this.groupBox1.ResumeLayou t(false);
this.groupBox1.PerformLayo ut();
this.ResumeLayout(false);
}
private System.Windows.Forms.Butto n btn1;
private System.Windows.Forms.Group Box groupBox1;
private System.Windows.Forms.TextB ox txtBxShapeInfo;
}//end of customform class
public class mainF : myCustomForm
{
private Graphics gObj;
SolidBrush brush = new SolidBrush(Color.Blue);
public mainF()
{
Initcomp();
gObj = this.CreateGraphics();
}
/*
private void btn1_Click(object sender, EventArgs e)
{
if (txtBxShapeInfo.Text == "Circle")
{
//change the text box info
this.txtBxShapeInfo.Text = "Rectangle";
//clean the form
gObj.Clear(this.BackColor) ;
// set brush to blue
brush.Color = Color.Blue;
// create filled rectangle
gObj.FillRectangle(brush, 90, 30, 150, 90);
}
else
{
//change the text box info
this.txtBxShapeInfo.Text = "Circle";
//clean the form
gObj.Clear(this.BackColor) ;
// set brush to red
brush.Color = Color.Red;
// draw Ellipse outline
gObj.FillEllipse(brush, 90, 30, 100, 100);
}
}*/
// override Form OnPaint method
protected override void OnPaint(PaintEventArgs paintEvent)
{
// get graphics object
Graphics g = paintEvent.Graphics;
// create filled rectangle
g.FillRectangle(brush, 90, 30, 150, 90);
} // end method OnPaint*/
}
}
using System;
using System.Collections.Generic ;
using System.Windows.Forms;
using System.Text;
namespace CFproject
{
static class mainprog
{
/// The main entry point for the application.
[STAThread]
static void Main()
{
Application.EnableVisualSt yles();
Application.SetCompatibleT extRenderi ngDefault( false);
Application.Run(new mainF());
}
}
}
but the compiler complains that the initcomp does not exist...could you perhaps tell me what might be wrong? do I need to create a constructor for the customForm aswell? and put initcomp inside?
here is the code
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
namespace CFproject
{
public class myCustomForm : Form
{
// Required designer variable.
private System.ComponentModel.ICon
//Clean up any resources being used.
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
private void InitComp()
{
this.btn1 = new System.Windows.Forms.Butto
this.groupBox1 = new System.Windows.Forms.Group
this.txtBxShapeInfo = new System.Windows.Forms.TextB
this.groupBox1.SuspendLayo
this.SuspendLayout();
//
// btn1
//
this.btn1.Location = new System.Drawing.Point(161, 19);
this.btn1.Name = "btn1";
this.btn1.Size = new System.Drawing.Size(92, 23);
this.btn1.TabIndex = 0;
this.btn1.Text = "Change shape?";
this.btn1.UseVisualStyleBa
//this.btn1.Click += new System.EventHandler(this.b
//
// groupBox1
//
this.groupBox1.Controls.Ad
this.groupBox1.Controls.Ad
this.groupBox1.Location = new System.Drawing.Point(12, 202);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(268, 52);
this.groupBox1.TabIndex = 1;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "ToolBox";
//
// txtBxShapeInfo
//
this.txtBxShapeInfo.Locati
this.txtBxShapeInfo.Name = "txtBxShapeInfo";
this.txtBxShapeInfo.ReadOn
this.txtBxShapeInfo.Size = new System.Drawing.Size(100, 20);
this.txtBxShapeInfo.TabInd
this.txtBxShapeInfo.Text = "Rectangle";
this.txtBxShapeInfo.TextAl
//
// CustomForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoS
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.gro
this.Name = "Form1";
this.Text = "Exercise One";
this.groupBox1.ResumeLayou
this.groupBox1.PerformLayo
this.ResumeLayout(false);
}
private System.Windows.Forms.Butto
private System.Windows.Forms.Group
private System.Windows.Forms.TextB
}//end of customform class
public class mainF : myCustomForm
{
private Graphics gObj;
SolidBrush brush = new SolidBrush(Color.Blue);
public mainF()
{
Initcomp();
gObj = this.CreateGraphics();
}
/*
private void btn1_Click(object sender, EventArgs e)
{
if (txtBxShapeInfo.Text == "Circle")
{
//change the text box info
this.txtBxShapeInfo.Text = "Rectangle";
//clean the form
gObj.Clear(this.BackColor)
// set brush to blue
brush.Color = Color.Blue;
// create filled rectangle
gObj.FillRectangle(brush, 90, 30, 150, 90);
}
else
{
//change the text box info
this.txtBxShapeInfo.Text = "Circle";
//clean the form
gObj.Clear(this.BackColor)
// set brush to red
brush.Color = Color.Red;
// draw Ellipse outline
gObj.FillEllipse(brush, 90, 30, 100, 100);
}
}*/
// override Form OnPaint method
protected override void OnPaint(PaintEventArgs paintEvent)
{
// get graphics object
Graphics g = paintEvent.Graphics;
// create filled rectangle
g.FillRectangle(brush, 90, 30, 150, 90);
} // end method OnPaint*/
}
}
using System;
using System.Collections.Generic
using System.Windows.Forms;
using System.Text;
namespace CFproject
{
static class mainprog
{
/// The main entry point for the application.
[STAThread]
static void Main()
{
Application.EnableVisualSt
Application.SetCompatibleT
Application.Run(new mainF());
}
}
}
ASKER
sorry it was the spelling that caused the problem.... but the question still remains should I call it in the customform or the mainform?
ASKER
the InitComp that is
ASKER
ok thanks for the quick response...