Link to home
Start Free TrialLog in
Avatar of Thunder_scream
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
Avatar of GENTP
GENTP

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Thunder_scream
Thunder_scream

ASKER

Hi! again sorry I have been away for a while..

ok thanks for the quick response...
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.IContainer 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.Button();
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.txtBxShapeInfo = new System.Windows.Forms.TextBox();
            this.groupBox1.SuspendLayout();
            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.UseVisualStyleBackColor = true;
            //this.btn1.Click += new System.EventHandler(this.btn1_Click);
            //
            // groupBox1
            //
            this.groupBox1.Controls.Add(this.txtBxShapeInfo);
            this.groupBox1.Controls.Add(this.btn1);
            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.Location = new System.Drawing.Point(27, 21);
            this.txtBxShapeInfo.Name = "txtBxShapeInfo";
            this.txtBxShapeInfo.ReadOnly = true;
            this.txtBxShapeInfo.Size = new System.Drawing.Size(100, 20);
            this.txtBxShapeInfo.TabIndex = 1;
            this.txtBxShapeInfo.Text = "Rectangle";
            this.txtBxShapeInfo.TextAlign = System.Windows.Forms.HorizontalAlignment.Center;
            //
            // CustomForm
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.groupBox1);
            this.Name = "Form1";
            this.Text = "Exercise One";
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.ResumeLayout(false);

        }
        private System.Windows.Forms.Button btn1;
        private System.Windows.Forms.GroupBox groupBox1;
        private System.Windows.Forms.TextBox 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.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new mainF());
        }
    }
}
sorry it was the spelling that caused the problem.... but the question still remains should I call it in the customform or the mainform?
the InitComp that is