MItem
Home
classes
MItem
About
Instances of the MItem class represents an items inside MListBox, MComboBox or MContextMenu components. Items are continuous, vertical indexes of text or images.
Usage
Instances of the MItem class itself are just blank items with no text and with basic animations on hover and click.
// Create a new item
MItem item = new MItem();
// Add item to listBox
listBox.Items.Add(item);
You can of course inherit your class from MItem and design your custom M-Item.
Writing custom items
In order to create your own custom M-Item you need to inherit your class from MItem base class. If your item should act as a divider item, set Divider property to true.
class CustomItem : MItem
{
}
Draw method
Every MItem instance have it’s own PrimaryText property which represents main text of an item. In MItem class this primary text is not rendered by default so you can draw it the way you like. In order to draw your item you need to override OnDrawItem() method. See example below:
class CustomItem : MItem
{
// Draw method
protected override void OnDrawItem(Graphics g, Rectangle bounds)
{
// Base call
base.OnDrawItem(g, bounds);
// Draws primary text
using (SolidBrush brush = new SolidBrush(Color.Black))
g.DrawString(PrimaryText, new Font("Segoe UI", 9), brush, Bounds);
}
}
If you do not like the way how basic implementation of MItem looks and feel, just remove base.OnDrawItem() method. By doing that the design of an item will be completely up to you.
Update method
In order to create animations you need to have an update method. Update method can be hooked up by overriding OnUpdate() method:
class CustomItem : MItem
{
// Draw method
protected override void OnUpdate(object sender, EventArgs e)
{
// Base update call
base.OnUpdate(sender, e);
}
}
Mouse methods
Sometimes you need to know if mouse is hovering over an item or if user clicked on it. That is the case where mouse methods gets really handy:
class CustomItem : MItem
{
// Mouse click
protected override void OnMouseClick(object sender, MouseEventArgs e)
{
base.OnMouseClick(sender, e);
}
// Mouse enter
protected override void OnMouseEnter(object sender, MouseEventArgs e)
{
base.OnMouseEnter(sender, e);
}
// Mouse leave
protected override void OnMouseLeave(object sender, MouseEventArgs e)
{
base.OnMouseLeave(sender, e);
}
// Mouse move
protected override void OnMouseMove(object sender, MouseEventArgs e)
{
base.OnMouseMove(sender, e);
}
}
Scaling
We suggest to use DIP class in order to achieve best viewing experience across all possible devices, especially when setting item’s height.
class CustomItem : MItem
{
// Constructor
CustomItem()
{
// Item height
Height = DIP.Set(50);
}
}
Conclusion
Writing custom MItem can be a bit challenging process. Below you can find an example on how SingleLineItem is written:
public class SingleLineItem : MItem
{
///
/// Alignment of primary text
///
ContentAlignment TextAlign { get; set; }
///
/// Constructor
///
public SingleLineItem(string primaryText, ContentAlignment textAlign = ContentAlignment.MiddleLeft)
{
// Primary text
PrimaryText = primaryText;
// Alignment
TextAlign = textAlign;
}
///
/// Draw item
///
protected override void OnDrawItem(Graphics g, Rectangle bounds)
{
// Base call
base.OnDrawItem(g, bounds);
// String format
// Converts alignment types
StringFormat format = new StringFormat();
int lNum = (int)Math.Log((double)TextAlign, 2);
format.LineAlignment = (StringAlignment)(lNum / 4);
format.Alignment = (StringAlignment)(lNum % 4);
// Draws primary text
using (SolidBrush brush = new SolidBrush(Selected ? Owner.Accent : Owner.Component.SourceTheme.COMPONENT_FOREGROUND.Normal.ToColor()))
g.DrawString(PrimaryText, new Font("Segoe UI", 9), brush, new Rectangle(Bounds.X + 10, Bounds.Y, Bounds.Width - 20, Bounds.Height), format);
}
}
Class content
Assembly |
Minimal.dll |
Namespace |
Minimal.Components.Items |
Inheritance |
- |
Attributes |
- |
Implements |
- |
Constructors
Name |
Description |
|
MItem() |
Initializes a new instance of the MItem class. |
|
Properties
Name |
Description |
Type |
Owner |
MListBox component which item belongs to. |
MListBox |
PrimaryText |
Main text of an item. |
string |
Bounds |
Client rectangle of an item. |
Rectanle |
Height |
Height of an item. |
string |
Hover |
True if mouse hovers over an item. |
bool |
Selected |
True if an item is selected by the user. |
bool |
ClickEffect |
Click effect of an item. Occurs on an item click. |
ClickEffect |
Fields
Name |
Description |
Type |
- |
- |
- |
Events
Name |
Description |
Type |
- |
- |
- |
Methods
Name |
Description |
Returns |
OnUpdate() |
Called when item is updated. |
- |
OnDrawItem() |
Called when item is being drawn. |
- |
OnMouseClick() |
Called on an item click. |
- |
OnMouseEnter() |
Called when mouse enters Bounds rentangle on an item. |
- |
OnMouseLeave() |
Called when mouse leaves Bounds rentangle on an item. |
- |
OnMouseMove() |
Called when mouse is moving in Bounds rentangle on an item. |
- |