CodePaste Logo
New Snippet New Snippet Recent Snippets Recent Snippets My Snippets My Snippets Web Code Search Snippets Search
Sign inor Register
Language: VB.NET

Using the DevExpress DropDownButton, DXPopupMenu and XML Serialization

2275 Views
Copy Code Show/Hide Line Numbers
   1:  'Form1.vb
   2:  #Region " Import Declaratives "
   3:   
   4:  Imports DevExpress.XtraEditors
   5:  Imports DevExpress.Utils.Menu
   6:  Imports System.IO
   7:  Imports System.Xml.Serialization
   8:   
   9:  #End Region
  10:   
  11:  Public Class Form1
  12:   
  13:      Private MsgFile As String = _
  14:          IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MsgFile.xml")
  15:      Private Msgs As New Messages
  16:      Private moMessages As DXPopupMenu
  17:   
  18:      ''' <summary>
  19:      ''' Close the form and dispose of everying
  20:      ''' </summary>
  21:      ''' <param name="sender"></param>
  22:      ''' <param name="e"></param>
  23:      ''' <remarks></remarks>
  24:      Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
  25:          For Each oItem As DXMenuItem In moMessages.Items
  26:              RemoveHandler oItem.Click, AddressOf moMessage_Click
  27:          Next
  28:          moMessages.Items.Clear()
  29:          moMessages.Dispose()
  30:          Msgs.Clear()
  31:          Msgs = Nothing
  32:      End Sub
  33:   
  34:      ''' <summary>
  35:      ''' Load the form and messages, build the DropDownButton context
  36:      ''' </summary>
  37:      ''' <param name="sender"></param>
  38:      ''' <param name="e"></param>
  39:      ''' <remarks>
  40:      ''' If necessary create the messages
  41:      ''' </remarks>
  42:      Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  43:          If Not IO.File.Exists(MsgFile) Then CreateMsgs()
  44:          LoadMsgs()
  45:          BuildDropDownButton()
  46:      End Sub
  47:   
  48:      ''' <summary>
  49:      ''' Create 10 Messages in the Message collection<br />
  50:      ''' Serialize the Message collection to a text XML file<br />
  51:      ''' </summary>
  52:      ''' <remarks>
  53:      ''' <example>
  54:      ''' If Not IO.File.Exists(MsgFile) Then CreateMsgs()
  55:      ''' </example>
  56:      ''' </remarks>
  57:      Private Sub CreateMsgs()
  58:          For ii As Int16 = 1 To 10
  59:              Msgs.Add(New Message(String.Format("Message {0}", ii), _
  60:                  "".PadLeft(100, "."c).Replace(".", String.Format("Text of message {0}{1}", ii, vbCrLf))))
  61:          Next
  62:          '
  63:          Dim objStreamWriter As New StreamWriter(MsgFile)
  64:          Dim x As New XmlSerializer(Msgs.GetType)
  65:          x.Serialize(objStreamWriter, Msgs)
  66:          objStreamWriter.Close()
  67:          x = Nothing
  68:          Msgs.Clear()
  69:      End Sub
  70:   
  71:      ''' <summary>
  72:      ''' Deserialize the Message collection from a text XML file
  73:      ''' </summary>
  74:      ''' <remarks>
  75:      ''' <example>
  76:      ''' LoadMsgs()
  77:      ''' </example>
  78:      ''' </remarks>
  79:      Private Sub LoadMsgs()
  80:          Dim objStreamReader As New StreamReader(MsgFile)
  81:          Dim x As New XmlSerializer(Msgs.GetType)
  82:          Msgs = CType(x.Deserialize(objStreamReader), Messages)
  83:          objStreamReader.Close()
  84:          x = Nothing
  85:      End Sub
  86:   
  87:      ''' <summary>
  88:      ''' Replace vbLf with vbCrLf since XML Serialization saves the CrLf as Lf
  89:      ''' and set the <c>Text</c> property of MemoEdit1
  90:      ''' </summary>
  91:      ''' <param name="sender">The <c>DXMenuItem</c> clicked on</param>
  92:      ''' <param name="e"></param>
  93:      ''' <remarks>
  94:      ''' <example>
  95:      ''' ii = moMessages.Items.Add(New DXMenuItem(msg.Description, AddressOf moMessage_Click))
  96:      ''' </example>
  97:      ''' </remarks>
  98:      Private Sub moMessage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  99:          MemoEdit1.Text = CStr(CType(sender, DXMenuItem).Tag).Replace(vbLf, vbCrLf)
 100:      End Sub
 101:   
 102:      ''' <summary>
 103:      ''' <list type="bullet">
 104:      ''' <item><description>Create a new DXPopupMenu</description></item>
 105:      ''' <item><description>Iterate through the Messages in the Message collection, creating new DXMenuItems</description></item>
 106:      ''' <list type="bullet">
 107:      ''' <item><description>Add the new DXMenuItem to the DXPopupMenu</description></item>
 108:      ''' <item><description>Set the click event of the DXMenuItem to moMessage_Click</description></item>
 109:      ''' <item><description>Set the tag of the DXMenuItem to the Message property of the Message item</description></item>
 110:      ''' </list>
 111:      ''' <item><description>Set the DropDownButton's DropDownControl property to the DXPopupMenu</description></item>
 112:      ''' </list>
 113:      ''' </summary>
 114:      ''' <remarks></remarks>
 115:      Private Sub BuildDropDownButton()
 116:          Dim ii As Integer
 117:          moMessages = New DXPopupMenu()
 118:          For Each msg As Message In Msgs
 119:              ii = moMessages.Items.Add(New DXMenuItem(msg.Description, AddressOf moMessage_Click))
 120:              moMessages.Items(ii).Tag = msg.Message
 121:          Next
 122:          Me.DropDownButton1.DropDownControl = moMessages
 123:      End Sub
 124:   
 125:  End Class
 126:   
 127:  'Messages.vb
 128:  #Region " Import Declaratives "
 129:   
 130:  Imports System
 131:  Imports System.Collections.Generic
 132:  Imports System.Collections.ObjectModel
 133:  Imports System.Xml.Serialization
 134:   
 135:  #End Region
 136:   
 137:  <XmlRoot(ElementName:="Messages")> _
 138:  Public Class Messages
 139:      Inherits Collection(Of Message)
 140:   
 141:      Public Event Changed As EventHandler(Of MessagesChangedEventArgs)
 142:   
 143:      Protected Overrides Sub InsertItem(ByVal index As Integer, ByVal newItem As Message)
 144:          MyBase.InsertItem(index, newItem)
 145:   
 146:          RaiseEvent Changed(Me, New MessagesChangedEventArgs( _
 147:              ChangeType.Added, newItem, Nothing))
 148:      End Sub
 149:   
 150:      Protected Overrides Sub SetItem(ByVal index As Integer, ByVal newItem As Message)
 151:          Dim replaced As Message = Items(index)
 152:          MyBase.SetItem(index, newItem)
 153:   
 154:          RaiseEvent Changed(Me, New MessagesChangedEventArgs( _
 155:              ChangeType.Replaced, replaced, newItem))
 156:      End Sub
 157:   
 158:      Protected Overrides Sub RemoveItem(ByVal index As Integer)
 159:          Dim removedItem As Message = Items(index)
 160:          MyBase.RemoveItem(index)
 161:   
 162:          RaiseEvent Changed(Me, New MessagesChangedEventArgs( _
 163:              ChangeType.Removed, removedItem, Nothing))
 164:      End Sub
 165:   
 166:      Protected Overrides Sub ClearItems()
 167:          MyBase.ClearItems()
 168:   
 169:          RaiseEvent Changed(Me, New MessagesChangedEventArgs( _
 170:              ChangeType.Cleared, Nothing, Nothing))
 171:      End Sub
 172:   
 173:  End Class
 174:   
 175:  'Message.vb
 176:  #Region " Import Declaratives "
 177:   
 178:  Imports System.Xml.Serialization
 179:   
 180:  #End Region
 181:   
 182:  Public Class Message
 183:   
 184:      Private _description As String
 185:      Private _message As String
 186:   
 187:      ''' <summary>
 188:      ''' Add a new blank message
 189:      ''' </summary>
 190:      ''' <remarks></remarks>
 191:      Public Sub New()
 192:          _description = Nothing
 193:          _message = Nothing
 194:      End Sub
 195:   
 196:      ''' <summary>
 197:      ''' Add a new message
 198:      ''' </summary>
 199:      ''' <param name="description">This is the text display in the DropDownButton list</param>
 200:      ''' <param name="message">This is the text that is the "body" of the message</param>
 201:      ''' <remarks>
 202:      ''' When a message is selected the "body" of the message is placed in the text of the MemoEdit
 203:      ''' </remarks>
 204:      Public Sub New(ByVal description As String, ByVal message As String)
 205:          _description = description
 206:          _message = message
 207:      End Sub
 208:   
 209:      Public Property Description() As String
 210:          Get
 211:              Return _description
 212:          End Get
 213:          Set(ByVal value As String)
 214:              _description = value
 215:          End Set
 216:      End Property
 217:   
 218:      <XmlElementAttribute(ElementName:="Body")> _
 219:      Public Property Message() As String
 220:          Get
 221:              Return _message
 222:          End Get
 223:          Set(ByVal value As String)
 224:              _message = value
 225:          End Set
 226:      End Property
 227:   
 228:  End Class
 229:   
 230:  'ChangeType.vb
 231:  Public Enum ChangeType
 232:   
 233:      Added
 234:      Removed
 235:      Replaced
 236:      Cleared
 237:   
 238:  End Enum
 239:   
 240:  'MessagesChangedEventArgs.vb
 241:  Public Class MessagesChangedEventArgs
 242:      Inherits EventArgs
 243:   
 244:      Public ReadOnly ChangedItem As Message
 245:      Public ReadOnly ChangeType As ChangeType
 246:      Public ReadOnly ReplacedWith As Message
 247:   
 248:      Public Sub New(ByVal change As ChangeType, ByVal item As Message, _
 249:          ByVal replacement As Message)
 250:   
 251:          ChangeType = change
 252:          ChangedItem = item
 253:          ReplacedWith = replacement
 254:      End Sub
 255:  End Class
by Thom Lamb
  February 11, 2010 @ 6:53am
Tags:
Description:
This code uses DevExpress controls to demonstrates how to populate a DropDownButton from a DXPopupMenu. It also demonstrates basic XML Serialization, serializing a collection (Messages) containing a specific class of objects (Message).

Add a comment


Report Abuse
brought to you by:
West Wind Techologies



If you find this site useful and use it frequently please consider making a donation to support this free service.
Donate