Movatterモバイル変換


[0]ホーム

URL:


Wayback Machine
22 captures
18 Dec 2007 - 11 Nov 2024
NovDECAug
Previous capture29Next capture
201020112024
success
fail
COLLECTED BY
Organization:Alexa Crawls
Starting in 1996,Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to theWayback Machine after an embargo period.
Collection:Alexa Crawls
Starting in 1996,Alexa Internet has been donating their crawl data to the Internet Archive. Flowing in every day, these data are added to theWayback Machine after an embargo period.
TIMESTAMPS
loading
The Wayback Machine - https://web.archive.org/web/20111229193719/http://www.codeproject.com:80/KB/edit/InputBox.aspx
Click here to Skip to main content
8,371,751 members and growing!
EmailPassword Lost password?
Home
Search within:




Licence 
First Posted 21 Apr 2005
Views 200,458
Downloads 1,534
Bookmarked 35 times

InputBox in C#

Byhestol | 21 Apr 2005
This is owner's own InputBox in C#, with only one function. So this InputBox do not inherit from a WinForm.
 
See Also
Print Article
add
Add to your CodeProject bookmarks
Discuss
Discuss this article
26
  3.00 (33 votes)
8 votes, 24.2%
1
3 votes, 9.1%
2
1 vote, 3.0%
3
4 votes, 12.1%
4
17 votes, 51.5%
5
3.00/5 - 33 votes
μ 3.04, σa 3.05 [?]
Sponsored Links

Sample Image - InputBox.gif

Introduction

Visual Basic 6.0 has anInputBox() function, Visual Basic .NET has one but in C# you don't. You can easily solve this by adding a reference to 'Microsoft.VisualBasic.dll' and using the static method 'Microsoft.VisualBasic.Interaction.InputBox()'.

See reference to MSDN 2001 in VB help.

InputBox Function

Displays a prompt in a dialog box, waits for the user to input text or click a button, and returns a string containing the contents of the text box.

Syntax
InputBox(prompt[,title] [,default] [,xpos] [,ypos] [,helpfile,context])

TheInputBox function syntax has these named arguments: see the help fileMSDN\2001OCT\1033.

In the VB 6.0, there were the title, default, xpos and ypos optional values to the call ofInputBox function. It is the same thing I have made.

But in this example we make ours own.

InputBox Class

TheInputBox class is a public class there arenot inherited fromSystem.Windows.Forms.Formclass. You use it by calling the static function name 'Show' like we also do inMessageBox. This method is returning a publicInputBoxResult. This object has two public properties one calledText with a string and aReturnCode.

There is anenum. It the same as theMessageBox returns, but theInputBox only returns these two parameters,DialogResult.OK orDialogResult.Cancel.

See the class header:

InputBox Class Image

Usage

You activate theInputBox by calling the staticShow() method. It has one required and four optional arguments (using overloading).

Why there are so many new lines in the prompt argument is that, the old VB 6.0 made theInputBox form bigger after the size of the prompt argument.

privatevoid button1_Click(object sender, System.EventArgs e){// This test that the InputBox can handle more newline than one.    InputBoxResult test = InputBox.Show("Prompt" +"\n" +"DDDD" +"Prompt" +"\n" +"DDDD" +"Prompt" +"\n" +"DDDD" +"Prompt" +"\n" +"DDDD" +"Prompt" +"\n" +"DDDD" +"Prompt" +"\n" +"DDDD"                  ,"Title","Default",100,0);if( test.ReturnCode == DialogResult.OK )        MessageBox.Show(test.Text);}

The best of all is that the drop down list there comes from theInputBox is very small. If we have inherited fromWindows.Form then it will have been bigger.

InputBox drop down list Image

This is one of the methods in theInputBox class where we assign values to all the properties in the control. It regulates theInputBox size and the prompt textbox size based on the size of the prompt input string.

staticprivatevoid LoadForm(){    OutputResponse.ReturnCode = DialogResult.Ignore;    OutputResponse.Text =string.Empty;    txtInput.Text = _defaultValue;    lblPrompt.Text = _formPrompt;    frmInputDialog.Text = _formCaption;// Retrieve the working rectangle from the Screen class// using the PrimaryScreen and the WorkingArea properties.    System.Drawing.Rectangle workingRectangle =          Screen.PrimaryScreen.WorkingArea;if((_xPos >=0 && _xPos < workingRectangle.Width) &&          (_yPos >=0 && _yPos < workingRectangle.Height))    {        frmInputDialog.StartPosition = FormStartPosition.Manual;        frmInputDialog.Location =new System.Drawing.Point(_xPos, _yPos);    }else     {// InputBox in the center if not specifier or out of screen size        frmInputDialog.StartPosition =             FormStartPosition.CenterScreen;      }string PrompText = lblPrompt.Text;int n =0;int Index =0;// Counting the new line in the Prompt stringwhile(PrompText.IndexOf("\n",Index) > -1)                {        Index = PrompText.IndexOf("\n",Index)+1;        n++;    }if( n ==0 )        n =1;// Down here making the form bigger.    System.Drawing.Point Txt = txtInput.Location;     Txt.Y = Txt.Y + (n*4);    txtInput.Location = Txt;     System.Drawing.Size form = frmInputDialog.Size;     form.Height = form.Height + (n*4);    frmInputDialog.Size = form;     txtInput.SelectionStart =0;    txtInput.SelectionLength = txtInput.Text.Length;    txtInput.Focus();}

Conclusion

TheInputBox a simple static class which you can use in Windows Forms application to prompt for a text. It can also be used from Visual Basic .NET when you compile it in a library and refer this library from your VB project.

If we will make our ownMessageBox, then we have to make in the same way as we made theInputBox. Then we will support other languages with our ownMessageBox.

Have fun!

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be foundhere

About the Author

hestol

Software Developer (Senior)
Cyber Com Consulting A/S
Denmark Denmark

Member
I have working with C since 1988, and updates to VC++ in 1998. The first software I was making in VC++ was Fan calculation program with graph. This Fan Was integrated with the Oracle system.
I've been working almost exclusively with C# and .NET since
beginning of the technology.
Now I am working with SOA structure, MVC 3.0, SQL Server 2008 and VS.NET 2010. The company Cyber Com Consulting A/S is a IT consult house.

loading...
Sign Up to vote  PoorExcellent
Add a reason or comment to your vote:x
Votes of 3 or less require a comment

Comments and Discussions

 
 RefreshFirstPrevNext
QuestionPasswordChar functionalitymemberMember 79167403:38 21 Dec '11  
GeneralMy vote of 5memberMember 79167403:34 21 Dec '11  
GeneralImprovement suggestionsmemberMario Majcica23:55 29 Mar '11  
GeneralRe: Improvement suggestionsmemberhestol2:57 7 Apr '11  
NewsInputBox in C++memberMember 37163605:53 21 Jan '11  
GeneralMy vote of 5memberCon Fuse3:05 16 Nov '10  
GeneralThank youmemberAvi Farah11:25 15 Nov '10  
AnswerNice Inteligent Solution (Just flamed the other guy for that VB hack rubish).memberDavePaterson11:32 11 Sep '10  
AnswerRe: Nice Inteligent Solution (Just flamed the other guy for that VB hack rubish).memberhestol4:19 13 Sep '10  
GeneralInputBoxmemberJoeNovak6910:52 10 Nov '09  
Last Visit: 19:00 31 Dec '99     Last Update: 9:37 29 Dec '11123Next »

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

Permalink |Advertise |Privacy |Mobile
Web04 |2.5.111208.1 |Last Updated 21 Apr 2005
Article Copyright 2005 by hestol
Everything elseCopyright ©CodeProject, 1999-2011
Terms of Use
Layout:fixed|fluid

See Also...
The Daily Insider

[8]ページ先頭

©2009-2025 Movatter.jp