Saturday, January 30, 2010

Accessing Master Page Properties from Child Page

 Hello Friends.

Today hot topic is that how to access properties from MasterPage into content page.
I think there is several methods you can access those properties or variable.

1. By Master property of content page
2. By interface
3. By Including a MasterPage Directive in Pages.

So here i am providing you some code stuff for the same.
1. Access Master Page properties and Method by Master property of Page.

Master Page Code

     public string ButtonText
      {
           get { return this.Button1.Text; }
           set { this.Button1.Text = value; }
      }

Child Page Code

        //Master page is the name of master page file
        MasterPage mast = this.Master as MasterPage;
        if (mast != null)
        {
            mast.ButtonText = "New Text";
        }

-----------------------------------------------------------------------------------
2. Access Master Page properties and Via Interface.
see here
http://dotnetbyexample.blogspot.com/2007/10/right-way-of-accessing-master-page.html

Wednesday, January 27, 2010

Setting the wallpaper from C# code

Hi Friends,

What if you want to change the wallpaper of your desktop from C#.Net. for that you have to use Unmanaged code.

You have to import and pass parameters to function from User32 dll.
Here I am posting the code for performing the same.

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SystemParametersInfo(int Action, int Param, string lParam, int WinIni);
call the function and pass some useful parameters like ...
Bitmap bmp = new Bitmap(Image.FromFile(@"C:\mytest.jpg"));
bmp.Save("MyFile.Bmp", ImageFormat.Bmp);
SystemParametersInfo(20, 0, "pic.bmp", 0x1);

The first parameter 20 is for Wallpaper
Third parameter is your file path as String.
Last is Update parameter keep it 0X1, 0X01 | 0X02 ..


Note:- Here you see that I am converting the jpeg file into bmp file. Because it state that image should be 24 bits, so keep in mind the change of file else no effect of this function.




Wednesday, January 20, 2010

Handling Null value in case of value type.

Hello friends,

As we can check for null values in case of Reference type.
What if want to do the same with value type.
Here the new feature of .net do it for you.

public int? x;
public int? X
{
get { return x; }
}

Now you can check
if( X != null) or if ( X == null)

Note:- Needed to be type cast to (int) where need int value.
Use:- Very usefull suppose you declared any field as integer in database table.
And you forget to declared default value as 0 (zero).
Then whenever you query to database you get a null value so it can be used for checking that.

Private setter for public getter

Hello Friends,

Now i am showing you some very good features of Net 2.0 over .Net 1.0.
Suppose you want to expose a public property. So for that you have
public getter method but what about its setter method.
from .net 2.0 tou can have a private setter for a public getter too like this.

private int x;
public int X
{
get { return x; }
private set { x = value; }
}

Monday, January 11, 2010

Creating Non-Rectangular Form in C#

Hello Friends,


Now here i am trying some very good stuff on C#.net.
The first in this row is creating non rectangular Form in C#.

The Idea comes when i was created Magnifier as a sub part of my project.
When i created a rectangular magnifier it's ok but when it comes to rounded then
i have to think.

From some book i get these idea (also available in many of GDI tutorials).

The idea behind this is that, GDI provides a new Class called GraphicsPath.
With the help of this class you can draw any closed arc, lines and shapes.

So either override onPaint event of form or Onload event put some code stuff like this.


System.Drawing.Drawing2D.GraphicsPath myShape = new System.Drawing.Drawing2D.GraphicsPath();
myShape.AddEllipse(0, 0, this.Width, this.Height);
this.Region = new System.Drawing.Region(shape);

Note:- You are not getting region property directly in intellisence because it is the
property of control class and here this refers the control.

Now in place of AddEllipse you can add any closed shape for your form.
Hope you enjoy....