Adding items to ListView from a Class - vb.net

I am having trouble adding an item to a listview from one of my classes. I am missing something that is obviously over my head. If someone could help me to understand what I am missing that would be great.
I don't know how much detail I need to provide. What I am trying to do is to basic so I know I have to be missing something. I have a class that I am trying to use to add an item to a listview I have on a form. There are no errors thrown, yet nothing gets added.
I have even tried using something as simple as frmGiveaways.lstAccounts.items.add("wtf") in the class, it doesn't throw any errors, the line is processed, yet no items appear in the list.
What are some things that would keep me from being able to do this?
Here is the class
Imports Simple_IRC_Client.Delegates
Imports System.Text.RegularExpressions
''' <summary>
''' Handles minimal IRC server response messages.
''' </summary>
Public Class OutputMessages : Implements IDisposable
#Region " Private Members "
Private ReadOnly ColorQuit As Color = Color.FromArgb(102, 54, 31)
Private ReadOnly ColorPrivmsg As Color = Color.FromArgb(76, 76, 76)
Private ReadOnly ColorTopic As Color = Color.FromArgb(176, 55, 176)
Private ReadOnly ColorKick As Color = Color.FromArgb(199, 50, 50)
Private ReadOnly ColorUserEvent As Color = Color.FromArgb(128, 128, 128)
Private WithEvents _ircConnection As InitiateConnection
Private _mainView As MainView
Private _window As RichTextBox
#End Region
#Region " Constructor "
Public Sub New(ByVal mainView As MainView, ByVal ircConnection As InitiateConnection)
_mainView = mainView
_ircConnection = ircConnection
_window = _mainView.rtbChannelView
End Sub
#End Region
#Region " EventArghs "
Private Sub ServerResponse(ByVal serverResponse As String) Handles _ircConnection.ServerResponseOutputEventArghs
' This setting has only been added for demonstration purposes of what raw data
' looks like.
If _mainView.mnuMainMenuOptionsrawData.Checked Then
OutputResponse(_window, serverResponse, Color.Black)
Exit Sub
End If
Dim parts() As String = serverResponse.Split(" "c)
Dim address As String = parts(0)
Select Case parts(1)
Case "PRIVMSG" : Privmsg(address, serverResponse.Substring(indexOf(serverResponse, 3) + 1).Substring(1))
Case "JOIN" : Join(address)
Case "PART", "QUIT" : Quit(address)
Case "ERROR" : Disconnected()
Case "332" : TopicOnjoin(serverResponse.Substring(indexOf(serverResponse, 4) + 1).Substring(1))
End Select
End Sub
#End Region
#Region " Private"
''' <summary>
''' Outputs a GUI message on me/user Privmsg.
''' </summary>
''' <param name="address">The source of the user's local host.</param>
''' <param name="message">The message text.</param>
''' <remarks>
''' Displays an output message to the normalview window with correct format and colouring on Me,
''' User Privmsg.
''' </remarks>
Private Sub Privmsg(ByVal address As String, ByVal message As String)
Dim outputFormat As String = String.Format("<{0}> {1}", Split(address), message)
OutputResponse(_window, outputFormat, Color.Black)
Select Case message
Case "" : _ircConnection.SendMessage(String.Format("PRIVMSG " & ConnectionInformation.Channel & " :" & "{0}", Split(address)))
Case frmGiveaways.keyword
_ircConnection.SendMessage(String.Format("PRIVMSG " & ConnectionInformation.Channel & " :" & "recieved keyword", Split(address)))
frmGiveaways.lstAccountsEntered.Items.Add(Split(address))
End Select
End Sub
Private Sub Join(ByVal address As String)
If Split(address) = ConnectionInformation.ChannelNick Then
Exit Sub
End If
Dim outputFortmat As String = String.Format("{0} has joined the conversation.", Split(address))
OutputResponse(_window, outputFortmat, ColorUserEvent)
'Welcome message proof of concept
'_ircConnection.SendMessage(String.Format("PRIVMSG " & ConnectionInformation.Channel & " :" & "Welcome, {0}", Split(address)))
End Sub
''' <summary>
''' Outputs a GUI message on user Quitting.
''' </summary>
''' <param name="address">The source of the user's local host.</param>
''' <remarks>
''' Displays an output message to the normalview window with correct format on user Quitting with Quit message.
''' </remarks>
Private Sub Quit(ByVal address As String)
Dim outputFortmat As String = String.Format("{0} has left the conversation.", Split(address))
OutputResponse(_window, outputFortmat, ColorUserEvent)
End Sub
Private Sub Disconnected()
Dim outputFortmat As String = "Disconnected!"
OutputResponse(_window, outputFortmat, Color.Red)
End Sub
Private Sub TopicOnjoin(ByVal subject As String)
OutputResponse(_window, String.Format("The chat's topic is: {0} ", subject), Color.Black)
NewLine()
End Sub
#End Region
#Region " Output Response "
''' <summary>
''' Displays the servers output response message.
''' </summary>
''' <param name="control">The control name.</param>
''' <param name="output">The server output.</param>
''' <param name="color">The control output line color</param>
''' <remarks>
''' Responsible for displaying all server and user response messages.
''' </remarks>
Public Sub OutputResponse(ByVal control As RichTextBox, ByVal output As String, ByVal color As Color)
Dim outputFormat As String = String.Format("{0}", output)
If control.InvokeRequired Then
control.Invoke(New OutputEventHandler(AddressOf OutputResponse), control, output, color)
Else
Dim start = control.TextLength
Dim length = outputFormat.Length
With control
.AppendText(outputFormat & Environment.NewLine)
.ScrollToCaret()
.Select(start, length)
.SelectionColor = color
End With
End If
End Sub
Private Sub NewLine()
If _window.InvokeRequired Then
_window.Invoke(New MethodInvoker(AddressOf NewLine))
Else
_window.AppendText(Environment.NewLine)
End If
End Sub
#End Region
#Region " Functions "
''' <summary>
'''
''' </summary>
''' <param name="s"></param>
''' <param name="instance"></param>
''' <returns></returns>
''' <remarks></remarks>
Private Function indexOf(ByVal s As String, ByVal instance As Integer) As Integer
Dim startAt As Integer = -1
For x As Integer = 1 To instance
startAt = s.IndexOf(" "c, startAt + 1)
Next
Return startAt
End Function
Private Function Split(ByVal name As String) As String
Return name.Split("!"c)(0)
End Function
#End Region
#Region " IDisposable "
Public Sub dispose() Implements IDisposable.Dispose
End Sub
#End Region
End Class
The part I am having a problem with is PrivMsg under the Region Private.

you could create a method on the form which receives a string to create a new LV item from the text passed:
Public Sub AddNewLVItem(txtName As String)
Dim LVI as New ListViewItem
LVI.Text = txtName
LVI.Group = xxx ' whatever other props there are
lstaccounts.items.Add(LVI)
End Sub
or shorthand, if there are no subitems, groups etc
lstaccounts.items.Add(New ListViewItem(txtName))
EDIT:
TYPICALLY, the form name is blank on MDI child forms only, I am not sure why that is the case here, but it obviously is. Forms are classes and SHOULD be instanced for use. They can use the old default instance method (FormName.Show) but that exists mainly for compatibility back to the VB4/5/6 days when forms were something apart from classes. The old default instance also makes it easier for tinkerers (non programmers) who dont have a clue about OOP, to get something woking easily.
First, you will need a reference to frmGiveAways with app level scope so that other classes all reference the same object. Add a module to your project (if there is not already one) and add:
Friend frmGive As frmGiveAways
(Or change the form name to something like FormGiveAways so the instance name and all the code referencing it can still use frmGiveAways).
Now, when you go to show the form in the menu:
If frmGive Is Nothing Then ' check to see if it already exists
frmGive = New frmGiveAways
frmGive.Show ' could be a separate 'If' if needed
End if
Now there is one instance of frmGiveAways using the reference frmGive and as long as everyone uses that reference, things will work fine. In the 'Sub AddEntry' it would pay to set a break and monitor the Me.Name value for a while to make sure all the calling code is properly refactored.
HTH

Instead of hard coding the listview, try passing it into the sub as a parameter.
Something like this:
Private Sub Privmsg(ByVal address As String, ByVal message As String, ByRef LV as ListView)
Dim outputFormat As String = String.Format("<{0}> {1}", Split(address), message)
OutputResponse(_window, outputFormat, Color.Black)
Select Case message
Case "" : _ircConnection.SendMessage(String.Format("PRIVMSG " & ConnectionInformation.Channel & " :" & "{0}", Split(address)))
Case frmGiveaways.keyword
_ircConnection.SendMessage(String.Format("PRIVMSG " & ConnectionInformation.Channel & " :" & "recieved keyword", Split(address)))
LV.Items.Add(Split(address))
End Select
End Sub

The basic syntax to add items to a ListView is this:
' Create list view items
Dim item1 As New ListViewItem("Item #1", 0)
Dim item2 As New ListViewItem("Item #2", 1)
Dim item3 As New ListViewItem("Item #3", 2)
Then you can add the list view items to the list view, either individually or as a group, like this:
Adding item one-by-one:
ListView1.Items.Add(item1)
ListView1.Items.Add(item2)
ListView1.Items.Add(item3)
Adding several items at once:
ListView1.Items.AddRange(New ListViewItem() {item1, item2, item3})
Note: The ListView1.Items.Add() and ListView1.Items.AddRange() methods both need ListViewItem objects, not string values.

Related

Adding a progress bar to show how much percentage of form had been loaded

I've a form having certain lines of code on the form_load event and thus it takes some time to be shown. I want to add a progress bar to show how much percentage of form has been loaded. How can I do so ..?
In short:- Like while browsing the web pages the progress as shown in web browser indicate the value of page has been loaded. I want to do so in vb.net.
How can I do so .?
You'll need to move your long running code into the form's .Shown event handler.
I would suggest adding a StatusStrip to your form and add a ToolStripProgressBar to that. I did this in a program I wrote and used the following bit of code work on it.
It's all pretty self explanatory :-)
''' <summary>
''' Sets minimum and maximum values for ToolStripProgressBar and shows it
''' </summary>
''' <param name="min">Minimum Value</param>
''' <param name="max">Maximum Value</param>
''' <param name="labeltext">Text to show in ToolStripLabel</param>
Private Sub SetAndShowProgressBar(min As Integer, max As Integer, labeltext As String)
ToolStripStatusLabel1.Text = labeltext
ToolStripProgressBar1.Minimum = min
ToolStripProgressBar1.Maximum = max + 5
ToolStripProgressBar1.Value = 1
ToolStripProgressBar1.Visible = True
StatusStrip1.Update()
End Sub
''' <summary>
''' Hides ToolStripProgressBar
''' </summary>
Private Sub HideProgressBar()
ToolStripStatusLabel1.Text = ""
ToolStripProgressBar1.Visible = False
StatusStrip1.Update()
End Sub
Private Delegate Sub incprogressbarDelegate()
Private Sub IncrementProgressBar()
If Me.InvokeRequired Then
Dim d As New incprogressbarDelegate(AddressOf IncrementProgressBar)
Me.Invoke(d, New Object())
Else
Me.ToolStripProgressBar1.Value += 1
StatusStrip1.Update()
End If
End Sub
Private Delegate Sub SetProgressBarDelegate(i As Integer)
Private Sub SetProgressBarValue(i As Integer)
If Me.InvokeRequired Then
Dim d As New SetProgressBarDelegate(AddressOf SetProgressBarValue)
Me.Invoke(d, New Object() {i})
Else
Me.ToolStripProgressBar1.Value = i
End If
End Sub

Methods to use the serial port in multiple forms

I'm designing a windows userform to interact with a microcontroller through the serial port.
The GUI includes multiple userforms which will use the same serialport. I researched how to do that and I found different ideas. Some I don't know if it works the others I'm not sure how to implement it in code. Let's say I have
Form1: Start.vb
Form2: Shield1.vb
1) Can I declare the serial port in the start userform as:
Public Shared SerialPort1 As New System.IO.Ports.SerialPort
And use it in the other forms ?
2) First alternative: Use a Module to declare a new Serialport
Module Module1
Public WithEvents mySerialPort1 As New IO.Ports.SerialPort
Private Sub mySerialPort1_DataReceived(sender As Object, _
e As System.IO.Ports.SerialDataReceivedEventArgs) _
Handles mySerialPort1.DataReceived
End Sub
End Module
Is this method right? If yes How do I use it in the code of my forms ? How to include the DataReceived Event in the code of the forms ?
3) Second alternative: Constructor of the Serialport in the start form and then pass the data to the other forms as mentionned in this post: Alternate Solution 1
private void OnSetup(object sender, EventArgs e)
{
this.port = new SerialPort(...);
// TODO: initialize port
Form2 f2 = new Form2(this.port);
f2.Show();
Form3 f3 = new Form3(this.port);
f3.Show();
Form4 f4 = new Form4(this.port);
f4.Show();
}
Are then the Events also included ? How do I use them?
4) Third alternative: Use a static class like done in this solution:
Alternate Solution 2
Is the code written in C# here right ? I'm writing my programm in VB.net but I could take this as a reference.
Which is the recommended solution for a beginner ? Could you please write it in a small code if you have another suggestion or a correction ?
I apologize in advance for any ambiguity or falsly used terms.
Thanks you!
I would follow the "Singleton" design pattern, which ensures that only one instance of a class is created. Here is a well-accepted template for such a class:
Public NotInheritable Class MySerial
Private Shared ReadOnly _instance As New Lazy(Of MySerial)(Function() New
MySerial(), System.Threading.LazyThreadSafetyMode.ExecutionAndPublication)
Private Sub New()
End Sub
Public Shared ReadOnly Property Instance() As MySerial
Get
Return _instance.Value
End Get
End Property
End Class
In the New() method you should set up your serial port as you need to. Then no matter where you need to use the port, you make your references to the Instance:
Dim singletonSerial As MySerial = MySerial.Instance
This is the canonical pattern to ensure that you have only one copy of an object without resorting to static classes. It's a design pattern that dates back more than 20 years and still works great when you need exactly one copy of an object.
I found this very helpful example of a sigleton C# class for serial port:
C# Singleton Example
As a beginner I used a Code converter to have it in VB.Net. Can you guys let me know if the resulting code is correct and I can use it ? Thanks a lot!!
Imports System
Imports System.IO
Imports System.IO.Ports
Imports System.Threading
Namespace HeiswayiNrird.Singleton
Public NotInheritable Class SerialPortManager
Private Shared lazy As Lazy(Of SerialPortManager) = New Lazy(Of SerialPortManager)(() => { }, New SerialPortManager)
Public Shared ReadOnly Property Instance As SerialPortManager
Get
Return lazy.Value
End Get
End Property
Private _serialPort As SerialPort
Private _readThread As Thread
Private _keepReading As Boolean
Private Sub New()
MyBase.New
Me._serialPort = New SerialPort
Me._readThread = Nothing
Me._keepReading = false
End Sub
''' <summary>
''' Update the serial port status to the event subscriber
''' </summary>
Public Event OnStatusChanged As EventHandler(Of String)
''' <summary>
''' Update received data from the serial port to the event subscriber
''' </summary>
Public Event OnDataReceived As EventHandler(Of String)
''' <summary>
''' Update TRUE/FALSE for the serial port connection to the event subscriber
''' </summary>
Public Event OnSerialPortOpened As EventHandler(Of Boolean)
''' <summary>
''' Return TRUE if the serial port is currently connected
''' </summary>
Public ReadOnly Property IsOpen As Boolean
Get
Return Me._serialPort.IsOpen
End Get
End Property
''' <summary>
''' Open the serial port connection using basic serial port settings
''' </summary>
''' <param name="portname">COM1 / COM3 / COM4 / etc.</param>
''' <param name="baudrate">0 / 100 / 300 / 600 / 1200 / 2400 / 4800 / 9600 / 14400 / 19200 / 38400 / 56000 / 57600 / 115200 / 128000 / 256000</param>
''' <param name="parity">None / Odd / Even / Mark / Space</param>
''' <param name="databits">5 / 6 / 7 / 8</param>
''' <param name="stopbits">None / One / Two / OnePointFive</param>
''' <param name="handshake">None / XOnXOff / RequestToSend / RequestToSendXOnXOff</param>
Public Sub Open(Optional ByVal portname As String = "COM1", Optional ByVal baudrate As Integer = 9600, Optional ByVal parity As Parity = Parity.None, Optional ByVal databits As Integer = 8, Optional ByVal stopbits As StopBits = StopBits.One, Optional ByVal handshake As Handshake = Handshake.None)
Me.Close
Try
Me._serialPort.PortName = portname
Me._serialPort.BaudRate = baudrate
Me._serialPort.Parity = parity
Me._serialPort.DataBits = databits
Me._serialPort.StopBits = stopbits
Me._serialPort.Handshake = handshake
Me._serialPort.ReadTimeout = 50
Me._serialPort.WriteTimeout = 50
Me._serialPort.Open
Me.StartReading
Catch As IOException
If (Not (OnStatusChanged) Is Nothing) Then
OnStatusChanged(Me, String.Format("{0} does not exist.", portname))
End If
Catch As UnauthorizedAccessException
If (Not (OnStatusChanged) Is Nothing) Then
OnStatusChanged(Me, String.Format("{0} already in use.", portname))
End If
Catch ex As Exception
If (Not (OnStatusChanged) Is Nothing) Then
OnStatusChanged(Me, ("Error: " + ex.Message))
End If
End Try
If Me._serialPort.IsOpen Then
Dim sb As String = StopBits.None.ToString.Substring(0, 1)
Select Case (Me._serialPort.StopBits)
Case StopBits.One
sb = "1"
Case StopBits.OnePointFive
sb = "1.5"
Case StopBits.Two
sb = "2"
End Select
Dim p As String = Me._serialPort.Parity.ToString.Substring(0, 1)
Dim hs As String = "No Handshake"
'TODO: Warning!!!, inline IF is not supported ?
(Me._serialPort.Handshake = Handshake.None)
Me._serialPort.Handshake.ToString
If (Not (OnStatusChanged) Is Nothing) Then
OnStatusChanged(Me, String.Format("Connected to {0}: {1} bps, {2}{3}{4}, {5}.", Me._serialPort.PortName, Me._serialPort.BaudRate, Me._serialPort.DataBits, p, sb, hs))
End If
If (Not (OnSerialPortOpened) Is Nothing) Then
OnSerialPortOpened(Me, true)
End If
Else
If (Not (OnStatusChanged) Is Nothing) Then
OnStatusChanged(Me, String.Format("{0} already in use.", portname))
End If
If (Not (OnSerialPortOpened) Is Nothing) Then
OnSerialPortOpened(Me, false)
End If
End If
End Sub
''' <summary>
''' Close the serial port connection
''' </summary>
Public Sub Close()
Me.StopReading
Me._serialPort.Close
If (Not (OnStatusChanged) Is Nothing) Then
OnStatusChanged(Me, "Connection closed.")
End If
If (Not (OnSerialPortOpened) Is Nothing) Then
OnSerialPortOpened(Me, false)
End If
End Sub
''' <summary>
''' Send/write string to the serial port
''' </summary>
''' <param name="message"></param>
Public Sub SendString(ByVal message As String)
If Me._serialPort.IsOpen Then
Try
Me._serialPort.Write(message)
If (Not (OnStatusChanged) Is Nothing) Then
OnStatusChanged(Me, String.Format("Message sent: {0}", message))
End If
Catch ex As Exception
If (Not (OnStatusChanged) Is Nothing) Then
OnStatusChanged(Me, String.Format("Failed to send string: {0}", ex.Message))
End If
End Try
End If
End Sub
Private Sub StartReading()
If Not Me._keepReading Then
Me._keepReading = true
Me._readThread = New Thread(ReadPort)
Me._readThread.Start
End If
End Sub
Private Sub StopReading()
If Me._keepReading Then
Me._keepReading = false
Me._readThread.Join
Me._readThread = Nothing
End If
End Sub
Private Sub ReadPort()
While Me._keepReading
If Me._serialPort.IsOpen Then
'byte[] readBuffer = new byte[_serialPort.ReadBufferSize + 1];
Try
'int count = _serialPort.Read(readBuffer, 0, _serialPort.ReadBufferSize);
'string data = Encoding.ASCII.GetString(readBuffer, 0, count);
Dim data As String = Me._serialPort.ReadLine
If (Not (OnDataReceived) Is Nothing) Then
OnDataReceived(Me, data)
End If
Catch As TimeoutException
End Try
Else
Dim waitTime As TimeSpan = New TimeSpan(0, 0, 0, 0, 50)
Thread.Sleep(waitTime)
End If
End While
End Sub
End Class
End Namespace

VB.NET XML Serialize a List of Inherited Objects

I'm trying to serialize a list of inherited objects. I can serialize each object individually when its declared according to its true type, but when its a list of the base type the serializer gets confused because its expecting to serialize a "base" object but then see different objects that had inherited the "base" object.
Any ideas on how to easily solve this? I've done a bunch of reading and have not found an elegant easy to maintain way to do this.
Trying to serialize:
'Define a list of the common inherited base for all print elements.
' This allows all types of print elements to be added to one list and in the user defined order
' Order is important for the layer of objects when 'drawing' them on the canvas during the on print options
<XmlArrayAttribute("ElementsList")> 'Tell the XML Seralizer this is a list
Public Elements As New List(Of CardItemBase) 'XML Serializer does not like this becuase the elements in this list are not CardItemBase, they are that + stuff, and it does not know what to do with stuff
Here are two examples of items that inherited the base, both of which will serialize (even in a list) if declared as their true type.:
Public Class TextString
'Inherit the common properties for a card item
Inherits CardItemBase
'Vars to hold string info to use when printing
Public StringToPrint As String = String.Empty
Public XLocation As Single = 0
Public YLocation As Single = 0
'Font info (store values instead of a Pen since Pen requires dispose and also does not work with Xml Serializing)
Public Font As String = Nothing
Public FontSize As Integer = 0
Public FontColor As System.Drawing.KnownColor = Drawing.KnownColor.Black 'Note, does NOT require dispose
'Constructor to force users to pass in all info
Public Sub New() 'Create an empty Sub New Overload for use with XML Serializer
End Sub
Public Sub New(ByVal CardSide As CardSideOptions, ByVal InputStringToPrint As String, ByVal TextFont As String, ByVal TextSize As Integer, ByVal TextColor As System.Drawing.KnownColor, ByVal InputXLocation As Single, ByVal InputYLocation As Single)
'Set the inherited objects
MyBase.New(CardSide:=CardSide)
'Save passed in info
StringToPrint = InputStringToPrint
Font = TextFont
FontSize = TextSize
FontColor = TextColor
XLocation = InputXLocation
YLocation = InputYLocation
End Sub
''' <summary>
''' Return the ture type of this object as DriverJob.PrintElementTypes even if the item is nested in a CardItemBase list.
''' </summary>
<XmlIgnore()>
Public Overrides ReadOnly Property MyType() As PrintElementTypes
Get
Return PrintElementTypes.Text
End Get
End Property
<XmlIgnore()>
Public Overrides ReadOnly Property ToString() As String
Get
Try
'Create a string that descipes this object
Dim ReturnStr As String = ""
'Concatinate a description of each element
ReturnStr += "X: " & XLocation.ToString() & ", Y: " & YLocation.ToString() & ", "
ReturnStr += Font & ", " & FontSize & ", "
ReturnStr += FontColor.ToString() & ", "
ReturnStr += "'" & StringToPrint & "'"
'Return the completed string.
Return ReturnStr
Catch ex As Exception
'Just return the error string
Return ex.Message
End Try
End Get
End Property
End Class
''' <summary>
''' Sub class for passing images to print to the printing method.
''' </summary>
Public Class Image
'Inherit the common properties for a card item
Inherits CardItemBase
'Vars to hold image info to use when printing
Public ImageFilePathToUse As String = ""
Public XLocation As Single = 0
Public YLocation As Single = 0
Public Height As Single = 0
Public Width As Single = 0
'Constructor to force users to pass in all info
Public Sub New() 'Create an empty Sub New Overload for use with XML Serializer
End Sub
Public Sub New(ByVal CardSide As CardSideOptions, ByVal InputImageFilePathToUse As String, Optional ByVal InputXLocation As Single = 0, Optional ByVal InputYLocation As Single = 0, Optional ByVal InputHeight As Single = 0, Optional ByVal InputWidth As Single = 0)
'Set the inherited objects
MyBase.New(CardSide:=CardSide)
'Save passed in info
ImageFilePathToUse = InputImageFilePathToUse
XLocation = InputXLocation
YLocation = InputYLocation
Height = InputHeight
Width = InputWidth
End Sub
''' <summary>
''' Return the ture type of this object as DriverJob.PrintElementTypes even if the item is nested in a CardItemBase list.
''' </summary>
<XmlIgnore()>
Public Overrides ReadOnly Property MyType() As PrintElementTypes
Get
Return PrintElementTypes.Image
End Get
End Property
''' <summary>
''' Creates a simple string description of the object
''' </summary>
<XmlIgnore()>
Public Overrides ReadOnly Property ToString() As String
Get
Try
'Create a string that descipes this object
Dim ReturnStr As String = ""
'Concatinate a description of each element
ReturnStr += "X: " & XLocation.ToString() & ", Y: " & YLocation.ToString() & ", "
ReturnStr += "Height: " & Height.ToString() & ", Width: " & Width.ToString() & ", "
ReturnStr += ImageFilePathToUse
'Return the completed string.
Return ReturnStr
Catch ex As Exception
'Just return the error string
Return ex.Message
End Try
End Get
End Property
End Class
After a few more hours of reading and testing I found a solution that will work for me.
Above the base class definition the XML include tag can be used define all the types that inherited this base class so the serializer won't be confused. This adds only one step to do when adding classes that inherit this base class, for my use I can live with this.
Here is line of code I added:
'Class to hold common properties that may be needed for describing all card elements. This is intented to be inherited
<XmlInclude(GetType(TextString)), XmlInclude(GetType(Line)), XmlInclude(GetType(Rectangle)), XmlInclude(GetType(Image)), XmlInclude(GetType(PrintAndTopcoatBlocking)), XmlInclude(GetType(MagstripeSetup)), XmlInclude(GetType(SmartCardSetup))>
Public MustInherit Class CardItemBase

How to read data from serial port in vb.net?

I made a class and there is this sub named SendUSSD, when this is called it sends a ussd code like *123# to a COM port where a gsm mobile is connected. This ussd is supposed to return the mobile balance.
If IsOpen = True Then 'checks if the port is open
SMSPort.WriteLine("AT+CUSD=1,""*123#""" vbCr) 'this sends the ussd code
Form1.TextBox2.Text = SMSPort.ReadLine().ToString() 'this shows the response
End If
Now the problem is sometimes I get the full response like "Your current balance is so and so". But, most of the time I get a part of the message like "Your curr". My guess is that it takes some time to get the response, so how do I make this Form1.TextBox2.Text = SMSPort.ReadLine().ToString() line wait until the last character which is a full-stop to appear and then execute the line?
im using this class to connect com ports.
'connect like this
Public comm As New CommunicationManager
comm.Parity = "None"
comm.StopBits = "One"
comm.DataBits = "8"
comm.BaudRate = "38400"
comm.PortName = comport_ismi
comm.OpenPort()
and the class
Imports System.Text
Imports System.Drawing
Imports System.IO.Ports
Imports System.Windows.Forms
Public Class CommunicationManager
#Region "Manager Enums"
''' <summary>
''' enumeration to hold our transmission types
''' </summary>
Public Enum TransmissionType
Text
Hex
End Enum
''' <summary>
''' enumeration to hold our message types
''' </summary>
Public Enum MessageType
Incoming
Outgoing
Normal
Warning
[Error]
End Enum
#End Region
#Region "Manager Variables"
'property variables
Private _baudRate As String = String.Empty
Private _parity As String = String.Empty
Private _stopBits As String = String.Empty
Private _dataBits As String = String.Empty
Private _portName As String = String.Empty
Private _transType As TransmissionType
Private _displayWindow As RichTextBox
'global manager variables
Private MessageColor As Color() = {Color.Blue, Color.Green, Color.Black, Color.Orange, Color.Red}
Private comPort As New SerialPort()
#End Region
#Region "Manager Properties"
''' <summary>
''' Property to hold the BaudRate
''' of our manager class
''' </summary>
Public Property BaudRate() As String
Get
Return _baudRate
End Get
Set(value As String)
_baudRate = value
End Set
End Property
''' <summary>
''' property to hold the Parity
''' of our manager class
''' </summary>
Public Property Parity() As String
Get
Return _parity
End Get
Set(value As String)
_parity = value
End Set
End Property
''' <summary>
''' property to hold the StopBits
''' of our manager class
''' </summary>
Public Property StopBits() As String
Get
Return _stopBits
End Get
Set(value As String)
_stopBits = value
End Set
End Property
''' <summary>
''' property to hold the DataBits
''' of our manager class
''' </summary>
Public Property DataBits() As String
Get
Return _dataBits
End Get
Set(value As String)
_dataBits = value
End Set
End Property
''' <summary>
''' property to hold the PortName
''' of our manager class
''' </summary>
Public Property PortName() As String
Get
Return _portName
End Get
Set(value As String)
_portName = value
End Set
End Property
''' <summary>
''' property to hold our TransmissionType
''' of our manager class
''' </summary>
Public Property CurrentTransmissionType() As TransmissionType
Get
Return _transType
End Get
Set(value As TransmissionType)
_transType = value
End Set
End Property
''' <summary>
''' property to hold our display window
''' value
''' </summary>
Public Property DisplayWindow() As RichTextBox
Get
Return _displayWindow
End Get
Set(value As RichTextBox)
_displayWindow = value
End Set
End Property
#End Region
#Region "Manager Constructors"
''' <summary>
''' Constructor to set the properties of our Manager Class
''' </summary>
''' <param name="baud">Desired BaudRate</param>
''' <param name="par">Desired Parity</param>
''' <param name="sBits">Desired StopBits</param>
''' <param name="dBits">Desired DataBits</param>
''' <param name="name">Desired PortName</param>
Public Sub New(baud As String, par As String, sBits As String, dBits As String, name As String, rtb As RichTextBox)
_baudRate = baud
_parity = par
_stopBits = sBits
_dataBits = dBits
_portName = name
_displayWindow = rtb
'now add an event handler
AddHandler comPort.DataReceived, New SerialDataReceivedEventHandler(AddressOf comPort_DataReceived)
End Sub
''' <summary>
''' Comstructor to set the properties of our
''' serial port communicator to nothing
''' </summary>
Public Sub New()
_baudRate = String.Empty
_parity = String.Empty
_stopBits = String.Empty
_dataBits = String.Empty
_portName = comport_ismi
_displayWindow = Nothing
'add event handler
AddHandler comPort.DataReceived, New SerialDataReceivedEventHandler(AddressOf comPort_DataReceived)
End Sub
#End Region
#Region "WriteData"
Public Sub WriteData(msg As String)
Select Case CurrentTransmissionType
Case TransmissionType.Text
'first make sure the port is open
'if its not open then open it
If Not (comPort.IsOpen = True) Then
comPort.Open()
End If
'send the message to the port
comPort.Write(msg)
'display the message
DisplayData(MessageType.Outgoing, msg & Convert.ToString(vbLf))
Exit Select
Case TransmissionType.Hex
Try
'convert the message to byte array
Dim newMsg As Byte() = HexToByte(msg)
'send the message to the port
comPort.Write(newMsg, 0, newMsg.Length)
'convert back to hex and display
DisplayData(MessageType.Outgoing, ByteToHex(newMsg) & Convert.ToString(vbLf))
Catch ex As FormatException
'display error message
DisplayData(MessageType.[Error], ex.Message)
Finally
_displayWindow.SelectAll()
End Try
Exit Select
Case Else
'first make sure the port is open
'if its not open then open it
If Not (comPort.IsOpen = True) Then
comPort.Open()
End If
'send the message to the port
comPort.Write(msg)
'display the message
DisplayData(MessageType.Outgoing, msg & Convert.ToString(vbLf))
Exit Select
End Select
End Sub
#End Region
#Region "HexToByte"
''' <summary>
''' method to convert hex string into a byte array
''' </summary>
''' <param name="msg">string to convert</param>
''' <returns>a byte array</returns>
Private Function HexToByte(msg As String) As Byte()
'remove any spaces from the string
msg = msg.Replace(" ", "")
'create a byte array the length of the
'divided by 2 (Hex is 2 characters in length)
Dim comBuffer As Byte() = New Byte(msg.Length / 2 - 1) {}
'loop through the length of the provided string
For i As Integer = 0 To msg.Length - 1 Step 2
'convert each set of 2 characters to a byte
'and add to the array
comBuffer(i / 2) = CByte(Convert.ToByte(msg.Substring(i, 2), 16))
Next
'return the array
Return comBuffer
End Function
#End Region
#Region "ByteToHex"
''' <summary>
''' method to convert a byte array into a hex string
''' </summary>
''' <param name="comByte">byte array to convert</param>
''' <returns>a hex string</returns>
Private Function ByteToHex(comByte As Byte()) As String
'create a new StringBuilder object
Dim builder As New StringBuilder(comByte.Length * 3)
'loop through each byte in the array
For Each data As Byte In comByte
'convert the byte to a string and add to the stringbuilder
builder.Append(Convert.ToString(data, 16).PadLeft(2, "0"c).PadRight(3, " "c))
Next
'return the converted value
Return builder.ToString().ToUpper()
End Function
#End Region
#Region "DisplayData"
''' <summary>
''' method to display the data to & from the port
''' on the screen
''' </summary>
''' <param name="type">MessageType of the message</param>
''' <param name="msg">Message to display</param>
<STAThread> _
Private Sub DisplayData(type As MessageType, msg As String)
'_displayWindow.Invoke(New EventHandler(Sub()
' _displayWindow.SelectedText = String.Empty
' _displayWindow.SelectionFont = New Font(_displayWindow.SelectionFont, FontStyle.Bold)
' _displayWindow.SelectionColor = MessageColor(CInt(type))
' _displayWindow.AppendText(msg)
' _displayWindow.ScrollToCaret()
' End Sub))
End Sub
#End Region
#Region "OpenPort"
Public Function OpenPort() As Boolean
Try
'first check if the port is already open
'if its open then close it
If comPort.IsOpen = True Then
comPort.Close()
End If
'set the properties of our SerialPort Object
comPort.BaudRate = Integer.Parse(_baudRate)
'BaudRate
comPort.DataBits = Integer.Parse(_dataBits)
'DataBits
comPort.StopBits = DirectCast([Enum].Parse(GetType(StopBits), _stopBits), StopBits)
'StopBits
comPort.Parity = DirectCast([Enum].Parse(GetType(Parity), _parity), Parity)
'Parity
comPort.PortName = _portName
'PortName
'now open the port
comPort.Open()
'display message
DisplayData(MessageType.Normal, "Port AÇILDI: " + DateTime.Now + vbLf)
'return true
Return True
Catch ex As Exception
DisplayData(MessageType.[Error], ex.Message)
Return False
End Try
End Function
#End Region
#Region "ClosePort"
Public Function ClosePort() As Boolean
Try
'first check if the port is already open
'if its open then close it
If comPort.IsOpen = True Then
comPort.Close()
End If
'display message
DisplayData(MessageType.Normal, "Port KAPANDI: " + DateTime.Now + vbLf)
'return true if port is closed
If comPort.IsOpen = False Then
Return True
End If
DisplayData(MessageType.Normal, "Kapatmada hata oluştu" & vbLf)
Return False
Catch ex As Exception
DisplayData(MessageType.[Error], ex.Message)
Return False
End Try
End Function
#End Region
#Region "SetParityValues"
Public Sub SetParityValues(obj As Object)
For Each str As String In [Enum].GetNames(GetType(Parity))
DirectCast(obj, ComboBox).Items.Add(str)
Next
End Sub
#End Region
#Region "SetStopBitValues"
Public Sub SetStopBitValues(obj As Object)
For Each str As String In [Enum].GetNames(GetType(StopBits))
DirectCast(obj, ComboBox).Items.Add(str)
Next
End Sub
#End Region
#Region "SetPortNameValues"
Public Sub SetPortNameValues(obj As Object)
For Each str As String In SerialPort.GetPortNames()
DirectCast(obj, ComboBox).Items.Add(str)
Next
End Sub
#End Region
#Region "comPort_DataReceived"
''' <summary>
''' method that will be called when theres data waiting in the buffer
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
Private Sub comPort_DataReceived(sender As Object, e As SerialDataReceivedEventArgs)
'determine the mode the user selected (binary/string)
Select Case CurrentTransmissionType
'user chose string
Case TransmissionType.Text
'read data waiting in the buffer
Dim msg As String = comPort.ReadExisting()
'display the data to the user
DisplayData(MessageType.Incoming, msg)
' + "\n"); **************
Exit Select
'user chose binary
Case TransmissionType.Hex
'retrieve number of bytes in the buffer
Dim bytes As Integer = comPort.BytesToRead
'create a byte array to hold the awaiting data
Dim comBuffer As Byte() = New Byte(bytes - 1) {}
'read the data and store it
comPort.Read(comBuffer, 0, bytes)
'display the data to the user
DisplayData(MessageType.Incoming, ByteToHex(comBuffer))
' + "\n");
Exit Select
Case Else
'read data waiting in the buffer
Dim str As String = comPort.ReadExisting()
'display the data to the user
DisplayData(MessageType.Incoming, str)
' + "\n");
Exit Select
End Select
End Sub
#End Region
End Class
I guess you are using the DataReceived event, if so, you can just take the data and split it by the lastIndexOf(Enviroment.NewLine).
you will have two part the first one is a string with some amount of lines and the second one is a string that contain no more lines.
You can take the first part and split it by new line and even create a new event (LineReceived).
for the second part(the part after the lastIndexOf(Enviroment.NewLine)) of the data just concatenate it to the beginning of the data that will arrive the next time.
Try setting comm.Newline. Probably should be vbCR.

How to make LinearLayout in Visual Basic (visual studio 2013)

(this is not related to VB6)
like above, I want to make a 'textbox' control between separated 'label' controls.
I used method to load those three controls (two labels and one textbox), but I cannot precisely locate the textbox and the following label.
In the application, there will be more than hundreds of sentences with almost the same number of blanks(textbox), so the perfect lining is required.
So, is there any good method to do that?
Thank you!
You can make a class that inherits from Panel and includes two Labels and a Textbox that get automatically realigned. I did something like that to get you started. Just paste the code into your project, recompile once and you can add these to your Form from the Toolbox (or create them dynamically of course). This is nowhere complete. You should make properties for the fonts for example and additional events based on your needs but you get my idea I think.
Public Class ctrlBlankSentence
Inherits Panel
Private WithEvents lblLeft As Label 'The left part of the sentence
Private WithEvents tbBlank As TextBox 'The blank text
Private WithEvents lblRight As Label 'The right part of the sentence
Public Event BlankTextChanged() 'Use events to react to user input
Private _Spacing As Integer
''' <summary>
''' Determines the distance between the labels and the textbox
''' </summary>
Public Property Spacing As Integer
Get
Return _Spacing
End Get
Set(value As Integer)
_Spacing = value
DoreAlign()
End Set
End Property
Private _TextboxWidth As Integer
''' <summary>
''' Determines the width of the textbox
''' </summary>
Public Property TextboxWidth As Integer
Get
Return _TextboxWidth
End Get
Set(value As Integer)
_TextboxWidth = value
DoreAlign()
End Set
End Property
Private _AutosizeControl As Boolean
''' <summary>
''' Determines if the panel is automatically resized.
''' </summary>
Public Property AutosizeControl As Boolean
Get
Return _AutosizeControl
End Get
Set(value As Boolean)
_AutosizeControl = value
DoreAlign()
End Set
End Property
''' <summary>
''' Used to get or set the left part of the sentence
''' </summary>
Public Property LeftText As String
Get
Return lblLeft.Text
End Get
Set(value As String)
lblLeft.Text = value
DoreAlign()
End Set
End Property
''' <summary>
''' Used to set the right part of the sentence
''' </summary>
Public Property RightText As String
Get
Return lblRight.Text
End Get
Set(value As String)
lblRight.Text = value
DoreAlign()
End Set
End Property
''' <summary>
''' Used to get or set the user input
''' </summary>
Public Property BlankText As String
Get
Return tbBlank.Text
End Get
Set(value As String)
tbBlank.Text = value
RaiseEvent BlankTextChanged()
End Set
End Property
#Region "Constructor"
Public Sub New()
lblLeft = New Label With {.AutoSize = True, .Text = "Left Text"}
lblRight = New Label With {.AutoSize = True, .Text = "Right Text"}
tbBlank = New TextBox
Spacing = 3
TextboxWidth = 100
AutosizeControl = True
Me.Controls.Add(lblLeft)
Me.Controls.Add(lblRight)
Me.Controls.Add(tbBlank)
DoreAlign()
End Sub
#End Region
#Region "The method the realigns the controls"
Private Sub DoreAlign()
lblLeft.Left = 0
tbBlank.Left = lblLeft.Right + Spacing
tbBlank.Width = TextboxWidth
lblRight.Left = tbBlank.Right + Spacing
If AutosizeControl Then
Me.Width = lblRight.Right
Me.Height = tbBlank.Height
End If
lblRight.Top = CInt(Me.Height / 2 - lblRight.Height / 2)
lblLeft.Top = lblRight.Top
End Sub
#End Region
Private Sub thisTextChanged() Handles tbBlank.TextChanged
RaiseEvent BlankTextChanged()
End Sub
End Class