We help IT Professionals succeed at work.

what is stock form object? and how do I...

Thunder_scream
on
452 Views
Last Modified: 2010-04-16
how do I add delegates to a stock form object? and what is a stock form object?
Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thanks for the quick response, sorry I was away for awhile.

So a stock form is a fresh form,...right I've written a simple program to understand delegates which contains a button and a rectangle to start off. When button is pressed the rectangle change to circle.

how would I use delegates in this case?

namespace FormAndBtn
{
    public partial class Form1 : Form
    {
        private Graphics gObj;
        SolidBrush brush = new SolidBrush(Color.Blue);

        public Form1()
        {
            InitializeComponent();
            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);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
           
        }

       // 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*/
    }
}


 

CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
Delegates are bound function pointers, and are commonly used to raise events.  My question would be, "What do you want your delegate to do?"

Bob
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
A couple of Internet references :

Delegates and Events: What are They and How are they used.
http://www.devhood.com/Tutorials/tutorial_details.aspx?tutorial_id=361

Delegates and Events in C# / .NET
http://www.akadia.com/services/dotnet_delegates_and_events.html

Bob

Author

Commented:
sorry for being unclear..about that ...basically I want to use delegate to draw i.e when btn click! change the shape to something else...in other words perform some action.

Can I do that?

...I ' ve read so many articles that my brain is cooking right now....I suppose it will take sometime to get this.

Cheers

Author

Commented:
I feel that this this question deserves higher point value
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
I came up with an example, tried pasting, had a problem with my computer, and couldn't get back to this until now:

  public delegate void ButtonClickEventHandler(object sender, EventArgs e);

  public event ButtonClickEventHandler ButtonClick;

  private void Form1_Load(object sender, System.EventArgs e)
  {
  }

  private void btn1_Click(object sender, System.EventArgs e)
  {
    if (this.ButtonClick != null)
      this.ButtonClick(this, EventArgs.Empty);
  }

Bob

Author

Commented:
Hi! Bob

I have tried to modify my code to use delegate, but I cant get it to compile correctly with my code can you see what is wrong. Could you tell me why "public event ButtonClickEventHandler ButtonClick;" part of the code is necessary. I want to use a stock form object and add delegates to it and nothing in my code should violates this. So if you spot somthing that could be "delegified" let me know".



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;


namespace UnderstandDelegateProject
{
    /**
      * stock form object i.e fresh new form  adding delegate to it
      */
    public class DelegateForm : Form
    {
        // Required designer variable.
        private IContainer components = null;

        //initilize button, groupbox and textbox for later use.
        private Button btn1;
        private GroupBox groupBox1;
        private TextBox txtBxShapeInfo;

        // Required grafics variable.
        private Graphics gObj;
       
        //set the inital color of the brush to blue
        private SolidBrush brush = new SolidBrush(Color.Blue);


        public delegate void ButtonClickEventHandler(object sender, EventArgs e);   >>>>this part is newly added

        public event ButtonClickEventHandler ButtonClick; >>>>this part is newly added
               
       
        public DelegateForm()
        {
            InitComp();
            gObj = this.CreateGraphics();
        }
       
        //Clean up any resources being used.
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private void InitComp()
        {
            //creates new button, groupbox and textbox object.
            this.btn1 = new Button();
            this.groupBox1 = new GroupBox();
            this.txtBxShapeInfo = new TextBox();
            this.groupBox1.SuspendLayout();
            this.SuspendLayout();

            //sets properties of button (btn1)
            this.btn1.Location = new Point(161, 19);
            this.btn1.Name = "btn1";
            this.btn1.Size = new Size(92, 23);
            this.btn1.TabIndex = 0;
            this.btn1.Text = "Change shape?";
            this.btn1.UseVisualStyleBackColor = true;
            //this.btn1.Click += new System.EventHandler(btn1_Click);  >>>>this part is newly added
            this.btn1.Click += ButtonClickEventHandler;    >>>>this part is newly added
     

            // set properties of groupBox1
            this.groupBox1.Controls.Add(this.txtBxShapeInfo);
            this.groupBox1.Controls.Add(this.btn1);
            this.groupBox1.Location = new Point(12, 202);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new Size(268, 52);
            this.groupBox1.TabIndex = 1;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "ToolBox";


            //set properties of textbox (txtBxShapeInfo)
            this.txtBxShapeInfo.Location = new Point(27, 21);
            this.txtBxShapeInfo.Name = "txtBxShapeInfo";
            this.txtBxShapeInfo.ReadOnly = true;
            this.txtBxShapeInfo.Size = new Size(100, 20);
            this.txtBxShapeInfo.TabIndex = 1;
            this.txtBxShapeInfo.Text = "Rectangle";
            this.txtBxShapeInfo.TextAlign = HorizontalAlignment.Center;


            //ste properites of form (CustomForm)
            this.AutoScaleDimensions = new SizeF(6F, 13F);
            this.AutoScaleMode = AutoScaleMode.Font;
            this.ClientSize = new 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);
        }
           
     
        //This button changes the shape of the rectangle to circle and vice versa
        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);
            }
        }//end btn1_click
       

        // 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
    }//end of DelegateForm
}//end of namespace UnderstandDelegateProject
CERTIFIED EXPERT
Most Valuable Expert 2012
Top Expert 2008

Commented:
So, I didn't explain myself very well.  That delegate example was an example of how to raise the button click outside of the form, and not a replacement for the button click delegate handler.  You have to keep the Click event handler with the same signature as is expected by the Button class.

Do you want to raise an event outside of the form?

Bob

Author

Commented:
I'm sorry to confuse you with my questions... I'm trying to do a exercise in the book OReilly.Learning.C.Sharp.2005 and the question says

"create a application where forinstance a button click will change the colour of the form background. Create this verison such that it adds delegates to a stockform object."

I'm having hard time understanding the version specification of the question, which is why I asked you to explain what a stockform object is, and i dont quite understand how the "adds delegate" should be interpreted.

Why, i'm not being specific about what i want to do with my delegate is because i'm not sure how to interpret the question, which is why I'm asking for help. I want to know how a rutined c# programmer would interpret the question.

I suspected that I only need to be concered with the button event and perhaps the onpaint event but I'm not quite sure what to do with them if I want to follow the specification of the question.

Author

Commented:
I feel we are leaving the quesiton topic some how and therefore I should perhaps start a new topic ... I will accept you help so far ...atleast I have figured out what a stock form object is.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.