Posts Tagged: Controls


22
Feb 09

DoUNo: ListView_ItemSelectionChangedEvent and MultiSelect

Today, I was trying to customise ListViewControl by playing with the item drawn event, thats when I came across this peculiar stuff.

In ListView, if you have subscribed for the ItemSelectionChangedEvent,the event will not be raised, if you select the same item more than once provided the ListView.MultiSelect = false;  Shhh. Quite difficult to understand, isn’t it ??

Ok. An example, Lets consider that  we have a list view control with two items, foo and bar. Also consider that we have subscribed for the ItemSelectionChangedEvent and the ListView’s MultiSelect is true. Now if you run the application keeping a break point in the ItemSelectionChangedEventHandler and if you select the item foo again and again the control will break. On the other hand, if your ListView’s MultiSelect is false, the control won’t hit the break point. Which means that the event will not be raised.


3
Feb 09

DoUNo: ListViewItems in C#

When you create a ListViewItem in C#, it automatically creates a ListViewSubItem for it and the ListViewItem.SubItem[0] is the ListViewItem


i.e, the default value of ListViewItem is the value of ListViewItem at SubItems[0] 
 

You can find more about ListViewItems in http://codelog.blogial.com/2008/07/18/using-listview-control/ 

Refer to http://stackoverflow.com/questions/502782/automatic-creation-of-listviewsubitems to know my experiences with the ListViewControl.


29
Oct 08

TreeView Control in C#

ListViewControl

ListViewControl

The folder list pane that you see in a Windows Explorer is called TreeViewControl. In C#, its really easy to use these controls. There are loads of facilities provided by this control. Some of them are really great. In this post, I will try to explain some of the basics of this control.

Adding a TreeViewControl

To add a TreeView control, just double click on the TreeView control from the Tools window or add the snippet,

TreeView myTreeView = new TreeView();
this.Controls.Add(myTreeView);  //adding the control to the presentation form

Adding a Node

Nodes are the items that you can find in a tree view control. This is the tricky part. You can have a node, that has another node, that has one more node, and so on. Just like the files and folders inside another folder. To add a node, the control has got, Nodes.Add() method. This is a overloaded method and it has loads of overloaded parameters. For example, you can have a string as a parameter to this method to add a node.

myTreeView.Nodes.Add("foo");  //will add a new node with the text foo in it
Continue reading →


15
Oct 08

Glossy buttons in c#

Is there any way to create glossy, eye candy buttons in c# ?? Before starting off, we need to know what makes a thing glossy. There are two factors,

  • Color selection
  • Linear gradient fill over the control

Ok. Now lets see the implementation. For colors, we have Color class, but for Lineargradient ?? Yeah, we do have a LinearGradientBrush in c#.

Note: The task of making a button glossy will be acheived in the following order,

  • Drawing the border
  • Filling the lower rectangle with a solid color
  • Filling the upper rectangle with a linear gradient
  1. Include a button to the project (myGlossyButton)
  2. Change the FlatStyle of the myGlossyButton to Flat

    myGlossyButton.FlatStyle = FlatStyle.Flat;
  3. Handle the paint event of this button. Lets name the handler method as GlossyPainter
  4. In the GlossyPainter, first we need to give our button a border [task:1]. Say White border.

    e.Graphics.DrawRectangle(Pens.White, new Rectangle(0, 0, e.ClipRectangle.Width - 1, e.ClipRectangle.Height - 1));  //1 is subtracted from the size to make the border completely visible
  5. Next up, we need to fill the rectangle with some base color [task:2]. This time, we will fill the bottom half with some solid color and the upper half with some gradient color.

    SolidBrush aBaseSolidbrush = new SolidBrush(Color.FromArgb(150, Color.YellowGreen));  //FromArgb allows you to even specify the opacity of the colore.Graphics.FillRectangle(aBaseSolidBrush,  new Rectangle(2, (e.ClipRectangle.Height - 4) / 2, e.ClipRectangle.Width - 4, e.ClipRectangle.Height - 4));  //4 is subtracted just to leave a blank space between the border and the button     

  6. Filling the upper part of the button with the linear gradient color [task:3]

    LinearGradientBrush aLineargradientBrush = new LinearGradientBrush(new Rectangle(2, 2, e.ClipRectangle.Width - 4, (e.ClipRectangle.Height - 4) / 2), Color.FromArgb(255, Color.White), Color.FromArgb(100, aColor), 90);  //Specifying how big the linear gradient should be and in what direction the gradient should appear

    e.Graphics.FillRectangle(aLinearGradientBrush, new Rectangle(2, 2, e.ClipRectangle.Width - 4, (e.ClipRectangle.Height - 4) / 2));  //Filling the upperpart of the button
  7. Run the application, you should be seeing a glossy button

This is one of the ways to make a control look glossy. There are many ways to acheive this. If you know a really simple way share it with us.


6
Sep 08

Custom shaped controls in C# — using GraphicsPath

Have you ever thought of having a circular button, triangular form, amoebic labels ?? Doing all these in C# is really simple and cool. All you need to do is, use the Region property of the control to change the shape. Lets try to change the shape of a button to ellipse.

  • Create a new button the usual way.

System.Windows.Forms.Button myButton = new System.Windows.Forms.Button();

  • Change the color of the button to distinguish it from the form’s background.

myButton.BackColor = Color.Blue; //Lets change the color

  • Increase the size of the button.

myButton.Size = new Size(100, 100);

  • Create a GraphicsPath object to define a custom path which will be associated with the button’s region in the next step. There are loads of methods like AddEllipse(). Try AddPloygon() which takes a set of points.

System.Drawing.Drawing2D.GraphicsPath aGraphicsPath = new System.Drawing.Drawing2D.GraphicsPath(); //The GraphicsPath class allows us to define custom paths

myGraphicsPath.AddEllipse(5, 5, 90, 90); //A big circle

  • Associate the GraphicsPath with the button’s Region property.

myButton.Region = new Region(aGraphicsPath); //Associating the path made by us to the button's region

Run the application and see for your self, a rounded button. Cool, isn’t it ? Play with GraphicsPath to create custom shaped controls.


24
Aug 08

Auto ellipsis, done __manually__ in C# using TextRenderer

Before starting off with this article, first we need to understand what AutoEllipsis means. Most of the control in C# will have a boolean property called AutoEllipsis. C#’s description goes this way:

// Summary:
// Gets or sets a value indicating whether the ellipsis character (...) appears
// at the right edge of the System.Windows.Forms.Label, denoting that the System.Windows.Forms.Label
// text ext
ends beyond the specified length of the System.Windows.Forms.Label.
//
// Returns:
// true if the additional label text is to be indicated by an ellipsis; otherwise,
// false. The default is false.
Continue reading →


14
Aug 08

Transparent images over controls

Before me starting off with the article, sorry for not posting anything last week. I was kinna busy with the office works :P Hmm. Lets begin.

Have you ever tried to put a transparent image over a button or some other control ?? I meant transparent. If not, lets try it.

Way that may not give you the solution you need

not_so_transparent

Way that you may be interested to know about

  • Add a button to your applicationReally transparent image


System.Windows.Forms.Button myTransparentImageButton = new System.Windows.Forms.Button();

this.Controls.Add(myTransparentImageButton);

  • For the picture … do we need a picturebox to put the image ?? No. Lets try to draw the image over the button.
  • Import an image to the resource file and add the following snippet to the paint event of the button
private void myTransparentImageButton_Paint(object sender, PaintEventArgs e)  //Handling the
paintevent as the image should be painted over the button when the button is painted
{
     Graphics aGraphics = e.Graphics;  //To draw the image, we use the abstract class -- Graphics
     Image aImage = (Image)<your_name_space>.<resource_file_class_name>.<image>;  //Creating an
image object using the bitmap from the resource file
     aGraphics.DrawImage(aImage, new Point(e.ClipRectangle.Width - aImage.Width, 0)); //Drawing the
image in the required position
}

28
Jul 08

Using ContextMenus in C#

In my last post I was talking about ContextMenus and ContextMenuStrips. In this one, I will try to explain some of the ways you can make use of that control. Before that a quick recap. ContextMenus are obsolete. Now a days we use ContextMenuStrip. It has many cool facilities that makes your application more eye cathy (can’t beat mac though). Lets begin with

Remember: ContextMenu, menu, ContextMenuStrip means ContextMenuStrip in the following explanations

Adding a ContextMenuStrip
ContextMenuStrip can be found under System.Windows.Forms.ContextMenuStrip. So to have a menu control, just create an object of the above

System.Windows.Forms.ContextMenuStrip myContextMenuStrip = new System.Windows.Forms.ContextMenuStrip(); //Just creates a usable object. Nothing else

Adding items to the ContextMenuStrip
Adding items to the menu is easy (as usual). Use the Items.Add() method to do so.

myContextMenu.Items.Add("foo"); //Now you can see and click foo from the contextmenu

Continue reading →


25
Jul 08

Introduction to ContextMenus

Two days back, I was thinking on what to post next. So I was playing with VisualStudio for a while looking for the controls that I can learn and tell you about. Then I saw the ContextMenuStrip. As usual I don’t want the VS to create the control for me. So I went to the designer and started to create an object of ContextMenu (Oops. Did I misspell ??). No. I was correct. There are two menus. One is ContextMenu and the other is ContextMenuStrip. So I googled for the difference between them but I hardly found something related to that. (Now I have a nice topic to blog about !!). Lets peep in to the difference between them and the functionalities they provide.

The ContextMenu
This can be found under System.Windows.Forms.ContextMenu. This is a derivative of the class called Menu. This is the usual context menu that you see when you right click in any Notepad window (in early releases of windows). This looks very blunt. Not much of a functionalities were provided for this control. This control exists just for backward compatiability. You can add text to the menu and get notified when they were clicked by the users.

The ContextMenuStrip
This can be found under System.Windows.Forms.ContextMenuStrip. This is a derivative of the class called Control. This is the flashy one that you see when you right click on MS Word or any other MS products. This is the later one and it has many functionalities like,

  • You can have images (icons) against each and every menu item
  • You can have checkboxes against each and every menu item
  • You can disable items (unlike the ComboBoxes where we do it manually like this one)
  • You can change the way it is rendered in the screen using the RenderMode property
  • You can be notified with many events like, when the menu pops up, checked state changed, clicked, double clicked, item has been added, etc.
  • You can toggle showshadow under the menu and more …

Ok. Now how to use all these ?? I will post it next.


21
Jul 08

Adding images to the ImageList

In my previous post, I was talking about ListView control, where I have mentioned about the ImageLists. In this one we will try to go deep in to it.

ImageList control is a container of images. Its like a list of images. You can’t see this control on the scren, yet you can make use of this control through other controls. Like for example, in the ListView control, one can add items to the list with some icons to its left. Such a thing is made possible through these ImageLists. This asts like a container of images, which can be associated with any of the controls and used in the application. Continue reading →