Click here to Skip to main content
15,917,320 members
Articles / Programming Languages / Visual Basic

Exposing VB InputBox Dialog to C# code

Rate me:
Please Sign up or sign in to vote.
1.14/5 (17 votes)
15 Oct 2010CPOL 71.9K   378   9   31
This is basically a VB InputBox Dialog wrapper dll for C#.

Introduction - VS2005

This solves the issue with trying to access the Microsoft.VisualBasic.Interaction.InputBox(); code. C# is not able to use the optional variables that VB uses so we will have to expose them as overloads. This code fully exposes the VB InputBox code.

Background

C# does not contain an InputBox. Maybe Microsoft will in the 2010 code (They did add support in VS2010). Or better yet, maybe Microsoft will make it to where someone can add a .vb file to a C# project and allow it to compile.

Using the code

Add InputBoxClassLibrary.dll to your C# References.
Add Using InputBoxClassLibrary; to the C# code.

VB Code to Expose Microsoft.VisualBasic.Interaction.InputBox():

VB.NET
Public Class InputBox

	Public Overloads Shared Function Show(ByVal message As String) As String
		Return Microsoft.VisualBasic.Interaction.InputBox(message)

	End Function

	Public Overloads Shared Function Show(ByVal message As String, _
			ByVal title As String) As String

		Return Microsoft.VisualBasic.Interaction.InputBox(message, title)
	End Function

	Public Overloads Shared Function Show(ByVal message As String, _
			ByVal title As String, _
			ByVal defaultValue As String) As String

		Return Microsoft.VisualBasic.Interaction.InputBox(message, title, defaultValue)
	End Function

	Public Overloads Shared Function Show(ByVal message As String, _
			ByVal title As String, _
			ByVal defaultValue As String, _
			ByVal xPos As Integer) As String

		Return Microsoft.VisualBasic.Interaction.InputBox(message, title, defaultValue, xPos)
	End Function

	Public Overloads Shared Function Show(ByVal message As String, _
			ByVal title As String, _
			ByVal defaultValue As String, _
			ByVal Null As Nullable, _
			ByVal yPos As Integer) As String

		Return Microsoft.VisualBasic.Interaction.InputBox(message, title, defaultValue, , yPos)
	End Function

	Public Overloads Shared Function Show(ByVal message As String, _
			ByVal title As String, _
			ByVal defaultValue As String, _
			ByVal xPos As Integer, _
			ByVal yPos As Integer) As String

		Return Microsoft.VisualBasic.Interaction.InputBox(message, title, defaultValue, xPos, yPos)
	End Function
End Class

C# Code used to expose the InputBox Class code:

C#
string Returned;

//This will show the InputBox center screen with a default title and no default val
Returned = InputBox.Show("Message1");
MessageBox.Show(Returned);

//This will show the InputBox center screen with a message and title and no default val
Returned = InputBox.Show("Message2", "title - no default val");
MessageBox.Show(Returned);

//This will show the InputBox center screen with a message, title, and default val
Returned = InputBox.Show("Message3", "title - no x and y", "defaultValue");
MessageBox.Show(Returned);

//This will show the InputBox 50 pix away from the side of the screen.
Returned = InputBox.Show("Message4", "title - x50", "defaultValue2", 50);
MessageBox.Show(Returned);

//This will show the InputBox 50 pix away from the top side of the screen.
Returned = InputBox.Show("Message5", "title - y50", "defaultValue2", null, 50);
MessageBox.Show(Returned);

//this will show the InputBox 50 pix away from the top and side of the screen.
Returned = InputBox.Show("Message6", "title - x50 and y50", "defaultValue2", 50, 50);
MessageBox.Show(Returned);

Points of Interest

Make sure that "Overloads Shared" is used in the VB code when exposing Optional variables in VB code.

History

01/15/09 - Document Created.
01/16/09 - Document Updated.
08/12/10 - No longer needed in VS2010.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 3 Pin
Sivaraman Dhamodharan31-Oct-10 19:49
Sivaraman Dhamodharan31-Oct-10 19:49 
Rant[My vote of 1] Nearly fell out of my chair! Pin
DavePaterson11-Sep-10 10:11
DavePaterson11-Sep-10 10:11 
GeneralMy vote of 1 Pin
johannesnestler13-Aug-10 6:55
johannesnestler13-Aug-10 6:55 
GeneralMy vote of 1 Pin
AxelM12-Aug-10 21:30
AxelM12-Aug-10 21:30 
GeneralMy vote of 1 Pin
karabax12-Aug-10 8:56
karabax12-Aug-10 8:56 
GeneralThis code is no longer needed in VS2010. Pin
omzig12-Aug-10 5:24
omzig12-Aug-10 5:24 
Generalsimple <-> complex Pin
Froghut23-Jun-09 23:32
Froghut23-Jun-09 23:32 
GeneralMy vote of 1 Pin
Alex Hazanov10-Mar-09 8:47
Alex Hazanov10-Mar-09 8:47 
GeneralSimplier way Pin
Alex Hazanov10-Mar-09 8:47
Alex Hazanov10-Mar-09 8:47 
GeneralRe: Simplier way - now simpler Pin
johannesnestler13-Aug-10 6:53
johannesnestler13-Aug-10 6:53 
GeneralMy Vote of 1 Pin
robertw01916-Jan-09 9:08
robertw01916-Jan-09 9:08 
GeneralRe: My Vote of 1 [modified] Pin
omzig16-Jan-09 9:55
omzig16-Jan-09 9:55 
GeneralRe: My Vote of 1 Pin
robertw01916-Jan-09 9:58
robertw01916-Jan-09 9:58 
GeneralRe: My Vote of 1 Pin
omzig16-Jan-09 10:03
omzig16-Jan-09 10:03 
GeneralMy vote of 1 Pin
robertw01916-Jan-09 9:06
robertw01916-Jan-09 9:06 
GeneralRe: My vote of 1 Pin
omzig16-Jan-09 9:55
omzig16-Jan-09 9:55 
GeneralMy vote of 1 PinPopular
Dave Kreskowiak16-Jan-09 7:15
mveDave Kreskowiak16-Jan-09 7:15 
GeneralRe: My vote of 1 Pin
omzig16-Jan-09 9:57
omzig16-Jan-09 9:57 
GeneralMy vote of 1 Pin
Rob Smiley16-Jan-09 2:43
Rob Smiley16-Jan-09 2:43 
QuestionWhat is the advantage of using VB code? Pin
Member 468065216-Jan-09 2:09
Member 468065216-Jan-09 2:09 
AnswerRe: What is the advantage of using VB code? [modified] Pin
omzig16-Jan-09 3:35
omzig16-Jan-09 3:35 
GeneralMy vote of 1 Pin
NormDroid15-Jan-09 21:09
professionalNormDroid15-Jan-09 21:09 
GeneralMy vote of 1 Pin
fiend15-Jan-09 19:33
fiend15-Jan-09 19:33 
GeneralMy vote of 1 Pin
teleprobst-ober15-Jan-09 11:56
teleprobst-ober15-Jan-09 11:56 
crap
GeneralMy vote of 1 Pin
Clutchplate15-Jan-09 11:47
Clutchplate15-Jan-09 11:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.