<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>box Archive - Robert Skibbe</title>
	<atom:link href="https://robbelroot.de/blog/tag/box/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>alias RobbelRoot – Freelance Full Stack Developer .NET</description>
	<lastBuildDate>Thu, 18 Jan 2024 00:55:56 +0000</lastBuildDate>
	<language>de</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://robbelroot.de/wp-content/uploads/2020/12/cropped-favicon-32x32.png</url>
	<title>box Archive - Robert Skibbe</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>VB NET removing Items from ListBoxes in 2024</title>
		<link>https://robbelroot.de/blog/vb-net-removing-items-from-listboxes/</link>
					<comments>https://robbelroot.de/blog/vb-net-removing-items-from-listboxes/#comments</comments>
		
		<dc:creator><![CDATA[Robert Skibbe]]></dc:creator>
		<pubDate>Fri, 11 Feb 2022 12:14:48 +0000</pubDate>
				<category><![CDATA[General]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Visual Basic .NET (EN)]]></category>
		<category><![CDATA[Visual Basic .NET troubleshooting]]></category>
		<category><![CDATA[box]]></category>
		<category><![CDATA[delete]]></category>
		<category><![CDATA[deleting]]></category>
		<category><![CDATA[discard]]></category>
		<category><![CDATA[drop]]></category>
		<category><![CDATA[help]]></category>
		<category><![CDATA[item]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[listbox]]></category>
		<category><![CDATA[listing]]></category>
		<category><![CDATA[remove]]></category>
		<category><![CDATA[removing]]></category>
		<category><![CDATA[troubleshooting]]></category>
		<category><![CDATA[vb]]></category>
		<category><![CDATA[vb.net]]></category>
		<category><![CDATA[vbnet]]></category>
		<category><![CDATA[visual basic]]></category>
		<guid isPermaLink="false">https://robbelroot.de/?p=8956</guid>

					<description><![CDATA[<p>I welcome you to todays post considering removing items from listboxes in VB NET. It&#8217;s one of the daily bread and butter tasks of developing Visual Basic NET applications. No matter if you are displaying a list of customers, or some sort of an address book. Even something like bills &#8230;</p>
<p>Der Beitrag <a href="https://robbelroot.de/blog/vb-net-removing-items-from-listboxes/">VB NET removing Items from ListBoxes in 2024</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-removing-Items-from-Listboxes-post-banner.jpg"><img fetchpriority="high" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-removing-Items-from-Listboxes-post-banner.jpg" alt="VB NET removing Items from Listboxes" class="wp-image-8960" title="VB NET removing Items from Listboxes"/></a><figcaption class="wp-element-caption">VB NET removing Items from ListBoxes</figcaption></figure>






<p>I <strong>welcome </strong>you to <strong>todays post </strong>considering <strong>removing items from listboxes in VB NET</strong>.</p>



<p>It&#8217;s one of the <strong>daily bread and butter tasks</strong> of developing Visual Basic NET applications.</p>



<p><strong>No matter if</strong> you are <strong>displaying </strong>a list of <strong>customers</strong>, <strong>or </strong>some sort of an <strong>address book</strong>.</p>



<p><strong>Even </strong>something like <strong>bills </strong>could be displayed using the good old fashioned <strong>listbox control</strong> in VB NET.</p>



<p>But <strong>there&#8217;s</strong> <strong>more</strong> to keep in mind, <strong>when using </strong>the <strong>listing </strong>control number 1 as well.</p>



<p><strong>One big thing </strong>to think about, is <strong>how </strong>you actually <strong>want </strong>to <strong>display </strong>your data.</p>



<p><strong>Sometimes </strong>the <a href="https://docs.microsoft.com/de-de/dotnet/api/system.windows.forms.listbox" target="_blank" rel="noreferrer noopener"><strong>ListBox</strong></a> control <strong>won&#8217;t just be enough</strong> – regarding the available space.</p>



<p>The <strong>key takeaway</strong> here will be your <strong>actual data itself</strong>, making you able to <strong>choose the right form of presentation</strong>.</p>



<p><strong>It </strong>all <strong>starts by </strong>like <strong>how deeply structured</strong> <strong>or </strong>how <strong>complex </strong>your data is nested behind the scenes.</p>



<p>There <strong>will be some easy cases</strong>, where you can just display some kind of <strong>names like </strong>&#8222;Lastname, Firstname&#8220;.</p>



<p>But there will <strong>also </strong>be <strong>cases</strong>, <strong>where </strong>you will have <strong>more complex data like relations </strong>between objects. </p>



<p>At this point, <strong>let&#8217;s not bother</strong> about that too much, we will just go ahead <strong>diving straight</strong> into this post!</p>



<h2 class="wp-block-heading" id="adding-elements-to-a-listbox">Adding elements to a listbox</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Adding-items-to-listbox-before-removing-them-again.jpg"><img decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Adding-items-to-listbox-before-removing-them-again.jpg" alt="VB NET Adding items to listbox before removing them again" class="wp-image-8976" title="VB NET Adding items to listbox before removing them again"/></a><figcaption class="wp-element-caption">VB NET Adding items to listbox before removing them again</figcaption></figure>



<p><strong>Well</strong>, I think <strong>before we can </strong>actually talk about <strong>removing </strong>items from the listbox control, we <strong>actually need </strong>to <strong>add </strong>some <strong>first</strong>.</p>



<p>Just like removing – as you will see in some seconds – <strong>adding items </strong>to a listbox <strong>isn&#8217;t very hard</strong> either.</p>



<p>Theres a <strong>good old function</strong> called &#8222;<strong>add</strong>&#8220; on every listbox instance, so let&#8217;s go ahead and use it.</p>



<p>When working with controls as usual, you just need to call that listbox by its name and say, what you want to do.</p>



<p>In this case we want to <strong>add an item first</strong>, so go ahead and <strong>use a variable</strong>, <strong>or </strong>call the add <strong>function with </strong>a string <strong>literal</strong>.</p>



<p><strong>Actually </strong>I need to kinda <strong>correct my </strong>first <strong>statement</strong>, because we&#8217;re <strong>not </strong>really <strong>adding to </strong>the <strong>listbox </strong>itself.</p>



<p><strong>Instead</strong>, we will <strong>use </strong>the &#8222;<strong>Items</strong>&#8222;-<strong>property</strong> of the listbox, which <strong>is </strong>an &#8222;<strong><a href="https://docs.microsoft.com/de-de/dotnet/api/system.windows.forms.listbox.objectcollection" target="_blank" rel="noreferrer noopener">ObjectCollection</a></strong>&#8222;-type.</p>



<p>This type <strong>makes us</strong> that <strong>shiny </strong>and <strong>easy </strong>&#8222;<strong>add</strong>&#8222;-function available!</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Dim itemOne = "My first item!"
lbListing.Items.Add(itemOne)</pre>



<p><strong>Letting </strong>Visual Studio <strong>execute </strong>this <strong>statement </strong>in for example the <strong>debug mode</strong>, our listbox will now look like this:</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/Adding-items-first-VB-NET-Removing-Items-from-ListBoxes.png"><img decoding="async" width="249" height="216" src="https://robbelroot.de/wp-content/uploads/2022/02/Adding-items-first-VB-NET-Removing-Items-from-ListBoxes.png" alt="Adding items first - VB NET Removing Items from ListBoxes" class="wp-image-8973" title="Adding items first - VB NET Removing Items from ListBoxes"/></a><figcaption class="wp-element-caption">Adding items first &#8211; VB NET Removing Items from ListBoxes</figcaption></figure>



<p>Let&#8217;s go <strong>one step further</strong> by <strong>generating </strong>more <strong>items</strong>, but kinda <strong>automated</strong>, not manually!</p>



<p>We <strong>will use</strong> an easy <strong>For-Loop</strong> for that:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">For i = 1 To 5
  lbListing.Items.Add($"Item {i}")
Next</pre>



<p><strong>If </strong>you are <strong>not familiar with </strong>that <strong>dollar </strong>sign <strong>thingy</strong>: That&#8217;s called <strong>string interpolation</strong>.</p>



<p>I <strong>will can writing</strong> those annoying &#8222;<strong>&amp;</strong>&#8222;-chains like this, pretty neat huh!?</p>



<p><strong>Then </strong>our much more filled <strong>listbox will look like this</strong>:</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/Adding-listbox-items-using-a-for-loop.png"><img loading="lazy" decoding="async" width="231" height="205" src="https://robbelroot.de/wp-content/uploads/2022/02/Adding-listbox-items-using-a-for-loop.png" alt="Adding listbox items using a for loop &amp; string interpolation" class="wp-image-8979" title="Adding listbox items using a for loop &amp; string interpolation"/></a><figcaption class="wp-element-caption">Adding listbox items using a for loop &amp; string interpolation</figcaption></figure>



<h2 class="wp-block-heading" id="vb-net-removing-an-item-from-a-listbox-again">VB NET removing an item from a listbox, again</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-removing-the-previously-added-listbox-items-again.jpg"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-removing-the-previously-added-listbox-items-again.jpg" alt="VB NET removing the previously added listbox items again" class="wp-image-9009" title="VB NET removing the previously added listbox items again"/></a><figcaption class="wp-element-caption">VB NET removing the previously added listbox items again</figcaption></figure>



<p>So <strong>now </strong>that we have <strong>populated </strong>our <strong>listbox </strong>with some kind of one dimensional data, we will <strong>remove </strong>it <strong>again</strong>.</p>



<p>For this to work, we <strong>will </strong>have to <strong>call other functions</strong>, which reside inside the &#8222;ObjectCollection&#8220;-type.</p>



<p>We can choose <strong>one of the following functions </strong>for this:</p>



<ul class="wp-block-list">
<li><strong><a href="https://docs.microsoft.com/de-de/dotnet/api/system.windows.forms.listbox.objectcollection.remove?view=windowsdesktop-6.0" target="_blank" rel="noreferrer noopener">Remove</a></strong></li>



<li><strong><a href="https://docs.microsoft.com/de-de/dotnet/api/system.windows.forms.listbox.objectcollection.removeat?view=windowsdesktop-6.0" target="_blank" rel="noreferrer noopener">RemoveAt</a></strong></li>
</ul>



<p>The <strong>first method </strong>&#8222;<strong>remove</strong>&#8222;, will only <strong>remove </strong>an <strong>entry by</strong> the <strong>corresponding object </strong>itself.</p>



<p>&#8222;<strong>RemoveAt</strong>&#8220; will <strong>drop </strong>the <strong>item located </strong>at a <strong>specific position</strong> from the listboxes collection behind.</p>



<h3 class="wp-block-heading" id="removing-a-single-item-from-a-listbox-by-index">Removing a single item from a listbox by index</h3>



<p>So we <strong>will </strong>just <strong>go ahead</strong> and <strong>use </strong>that &#8222;<strong>RemoveAt</strong>&#8220; function <strong>to remove an item from the listbox</strong> like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">lbListing.Items.RemoveAt(2)</pre>



<p><strong>Keep in mind</strong>, that those <strong>numbers are zero based</strong>, so when talking about &#8222;2&#8220; we actually mean the <strong>third element</strong>.</p>



<p>I <strong>took </strong>that <strong>third element </strong>on purpose, because the <strong>first one</strong> would&#8217;ve been <strong>boring</strong>.</p>



<p>As you can see now, the &#8222;<strong>Item 3</strong>&#8220; <strong>entry </strong>inside that listbox <strong>is </strong>now <strong>removed</strong>:</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-removed-the-third-listbox-item-by-index.png"><img loading="lazy" decoding="async" width="231" height="205" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-removed-the-third-listbox-item-by-index.png" alt="VB NET removed the third listbox item by index" class="wp-image-8989" title="VB NET removed the third listbox item by index"/></a><figcaption class="wp-element-caption">VB NET removed the third listbox item by index</figcaption></figure>



<h3 class="wp-block-heading" id="removing-multiple-listbox-items-with-a-loop">Removing multiple listbox items with a loop</h3>



<p><strong>Removing </strong>the <strong>first element would </strong>be <strong>boring</strong>, unless we would do it <strong>in </strong>some kind of <strong>loop</strong>.</p>



<p>So let&#8217;s just try to <strong>remove </strong>the first <strong>3 items</strong> by <strong>calling </strong>that &#8222;RemoveAt&#8220; function – you guessed it – <strong>3 times</strong> in a row.</p>



<p><strong>Don&#8217;t exaggerate</strong> here, <strong>or </strong>you will get some kind of <strong>index error</strong>!</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">For i = 1 To 3
  lbListing.Items.RemoveAt(0)
Next</pre>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-only-2-items-left-from-removed-items-of-listbox.png"><img loading="lazy" decoding="async" width="231" height="207" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-only-2-items-left-from-removed-items-of-listbox.png" alt="Only 2 items left from removed items of listbox" class="wp-image-8995" title="Only 2 items left from removed items of listbox"/></a><figcaption class="wp-element-caption">Only 2 items left from removed items of listbox</figcaption></figure>



<h3 class="wp-block-heading" id="how-to-remove-all-listbox-items-at-once">How to remove all listbox items at once</h3>



<p>Maybe you&#8217;re asking yourself now, how you could actually <strong>remove all listbox items at once</strong>.</p>



<p>Well, <strong>maybe </strong>this is even the <strong>easiest example</strong>, because you don&#8217;t need to play with like indices.</p>



<p>Just <strong>use </strong>that nice &#8222;<strong>Clear</strong>&#8222;-<strong>function</strong>, but be careful to call it on the &#8222;<strong>Items</strong>&#8222;-<strong>property</strong>, <strong>not on </strong>the <strong>listbox </strong>itself.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">lbListing.Items.Clear()</pre>



<h3 class="wp-block-heading" id="removing-a-specific-string-item-value-from-the-listbox">Removing a specific string item value from the listbox</h3>



<p>The <strong>last question </strong>could be, <strong>how to target a special string value</strong> like &#8222;Item 3&#8220;.</p>



<p>This isn&#8217;t really hard either, just <strong>use the correct method</strong> for that:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">lbListing.Items.Remove("Item 3")</pre>



<h3 class="wp-block-heading" id="warning-when-working-with-objects">Warning when working with objects</h3>



<p><strong>When working with </strong>listboxes in combination with <strong>objects</strong>, it&#8217;s a bit <strong>different</strong>.</p>



<p>Then you <strong>will be better off</strong>, by actually <strong>using databinding </strong>on the listbox <strong>and </strong>the corresponding <strong>datasource</strong>.</p>



<p>With this approach, you can <strong>focus </strong>on <strong>working</strong> <strong>on </strong>your <strong>source </strong>behind, an <strong>not on</strong> that <strong>listbox itself</strong>.</p>



<p>This will be a <strong>part of another blog post</strong>!</p>



<h2 class="wp-block-heading" id="conclusion">Conclusion</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/Conclusion.jpg"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/Conclusion.jpg" alt="VB NET Removing items from a listbox - conclusion" class="wp-image-8867" title="VB NET Removing items from a listbox - conclusion"/></a><figcaption class="wp-element-caption">VB NET Removing items from a listbox &#8211; conclusion</figcaption></figure>



<p>So <strong>in todays</strong> blog <strong>post</strong>, I&#8217;ve showed you <strong>how to first create</strong> some <strong>items </strong>inside a listbox.</p>



<p>Then we <strong>continued on how</strong> to <strong>remove </strong>the <strong>items </strong>again.</p>



<p>We <strong>used different styles</strong> and <strong>methods </strong>to actually <strong>remove</strong> those <strong>items from the listbox</strong>.</p>



<p>At first, we <strong>used</strong> a <strong>method</strong>, to remove items <strong>by the corresponding index</strong>.</p>



<p><strong>After that </strong>we tried on using <strong>some </strong>kind of <strong>loop</strong>, to do some iterative <strong>deletion operation</strong>.</p>



<p><strong>Finally</strong>, we just figured out, <strong>how </strong>we can <strong>remove a specific string</strong>-ish value <strong>from </strong>the <strong>listbox items</strong>, to be more clear on the target.</p>



<h2 class="wp-block-heading" id="downloads">Downloads</h2>



<p><strong>No downloads available</strong> for this post, sorry!</p>



<h2 class="wp-block-heading" id="related-posts">Related posts</h2>



<ul class="wp-block-list">
<li><strong><a href="https://robbelroot.de/blog/creating-a-vb-net-datagridview-filter-functionality/" target="_blank" rel="noreferrer noopener">How to filter a DataGridView in VB NET</a></strong></li>



<li><strong><a href="https://robbelroot.de/blog/hacking-a-net-application/" target="_blank" rel="noreferrer noopener">Hacking a .NET Application</a></strong></li>
</ul>
<p>Der Beitrag <a href="https://robbelroot.de/blog/vb-net-removing-items-from-listboxes/">VB NET removing Items from ListBoxes in 2024</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://robbelroot.de/blog/vb-net-removing-items-from-listboxes/feed/</wfw:commentRss>
			<slash:comments>3</slash:comments>
		
		
			</item>
		<item>
		<title>VBS MsgBox – Nachrichten in 2023 anzeigen</title>
		<link>https://robbelroot.de/blog/vbs-msgbox/</link>
					<comments>https://robbelroot.de/blog/vbs-msgbox/#respond</comments>
		
		<dc:creator><![CDATA[Robert Skibbe]]></dc:creator>
		<pubDate>Thu, 15 Jul 2021 09:21:55 +0000</pubDate>
				<category><![CDATA[Allgemein]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Visual Basic Script]]></category>
		<category><![CDATA[Visual Basic Script lernen]]></category>
		<category><![CDATA[basic]]></category>
		<category><![CDATA[box]]></category>
		<category><![CDATA[create]]></category>
		<category><![CDATA[dialog]]></category>
		<category><![CDATA[message]]></category>
		<category><![CDATA[messagebox]]></category>
		<category><![CDATA[messagedialog]]></category>
		<category><![CDATA[msgbox]]></category>
		<category><![CDATA[nachricht]]></category>
		<category><![CDATA[open]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[vbs]]></category>
		<category><![CDATA[vbscript]]></category>
		<category><![CDATA[visual]]></category>
		<category><![CDATA[visual basic script]]></category>
		<category><![CDATA[window]]></category>
		<guid isPermaLink="false">https://robbelroot.de/?p=4029</guid>

					<description><![CDATA[<p>VBS MsgBox – Visual Basic Script MsgBox Du möchtest eine VBS MsgBox (Visual Basic Script MessageBox) anzeigen und dem Nutzer eine Nachricht o. Ä. mitteilen? Dann freue ich mich, Dich hier auf meiner Seite, bzw. in meinem kleinen Beitrag begrüßen zu dürfen. Die Nachrichtenbox (engl. Messagebox) ist in so gut &#8230;</p>
<p>Der Beitrag <a href="https://robbelroot.de/blog/vbs-msgbox/">VBS MsgBox – Nachrichten in 2023 anzeigen</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2021/07/VBS-MsgBox.png"><img loading="lazy" decoding="async" width="640" height="335" src="https://robbelroot.de/wp-content/uploads/2021/07/VBS-MsgBox.png" alt="VBS MsgBox – Nachrichten anzeigen" class="wp-image-11244" title="VBS MsgBox – Nachrichten anzeigen"/></a><figcaption class="wp-element-caption">VBS MsgBox – Nachrichten anzeigen</figcaption></figure>






<h2 class="wp-block-heading">VBS MsgBox – Visual Basic Script MsgBox</h2>



<p>Du möchtest eine <strong>VBS MsgBox</strong> (Visual Basic Script MessageBox) anzeigen und dem Nutzer eine Nachricht o. Ä. mitteilen? Dann freue ich mich, Dich hier auf meiner Seite, bzw. in meinem kleinen Beitrag begrüßen zu dürfen.</p>



<p>Die Nachrichtenbox (engl. Messagebox) ist in so gut wie allen gängigen Programmier- und Skriptsprachen präsent. Sie ist zwar nicht immer gleich umgesetzt, aber zumindest in irgendeiner Form vorhanden. Man möchte als Entwickler ja auch verständlicherweise nicht ohne Basis-Werkzeug dastehen, oder?</p>



<p class="info-banner">&#x1f4a1; Keine Zeit für Blabla? Kein Problem! Wenn Du ein paar schnelle Beispiele für die VBS MsgBox brauchst, scrolle einfach zu den <strong><a href="#vbs-msgbox-beispiele">schnellen Beispielen</a></strong>.</p>



<style>
.info-banner {
  background: #8acc47;
  padding: 2em;
  border-radius: 0.5em;
  color: whitesmoke;
}

.info-banner a {
 color: #2E86AB !important; 
}
</style>



<h2 class="wp-block-heading">++ UPDATE 05.08.2022 ++</h2>



<p>So, ein paar weitere Monate sind vergangen und ich habe noch ein wenig nachgearbeitet. Unter anderem habe ich die Texte ein wenig verbessert und weitere Informationen ergänzt. Besonders gestört hat mich im Nachhinein auch die Formatierung, daher ist Diese auch geändert worden.</p>



<h2 class="wp-block-heading">++ UPDATE 25.01.2022 ++</h2>



<p>Ich habe nach einiger Zeit noch weitere Bildchen zur besseren Veranschaulichung hinzugefügt. Ebenso habe ich ein Fazit ergänzt und noch ein paar Erklärungen besser ausgeführt. Man ist dann als Entwickler eben doch ein wenig pingelig!</p>



<h2 class="wp-block-heading">Hintergrund – VBS MsgBox</h2>



<p>Ein Dialog, Dialogfeld, Dialogfenster oder auch eine Dialogbox sind alles letztendlich Synonyme für das Gleiche: Es handelt sich um eine Art, bzw. eine Methode, einem Nutzer eine Nachricht anzuzeigen. Zusätzlich zu dieser angezeigten Nachricht, bietet der Dialog häufig auch noch mehr. Meist spiegelt sich das in einer Art Erwartung, in Form einer Antwort &#8222;an das Programm&#8220; wider.</p>



<h2 class="wp-block-heading">Einfache und komplexe Möglichkeiten</h2>



<p>Wie so oft in der Programmierung, bzw. beim Scripting gibt es verschiedene Möglichkeiten. Dabei ist auch die &#8222;MsgBox&#8220; keine Ausnahme, also wie man einen Dialog aufbauen kann. Vom simplen &#8222;OK&#8220;-Nachrichten-Dialog, wo man nur etwas Informatives mitteilen möchte, bis hin zu komplexen Formularen mit Eingabefeldern und verschiedenen Knöpfen. Auch wenn wir uns in diesem Tutorial auf die simple MsgBox-Methode von Visual Basic Script beziehen, lohnt es sich, auch mal links und rechts zu schauen.</p>



<h2 class="wp-block-heading">Modal oder nicht modal</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/VBS-MsgBox-modale-und-non-modale-darstellung-1.jpg"><img loading="lazy" decoding="async" width="659" height="370" src="https://robbelroot.de/wp-content/uploads/2022/01/VBS-MsgBox-modale-und-non-modale-darstellung-1.jpg" alt="Modale und non modale Darstellung von Dialogen" class="wp-image-7812" title="VBS MsgBox - modale und non modale darstellung"/></a><figcaption class="wp-element-caption">Modale und non modale Darstellung von Dialogen</figcaption></figure>



<p>Meistens sind <a href="https://de.wikipedia.org/wiki/Dialog_(Benutzeroberfl%C3%A4che)" target="_blank" rel="noreferrer noopener"><strong>Dialoge</strong></a> modal, das heißt, Sie sperren den Rest der Anwendung, um den Fokus eben nur auf sich selbst zu legen. Andererseits gibt es auch non-modale Dialog, Diese erlauben weiterhin den Zugriff auf den Rest der Anwendung. Ein konkretes, Beispiel für einen non-modalen Dialoge wäre z. B. ein Eingabefenster für einen Termin.</p>



<p>So kann ich einerseits in meiner Haupt-Anwendung arbeiten, jedoch gleichzeitig schonmal gewisse Daten für einen Termin ausfüllen. Ebenso erlaubt es mir, mehrere Termine gleichzeitig anzulegen und eventuell fehlende Informationen zu recherchieren. Ich würde daher sagen, die non-modale Variante ist somit Multitasking-freundlicher.</p>



<p>Möchte ich meinen Nutzer hingegen auf einen Inhalt fokussiert halten und vermeiden, dass kreuz und quer gesprungen wird, dann ist vermutlich die modale Variante vorzuziehen.</p>



<h2 class="wp-block-heading">Stil und Aufbau</h2>



<p>Meistens folgen bekannte Tools zum Erstellen solcher Dialoge gängigen Designstandards. Damit finden sich Nutzer einfacher durch eine Art vertraute Umgebung zurecht. In der Mitte des Dialoges befinden sich üblicherweise die beschrifteten Eingabefelder inkl. Bezeichnungen.</p>



<p>Diese werden häufig mit Hilfe von Designer-Toolkits zusammengeklickt und korrekt angeordnet. Unten rechts befinden sich dann zumeist die primären Aktionen, wie z. B. das Bestätigen, oder das Abbrechen des jeweiligen Dialoges.</p>



<p>Hat der Nutzer letztendlich alles fertig bearbeitet und den Dialog geschlossen, geht es in dem jeweiligen anderen Teil der Anwendung weiter.</p>



<h2 class="wp-block-heading">Bekannte Dialoge</h2>



<p>Als PC-Nutzer kennt man vermutlich schon viele Dialoge, ohne Diese bewusst als Solche zu sehen, hier einige Beispiele:</p>



<ul class="wp-block-list">
<li><strong>Meldungen </strong>– Wie Diese aus unserem Beitrag hier, Welche Informationen anzeigen und Kombinationen von Buttons verwenden.</li>



<li><strong>Texteingabedialoge</strong> – Simple Dialoge, Welche meist nur einen simplen Text entgegennehmen.</li>



<li><strong>Datums-Dialoge</strong> – Dialog zur Auswahl von einzelnen Daten (plural Datum), oder Zeiträumen.</li>



<li><strong>Dateiauswahldialoge </strong>– Ein Dialog zur Auswahl von Dateien, oder Verzeichnissen.</li>



<li><strong>Farbauswahldialoge</strong> – Dialoge, Welche die Auswahl einzelnen, bzw. mehrere Farben anbieten.</li>
</ul>



<h2 class="wp-block-heading">Code – VBS MsgBox</h2>



<p>Schauen wir uns nach den einzelnen Erklärungen nun ein konkretes &#8222;Visual Basic Script&#8220;-Beispiel für eine MsgBox an.</p>



<h3 class="wp-block-heading">Syntax – Schreibweise</h3>



<p>Die allgemeine Schreibweise für den Aufruf einer VBS MsgBox sieht wie folgt aus. Zuerst kommt der Befehl selbst, danach der Text für die Nachrichtenbox. Anschließend kann man Konstanten für die Darstellung verschiedener Buttons bestimmen. Zum Schluss kann man dann noch den Titel des erscheinenden Fensters bestimmen.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">' grober Aufbau
MsgBox &lt;TextDerBox>, &lt;KonstantenFürButtons>, &lt;TitelDerBox></pre>



<h3 class="wp-block-heading" id="vbs-msgbox-beispiele">Simples Beispiel</h3>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">' simples Beispiel
MsgBox "Eine beliebige Nachricht", VBOKOnly, "Der Titel"</pre>



<p>Dieses simple Beispiel sieht ausgeführt wie gleich folgend aus. Man bekommt einen Dialog mit dem gewünschten Titel, dem gewählten Inhalt und dem &#8222;OK&#8220;-Button angezeigt.</p>



<figure class="wp-block-image size-large"><a href="https://robbelroot.de/wp-content/uploads/2021/07/VBS-MsgBox-Beispiel.png"><img loading="lazy" decoding="async" width="173" height="137" src="https://robbelroot.de/wp-content/uploads/2021/07/VBS-MsgBox-Beispiel.png" alt="VBS MsgBox Beispiel" class="wp-image-4048" title="VBS MsgBox Beispiel"/></a><figcaption class="wp-element-caption">VBS MsgBox Beispiel</figcaption></figure>



<h3 class="wp-block-heading">Als Kurzform</h3>



<p>Als Kurzform mit ausgelassenen optionalen Parametern wie &#8222;Buttons&#8220; und &#8222;Title&#8220;, sieht das Beispiel wie gleich folgend aus. Dies ist offensichtlich die einfachste und bekannteste Variante, um die Nachricht anzuzeigen. Ohne viel &#8222;Plim-Plam&#8220; kommen wir zum Ergebnis, jedoch verzichten wir somit auch auf weitere Funktionalität.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">MsgBox "Eine beliebige Nachricht"</pre>



<h3 class="wp-block-heading">Beispiel mit mehreren Buttons</h3>



<p>Im nächsten Beispiel verwenden wir den &#8222;OR-Operator&#8220;, um zwei verschiedene Konstanten miteinander zu &#8222;verodern&#8220;. Somit können wir mehrere Informationen auf einmal übergeben und den Dialog besser gestalten. Ebenso kommen dadurch nun auch zwei Buttons statt Einem zum Einsatz, toll, oder!?</p>



<p>Hier wäre der Code:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">MsgBox "Eine beliebige Nachricht", vbYesNo OR vbQuestion, "Der Titel"</pre>



<p>Und so sieht das Ganze dann als fertige Messagebox aus:</p>



<figure class="wp-block-image size-large"><a href="https://robbelroot.de/wp-content/uploads/2021/07/VBS-MsgBox-Beispiel-mit-Icon-und-zwei-Buttons.png"><img loading="lazy" decoding="async" width="224" height="155" src="https://robbelroot.de/wp-content/uploads/2021/07/VBS-MsgBox-Beispiel-mit-Icon-und-zwei-Buttons.png" alt="VBS MsgBox Beispiel mit Icon und zwei Buttons" class="wp-image-4066" title="VBS MsgBox Beispiel mit Icon und zwei Buttons"/></a><figcaption class="wp-element-caption">VBS MsgBox Beispiel mit Icon und zwei Buttons</figcaption></figure>



<h3 class="wp-block-heading">Ergebnis abfragen</h3>



<p>Um das Ergebnis einer MsgBox abzufragen, kannst Du beispielsweise wie folgt vorgehen:</p>



<p>Im ersten Schritt deklarieren wir eine Variable, um den gleich folgenden Wert zu speichern. Danach rufen wir die &#8222;MsgBox&#8220;-Funktion selbst auf und verwenden dafür passende Parameter. Darunter befinden sich dann die Informationen, dass wir gerne &#8222;Ja&#8220;, &#8222;nein&#8220; und ein passendes Symbol sehen möchten.</p>



<p>Im letzten Schritt werten wir dann die Antwort, also das Ergebnis der Abfrage aus. Falls die &#8222;VBS MsgBox&#8220; mit &#8222;ja&#8220; beantwortet wurde, also &#8222;vbYes&#8220; als Rückgabewert kommt, zeigen wir eine weitere Nachricht an.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Dim x
x = MsgBox ("Eine beliebige Nachricht", vbYesNo OR vbQuestion, "Der Titel")
If x = vbYes Then MsgBox "Es wurde JA gedrückt!"</pre>



<h3 class="wp-block-heading">Button-Konstanten</h3>



<p>In der folgenden Tabelle findest Du die möglichen Button-Konstanten der MsgBox-Funktion aus Visual Basic Script.</p>



<figure class="wp-block-table"><table><tbody><tr><td><strong>Konstante</strong></td><td><strong>Wert</strong></td><td><strong>Beschreibung</strong></td></tr><tr><td>vbOKOnly</td><td>0</td><td>Nur &#8222;OK&#8220; wird angezeigt</td></tr><tr><td>vbOKCancel</td><td>1</td><td>&#8222;OK&#8220; und &#8222;Abbrechen&#8220; werden angezeigt</td></tr><tr><td>vbAbortRetryIgnore</td><td>2</td><td>&#8222;Abbrechen&#8220;, &#8222;Wiederholen&#8220; und &#8222;Ignorieren&#8220; werden angezeigt</td></tr><tr><td>vbYesNoCancel</td><td>3</td><td>&#8222;Ja&#8220;, &#8222;Nein&#8220; und &#8222;Abbrechen&#8220; werden angezeigt</td></tr><tr><td>vbYesNo</td><td>4</td><td>&#8222;Ja&#8220; und &#8222;Nein&#8220; werden angzeigt</td></tr><tr><td>vbRetryCancel</td><td>5</td><td>&#8222;Wiederholen&#8220; und &#8222;Abbrechen&#8220; werden angezeigt</td></tr><tr><td>vbCritical</td><td>16</td><td>Ein roter Kreis inkl. weißes Kreuz mit Windows-Error Sound werden dargestellt</td></tr><tr><td>vbQuestion</td><td>32</td><td>Eine Sprechblase mit Ausrufezeichen und dem nachfragenden Windows-Sound werden dargestellt</td></tr><tr><td>vbExclamation</td><td>48</td><td>Ein gelbes Warndreieck und der Windows-Sound &#8222;Warnung&#8220; wird dargestellt</td></tr><tr><td>vbInformation</td><td>64</td><td>Eine Sprechblase mit Ausrufezeichen wird dargestellt und der Benachrichtigungs-Sound abgespielt</td></tr><tr><td>vbDefaultButton1</td><td>0</td><td>Der erste Knopf ist bei Darstellung selektiert </td></tr><tr><td>vbDefaultButton2</td><td>256</td><td>Der zweite Knopf ist bei Anzeige selektiert</td></tr><tr><td>vbDefaultButton3</td><td>512</td><td>Der dritte Knopf ist bei Darstellung selektiert</td></tr><tr><td>vbDefaultButton4</td><td>768</td><td>Der vierte Knopf ist bei Anzeige selektiert</td></tr><tr><td>vbApplicationModal</td><td>0</td><td>Ein Flag um den Dialog als modalen Dialog zu kennzeichnen</td></tr><tr><td>vbSystemModal</td><td>4096</td><td>Unterbricht weitere System-Prozesse bis MsgBox beantwortet wurde</td></tr><tr><td>vbMsgBoxHelpButton</td><td>16384</td><td>Fügt einen Hilfs-Button zur Box hinzu</td></tr><tr><td>VbMsgBoxSetForeground</td><td>65536</td><td>Lässt das Dialogfeld in den Vordergrund rücken</td></tr><tr><td>vbMsgBoxRight</td><td>524288</td><td>Der Text wird rechtsbündig dargestellt</td></tr><tr><td>vbMsgBoxRtlReading</td><td>1048576</td><td>Lässt den Text von rechts nach links dargestellt werden</td></tr></tbody></table><figcaption class="wp-element-caption">VBS MsgBox Button-Konstanten</figcaption></figure>



<h3 class="wp-block-heading">Rückgabewert der VBS MsgBox</h3>



<p>Hier findest Du eine kleine Tabelle, Welche die einzelnen Rückgabewerte der Messagebox aufschlüsselt.</p>



<figure class="wp-block-table"><table><tbody><tr><td><strong>Konstante</strong></td><td><strong>Wert</strong></td><td><strong>Button</strong></td></tr><tr><td>vbOK</td><td>1</td><td>OK</td></tr><tr><td>vbCancel</td><td>2</td><td>Abbrechen</td></tr><tr><td>vbAbort</td><td>3</td><td>Abbrechen</td></tr><tr><td>vbRetry</td><td>4</td><td>Wiederholen</td></tr><tr><td>vbIgnore</td><td>5</td><td>Ignorieren</td></tr><tr><td>vbYes</td><td>6</td><td>Ja</td></tr><tr><td>vbNo</td><td>7</td><td>Nein</td></tr></tbody></table><figcaption class="wp-element-caption">Übersicht VBS MsgBox-Konstanten</figcaption></figure>



<h2 class="wp-block-heading">Fazit – VBS MsgBox</h2>



<p>Wie eingangs erwähnt, ist die Messagebox ein absolutes &#8222;Basic Tool&#8220; im Bereich der Softwareentwicklung. Aufgrund dessen hat auch die Skriptsprache Visual Basic Script (kurz VBS) sich nicht lumpen lassen. Wir finden in VBS die Möglichkeit, eine Nachrichtenbox mit dem kurzen Befehl &#8222;MsgBox&#8220; anzeigen zu lassen.</p>



<p>Dabei haben wir verschiedene Ansätze, die &#8222;MsgBox&#8220; anzeigen zu lassen. Wir können aus verschiedenen Knöpfen/Kombinationen wählen und mit den unterschiedlichen Rückgabewerten arbeiten.</p>



<p>Für komplexere Eingaben, wie z. B. eine Adresse, oder ein Datum ist die &#8222;MsgBox&#8220; allerdings nicht geeignet.</p>



<h2 class="wp-block-heading">Relevante Beiträge</h2>



<ul class="wp-block-list">
<li><a href="https://robbelroot.de/blog/vbscript-array/" target="_blank" rel="noreferrer noopener"><strong>VBScript Array &#8211; &#8222;Felder&#8220; in Visual Basic Script</strong></a></li>



<li><a href="https://robbelroot.de/blog/vbscript-instr" target="_blank" rel="noreferrer noopener"><strong>VBScript InStr</strong> <strong>&#8211; Prüfen, ob ein String einen String beinhaltet</strong></a></li>
</ul>
<p>Der Beitrag <a href="https://robbelroot.de/blog/vbs-msgbox/">VBS MsgBox – Nachrichten in 2023 anzeigen</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://robbelroot.de/blog/vbs-msgbox/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>VB.NET InputBox Beispiel</title>
		<link>https://robbelroot.de/blog/vbnet-inputbox-beispiel/</link>
					<comments>https://robbelroot.de/blog/vbnet-inputbox-beispiel/#comments</comments>
		
		<dc:creator><![CDATA[Robert Skibbe]]></dc:creator>
		<pubDate>Fri, 02 Jul 2021 21:34:58 +0000</pubDate>
				<category><![CDATA[Allgemein]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Visual Basic .NET]]></category>
		<category><![CDATA[Visual Basic .NET lernen]]></category>
		<category><![CDATA[bearbeiten]]></category>
		<category><![CDATA[beispiel]]></category>
		<category><![CDATA[box]]></category>
		<category><![CDATA[dialog]]></category>
		<category><![CDATA[eingabe]]></category>
		<category><![CDATA[eingabefenster]]></category>
		<category><![CDATA[example]]></category>
		<category><![CDATA[fenster]]></category>
		<category><![CDATA[input]]></category>
		<category><![CDATA[inputbox]]></category>
		<category><![CDATA[sample]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[vb.net]]></category>
		<category><![CDATA[vbnet]]></category>
		<category><![CDATA[verarbeiten]]></category>
		<guid isPermaLink="false">https://robbelroot.de/?p=2809</guid>

					<description><![CDATA[<p>VB.NET InputBox Beispiel In meinem VB.NET InputBox Beispiel geht es um eine einfache und rudimentäre Variante, die simple Eingabe eines Nutzers entgegenzunehmen. Falls Du hingegen komplexere Eingaben, wie z. B. die Eingaben einer Adresse via Dialog-Fenster beabsichtigst, ist vermutlich dieses Tutorial hier für Dich besser geeignet. Da wir in diesem &#8230;</p>
<p>Der Beitrag <a href="https://robbelroot.de/blog/vbnet-inputbox-beispiel/">VB.NET InputBox Beispiel</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-image size-large"><a href="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-InputBox-Beispiel.png"><img loading="lazy" decoding="async" width="1024" height="536" src="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-InputBox-Beispiel-1024x536.png" alt="VB.NET InputBox Beispiel" class="wp-image-2811" title="VB.NET InputBox Beispiel" srcset="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-InputBox-Beispiel-1024x536.png 1024w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-InputBox-Beispiel-300x157.png 300w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-InputBox-Beispiel-768x402.png 768w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-InputBox-Beispiel-700x366.png 700w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-InputBox-Beispiel-332x174.png 332w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-InputBox-Beispiel.png 1200w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a><figcaption>VB.NET InputBox Beispiel</figcaption></figure>






<h2 class="wp-block-heading">VB.NET InputBox Beispiel</h2>



<p>In meinem <strong>VB.NET InputBox Beispiel</strong> <strong>geht </strong>es <strong>um </strong>eine <strong>einfache </strong>und rudimentäre <strong>Variante</strong>, die <strong>simple Eingabe </strong>eines Nutzers <strong>entgegenzunehmen</strong>.</p>



<p><strong>Falls </strong>Du hingegen <strong>komplexere Eingaben</strong>, wie z. B. die Eingaben einer Adresse <strong>via Dialog-Fenster</strong> <strong>beabsichtigst</strong>, <strong>ist </strong>vermutlich <strong><a href="/blog/vbnet-input-dialog" target="_blank" rel="noreferrer noopener">dieses Tutorial hier</a></strong> für Dich <strong>besser geeignet</strong>.</p>



<p><strong>Da </strong>wir <strong>in diesem Beispiel </strong>in erster Linie <strong>String</strong>-Eingaben <strong>verarbeiten </strong>– <strong>aber </strong>natürlich <strong>auch </strong>in <strong>Zahlen </strong>umwandeln können – <strong>bieten sich </strong>unter anderem ggf. <strong>noch folgende Beiträge </strong>an: <strong><a href="https://robbelroot.de/blog/vbnet-string-zerlegen/" target="_blank" rel="noreferrer noopener">String zerlegen, bzw. splitten</a></strong>, <strong><a href="https://robbelroot.de/blog/vbnet-erstes-zeichen-aus-string-entfernen/" target="_blank" rel="noreferrer noopener">erstes Zeichen aus String entfernen</a></strong>, <strong><a href="https://robbelroot.de/blog/vbnet-letztes-zeichen-aus-string-entfernen/" target="_blank" rel="noreferrer noopener">letztes Zeichen aus String entfernen</a></strong>.</p>



<h2 class="wp-block-heading">++ Update 19.01.2022 ++</h2>



<p>Ich habe mich <strong>beim Aufräumen der Homepage</strong> heute dazu entschieden, den <strong>Beitrag </strong>noch einmal <strong>anzupassen</strong>.</p>



<p><strong>Neben </strong>des wichtigen <strong>Fazits </strong>bin ich auch noch <strong>auf </strong>einige <strong>einzelne Punkte</strong> genauer eingegangen.</p>



<p>Dazu habe ich <strong>passende Passagen</strong> hinzugefügt <strong>und </strong>den <strong>Download</strong>-Link <strong>aktualisiert</strong>!</p>



<h2 class="wp-block-heading">Szenario – InputBox</h2>



<p><strong>Jeder kommt hier und da </strong>irgendwann mal <strong>an den Punkt </strong>beim Programmieren, <strong>wo </strong>man einem <strong>Nutzer ganz einfach </strong>eine <strong>Textbox zeigen </strong>will <strong>und </strong>an <strong>dessen Text</strong>-Eingabe kommen möchte.</p>



<p><strong>Nun stellt sich </strong>die <strong>Frage</strong>: &#8222;Och man, <strong>muss ich </strong>dafür jetzt ein <strong>komplett eigenes Fenster </strong>erstellen? <strong>Geht das nicht einfacher und </strong>vor allem <strong>schneller</strong>!?&#8220;.</p>



<p><strong>Zum Glück </strong>muss man – jedenfalls in simplen Fällen – <strong>für </strong>die <strong>Eingabe </strong>von z. B. <strong>Text</strong>, einer <strong>Zahl</strong>, oder einem <strong>Datum keine eigene Maske </strong>mit Bestätigungs-, bzw. Abbruch-Funktionalität <strong>erstellen</strong>.</p>



<p><strong>Dafür kommt </strong>uns die <strong><a href="https://docs.microsoft.com/de-de/dotnet/api/microsoft.visualbasic.interaction.inputbox?view=net-5.0" target="_blank" rel="noreferrer noopener">InputBox-Funktion</a></strong> vom .NET <strong>Framework </strong>zu Hilfe, allerdings <strong>sieht </strong>Diese <strong>nicht </strong>so <strong>prickelnd </strong>schön <strong>aus</strong>.</p>



<h2 class="wp-block-heading">VB.NET Inputbox Code-Beispiele</h2>



<h3 class="wp-block-heading">Form1-Klasse</h3>



<p>Die <strong>gängige Hauptklasse </strong>unserer Beispiele, <strong>insofern keine weitere Dialoge oder Formulare </strong>notwendig sind.</p>



<h4 class="wp-block-heading">Methoden</h4>



<h5 class="wp-block-heading">Form1_Load</h5>



<p><strong>In </strong>der <strong>Load</strong>-Methode der Form, bzw. <strong>genauer genommen </strong>in dessen <strong>Load-Ereignishandler</strong>, <strong>rufen </strong>wir <strong>3 verschiedene Beispiele </strong>der InputBox für unser <strong>VB.NET <a href="https://docs.microsoft.com/de-de/dotnet/api/microsoft.visualbasic.interaction.inputbox?view=net-5.0" target="_blank" rel="noreferrer noopener">InputBox</a> Beispiel</strong> auf.</p>



<p><strong>Natürlich</strong> <strong>kannst </strong>Du Beispiele <strong>nach Belieben aus</strong>&#8211; <strong>und ein-kommentieren</strong>, <strong>um </strong>einen <strong>besseren Blick </strong>über das ausgewählte Beispiel <strong>zu erhalten</strong>.</p>



<p>Das war <strong>letztendlich auch </strong>die <strong>Absicht </strong>hinter den <strong>einzelnen Methoden</strong>: <strong>Beschreibung und </strong>die <strong>saubere Trennung </strong>der Beispiele.</p>



<h5 class="wp-block-heading">DoSimpleGreeting</h5>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/VB.NET-Inputbox-Beispiel-fuer-Begruessung.png"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/01/VB.NET-Inputbox-Beispiel-fuer-Begruessung.png" alt="VB.NET Inputbox Beispiel für Begrüßung" class="wp-image-7483" title="VB.NET Inputbox Beispiel für Begrüßung"/></a><figcaption>VB.NET Inputbox Beispiel für Begrüßung</figcaption></figure>



<p>Diese <strong>Methode </strong>dient wohl dem <strong>einfachsten</strong>, bzw. gängigsten <strong>Beispiel</strong>, <strong>also </strong>der simplen <strong>Abfrage eines Strings</strong>.</p>



<p>Wir <strong>fragen </strong>in diesem Fall <strong>den Namen des Nutzers </strong>ab und <strong>prüfen ob </strong>dieser <strong>leer </strong>ist.</p>



<p><strong>Wenn </strong>der <strong>Name leer </strong>ist, <strong>also </strong>falls der <strong>Dialog abgebrochen </strong>wurde, <strong>oder </strong>der Dialog <strong>leer bestätigt </strong>wurde, <strong>dann </strong>wird eine <strong>Meldung </strong>ausgegeben <strong>und </strong>die <strong>Sub </strong>mit &#8222;Early-Return&#8220; <strong>verlassen</strong>.</p>



<p><strong>Haben </strong>wir einen <strong>String vorliegen</strong>, <strong>nehmen </strong>wir <strong>an</strong>, <strong>dass </strong>dies der <strong>Name </strong>des Nutzers ist <strong>und geben </strong>eine entsprechende <strong>Meldung aus</strong>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Private Sub DoSimpleGreeting()
    Dim content = "We would like to be more personal, so please provide your name so we can talk to you better :)!"
    Dim title = "Please enter your name"
    Dim input = InputBox(content, title)
    Dim cancelled = String.IsNullOrEmpty(input)
    If cancelled Then
        MessageBox.Show("Ok, so you don't want to tell us your name.. :(")
        Return
    End If
    MessageBox.Show($"Nice to meet you, {input}")
End Sub</pre>



<h5 class="wp-block-heading">WorkWithInteger</h5>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/VB.NET-Inputbox-Beispiel-fuer-Zahlen-wie-Alter.png"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/01/VB.NET-Inputbox-Beispiel-fuer-Zahlen-wie-Alter.png" alt="VB.NET Inputbox Beispiel für Zahlen wie Alter" class="wp-image-7488" title="VB.NET Inputbox Beispiel für Zahlen wie Alter"/></a><figcaption>VB.NET Inputbox Beispiel für Zahlen wie Alter</figcaption></figure>



<p><strong>Dieses Beispiel </strong>sieht <strong>ähnlich </strong>aus, <strong>nur </strong>geht es <strong>hier </strong>um die <strong>Eingabe </strong>eines <strong>Alters</strong>.</p>



<p>Ein <strong>weiterer Unterschied </strong>ist <strong>unter anderem</strong>, <strong>dass </strong>wir weiter unten den <strong>String </strong>versuchen <strong>in </strong>einen <strong>Integer </strong>(also eine Ganzzahl) <strong>umwandeln</strong>.</p>



<p><strong>Falls </strong>dies <strong>nicht </strong>erfolgreich ist, <strong>verfahren </strong>wir <strong>auch hier </strong>ähnlich, <strong>geben </strong>eine <strong>Meldung </strong>an den Nutzer <strong>und verlassen </strong>die <strong>Sub </strong>mit &#8222;Early-Return&#8220;.</p>



<p><strong>Wenn alles geklappt </strong>hat, <strong>geben wir </strong>letztendlich eine <strong>Meldung mit </strong>dem <strong>Alter aus</strong>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Private Sub WorkWithInteger()
    Dim content = "Please enter your age!"
    Dim title = "Your age?"
    Dim input = InputBox(content, title)
    Dim cancelled = String.IsNullOrEmpty(input)
    If cancelled Then
        MessageBox.Show("We need to know your age to verify you're old enough :/. ")
        Return
    End If
    Dim age = 0
    If Not Integer.TryParse(input, age) Then
        MessageBox.Show($"Hmm, seems like you entered an invalid age")
        Return
    End If
    MessageBox.Show($"Oh, so you're {age} years old")
End Sub</pre>



<h5 class="wp-block-heading">WorkWithDate</h5>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/VB.NET-Inputbox-Beispiel-fuer-ein-Datum.png"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/01/VB.NET-Inputbox-Beispiel-fuer-ein-Datum.png" alt="VB.NET Inputbox Beispiel für ein Datum" class="wp-image-7493" title="VB.NET Inputbox Beispiel für ein Datum"/></a><figcaption>VB.NET Inputbox Beispiel für ein Datum</figcaption></figure>



<p><strong>Ähnelt sehr stark</strong> dem <strong>vorherigen Beispiel</strong>, nur das wir <strong>hier ein Datum verarbeiten</strong> und <strong>später formatiert </strong>im String <strong>anzeigen</strong>.</p>



<p><strong>Hierbei </strong>muss man natürlich <strong>auf </strong>die <strong>Eingabe achten</strong>, also das man ein <strong>&#8222;richtiges&#8220; Datum</strong> angibt.</p>



<p>Sonst kann es natürlich <strong>bei falscher Eingabe</strong> zu einem <strong>Verarbeitungsfehler</strong> von der &#8222;DateTime.TryParse&#8220;-Funktion kommen.</p>



<p><strong>Falls </strong>man ein <strong>falsches Datum</strong> eingibt, geben wir <strong>auch dies</strong> als Fehler <strong>mit </strong>einer kleinen <strong>Nachricht aus</strong>. </p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Private Sub WorkWithDate()
    Dim content = "Please enter your date of birth!"
    Dim title = "Your birthday?"
    Dim input = InputBox(content, title)
    Dim cancelled = String.IsNullOrEmpty(input)
    If cancelled Then
        MessageBox.Show("We need to know your birthday to verify you're old enough :/. ")
        Return
    End If
    Dim birthday = DateTime.MinValue
    If Not DateTime.TryParse(input, birthday) Then
        MessageBox.Show($"Hmm, seems like you entered an invalid date")
        Return
    End If
    MessageBox.Show($"Oh, so you're born on {birthday:dd.MM.yyyy}")
End Sub</pre>



<h2 class="wp-block-heading">Fazit</h2>



<p>Die <strong>Inputbox ist und bleibt</strong> wohl <strong>eine </strong>der einfachsten <strong>Möglichkeiten simple Benutzereingaben </strong>entgegenzunehmen.</p>



<p>Das <strong>kleine Eingabefeld</strong> ist allerdings <strong>nur für</strong> die Eingabe wirklich <strong>rudimentärer Daten</strong> geeignet und stößt bei mehr auf Granit.</p>



<p>Eine <strong>komplette Adresse</strong>, also eine Straße, PLZ, usw., muss <strong>dann wohl doch mit</strong> einem größeren <strong>Formular </strong>erfasst werden. </p>



<h2 class="wp-block-heading">Kompletter Code – VB.NET InputBox Beispiel</h2>



<p><strong>Für die Schnellen </strong>habe ich <strong>hier </strong>im Sinne von TL;DR (Too Long Didn&#8217;t Read) den <strong>gesamten Code</strong> in einem Stück <strong>zusammengeschrieben</strong>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Public Class Form1

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DoSimpleGreeting()
        WorkWithInteger()
        WorkWithDate()
    End Sub

    Private Sub DoSimpleGreeting()
        Dim content = "We would like to be more personal, so please provide your name so we can talk to you better :)!"
        Dim title = "Please enter your name"
        Dim input = InputBox(content, title)
        Dim cancelled = String.IsNullOrEmpty(input)
        If cancelled Then
            MessageBox.Show("Ok, so you don't want to tell us your name.. :(")
            Return
        End If
        MessageBox.Show($"Nice to meet you, {input}")
    End Sub

    Private Sub WorkWithInteger()
        Dim content = "Please enter your age!"
        Dim title = "Your age?"
        Dim input = InputBox(content, title)
        Dim cancelled = String.IsNullOrEmpty(input)
        If cancelled Then
            MessageBox.Show("We need to know your age to verify you're old enough :/. ")
            Return
        End If
        Dim age = 0
        If Not Integer.TryParse(input, age) Then
            MessageBox.Show($"Hmm, seems like you entered an invalid age")
            Return
        End If
        MessageBox.Show($"Oh, so you're {age} years old")
    End Sub

    Private Sub WorkWithDate()
        Dim content = "Please enter your date of birth!"
        Dim title = "Your birthday?"
        Dim input = InputBox(content, title)
        Dim cancelled = String.IsNullOrEmpty(input)
        If cancelled Then
            MessageBox.Show("We need to know your birthday to verify you're old enough :/. ")
            Return
        End If
        Dim birthday = DateTime.MinValue
        If Not DateTime.TryParse(input, birthday) Then
            MessageBox.Show($"Hmm, seems like you entered an invalid date")
            Return
        End If
        MessageBox.Show($"Oh, so you're born on {birthday:dd.MM.yyyy}")
    End Sub

End Class</pre>



<h2 class="wp-block-heading">Downloads</h2>



<div class="wp-block-buttons is-layout-flex wp-block-buttons-is-layout-flex">
<div class="wp-block-button"><a class="wp-block-button__link" href="https://bit.ly/inputbox-beispiel-download" target="_blank" rel="noreferrer noopener">InputBoxExample.zip</a></div>
</div>
<p>Der Beitrag <a href="https://robbelroot.de/blog/vbnet-inputbox-beispiel/">VB.NET InputBox Beispiel</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://robbelroot.de/blog/vbnet-inputbox-beispiel/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
	</channel>
</rss>
