prevent opening popup in internet explorer

A common problem while using webBrowser control is open pop up windows in instance of Internet Explorer and not in your application.
There’s a simple way to prevent this.

Logic is to capture the event when New Window is opened | Grab Link | Open it programatically in our application | cancel opening IE.

and code would be :

Continue reading

Add BindingNavigator for DataGridView

To add a BindingNavigator for a DataGridView, even when there’s no DataSource like DataSet or DataTable in use, you can have DataGridView’s rows collection as DataSource for BindingSource.
And set that BindingSource to BindingSource property of BindingNavigator.

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles MyBase.Load

        BindingSource2.DataSource = DataGridView1.Rows
        BindingNavigator1.BindingSource = BindingSource2

        'instead of adding event handler for MoveFirst,MoveLast,MoveNext,MovePrevious
        'chose this one. it will fire anyway
        AddHandler BindingNavigatorPositionItem.TextChanged, AddressOf bindingnavigator_PostionChanged

    End Sub

Continue reading

Convert image to string

Convert image to base64 encoded string and vice versa. Like Resource.resx file does. Can be used with settings file.

    'usage
    Private Sub saveImageToSettings()
        My.Settings.imageSettingAsString = getBase64String(PictureBox1.Image)
        My.Settings.Save()
    End Sub

    Private Sub getImageFromString()
        PictureBox1.Image = getImageFromBase64String(My.Settings.imageSettingAsString)
    End Sub

Continue reading

Using Explorer Form in vb.net

Adding directory structure to explorer form to use it like Windows Explorer:

Right click your project in Solution Explorer and click Add > Add New Item | Select Explorer Form from the list and Click ok, [keep default name if you dont know changing it in further code]

When new Form is added to your project, double click it and replace whole code with the following. Result of which will be like this-

Continue reading

Restore minimized window from vb.net

    Private Declare Function ShowWindow Lib "user32.dll" (ByVal hwnd As IntPtr, ByVal nCmdShow As Int32) As Boolean

    Enum SW As Int32
        Hide = 0
        Normal = 1
        ShowMinimized = 2
        ShowMaximized = 3
        ShowNoActivate = 4
        Show = 5
        Minimize = 6
        ShowMinNoActive = 7
        ShowNA = 8
        Restore = 9
        ShowDefault = 10
        ForceMinimize = 11
        Max = 11
    End Enum

    'Usage
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
    Handles Button1.Click

        Dim hWnd As IntPtr = Process.GetProcessById(12345).MainWindowHandle
        ShowWindow(hWnd, SW.Restore)

    End Sub

Flash Taskbar button

Flash Taskbar button when window is inactive or at background. Click button and minimize window to test this sample.

Public Class Form1
    Public Structure FLASHWINFO
        Public cbSize As Int32
        Public hwnd As IntPtr
        Public dwFlags As Int32
        Public uCount As Int32
        Public dwTimeout As Int32
    End Structure

    Private Declare Function FlashWindowEx Lib "user32.dll" (ByRef pfwi As FLASHWINFO) As Int32

    Public Const FLASHW_STOP = 0        ' Stop flashing. The system restores the window to its original state.
    Public Const FLASHW_CAPTION = &H1   ' Flash the window caption.
    Public Const FLASHW_TRAY = &H2      ' Flash the taskbar button.
    Public Const FLASHW_ALL = &H3       ' Flash both the window caption and taskbar button.
    Public Const FLASHW_TIMER = &H4     ' Flash continuously, until the FLASHW_STOP flag is set.
    Public Const FLASHW_TIMERNOFG = &HC ' Flash continuously until the window comes to the foreground.

    Public Sub FlashIcon(ByVal Handle%, ByVal Flags%)
        Dim flash As New FLASHWINFO
        flash.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(flash) '/// size of structure in bytes
        flash.hwnd = Handle '/// Handle to the window to be flashed
        flash.dwFlags = Flags
        flash.dwTimeout = 500 '/// speed of flashes in MilliSeconds ( can be left out )
        FlashWindowEx(flash) '/// flash the window
    End Sub

    Dim WithEvents tmr As New Timer
    Dim count As Integer = 0
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        tmr.Interval = 1000
        tmr.Start()
    End Sub
    Private Sub test(ByVal sender As Object, ByVal e As EventArgs) Handles tmr.Tick
        If count >= 5 Then
            tmr.Stop()
            FlashIcon(MyBase.Handle, FLASHW_TRAY + FLASHW_TIMERNOFG)
        End If
        count += 1
    End Sub
End Class