Skip to content

Instantly share code, notes, and snippets.

@shaxxx
shaxxx / taskFactory.vb
Created November 5, 2014 13:58
Creating Task from TaskFactory with continuation and function/action parameters
Dim t As Task(Of String) = Task.Factory.StartNew(Function()
Debug.Write("Sleeping for 5 seconds...")
Thread.Sleep(5000)
Debug.Write("Ready to talk!")
Return "Hello World!"
End Function).ContinueWith(Sub(x)
MessageBox.Show(x.Result)
End Sub)
@shaxxx
shaxxx / CustomComponentResourceManager.vb
Created October 23, 2014 10:17
Form of T with workaround for form resources
Public Class CustomComponentResourceManager
Inherits System.ComponentModel.ComponentResourceManager
Public Sub New(type As Type, resourceName As String)
MyBase.New(type)
Me.BaseNameField = resourceName
End Sub
End Class
@shaxxx
shaxxx / invokerequired_extension.vb
Created October 22, 2014 13:48
InvokeIfRequired extension method that checks if control needs invocation and runs action on the main thread
Imports System.Runtime.CompilerServices
Module ControlExtensions
''' <summary>
''' Checks if control needs invocation and runs action on the main thread
''' </summary>
''' <param name="control"></param>
''' <param name="action"></param>
<Extension()>
@shaxxx
shaxxx / gui_class_interfaces.vb
Created October 22, 2014 13:32
Class skeleton for object that supports IEditableObject, INotifyPropertyChanged, IValidatableObject and IDataErrorInfo interfaces with FluentValidation as validation library
Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.ComponentModel.DataAnnotations
Imports FluentValidation
Public Class MyObject
Implements IEditableObject
Implements INotifyPropertyChanged
@shaxxx
shaxxx / propertyinfo_linq_expression.vb
Created October 22, 2014 13:26
Return PropertyInfo object from Linq expressions
Imports System.Linq.Expressions
Imports System.Reflection
Public Module utils
''' <summary>
''' Returns property info from specified Linq Expression
''' </summary>
''' <param name="instance">instance of the object that has the propery</param>
@shaxxx
shaxxx / vb_round.vb
Created October 22, 2014 13:20
Vb.net TRUE round function that handles 0.5 values correctly
<System.Diagnostics.DebuggerStepThrough()> Public Function VbRound(ByVal Number As Double, ByVal DecimalPlaces As Integer) As Double
Dim varDec As Double, intX As Long, varX As Double, varDelta As Double
varDelta = 10 ^ (-DecimalPlaces)
varX = Number / varDelta
'intX = Int(varX)
intX = Math.Sign(varX) * Int(Math.Abs(varX))
varDec = CDec(varX) - intX
@shaxxx
shaxxx / sql_dates_format.vb
Created October 22, 2014 13:16
VB.net format function that returns DateTime values formated as MsSQL date/time strings
<System.Diagnostics.DebuggerStepThrough()> Public Function SqlDate(ByVal value As Date) As String
Return Format(value.Year, "0000") & "-" & Format(value.Month, "00") & "-" & Format(value.Day, "00")
End Function
<System.Diagnostics.DebuggerStepThrough()> Public Function SqlDateTime(ByVal value As DateTime) As String
Return Format(value.Year, "0000") & "-" & Format(value.Month, "00") & "-" & Format(value.Day, "00") & " " & Format(value.Hour, "00") & ":" & Format(value.Minute, "00") & ":" & Format(value.Second, "00") & "." & value.Millisecond.ToString
End Function
@shaxxx
shaxxx / Class1.vb
Created October 22, 2014 13:09
New VB.net class skeleton that supports IEditableObject with Memento class
Imports System.ComponentModel
Public Class Class1
Implements IEditableObject
#Region "IEditableObject"
Private _isEditing As Boolean
Private _isRestoring As Boolean
@shaxxx
shaxxx / xtragrid_checkbox_update.vb
Last active August 29, 2015 14:07
Update object value when grid checkbox is clicked immediately, without having to change focused row
1. Add repository item to column you want to update
2. Handle EditValueChanged
3. Call PostEditor() method on gridview
Private Sub repItem_EditValueChanged(sender As Object, e As EventArgs) Handles repItem.EditValueChanged
gv.PostEditor()
End Sub
@shaxxx
shaxxx / linq_list_group_sum_calculate_tax.vb
Created October 22, 2014 12:54
VB.net Linq query to calculate tax from list property on object
Dim query = From sr In ListProperty
Where sr.TaxRate.HasValue AndAlso sr.TaxRate > 0
Group sr By sr.TaxRate Into gr = Group
Select TaxRate, Amount = gr.Sum(Function(sr) (sr.Amount - sr.Discount) * (sr.TaxRate / 100))
Dim taxAmount = query.Sum(Function(x) x.Amount)
If isNothing(taxAmount) Then
Return 0
Else
Return Round(taxAmount, 2)