Results 1 to 7 of 7

Thread: Small desktop application. I need an experienced VB user to help me on a few points.

Threaded View

  1. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,434

    Re: Small desktop application. I need an experienced VB user to help me on a few poin

    i'd use a class (call it record), with public properties for:

    - Title
    - Artist
    - Type
    - Year
    - Image

    vb.net Code:
    1. <System.Serializable()> Public Class record
    2.  
    3.     Private _Title As String
    4.     Private _Artist As String
    5.     Private _Type As String
    6.     Private _Year As Integer
    7.     Private _Image As Image
    8.  
    9.     Public Property Title() As String
    10.         Get
    11.             Return _Title
    12.         End Get
    13.         Set(ByVal value As String)
    14.             _Title = value
    15.         End Set
    16.     End Property
    17.  
    18.     Public Property Artist() As String
    19.         Get
    20.             Return _Artist
    21.         End Get
    22.         Set(ByVal value As String)
    23.             _Artist = value
    24.         End Set
    25.     End Property
    26.  
    27.     Public Property Type() As String
    28.         Get
    29.             Return _Type
    30.         End Get
    31.         Set(ByVal value As String)
    32.             _Type = value
    33.         End Set
    34.     End Property
    35.  
    36.     Public Property Year() As Integer
    37.         Get
    38.             Return _Year
    39.         End Get
    40.         Set(ByVal value As Integer)
    41.             _Year = value
    42.         End Set
    43.     End Property
    44.  
    45.     Public Property Image() As Image
    46.         Get
    47.             Return _Image
    48.         End Get
    49.         Set(ByVal value As Image)
    50.             _Image = value
    51.         End Set
    52.     End Property
    53.  
    54. End Class

    you need to create a dialog for adding new records, with 3 textboxes, a numericupdown, a picturebox + 3 buttons (see image).

    Name:  01-09-2012 17.51.07.jpg
Views: 622
Size:  31.5 KB

    note: the cancel button's dialogresult property is Cancel + the save button's dialogresult property is OK.


    in the dialog form's sub new you'd load the controls from the instance of record passed in.
    it could either be a new record, or you've reopened an existing record:

    vb.net Code:
    1. Public Class frmRecord
    2.  
    3.     Private _currentRecord As New record
    4.     Public Property currentRecord() As record
    5.         Get
    6.             Return _currentRecord
    7.         End Get
    8.         Set(ByVal value As record)
    9.             _currentRecord = value
    10.         End Set
    11.     End Property
    12.  
    13.     Public Sub New(ByVal r As record)
    14.         InitializeComponent()
    15.  
    16.         TextBox1.Text = r.Title
    17.         TextBox2.Text = r.Artist
    18.         TextBox3.Text = r.Type
    19.         NumericUpDown1.Value = If(r.Year <> 0, r.Year, Now.Year)
    20.         PictureBox1.Image = r.Image
    21.  
    22.         currentRecord = r
    23.  
    24.     End Sub
    25.  
    26.     Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
    27.         currentRecord.Title = TextBox1.Text
    28.     End Sub
    29.  
    30.     Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
    31.         currentRecord.Artist = TextBox2.Text
    32.     End Sub
    33.  
    34.     Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
    35.         currentRecord.Type = TextBox3.Text
    36.     End Sub
    37.  
    38.     Private Sub NumericUpDown1_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NumericUpDown1.ValueChanged
    39.         currentRecord.Year = CInt(NumericUpDown1.Value)
    40.     End Sub
    41.  
    42.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    43.         'declare new OpenFileDialog + set it's initial properties
    44.         Dim ofd As New OpenFileDialog With { _
    45.         .Title = "Select image file", _
    46.         .Filter = "BMP (*.bmp)|*.bmp|GIF (*.gif)|*.gif|JPG (*.jpg)|*.jpg|PNG (*.png)|*.png|All Files (*.*)|*.*", _
    47.         .FilterIndex = 0, _
    48.         .InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)}
    49.  
    50.         'show the dialog
    51.         If ofd.ShowDialog = DialogResult.OK Then
    52.             PictureBox1.Image = New Bitmap(ofd.FileName)
    53.             currentRecord.Image = PictureBox1.Image
    54.         End If
    55.  
    56.     End Sub
    57.  
    58. End Class

    then in your main form a list(of record) + an add new record button, an imageList, + a listview setup for large icon.

    Name:  01-09-2012 17.55.00.jpg
Views: 560
Size:  17.5 KB

    in the button_click event, create a new instance of record class + a new instance of your add dialog form + pass the record variable
    in the listview doubleclick, you'd open the selected item in the add new dialog.

    at form_closed you'd serialize the list(of record) as binary, then in form_load you'd check if the binary file exists + if so, deserialize it back in to your list(of record)

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Dim records As New List(Of record)
    4.     Dim iml As New ImageList
    5.  
    6.     Private Sub Form1_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
    7.         Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
    8.         Dim myWriter As New IO.FileStream("test.bin", IO.FileMode.Create)
    9.         formatter.Serialize(myWriter, records)
    10.         myWriter.Close()
    11.     End Sub
    12.  
    13.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    14.         ListView1.LargeImageList = iml
    15.         iml.ImageSize = New Size(64, 64)
    16.  
    17.         If IO.File.Exists("test.bin") Then
    18.             Dim formatter As New Runtime.Serialization.Formatters.Binary.BinaryFormatter
    19.             Dim fs As New IO.FileStream("test.bin", IO.FileMode.Open)
    20.             records = DirectCast(formatter.Deserialize(fs), Global.System.Collections.Generic.List(Of record))
    21.             fs.Close()
    22.  
    23.             For Each r In records
    24.                 iml.Images.Add("", New Bitmap(r.Image, 64, 64))
    25.                 Dim item As ListViewItem = ListView1.Items.Add(r.Artist & Environment.NewLine & r.Title, iml.Images.Count - 1)
    26.                 item.Tag = r
    27.             Next
    28.  
    29.         End If
    30.     End Sub
    31.  
    32.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    33.         Dim r As New record
    34.         Dim frm As New frmRecord(r)
    35.         If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
    36.             records.Add(frm.currentRecord)
    37.             iml.Images.Add("", New Bitmap(frm.currentRecord.Image, 64, 64))
    38.             Dim item As ListViewItem = ListView1.Items.Add(frm.currentRecord.Artist & Environment.NewLine & frm.currentRecord.Title, iml.Images.Count - 1)
    39.             item.Tag = frm.currentRecord
    40.         End If
    41.     End Sub
    42.  
    43.     Private Sub ListView1_MouseDoubleClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseDoubleClick
    44.  
    45.         If ListView1.HitTest(e.Location).Item.Index > -1 Then
    46.             Dim item As ListViewItem = ListView1.HitTest(e.Location).Item
    47.             Dim frm As New frmRecord(DirectCast(item.Tag, record))
    48.             If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
    49.                 records(ListView1.HitTest(e.Location).Item.Index) = frm.currentRecord
    50.                 iml.Images(ListView1.HitTest(e.Location).Item.Index) = New Bitmap(frm.currentRecord.Image, 64, 64)
    51.                 ListView1.HitTest(e.Location).Item.Text = frm.currentRecord.Artist & Environment.NewLine & frm.currentRecord.Title
    52.                 ListView1.HitTest(e.Location).Item.Tag = frm.currentRecord
    53.             End If
    54.         End If
    55.     End Sub
    56.  
    57. End Class
    Last edited by .paul.; Sep 1st, 2012 at 12:15 PM.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width