Solved

conver VB to C#

Posted on 2011-09-27
9
350 Views
Last Modified: 2013-12-17
can someone assist in converting this VB code to C#?
Imports System.Runtime.InteropServices
Imports System.Threading

Public Class Form1
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        TabBusting.BeginBust(3)
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        TabBusting.EndBust()
    End Sub
End Class

Public NotInheritable Class TabBusting

    Private Shared _shell As Object = Nothing
    Private Shared _windows As Object = Nothing
    Private Shared _timer As Threading.Timer = Nothing
    Private Shared _maxTabs As UInteger = 0

    Public Shared Sub BeginBust(ByVal maxTabs As UInteger)
        _shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"), True)
        _maxTabs = maxTabs
        ' Setup tab busting every second.
        _timer = New Threading.Timer(AddressOf BustWindows, Nothing, New TimeSpan(0, 0, 0), New TimeSpan(0, 0, 1))
    End Sub

    Public Shared Sub EndBust()
        _timer.Dispose()
        _timer = Nothing
        Marshal.FinalReleaseComObject(_shell)
        _shell = Nothing
    End Sub
   
    Private Shared Sub BustWindows(ByVal state As Object)
         
        Dim windowList As New List(Of Object)
        Dim i As Integer

        ' Populate window collection
        _windows = _shell.Windows

        ' Filters ShellView windows
        For Each obj As Object In _windows
            If obj.Document.GetType.Name.Contains("HTMLDocument") Then
                windowList.Add(obj)
            End If
        Next
         
        If windowList.Count >= _maxTabs Then
            For i = 0 To windowList.Count - 1
                If i >= _maxTabs Then
                    windowList(i).Quit() ' bust tabs above maximum allowed.
                End If
            Next i
        End If

        windowList.Clear()
        windowList = Nothing
        _windows = Nothing

    End Sub
End Class

Open in new window

0
Comment
Question by:OrenRozen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 36714723

refer
http://www.developerfusion.com/tools/convert/vb-to-csharp/ 
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Threading;

public class Form1
{
	private void Form1_Load(System.Object sender, System.EventArgs e)
	{
		TabBusting.BeginBust(3);
	}

	private void Form1_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e)
	{
		TabBusting.EndBust();
	}
	public Form1()
	{
		FormClosing += Form1_FormClosing;
		Load += Form1_Load;
	}
}

public sealed class TabBusting
{

	private static object _shell = null;
	private static object _windows = null;
	private static System.Threading.Timer _timer = null;

	private static uint _maxTabs = 0;
	public static void BeginBust(uint maxTabs)
	{
		_shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"), true);
		_maxTabs = maxTabs;
		// Setup tab busting every second.
		_timer = new System.Threading.Timer(BustWindows, null, new TimeSpan(0, 0, 0), new TimeSpan(0, 0, 1));
	}

	public static void EndBust()
	{
		_timer.Dispose();
		_timer = null;
		Marshal.FinalReleaseComObject(_shell);
		_shell = null;
	}

	private static void BustWindows(object state)
	{

		List<object> windowList = new List<object>();
		int i = 0;

		// Populate window collection
		_windows = _shell.Windows;

		// Filters ShellView windows
		foreach (object obj in _windows) {
			if (obj.Document.GetType.Name.Contains("HTMLDocument")) {
				windowList.Add(obj);
			}
		}

		if (windowList.Count >= _maxTabs) {
			for (i = 0; i <= windowList.Count - 1; i++) {
				if (i >= _maxTabs) {
					windowList[i].Quit();
					// bust tabs above maximum allowed.
				}
			}
		}

		windowList.Clear();
		windowList = null;
		_windows = null;

	}
}

Open in new window

0
 
LVL 17

Expert Comment

by:sonawanekiran
ID: 36714734
Telerik Converter is one of the best
http://converter.telerik.com/
0
 
LVL 3

Author Comment

by:OrenRozen
ID: 36714758
@pratima_mcs
line  59 return an error:
'object' does not contain a definition for 'Windows' and no extension method 'Windows' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

line 63 return an error:
'object' does not contain a definition for 'Document' and no extension method 'Document' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

line 71 return an error:
'object' does not contain a definition for 'Quit' and no extension method 'Quit' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

@sonawanekiran
Same errors with Telerix

Is there a missing invoke? Running this code as VB is working perfectly.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36715282
Please try the language translator: http://www.carlosag.net/tools/codetranslator/
using System.Runtime.InteropServices;
using System.Threading;

public class Form1 {
    
    private void Form1_Load(object sender, System.EventArgs e) {
        TabBusting.BeginBust(3);
    }
    
    private void Form1_FormClosing(object sender, System.Windows.Forms.FormClosingEventArgs e) {
        TabBusting.EndBust();
    }
}
public sealed class TabBusting {
    
    private static object _shell = null;
    
    private static object _windows = null;
    
    private static Threading.Timer _timer = null;
    
    private static System.UInt32 _maxTabs = 0;
    
    public static void BeginBust(System.UInt32 maxTabs) {
        _shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"), true);
        _maxTabs = maxTabs;
        //  Setup tab busting every second.
        _timer = new Threading.Timer(new System.EventHandler(this.BustWindows), null, new TimeSpan(0, 0, 0), new TimeSpan(0, 0, 1));
    }
    
    public static void EndBust() {
        _timer.Dispose();
        _timer = null;
        Marshal.FinalReleaseComObject(_shell);
        _shell = null;
    }
    
    private static void BustWindows(object state) {
        List windowList = new List(Of, Object);
        int i;
        //  Populate window collection
        _windows = _shell.Windows;
        //  Filters ShellView windows
        foreach (object obj in _windows) {
            if (obj.Document.GetType.Name.Contains("HTMLDocument")) {
                windowList.Add(obj);
            }
        }
        if ((windowList.Count >= _maxTabs)) {
            for (i = 0; (i 
                        <= (windowList.Count - 1)); i++) {
                if ((i >= _maxTabs)) {
                    windowList[i].Quit();
                    //  bust tabs above maximum allowed.
                }
            }
        }
        windowList.Clear();
        windowList = null;
        _windows = null;
    }
}

Open in new window

0
 
LVL 16

Expert Comment

by:Easwaran Paramasivam
ID: 36715321
Please do ensure that you have added all required DLLs in the reference section of the project.
0
 
LVL 3

Author Comment

by:OrenRozen
ID: 36715808
@EaswaranP:
Same errors using this translator

@EaswaranP:
there are no specific dll's. IUf you'll take the VB code and paste it to a new VB solution it will work.
But the converted code is missing somthing. My guess it has something to do with this line:
_shell = Activator.CreateInstance(Type.GetTypeFromProgID("Shell.Application"), True)
but I don't know how to make it work with C#. This is why I think there is some invoke missing.
0
 
LVL 28

Expert Comment

by:strickdd
ID: 36716839
I don't know if this will work, but the concept is there. You need to TypeCast the _shell variable as the Shell.Application type that is actually created. You may need to use some more reflection to do this if the code below doesn't work.

_windows = ((Type.GetTypeFromProgID("Shell.Application"))_shell).Windows;


0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 36716890
The VB code "works" because the default for "Option Strict" is Off:
http://msdn.microsoft.com/en-us/library/zcd4xwzs(VS.80).aspx

    "In addition to disallowing implicit narrowing conversions, Option Strict generates an error for late binding. An object is late bound when it is assigned to a variable that is declared to be of type Object."

The variable "_shell" has been declare as Object and is using late binding.

To make it work in C# you'd also have to turn Option Strict Off or use early binding.
0
 
LVL 29

Accepted Solution

by:
nffvrxqgrcfqvvc earned 500 total points
ID: 36717362
Here is example using COM references. In the Add Reference choose the COM Tab and add the references next to the using statements in the code section.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using Shell32; // Microsoft Shell Controls And Automation
using SHDocVw;// Microsoft Internet Controls


namespace TabBuster
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            TabBusting.BeginBust(3);

        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            TabBusting.EndBust();
        }
    }
}

public sealed class TabBusting
{

    private static Shell32.Shell _shell = null;
    private static IShellWindows _windows = null;
    private static System.Threading.Timer _timer = null;

    private static uint _maxTabs = 0;
    public static void BeginBust(uint maxTabs)
    {
        _shell = new Shell32.Shell();
        _maxTabs = maxTabs;
        _timer = new System.Threading.Timer(BustWindows, null, new TimeSpan(0, 0, 0), new TimeSpan(0, 0, 1));
    }

    public static void EndBust()
    {
        _timer.Dispose();
        _timer = null;
        _shell = null;
    }


    private static void BustWindows(object state)
    {
        List<SHDocVw.InternetExplorer> windowList = new List<SHDocVw.InternetExplorer>();
        int i = 0;

        // Populate window collection
        _windows = (IShellWindows)_shell.Windows();

        // Filters ShellView windows
        foreach (SHDocVw.InternetExplorer obj in _windows)
        {
            //Console.WriteLine(obj.GetType().FullName.ToString());
            
            if (obj.Document !=  null)
            {
                if (obj.Document.GetType().Name.Contains("HTMLDocument"))
                {
                    windowList.Add(obj);
                }
            }
        }

        if (windowList.Count >= _maxTabs)
        {
            for (i = 0; i <= windowList.Count - 1; i++)
            {
                if (i >= _maxTabs)
                {
                    ((SHDocVw.InternetExplorer)windowList[i]).Quit();
                    // Bust tabs above maximum allowed.
                }
            }
        }

        windowList.Clear();
        windowList = null;
        _windows = null;

    }

}

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question