27.1.13

Dynamic controls using C# in windows forms


Here is the small piece of coding to do some Inheritance concept in C#.

Here is the problem,

  1. I want to write custom code to create controls on my windows forms page.
  2. Instead of Button class, I need to use inheritance to create my own button class and create objects.



  public class MyButton : Button
        {
            public MyButton()
                : base()
            {
                // set whatever styling properties needed here
                ForeColor = Color.Red;
                BackColor = Color.Green;
                 Location = new System.Drawing.Point(269, 96);
            }
        }

        public class MyButton1 : MyButton
        {
            public MyButton1()
                : base()
            {
                // set whatever styling properties needed here
                ForeColor = Color.Red;
                BackColor = Color.Green;
                Location = new System.Drawing.Point(269, 96);
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            MyButton1 btn = new MyButton1();
            btn.Name = "MyButton";
            this.Controls.Add(btn);
        }
    }


Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech

15.1.13

GridView1_RowCommand event is not firing



Here is a small problem, which I've faced in C# windows forms.



GridView1_RowCommand is not firing

Solution: 

 In this case, you need to go with disabling the view state property of the form.


Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech, Chennai

Make 3d chart in C#

Here is the simple steps to enable or include 3d chart in C#

1. Add your chart control in the windows forms.
2. Enable the chart control.
3. As usual, add the code which i have posted in my previous post.
4. Add the below line to modify your 2d chart into 3d chart.


chart1.ChartAreas[0].Area3DStyle.Enable3D = true;

Cheers,
Venkatesan Prabu .J
Head, KaaShiv InfoTech
www.kaashivinfotech.com

Error in chart control in C#


Error:
A chart element with the name 'Default' could not be found in the 'SeriesCollection'.

Solution: 

You need to create a series element for a chart before accessing it.

          Series series1 = this.chart3.Series.Add("Default");
            chart3.Series["Default"].Points.DataBindY(myReader1, "OrderQty");
         

Complete coding to draw a chart in C#


  string myConnectionString = @"Data Source=XXXX;Integrated Security=true;Initial Catalog=DBName";

            // Define the database query    
            string mySelectQuery = "SELECT Top 5 OrderQty,ProductID,SpecialOfferID, UnitPrice   FROM [SalesOrderDetail]";

            // Create a database connection object using the connection string    
            SqlConnection myConnection = new SqlConnection(myConnectionString);

            // Create a database command on the connection using query    
            SqlCommand myCommand = new SqlCommand(mySelectQuery, myConnection);

            // Open the connection    
            myCommand.Connection.Open();

            // Create a database reader    
            SqlDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
            Series series = this.chart1.Series.Add("Default");

            chart1.Series["Default"].Points.DataBind(myReader, "OrderQty", "ProductID", "Tooltip=SpecialOfferID, Label=UnitPrice{C2}");
            chart1.ChartAreas[0].Area3DStyle.Enable3D = true;

            // Close the reader and the connection

Cheers,
Venkatesan Prabu. J
Head, KaaShiv InfoTech, Chennai.


14.1.13

get specified format of the date from datetime picker

Here is a minor syntax to extract the date information from datetimepicker in windows forms c#


string VenkatDate = dateTimePicker1.Value.ToShortDateString();
To get the date in specified format:
string VenkatDate = dateTimePicker1.Value.ToString("yyyy-MM-dd");
Cheers,
Venkatesan Prabu. J
Head, KaaShiv InfoTech
www.kaashivinfotech.com

13.1.13

Image Height and width in C#


Code to get the height and width of an image.


System.Drawing.Image objImage = System.Drawing.Image.FromFile("C:\venkat.gif");
width = objImage.Width;
height = objImage.Height;  


Cheers,
Venkatesan prabu .J
http://www.facebook.com/KaaShivInfoTech

Remove drawn object in C# windows forms

Questions:

How can I delete (clear) lines that were made useing Graphics.DrawLine() ?
 
Drawn a circle in windows form. I want the remove the drawn object in C# windows forms.
Pen pen = new Pen(Color.Black, 3); Graphics gr = this.CreateGraphics(); gr.DrawEllipse(pen, 5,5,20,20);


Quick Answer:

this.Invalidate();
 
Cheers,
Venkatesan Prabu . J
https://www.facebook.com/KaaShivInfoTech