• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 360
  • Last Modified:

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

how do I add delegates to a stock form object? and what is a stock form object?
0
Thunder_scream
Asked:
Thunder_scream
  • 6
  • 5
1 Solution
 
Bob LearnedCommented:
I believe that a stock form object is an Form class that doesn't have any code (fresh new form).

Delegate example:

   public delegate void SimpleDelegate();


   ...

   // Instantiation
   SimpleDelegate simpleDelegate = new SimpleDelegate(MyFunc);

   // Invocation
   simpleDelegate();
   
    public static void MyFunc()
    {
            MessageBox.Show("I was called by delegate ...");
    }
     
Bob
0
 
Thunder_screamAuthor 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*/
    }
}


 

0
 
Bob LearnedCommented:
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
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Bob LearnedCommented:
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
0
 
Thunder_screamAuthor 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
0
 
Thunder_screamAuthor Commented:
I feel that this this question deserves higher point value
0
 
Bob LearnedCommented:
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
0
 
Thunder_screamAuthor 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
0
 
Bob LearnedCommented:
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
0
 
Thunder_screamAuthor 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.

0
 
Thunder_screamAuthor 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.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 6
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now