Link to home
Start Free TrialLog in
Avatar of Johny Bravo
Johny Bravo

asked on

Help in conerting VB code to C#.net

Hi Experts,
I am developing a windows application.After login,in mdi form i have two different tabs namely,'Switch' n 'Multisession'.
When the user click 'multisession' and then click 'Switch',he should be able to view a new instance of the form.It means he will be able to see two instaces of the application.
Maximum 2 instances can be run at a time.

I have the VB code to do this.Can you pls tell me how can I implement it in C#.
Should I need to change anything in this?
The below code is for both 'Switch' n 'Multisession'
Option Explicit
 
'
 
' Required Win32 API Declarations
 
'
 
' The AttachThreadInput function attaches the input processing
 
' mechanism of one thread to that of another thread.
 
' Windows created in different threads typically process input
 
' independently of each other - they have their own input states
 
' (focus, active, capture windows, key state, queue status, ...),
 
' and they are not synchronized with the input processing of other
 
' threads. By using the AttachThreadInput function, a thread can
 
' attach its input processing to another thread. This also allows
 
' threads to share their input states, so they can call the
 
' SetFocus function to set the keyboard focus to a window of a
 
' different thread. This also allows threads to get key-state
 
' information. These capabilities are not generally possible.
 
'
 
Declare Function AttachThreadInput Lib "user32" (ByVal idAttach As Long, ByVal idAttachTo As Long, ByVal fAttach As Long) As Long
 
Declare Function EnumWindows Lib "user32" (ByVal lpEnumFunc As Long, ByVal lParam As Long) As Long
 
Declare Function GetForegroundWindow Lib "user32" () As Long
 
Declare Function GetParent Lib "user32" (ByVal hWnd As Long) As Long
 
Declare Function GetWindow Lib "user32" (ByVal hWnd As Long, ByVal wCmd As Long) As Long
 
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
 
Declare Function GetWindowText Lib "user32" Alias "GetWindowTextA" (ByVal hWnd As Long, ByVal lpString As String, ByVal cch As Long) As Long
 
Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long, lpdwProcessId As Long) As Long
 
Declare Function IsIconic Lib "user32" (ByVal hWnd As Long) As Long
 
Declare Function IsWindowVisible Lib "user32" (ByVal hWnd As Long) As Long
 
Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
 
Declare Function SetForegroundWindow Lib "user32" (ByVal hWnd As Long) As Long
 
Declare Function ShowWindow Lib "user32" (ByVal hWnd As Long, ByVal nCmdShow As Long) As Long
 
'
 
' Constants used with APIs
 
'
 
Public Const SW_SHOW = 5
 
Public Const SW_RESTORE = 9
 
Public Const GW_OWNER = 4
 
Public Const GWL_HWNDPARENT = (-8)
 
Public Const GWL_EXSTYLE = (-20)
 
Public Const WS_EX_TOOLWINDOW = &H80
 
Public Const WS_EX_APPWINDOW = &H40000
 
Public i As Integer
 
'
 
' Listbox messages
 
'
 
Public Const LB_ADDSTRING = &H180
 
Public Const LB_SETITEMDATA = &H19A
 
Enum MENUINFO_STYLES
 
    MNS_NOCHECK = &H80000000
 
    MNS_MODELESS = &H40000000
 
    MNS_DRAGDROP = &H20000000
 
    MNS_AUTODISMISS = &H10000000
 
    MNS_NOTIFYBYPOS = &H8000000
 
    MNS_CHECKORBMP = &H4000000
 
End Enum
 
 
 
 
 
Enum MENUINFO_MASKS
 
    MIM_MAXHEIGHT = &H1
 
    MIM_BACKGROUND = &H2
 
    MIM_HELPID = &H4
 
    MIM_MENUDATA = &H8
 
    MIM_STYLE = &H10
 
    MIM_APPLYTOSUBMENUS = &H80000000
 
End Enum
 
 
 
 
 
Public Type MENUINFO
 
    cbSize As Long
 
    fMask As MENUINFO_MASKS
 
    dwStyle As MENUINFO_STYLES
 
    cyMax As Long
 
    hbrBack As Long
 
    dwContextHelpID As Long
 
    dwMenuData As Long
 
End Type
 
 
 
 
 
Public Declare Function GetMenuInfo Lib "user32" ( _
 
    ByVal hMenu As Long, _
 
    mi As MENUINFO) As Long
 
 
 
 
 
Public Declare Function SetMenuInfo Lib "user32" ( _
 
    ByVal hMenu As Long, _
 
    mi As MENUINFO) As Long
 
 
 
 
 
Public Declare Function GetMenu Lib "user32" (ByVal hWnd As Long) As Long
 
 
 
 
 
Public Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
 
Public Declare Function StretchBlt Lib "gdi32" (ByVal hDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
 
Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, ByVal X As Long, ByVal Y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
 
Public Declare Function GetDesktopWindow Lib "user32" () As Long
 
Public Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) As Long
 
Public Declare Function GetDeviceCaps Lib "gdi32" (ByVal hDC As Long, ByVal nIndex As Long) As Long
 
Dim frmH As Long
 
Dim magnify As Integer
 
Public Const HORZRES = 8
 
Public Const VERTRES = 10
 
Public Const RDW_ERASE = &H4
 
Public Const RDW_INVALIDATE = &H1
 
Public Const SRCCOPY = &HCC0020
 
Public Const WM_PAINT = &HF
 
Public Type POINTAPI
 
X As Long
 
Y As Long
 
End Type
 
 
 
Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" ( _
 
    ByVal hWnd1 As Long, ByVal hWnd2 As Long, _
 
    ByVal lpsz1 As String, ByVal lpsz2 As String) As Long
 
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
 
Private Declare Function SetClassLong Lib "user32" Alias "SetClassLongA" ( _
 
                ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwnewlong As Long) As Long
 
Public Declare Function InvalidateRect Lib "user32" (ByVal hWnd As Long, lpRect As Long, ByVal bErase As Long) As Long
 
 
 
Public Declare Function CreatePatternBrush Lib "gdi32" (ByVal hBitmap As Long) As Long
 
 
 
Public Enum enuTBType
 
    enuTB_FLAT = 1
 
    enuTB_STANDARD = 2
 
End Enum
 
 
 
Private Const GCL_HBRBACKGROUND = (-10)
 
Public ArryListMuliSession() As Long
 
Public MultiSessionCount As Integer
 
Public ArrMultiSessionCount As Integer
 
 
 
Public Sub pSetForegroundWindow(ByVal hWnd As Long)
 
Dim lForeThreadID As Long
 
Dim lThisThreadID As Long
 
Dim lReturn       As Long
 
'
 
' Make a window, specified by its handle (hwnd)
 
' the foreground window.
 
'
 
' If it is already the foreground window, exit.
 
'
 
If hWnd <> GetForegroundWindow() Then
 
    '
 
    ' Get the threads for this window and the foreground window.
 
    '
 
    lForeThreadID = GetWindowThreadProcessId(GetForegroundWindow, ByVal 0&)
 
    lThisThreadID = GetWindowThreadProcessId(hWnd, ByVal 0&)
 
    '
 
    ' By sharing input state, threads share their concept of
 
    ' the active window.
 
    '
 
    If lForeThreadID <> lThisThreadID Then
 
        ' Attach the foreground thread to this window.
 
        Call AttachThreadInput(lForeThreadID, lThisThreadID, True)
 
        ' Make this window the foreground window.
 
        lReturn = SetForegroundWindow(hWnd)
 
        ' Detach the foreground window's thread from this window.
 
        Call AttachThreadInput(lForeThreadID, lThisThreadID, False)
 
    Else
 
       lReturn = SetForegroundWindow(hWnd)
 
    End If
 
    '
 
    ' Restore this window to its normal size.
 
    '
 
    If IsIconic(hWnd) Then
 
       Call ShowWindow(hWnd, SW_RESTORE)
 
    Else
 
       Call ShowWindow(hWnd, SW_SHOW)
 
    End If
 
End If
 
End Sub
 
 
 
Public Function fEnumWindows(lst As Object) As Long
 
'
 
' Clear list, then fill it with the running
 
' tasks. Return the number of tasks.
 
'
 
' The EnumWindows function enumerates all top-level windows
 
' on the screen by passing the handle of each window, in turn,
 
' to an application-defined callback function. EnumWindows
 
' continues until the last top-level window is enumerated or
 
' the callback function returns FALSE.
 
'
 
With lst
 
    .Clear
 
    Call EnumWindows(AddressOf fEnumWindowsCallBack, .hWnd)
 
    fEnumWindows = .ListCount
 
End With
 
End Function
 
Public Function fEnumWindowsa(lst As Object) As Long
 
 
 
'
 
' Clear list, then fill it with the running
 
' tasks. Return the number of tasks.
 
'
 
' The EnumWindows function enumerates all top-level windows
 
' on the screen by passing the handle of each window, in turn,
 
' to an application-defined callback function. EnumWindows
 
' continues until the last top-level window is enumerated or
 
' the callback function returns FALSE.
 
 
 
'
 
Dim hWnd As Long
 
'With lst
 
   
 
    Call EnumWindows(AddressOf fEnumWindowsCallBack, hWnd)
 
 '   fEnumWindowsa = i
 
'End With
 
End Function
 
 
 
 
 
Private Function fEnumWindowsCallBack(ByVal hWnd As Long, ByVal lParam As Long) As Long
 
Dim lReturn     As Long
 
Dim lExStyle    As Long
 
Dim bNoOwner    As Boolean
 
Dim sWindowText As String
 
'
 
' This callback function is called by Windows (from
 
' the EnumWindows API call) for EVERY window that exists.
 
' It populates the listbox with a list of windows that we
 
' are interested in.
 
'
 
' Windows to display are those that:
 
'   -   are not this app's
 
'   -   are visible
 
'   -   do not have a parent
 
'   -   have no owner and are not Tool windows OR
 
'       have an owner and are App windows
 
'
 
If hWnd <> MDI_ComStock.hWnd Then
 
    If IsWindowVisible(hWnd) Then
 
        If GetParent(hWnd) = 0 Then
 
            bNoOwner = (GetWindow(hWnd, GW_OWNER) = 0)
 
            lExStyle = GetWindowLong(hWnd, GWL_EXSTYLE)
 
            
 
            If (((lExStyle And WS_EX_TOOLWINDOW) = 0) And bNoOwner) Or _
 
                ((lExStyle And WS_EX_APPWINDOW) And Not bNoOwner) Then
 
                '
 
                ' Get the window's caption.
 
                '
 
                sWindowText = Space$(256)
 
                lReturn = GetWindowText(hWnd, sWindowText, Len(sWindowText))
 
                If lReturn Then
 
                   '
 
                   ' Add it to our list.
 
                   '
 
                   sWindowText = left$(sWindowText, lReturn)
 
                   lReturn = SendMessage(lParam, LB_ADDSTRING, 0, ByVal sWindowText)
 
                   If left(sWindowText, 5) = "Build" And ArrMultiSessionCount < 2 Then
 
                 '  ArryListMuliSession(0) = SendMessage(lParam, LB_SETITEMDATA, lReturn, ByVal hwnd)
 
                   ArryListMuliSession(ArrMultiSessionCount) = hWnd
 
                   ArrMultiSessionCount = ArrMultiSessionCount + 1
 
                   
 
                   End If
 
                   
 
                   'Call SendMessage(lParam, LB_SETITEMDATA, lReturn, ByVal hwnd)
 
                End If
 
            End If
 
        End If
 
    End If
 
End If
 
fEnumWindowsCallBack = True
 
End Function
 
Public Sub ChangeTBBack(TB As Object, PNewBack As Long, pType As enuTBType)
 
Dim lTBWnd      As Long
 
    Select Case pType
 
        Case enuTB_FLAT     'FLAT Button Style Toolbar
 
            'Apply directly to TB Hwnd
 
            DeleteObject SetClassLong(TB.hWnd, GCL_HBRBACKGROUND, PNewBack)
 
        Case enuTB_STANDARD 'STANDARD Button Style Toolbar
 
            lTBWnd = FindWindowEx(TB.hWnd, 0, "msvb_lib_toolbar", vbNullString) 'Find Hwnd first
 
            DeleteObject SetClassLong(lTBWnd, GCL_HBRBACKGROUND, PNewBack)      'Set new Back
 
    End Select
 
End Sub
 
Public Sub pGetTasks()
 
Dim Arr As New Collection
 
Dim Arr1() As String
 
'
 
'Call fEnumWindows(lstTasks)
 
ReDim ArryListMuliSession(1)
 
Call fEnumWindowsa(Arr)
 
On Error Resume Next
 
End Sub

Open in new window

Avatar of Pratima
Pratima
Flag of India image

Avatar of Johny Bravo
Johny Bravo

ASKER

Not able to convert:

Conversion results
-- line 35 col 1: EOF expected
If there is any other way to achieve the same that will also do.
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I dnt know VB but I tried my best to convert it to VB.net without success.
Is there any way running two MDIForms at a time.

What I am doing for trial is,

 private void multiSessionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            inst = 1;
           
        }

        private void switchToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (inst == 1)
            {
                MDIForm f1 = new MDIForm();
                f1.Show();

                inst = 0;
            }
        }

this works for this form well.But if the user user click Multisession on another MDIForm  and click Switch then it opens a new MDIForm.

Any idea to solve this?
How can I know that how many MDIForms are runninng?
Please help!!
Thanks