<?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>tutorial Archive - Robert Skibbe</title>
	<atom:link href="https://robbelroot.de/blog/tag/tutorial/feed/" rel="self" type="application/rss+xml" />
	<link></link>
	<description>alias RobbelRoot – Freelance Full Stack Developer .NET</description>
	<lastBuildDate>Wed, 25 Mar 2026 12:45:11 +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>tutorial Archive - Robert Skibbe</title>
	<link></link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>PHP generate a UUID with ease in 2026</title>
		<link>https://robbelroot.de/blog/making-php-generate-a-uuid-with-ease-heres-how/</link>
					<comments>https://robbelroot.de/blog/making-php-generate-a-uuid-with-ease-heres-how/#comments</comments>
		
		<dc:creator><![CDATA[Robert Skibbe]]></dc:creator>
		<pubDate>Mon, 29 Jan 2024 04:00:00 +0000</pubDate>
				<category><![CDATA[General]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP HowTo's]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[binary]]></category>
		<category><![CDATA[bytes]]></category>
		<category><![CDATA[convert]]></category>
		<category><![CDATA[create]]></category>
		<category><![CDATA[creating]]></category>
		<category><![CDATA[generate]]></category>
		<category><![CDATA[generating]]></category>
		<category><![CDATA[guid]]></category>
		<category><![CDATA[help]]></category>
		<category><![CDATA[hex]]></category>
		<category><![CDATA[id]]></category>
		<category><![CDATA[identifier]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[print]]></category>
		<category><![CDATA[printf]]></category>
		<category><![CDATA[random]]></category>
		<category><![CDATA[server]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[uuid]]></category>
		<category><![CDATA[uuid4]]></category>
		<category><![CDATA[vsprintf]]></category>
		<guid isPermaLink="false">https://robbelroot.de/?p=7715</guid>

					<description><![CDATA[<p>To make PHP generate a UUID, the most basic way would be using the native &#8222;uniqid&#8220; function. However, keep in mind, that this one isn&#8217;t an ID considering the known UUID standards (being UUIDs, versions 1-5). If you want to generate these UUIDs, you should take a well known and &#8230;</p>
<p>Der Beitrag <a href="https://robbelroot.de/blog/making-php-generate-a-uuid-with-ease-heres-how/">PHP generate a UUID with ease in 2026</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/01/HowTo-make-PHP-generate-uuids-tutorial.png"><img fetchpriority="high" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/01/HowTo-make-PHP-generate-uuids-tutorial.png" alt="HowTo make PHP generate uuids tutorial" class="wp-image-7720" title="HowTo make PHP generate uuids tutorial"/></a><figcaption class="wp-element-caption">How to make PHP generate uuids tutorial</figcaption></figure>



<p><strong>To make PHP generate a UUID, the most basic way would be using the native &#8222;uniqid&#8220; function. However, keep in mind, that this one isn&#8217;t an ID considering the known UUID standards (being UUIDs, versions 1-5). If you want to generate these UUIDs, you should take a well known and tested library like ramsey/uuid.</strong></p>






<h2 class="wp-block-heading" id="you-want-to-make-php-generate-uuids-for-you">You want to make PHP generate UUIDs for you!?</h2>



<p>I welcome you to todays post on &#8222;How to make <strong>PHP generate a UUID</strong> for you&#8220;. In this post I will dive into<strong> </strong>the thing called <strong><a href="https://en.wikipedia.org/wiki/Universally_unique_identifier" target="_blank" rel="noreferrer noopener">universally unique identifiers</a></strong>. Those &#8222;special kind&#8220; identifiers are – as the name obviously suggests – for uniquely identifying &#8222;things&#8220;.</p>


<p class="rr-alert info" style="background:#e3f7fc;"><img decoding="async" alt="Info notice" src="https://robbelroot.de/wp-content/themes/pinboard-child/imgs/info.png"><span style="color:black;align-self: center;">If you would like to skip the &#8222;blabla&#8220;, <a href="#php-generate-a-uuid-the-code">jump to the manual creation here</a>. If you would like a short library based example, <a href="#using-ramsey-uuid">click here</a>. I would encourage you though, to study the <a href="#uuid-versions">different versions of UUIDs</a>.</span></p>



<p>There are <strong>different versions of UUIDs</strong>, mainly you can number them from versions 1 to 7. Many projects nowadays require a new way of generating that unique identifier for like your entities. It doesn&#8217;t matter if it&#8217;s stored in a good old<a href="https://www.mysql.com/" target="_blank" rel="noreferrer noopener"><strong>MySQL Database</strong></a>, or in one of the <strong><a href="https://en.wikipedia.org/wiki/Document-oriented_database" target="_blank" rel="noreferrer noopener">document based alternatives</a></strong>.</p>



<p>Using a different approach than those normal, incremental &#8222;1, 2, ..&#8220; ids, has become more and more popular. This doesn&#8217;t only apply for the developers side, customers and other involved parties actually want something else, too. So let&#8217;s get into the background story and later into how we can actually create/generate one.</p>



<h2 class="wp-block-heading" id="the-background-story-php-generate-uuid-s">The background story – PHP generate <strong>UUID</strong>&#8217;s</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/Background-story-PHP-generate-uuids.jpg"><img decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/01/Background-story-PHP-generate-uuids.jpg" alt="Background story - PHP generate uuids" class="wp-image-7743" title="Background story - PHP generate uuids"/></a><figcaption class="wp-element-caption">Background story &#8211; PHP generate UUIDs</figcaption></figure>



<p>Before we actually start thinking about code, we could also <strong>start from a little different angle</strong>. So imagine having a graphical user interface, like most of the time, with some kind of list. You got that usual screen with something like a table, which displays some data about the listed entities.</p>



<p>So far this isn&#8217;t really exciting, I know, but what comes into your mind, when thinking about the first column? The first column inside that table, will usually display the unique identifier for that entity. So basically this will be an incrementing number, which starts at like 1 and is constantly and linearly growing.</p>



<p>Sure, there are some cases where people want to define another starting point like at 5000, but.. Here we are, already facing one of the common problems when using &#8222;normal&#8220; numbers.</p>



<h3 class="wp-block-heading" id="the-thing-with-guessability">The thing with guessability</h3>



<p>No matter, if typical numbers start at 300, or at 300000, you can always kinda guess, what could potentially be the next one. When you have one id, you can increment that by yourself and you could test &#8222;Hmm, what&#8217;s the next one about!?&#8220;. This is literally one thing, where some customers and developers argue about doing it different.</p>



<p>The main reason for the customer would mostly be, to hide actual counts from the end user. End users could guess from order number &#8222;5&#8220;, that this shop maybe just started. This could lead the end user to actually interpret more, maybe negative things into that.</p>



<p>Thoughts like &#8222;Hmm, this shop did just start, maybe it&#8217;s a scam..&#8220; could arise.</p>



<h3 class="wp-block-heading" id="being-able-to-generate-offline">Being able to generate offline</h3>



<p>Usually the approach of working with the database or entities in general, is like the following. You have that basic overview screen, where you click this sweet &#8222;+&#8220;-button and voila. After that, you will have your detail screen displayed, which is ready to get filled out.</p>



<p>You will most likely fill out that form in front of you and then press something like &#8222;confirm&#8220;, or &#8222;save&#8220;. This will call some kind of api in the background, which then creates that new entity for you. During that process, the database will be contacted as central unit, being responsible for generating the new ID.</p>



<p>Now think about what happens, when you can&#8217;t contact the database, if you&#8217;re offline. Well I think you are correct – usually nothing will happen.. At this point, UUIDs come in handy, as they can be generated offline and can still be unique!</p>



<h3 class="wp-block-heading" id="uniqueness-accross-systems-replications">Uniqueness accross systems &amp; replications</h3>



<p>Imagine you have that one online shop, where you already got thousands of orders, bills and more data. Now imagine building up on that one shop, where you want to export and import the data in another system. Well, if you&#8217;re using normal, incremental numbers, you could easily run into some id clashes.</p>



<p>With UUIDs on the other hand, this most likely won&#8217;t be the case (care for the different versions here). By the word &#8222;universally&#8220; of the full name &#8222;universally unique identifier&#8220; this might get clearer. As I already promised, I won&#8217;t go to deep on this aspect of UUID, maybe I&#8217;ll do so in a later post!</p>



<p>You can just keep in mind, that UUIDs will prevent clashing, even generated on different systems.</p>



<h2 class="wp-block-heading">What&#8217;s does the trend say?</h2>



<p>As I mentioned above, the typical UUID spectrum consists of different versions, being 1 &#8211; 5. They all have their different aspects and considerations, you have to be aware of. I would therefore encourage you to do some studies, before actually using, not to mention, even generating them for yourself.</p>



<p>Here&#8217;s a graphic, which could give you a small overview of like which UUID versions are relevant / trending. Usually, this trend counts for like &#8222;in general&#8220;, not only for PHP. Between, this graphic is from today&#8217;s date (25.03.2026) considering data from the last 12 months:</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2024/01/php-uuid-google-trends-legend-2026.png"><img decoding="async" width="1076" height="483" src="https://robbelroot.de/wp-content/uploads/2024/01/php-uuid-google-trends-legend-2026.png" alt="Google Trends – UUID versions v1 to v7 color legend – 2026" class="wp-image-19783"/></a><figcaption class="wp-element-caption">Google Trends – UUID versions v1 to v7 (except 5) color legend – 2026</figcaption></figure>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2024/01/php-uuid-google-trends-v1-v4-v7-2026.png"><img loading="lazy" decoding="async" width="1094" height="584" src="https://robbelroot.de/wp-content/uploads/2024/01/php-uuid-google-trends-v1-v4-v7-2026.png" alt="Google Trends – UUID version interest over time worldwide, past year 2026" class="wp-image-19786"/></a><figcaption class="wp-element-caption">Google Trends comparison of UUID versions v1, v2, v3, v4 &amp; v7 worldwide – last 12 months 2026</figcaption></figure>



<p>Looking at the data, UUID v4 leads with an average interest of 64, closely followed by the newcomer UUID v7 at 52 &#x27a1;&#xfe0f; a clear sign of rapid adoption since its standardization in 2024. UUID v1 and v2 show minimal interest. UUID v5 was excluded from the chart as its search volume is negligible compared to the others.</p>



<h2 class="wp-block-heading" id="uuid-versions">The different versions of UUIDs</h2>



<p>UUIDs (Universally Unique Identifiers) can be generated in different versions, each having a specific purpose. Of course, you need to be sure, that the underlying library (or even yourself) is correctly implementing these aspects. Here are the commonly recognized versions of UUIDs:</p>



<h3 class="wp-block-heading">UUID1 (Time-based)</h3>



<p>This version is generated from the current time and the MAC address of the computer generating the UUID. It is unique within a particular network and time period.</p>



<h3 class="wp-block-heading">UUID2 (DCE Security)</h3>



<p>It&#8217;s pretty similar to UUID version 1, but it has support for <strong><a href="https://de.wikipedia.org/wiki/Distributed_Computing_Environment">DCE security</a></strong>. It&#8217;s not pretty much used from my experience – supported by the shown data from above, as well.</p>



<h3 class="wp-block-heading">UUID3 (Name-based, MD5 hash)</h3>



<p>This UUID version is generated from a namespace identifier and an arbitrary name. It uses MD5 hashing to create a UUID based on the namespace and name.</p>



<h3 class="wp-block-heading">UUID4 (Random)</h3>



<p>This is – in my experience and according to the above data – still the most widely used UUID version. It&#8217;s generated using random or pseudo-random numbers and provides a high probability of uniqueness. However, UUID v7 is catching up fast (see the trend data above).</p>



<h3 class="wp-block-heading">UUID5 (Name-based, SHA-1 hash)</h3>



<p>The version 5 UUID is pretty similar to version 3, but it uses SHA-1 (Secure Hash Algorithm) hashing instead of MD5 (the message digest algorithm).</p>



<p>Note: UUID v5 is not included in the Google Trends comparison above, as its search interest is too low to be meaningfully compared to v1, v4 and v7.</p>



<h3 class="wp-block-heading">UUID7 (Time-ordered, since RFC 9562 — 2024)</h3>



<p>UUID version 7 was officially standardized in May 2024 (RFC 9562) and is gaining rapid adoption. Unlike UUID4, it is time-ordered — meaning UUIDs generated later will always sort higher. This greatly improves database index performance compared to random UUID4.</p>



<p>The ramsey/uuid library supports UUID7 since version 4.7.0:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$uuid7 = Uuid::uuid7();</pre>



<p>If you are starting a new project in 2026, UUID7 is worth considering over UUID4 for database-backed entities.</p>



<h2 class="wp-block-heading" id="disadvantages-of-using-uuids">Disadvantages of using UUIDs</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/Disadvantages-of-using-uuids.jpg"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/01/Disadvantages-of-using-uuids.jpg" alt="Disadvantages of using uuids" class="wp-image-7760" title="Disadvantages of using uuids"/></a><figcaption class="wp-element-caption">Disadvantages of using UUIDs</figcaption></figure>



<p>I think every upside on a specific topic, will mostly have a downside as well, so let&#8217;s talk about that. On the one side, UUIDs will be that kind of added value from the aspects mentioned above. The other side like performance, indexability, etc. counts as well, for sure.</p>



<h3 class="wp-block-heading" id="indexability-and-performance">Indexability and performance</h3>



<p>When using uuids as keys inside your database, theres one big thing which primarily comes into your mind. I&#8217;m talking about the &#8222;indexability&#8220; and performance of actually storing the UUIDs inside your database tables. I can&#8217;t go to deep here, as there are already so many opinions on the web, so I would recommend doing further research.</p>



<p>One of the most reasons I&#8217;ve heard about not using UUIDs is like hard dropping performance when used on multiple joins. You should take a read on the web with search queries like &#8222;index fragmentation&#8220;.</p>



<h3 class="wp-block-heading" id="storage-size">Storage size</h3>



<p>I&#8217;ve seen it many times, where people stored the UUIDs as like &#8222;CHAR(36)&#8220;, means strings consisting of 36 characters. Actually, the UUIDs can be stored as a 128 bit = 16 byte, unsigned number, which is kinda more space optimized. Speaking about storage size in general I think, nowadays storage is much cheaper.</p>



<p>Especially if you can provide a better performance, I would preferably go for that over storage. For sure, this also depends on your use case and more things like budget, etc. Anyways i&#8217;m a big fan of optimization, where it actually makes sense.</p>



<p>If effort doesn&#8217;t outplay the result as hard as I don&#8217;t know, then it would make sense for me, so.. Generally you could say, that an UUID needs 4 times more space than like a 4 byte integer. It&#8217;s also really nice, that MySQL added more support for UUIDs on <strong><a href="https://dev.mysql.com/blog-archive/mysql-8-0-uuid-support/" target="_blank" rel="noreferrer noopener">their version 8</a></strong>.</p>



<h3 class="wp-block-heading" id="readability">Readability</h3>



<p>Another important aspect, which has its downside as well as its upside (as mentioned in &#8222;guessability&#8220;) is readability. While it can be nice for some folks out there, that you can&#8217;t really read those identifiers as easy as simple numbers. Other stress on having non readable identifiers, as like telling your customer on a request: &#8222;Hey, do you have an id for me?&#8220;.</p>



<p>For sure, this wouldn&#8217;t be a good experience, having your customer tell you like 36 characters. Theres another thing coming along with that readability point, especially when talking about URLs. You can tell by just looking at those URLs:</p>



<ul class="wp-block-list">
<li>/api/users/<strong>cc4f998b-29d8-4163-8bbe-2a44a1ccb2ac</strong></li>



<li>/api/users/<strong>55</strong></li>



<li>/api/users/<strong>my_nice_nickname</strong></li>
</ul>



<h2 class="wp-block-heading" id="php-generate-a-uuid-the-code">PHP generate a UUID / GUID – Manually</h2>



<p>After discussing some aspects about using those UUIDs, let&#8217;s now actually talk about generating a UUID with PHP. As I already mentioned, we can&#8217;t dive to deep into the different versions here, so I will focus on <a href="https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_(random)" target="_blank" rel="noreferrer noopener"><strong>UUID version 4</strong></a>. Many online code examples use the <a href="https://www.php.net/manual/en/function.mt-rand.php" target="_blank" rel="noreferrer noopener"><strong>mt_rand PHP function</strong></a>, which is (as it states itself from the docs) not good for generating cryptographic thingys.</p>



<p>After finding different examples, I think this one here is the best one, being a mix of different answers. In the first step we need to choose the &#8222;correct&#8220; function for generating 16 random bytes. Below PHPs major version 7, this is supported by using the &#8222;<a href="https://www.php.net/manual/en/function.openssl-random-pseudo-bytes.php" target="_blank" rel="noreferrer noopener"><strong>openssl_random_pseudo_bytes</strong></a>&#8222;-function.</p>



<p>Starting with major PHP version 7 we can use the &#8222;<strong><a href="https://www.php.net/manual/en/function.random-bytes.php" target="_blank" rel="noreferrer noopener">random_bytes</a></strong>&#8222;-function. With an easy ternary operation, it could look like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">// old version
// $data = PHP_MAJOR_VERSION &lt; 7 ? openssl_random_pseudo_bytes(16) : random_bytes(16);

$data = random_bytes(16); // PHP 7+ / PHP 8.x</pre>



<p>In the <strong>next step</strong> we are <strong>arranging the data</strong>, so that the version of the UUID, etc. actually matches:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$data[6] = chr(ord($data[6]) &amp; 0x0f | 0x40);    // Set version to 0100
$data[8] = chr(ord($data[8]) &amp; 0x3f | 0x80);    // Set bits 6-7 to 10</pre>



<p><strong>In the last step</strong> we are displaying the actual UUID by <strong>returning it as formatted string</strong>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">$uuid = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));</pre>



<p>You <strong>could </strong>also <strong>split up</strong> that <strong>last step into multiple</strong> other <strong>calls</strong>, but I think this doesn&#8217;t really help visibility here.</p>



<p>So the <strong>complete function</strong> could look like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">// old
function get_guid() {
    $data = PHP_MAJOR_VERSION &lt; 7 ? openssl_random_pseudo_bytes(16) : random_bytes(16);
    $data[6] = chr(ord($data[6]) &amp; 0x0f | 0x40);    // Set version to 0100
    $data[8] = chr(ord($data[8]) &amp; 0x3f | 0x80);    // Set bits 6-7 to 10
    return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}

// new
function get_guid(): string {
    $data = random_bytes(16); // requires PHP 7+, works in PHP 8.x
    $data[6] = chr(ord($data[6]) &amp; 0x0f | 0x40);    // Set version to 0100
    $data[8] = chr(ord($data[8]) &amp; 0x3f | 0x80);    // Set bits 6-7 to 10
    return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}</pre>



<p>You could also expand further by going the &#8222;Microsoft way&#8220;, with the &#8222;<strong><a href="https://www.php.net/manual/en/function.com-create-guid.php" target="_blank" rel="noreferrer noopener">com_create_guid</a></strong>&#8222;-function. This function will only (as far as I know) work, if you&#8217;re on a Windows based server. It will generate a UUID with prefixed and appended curly braces, hence the trim statement.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">function get_guid() {
    if (function_exists('com_create_guid') === true)
        return trim(com_create_guid(), '{}');
    $data = PHP_MAJOR_VERSION &lt; 7 ? openssl_random_pseudo_bytes(16) : random_bytes(16);
    $data[6] = chr(ord($data[6]) &amp; 0x0f | 0x40);    // Set version to 0100
    $data[8] = chr(ord($data[8]) &amp; 0x3f | 0x80);    // Set bits 6-7 to 10
    return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}</pre>



<h2 class="wp-block-heading" id="using-ramsey-uuid">Generating a UUID with ramsey/uuid</h2>



<p>Before doing your own experiments and implementing the versions of UUIDs for yourself, it&#8217;s probably better to use an existing library. Usually, I&#8217;m a fan of &#8222;doing things myself&#8220;, but when it comes to security and randomness, I would love to rely on broadly spreaded projects, that have been tested by many persons.</p>



<p>Of course you could have typical problems like the author abandoning the projects, etc. but in the end, you have to decide the common factors like:</p>



<ul class="wp-block-list">
<li>Maintainability</li>



<li>Security</li>



<li>Testing</li>



<li>Time-savings</li>



<li>&#8230;.</li>
</ul>



<p>So let&#8217;s now take a look at an example code from the <strong><a href="https://github.com/ramsey/uuid" target="_blank" rel="noreferrer noopener">ramsey/uuid library</a></strong>, feel free to visit the project on Github.</p>



<h3 class="wp-block-heading">Installing the library</h3>



<p>As with the most of PHP based libraries, we will install this one with composer as well. Open a terminal in your project folder and run the following installation command:</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="">composer require ramsey/uuid</pre>



<h3 class="wp-block-heading">Import and usage</h3>



<p>After you have installed the library, you can now import the corresponding classes. Put import statement on the usual upper part, of your code file. You can then just call the static helper functions, to generate a UUID of your desire.</p>



<p>We will generate a UUID4 in this case:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php
namespace yournamespace;

// here's the important part...
use Ramsey\Uuid\Uuid;

// here's the generation of the UUID4 itself
$myUuid4 = Uuid::uuid4();</pre>



<h3 class="wp-block-heading">Formatting and stuff</h3>



<p>Keep in mind, that the previous example created an instance of the following class:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">Ramsey\Uuid\Rfc4122\UuidV4</pre>



<p>If you want to do some formatting actions, you can use things – <strong><a href="https://uuid.ramsey.dev/en/stable/quickstart.html#install-with-composer" target="_blank" rel="noreferrer noopener">a</a><a href="https://uuid.ramsey.dev/en/stable/quickstart.html#install-with-composer">ccording to the documentation</a></strong> – like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="php" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;?php
namespace yournamespace;

// import
use Ramsey\Uuid\Uuid;

// creation
$uuid = Uuid::uuid4();

// output
printf(
    "UUID: %s\nVersion: %d\n",
    $uuid->toString(),
    $uuid->getFields()->getVersion()
);</pre>



<div class="schema-faq wp-block-yoast-faq-block"><div class="schema-faq-section" id="faq-question-1774430427284"><strong class="schema-faq-question"><strong>What is the easiest way to generate a UUID in PHP?</strong></strong> <p class="schema-faq-answer">Use the ramsey/uuid library: <code>Uuid::uuid4()</code> generates a random UUID v4 in one line. For UUID v7 (time-ordered), use <code>Uuid::uuid7()</code>.</p> </div> <div class="schema-faq-section" id="faq-question-1774430463779"><strong class="schema-faq-question"><strong>Does PHP have a built-in UUID function?</strong></strong> <p class="schema-faq-answer">PHP has <code>uniqid()</code>, but it does not follow UUID standards (RFC 4122/9562). For proper UUIDs, use <code>random_bytes()</code> manually or the ramsey/uuid package.</p> </div> <div class="schema-faq-section" id="faq-question-1774430472195"><strong class="schema-faq-question"><strong>Should I use UUID v4 or UUID v7 in 2026?</strong></strong> <p class="schema-faq-answer">If database performance and sorting matter, use UUID v7 — it&#8217;s time-ordered and avoids index fragmentation. For simple, random IDs without a database, UUID v4 is still fine.</p> </div> </div>



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



<ul class="wp-block-list">
<li><strong><a href="https://robbelroot.de/blog/hacking-a-webshop/">Hacking a webshop</a></strong></li>



<li><strong><a href="https://robbelroot.de/blog/wordpress-loading-scripts-only-on-specific-pages-or-posts/" target="_blank" rel="noreferrer noopener">WordPress loading only page specific assets</a></strong></li>
</ul>
<p>Der Beitrag <a href="https://robbelroot.de/blog/making-php-generate-a-uuid-with-ease-heres-how/">PHP generate a UUID with ease in 2026</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://robbelroot.de/blog/making-php-generate-a-uuid-with-ease-heres-how/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>The WPF ListView Control – the complete Guide in 2026</title>
		<link>https://robbelroot.de/blog/the-wpf-listview-control-the-complete-guide/</link>
					<comments>https://robbelroot.de/blog/the-wpf-listview-control-the-complete-guide/#comments</comments>
		
		<dc:creator><![CDATA[Robert Skibbe]]></dc:creator>
		<pubDate>Sun, 08 Jan 2023 22:20:21 +0000</pubDate>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Visual Basic .NET (EN)]]></category>
		<category><![CDATA[basic]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[control]]></category>
		<category><![CDATA[csharp]]></category>
		<category><![CDATA[foundation]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[item]]></category>
		<category><![CDATA[items]]></category>
		<category><![CDATA[list]]></category>
		<category><![CDATA[listing]]></category>
		<category><![CDATA[listview]]></category>
		<category><![CDATA[mvvm]]></category>
		<category><![CDATA[presentation]]></category>
		<category><![CDATA[repeat]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[vb]]></category>
		<category><![CDATA[vb.net]]></category>
		<category><![CDATA[vbnet]]></category>
		<category><![CDATA[view]]></category>
		<category><![CDATA[visual]]></category>
		<category><![CDATA[visual basic]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[wpf]]></category>
		<guid isPermaLink="false">https://robbelroot.de/?p=14035</guid>

					<description><![CDATA[<p>The WPF ListView – listing items / things In today&#8217;s post, we are going to talk about one of the most basic controls: The WPF ListView. We will take a look at the different use cases of the ListView and we will try out some of the architectural styles. As &#8230;</p>
<p>Der Beitrag <a href="https://robbelroot.de/blog/the-wpf-listview-control-the-complete-guide/">The WPF ListView Control – the complete Guide in 2026</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/2023/01/WPF-ListView-Control-the-complete-Guide-640px.png"><img loading="lazy" decoding="async" width="640" height="360" src="https://robbelroot.de/wp-content/uploads/2023/01/WPF-ListView-Control-the-complete-Guide-640px.png" alt="WPF ListView Control - the complete Guide" class="wp-image-14042" title="WPF ListView Control - the complete Guide"/></a><figcaption class="wp-element-caption">WPF ListView Control &#8211; the complete Guide</figcaption></figure>






<h2 class="wp-block-heading">The WPF ListView – listing items / things</h2>



<p>In today&#8217;s post, we are going to talk about one of the most basic controls: The WPF ListView. We will take a look at the different use cases of the ListView and we will try out some of the architectural styles. As MVVM is like the &#8222;way to go&#8220; in WPF, I will definitely examine this, but we won&#8217;t forget the &#8222;old way&#8220;. Like always, I will provide different source code examples in the two most used .NET languages, being C# and VB.NET.</p>



<p>This guide and all examples are compatible with .NET 8 and .NET 9.</p>



<p>Speaking of languages: Make sure Visual Studio is set up correctly → <strong><a href="https://robbelroot.de/blog/how-to-change-the-visual-studio-installer-language/" type="post" id="16369">Change VS Installer Language</a></strong>.</p>


<p class="rr-alert info" style="background:#e3f7fc;"><img decoding="async" alt="Info notice" src="https://robbelroot.de/wp-content/themes/pinboard-child/imgs/info.png"><span style="color:black;align-self: center;">Need quick help? Jump to the corresponding sections by using the <a href="#toc_container">table of contents</a>. When you are using the <a href="#code-behind-approach">code behind based approach</a>, jump there. If you are more the MVVM guy, you could jump and start with the <a href="#working-with-real-data">&#8222;MVVM approach&#8220; section</a>.</span></p>



<h2 class="wp-block-heading">What is a WPF ListView?</h2>



<p>When stumbling upon this control, the first question coming to your mind could be: &#8222;Well, what is a ListView – in the Windows Presentation Foundation – in the first place?&#8220;. I mean, by the name, you could just assume, that it&#8217;s something for displaying like a &#8222;list&#8220; of something, right? I mean, you are pretty much on the right side, but what makes it different from like a DataGrid, or even a ListBox? Don&#8217;t worry, we will cover this is a few minutes, further down below.</p>



<h3 class="wp-block-heading">The definition, I guess?</h3>



<p>Let&#8217;s first define, what a ListView really is by like definition:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><em>A WPF ListView provides the basic tools needed, to help you displaying a set of data items. This can be easily done in like different views or layouts, depending on your usecase. Each item of those ListViews are displayed, using something called ListViewItem. A ListViewItem iteself  is – more or less – just another template-ish element, being a ContentControl.</em></p>
</blockquote>



<h3 class="wp-block-heading">Examining the ListView</h3>



<p>Let&#8217;s take a quick sneak peek into the inheritance-tree, of the WPF ListView Control, by pressing F12, while being on one of those with your mouse cursor. There you can see, that it actually inherits from the class ListBox.</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2023/01/WPF-ListView-Control-Class-inheriting-from-ListBox.png"><img loading="lazy" decoding="async" width="809" height="363" src="https://robbelroot.de/wp-content/uploads/2023/01/WPF-ListView-Control-Class-inheriting-from-ListBox.png" alt="WPF ListView Control Class - inheriting from ListBox" class="wp-image-14056" title="WPF ListView Control Class - inheriting from ListBox"/></a><figcaption class="wp-element-caption">WPF ListView Control Class &#8211; inheriting from ListBox</figcaption></figure>



<h3 class="wp-block-heading">Inheritance tree</h3>



<p>Going further down, there are some other classes in the inheritance tree as well, like the Selector &amp; the ItemsControl class. I think, for most persons, it&#8217;s actually a surprise / a &#8222;good to know&#8220;-thing, that the ListView actually inherits from the ListBox class.</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2023/01/WPF-ListView-Inheritance-Tree-640px.png"><img loading="lazy" decoding="async" width="640" height="360" src="https://robbelroot.de/wp-content/uploads/2023/01/WPF-ListView-Inheritance-Tree-640px.png" alt="WPF ListView Inheritance Tree" class="wp-image-14088" title="WPF ListView Inheritance Tree"/></a><figcaption class="wp-element-caption">WPF ListView Inheritance Tree</figcaption></figure>



<p>At this point, you can see, that there&#8217;s pretty much going on in the background. The ListView itself is actually derived from a few levels of control hierarchy and is therefore bringing a lot &#8222;to the table&#8220;. For sure, we can&#8217;t go to every level in detail, but I think the most important ones for you are beginning at the ItemsControl.</p>



<p>I mean, in a real application scenario, you would most likely fetch some data like the &#8222;charts&#8220; or something like that. Those could then be displayed inside your ListView by specifying what should actually appear. The chart object could be composed from many different objects and therefore you would need to specify a bit further. </p>



<h2 class="wp-block-heading" id="code-behind-approach">A first example – with XAML &amp; code behind</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2023/01/A-first-WPF-ListView-example-–-hardcoded-in-XAML-640px.png"><img loading="lazy" decoding="async" width="640" height="360" src="https://robbelroot.de/wp-content/uploads/2023/01/A-first-WPF-ListView-example-–-hardcoded-in-XAML-640px.png" alt="A first WPF ListView example – hardcoded in XAML" class="wp-image-14094" title="A first WPF ListView example – hardcoded in XAML"/></a><figcaption class="wp-element-caption">A first WPF ListView example – hardcoded in XAML</figcaption></figure>



<p>Let&#8217;s now focus on creating an actual first example of the ListView Control. To do so, we usually need to first have some kind of data to display. At this point, we will just create some hardcoded items, to see, how the ListView and its ListViewItems are created in the first place. Keep in mind, that we will do this and more in an MVVM manner, later – which is the preferred way for me personally.</p>



<p>We will surround the ListView by a GroupBox Control, to have some kind of labelling, etc. So go ahead and create a GroupBox XAML markup inside your MainWindow.xaml (inside the root Grid). You can write something like &#8222;Music stars&#8220;, etc. inside the Header Property of that GroupBox, like this, we have some kind of heading for our list.</p>



<p>After that, we can add some sort of styling like padding, a width and alignments to that GroupBox, to make it look less bruised. The final, but first testing XAML code could therefore look like this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">    &lt;Grid>
        &lt;GroupBox Header="Some items" Padding="8" Width="200" VerticalAlignment="Center" HorizontalAlignment="Center">
            &lt;ListView>
                &lt;ListView.Items>
                    &lt;ListViewItem Content="Jimmy Hendrix" />
                    &lt;ListViewItem Content="Michael Jackson" />
                    &lt;ListViewItem Content="The Beatles" />
                &lt;/ListView.Items>
            &lt;/ListView>
        &lt;/GroupBox>
    &lt;/Grid></pre>



<p>The visual representation of that XAML code from above, will just look as simple as this.</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2023/01/WPF-ListView-–-Displaying-Music-stars-as-simple-list.png"><img loading="lazy" decoding="async" width="789" height="449" src="https://robbelroot.de/wp-content/uploads/2023/01/WPF-ListView-–-Displaying-Music-stars-as-simple-list.png" alt="ListView – Displaying Music stars as simple list" class="wp-image-14099" title="ListView – Displaying Music stars as simple list"/></a><figcaption class="wp-element-caption">WPF ListView – Displaying Music stars as simple list</figcaption></figure>



<h3 class="wp-block-heading">How to add items to the WPF ListView?</h3>



<p>To add some items to the ListView in a &#8222;code behind&#8220;-manner, we need to specify a usual handler for like the click event, of for example a button. We can then react to that click inside of the handler and actually add a new item to the ListView. Go ahead and add a button inside of your MainWindow (Grid) and specify the &#8222;Click&#8220;-Property.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;Button Click="Button_Click" /></pre>



<p>It will suggest, to create an actual handler, you can confirm this and press F12 while being on the &#8222;Button_Click&#8220; text. After that, it will automatically jump to the corresponding part in your code – the code behind file. Feel free to rename that handler (in XAML &amp; code) to pick a better name, like &#8222;btnAddItem_Click&#8220;.</p>



<p>Inside of the click handler, we need to somehow say: &#8222;Hey, ListView, please remove an item!&#8220;. And we are only able to do so, if we have something to talk to. Currently, the ListView doesn&#8217;t have a name, so you should go back and specify a name for it:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;ListView x:Name="MusicArtistList" ....>
    &lt;!-- .... -->
&lt;/ListView></pre>



<p>The button click handler itself could just look like the following. We are just telling the property of the &#8222;MusicArtistList&#8220;, called &#8222;Items&#8220;: &#8222;Hey, please add another entry to yourself, thanks!&#8220;.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="btn-add-item-code-behind">    private void Button_Click(object sender, RoutedEventArgs e)
    {
        MusicArtistList.Items.Add("Sam Smith");
    }</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="VB.NET" data-enlighter-group="btn-add-item-code-behind">    Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
        MusicArtistList.Items.Add("Sam Smith")
    End Sub</pre>



<h3 class="wp-block-heading">How to remove items from the ListView again?</h3>



<p>If you have followed the above steps to actually add an item inside of our ListView, then you could be ready to remove it again. Keep in mind, that you should have like the click handler ready (by specifying the Buttons click property with like &#8222;btnRemove_Click&#8220;) and that the ListView needs a name. Otherwise, we wouldn&#8217;t be able to target it from the code behind file.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="btn-remove-item-code-behind">    private void btnRemove_Click(object sender, RoutedEventArgs e)
    {
        // this only works that easy due to our current "strings" example
        MusicArtistList.Items.Remove("Sam Smith");
    }</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="VB.NET" data-enlighter-group="btn-remove-item-code-behind">    Private Sub btnRemove_Click(sender As Object, e As RoutedEventArgs)
        ' this only works that easy due to our current "strings" example
        MusicArtistList.Items.Remove("Sam Smith")
    End Sub</pre>



<h3 class="wp-block-heading">How to delete the selected item in the ListView?</h3>



<p>This one here is a bit different, because we can&#8217;t know at compile time, which one of the items, the user will select. We can&#8217;t therefore hardcode like a specific string thing, to be deleted later. We need a possibility, to get the currently selected item and delete it somehow.</p>



<p>This is actually pretty easy, as we get everything we need to know, from the ListView itself. We will first check, if there&#8217;s an actual selection and if so, we will delete that item by providing its index.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="btn-remove-selecteditem-code-behind">    private void btnRemoveSelectedItem_Click(object sender, RoutedEventArgs e)
    {
        bool noItemSelected = MusicArtistList.SelectedIndex == -1;
        if (noItemSelected)
            return;
        MusicArtistList.Items.RemoveAt(MusicArtistList.SelectedIndex);
    }</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="VB.NET" data-enlighter-group="btn-remove-selecteditem-code-behind">Private Sub btnRemoveSelectedItem_Click(sender As Object, e As RoutedEventArgs)
    Dim noItemSelected As Boolean = MusicArtistList.SelectedIndex = -1
    If noItemSelected Then
        Return
    End If
    MusicArtistList.Items.RemoveAt(MusicArtistList.SelectedIndex)
End Sub</pre>



<h3 class="wp-block-heading">Okay, but not flexible, nor practical – imo</h3>



<p>So in the end, we just created a small ListView representation with like 3 entries for our music stars / artists. Currently, this isn&#8217;t really good, as it&#8217;s not dynamic or based on &#8222;real data&#8220;. We just hardcoded those ListViewItems inside the ListView and this isn&#8217;t pretty much practical.</p>



<p>The other problem with our current example is, that it&#8217;s not pretty flexible, currently we are having only like &#8222;1 Column&#8220;. With that, we can only display one &#8222;thing&#8220; per item and we are not really different from a ListBox. What if we wanted to publish some sort of &#8222;year of birth&#8220; information? I mean sure, we could just pump it into one string, but nah, that doesn&#8217;t feel right.</p>



<p>What we really need are some sort of columns and guess what, the ListView supports this out of the box, by just specifying the View property.</p>



<div style="box-sizing:border-box;margin:36px 0;border-radius:12px;overflow:hidden;background:#1a1a2e;position:relative;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;">
  <div style="position:absolute;inset:0;background-image:linear-gradient(rgba(255,255,255,0.03) 1px,transparent 1px),linear-gradient(90deg,rgba(255,255,255,0.03) 1px,transparent 1px);background-size:40px 40px;pointer-events:none;"></div>
  <div style="position:absolute;top:-60px;right:-60px;width:240px;height:240px;background:radial-gradient(circle,rgba(230,126,34,0.18) 0%,transparent 70%);pointer-events:none;"></div>
  <div style="position:relative;padding:32px 28px;">
    <div style="display:inline-block;font-size:11px;font-weight:700;letter-spacing:2.5px;text-transform:uppercase;color:#e67e22;margin-bottom:14px;border:1px solid rgba(230,126,34,0.35);border-radius:4px;padding:3px 10px;">Need a WPF developer?</div>
    <p style="margin:0 0 10px 0;font-size:clamp(17px,4vw,21px);font-weight:700;color:#ffffff;line-height:1.35;">Got a WPF project that needs professional hands?</p>
    <p style="margin:0 0 24px 0;font-size:14px;color:rgba(255,255,255,0.65);line-height:1.7;max-width:540px;">I build WPF applications with clean MVVM architecture in C# and VB.NET — professionally and efficiently. Just send me a message.</p>
    <div style="display:flex;flex-wrap:wrap;gap:12px;align-items:center;">
      <a href="https://robbelroot.de/contact/" style="display:inline-block;padding:12px 24px;background:#e67e22;color:#ffffff;font-size:14px;font-weight:700;text-decoration:none;border-radius:7px;white-space:nowrap;" onmouseover="this.style.background='#cf6d17'" onmouseout="this.style.background='#e67e22'">→ Get in touch</a>
    </div>
  </div>
</div>



<h2 class="wp-block-heading" id="working-with-real-data">Working with &#8222;real data&#8220; – MVVM approach</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2023/01/Working-with-a-databound-ListView-–-dynamic-data-640px.png"><img loading="lazy" decoding="async" width="640" height="360" src="https://robbelroot.de/wp-content/uploads/2023/01/Working-with-a-databound-ListView-–-dynamic-data-640px.png" alt="Working with a databound ListView – dynamic data" class="wp-image-14110" title="Working with a databound ListView – dynamic data"/></a><figcaption class="wp-element-caption">Working with a databound ListView – dynamic data</figcaption></figure>



<p>Moving away from that unpractical non-databased &#8222;stringish&#8220; approach from above, we will now start a newer, clean version of our ListView Control. But first, we need to actually define &#8222;what&#8220; to display, I mean, we need some sort of a so called &#8222;Model&#8220; (or even a ViewModel – but I&#8217;ll leave it at that for now). As we wanted to display some sort of &#8222;music star&#8220;-overview, we could think of a better term in the first place. I will call it list of artists, so let&#8217;s now <strong>create a new class called &#8222;MusicArtist&#8220;</strong> inside a matching folder.</p>



<p>Go ahead and <strong>create the corresponding folder</strong> inside your WPF project, first. We will call that folder like &#8222;<strong>Models</strong>&#8220; – remember, I don&#8217;t want to go too deep into the MVVM cosmos. I would encourage you to <strong><a href="https://robbelroot.de/blog/mvvm-csharp/" target="_blank" rel="noreferrer noopener">refer to my C# MVVM post</a></strong> for that and don&#8217;t be confused, it will for work VB.NET as well. I mean the characteristics of Model-View-ViewModel are in the focus, not the language itself :).</p>



<h3 class="wp-block-heading">The &#8222;MusicArtist&#8220; model class</h3>



<p>Thinking about the class itself and keeping our example from above in mind, we could say, that a music artist has the following properties. For sure, it&#8217;s just for our example and I&#8217;m trying to stay as easy as possible, so feel free to change anything you would like to.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="MusicArtist.cs" data-enlighter-group="artist-class">using System;

namespace WpfListViewExampleCs.Models;

public class MusicArtist
{

    public string FirstName { get; set; }

    public string LastName { get; set; }

    public DateTime Birthday { get; set; }

    public MusicArtist(string firstName, string lastName, DateTime birthday)
    {
        FirstName = firstName;
        LastName = lastName;
        Birthday = birthday;
    }
}</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="MusicArtist.vb" data-enlighter-group="artist-class">Imports System

Namespace WpfListViewExampleCs.Models

    Public Class MusicArtist

        Public Property FirstName As String

        Public Property LastName As String

        Public Property Birthday As DateTime

        Public Sub New(firstName As String, lastName As String, birthday As DateTime)
            Me.FirstName = firstName
            Me.LastName = lastName
            Me.Birthday = birthday
        End Sub

    End Class

End Namespace</pre>



<p>In my example here, the music artist has two properties for the name, meaning a first- and a lastname. I don&#8217;t like the &#8222;name&#8220; only approach, as you can&#8217;t easily separate the names in some situations. Well, I mean yeah, before you mention it: &#8222;The Beatles&#8220; isn&#8217;t really nicely separatable in this manner, but, welcome to the world of software development. I would say, this isn&#8217;t the right moment to argue about this, so feel free – again – to change that as you wish. Additionally I thought about using the birthday property, to show some sort of formatting in the XAML as well.</p>



<h2 class="wp-block-heading">Displaying bound data inside the WPF ListView</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2023/01/Displaying-bound-data-inside-the-WPF-ListView-640px.png"><img loading="lazy" decoding="async" width="640" height="360" src="https://robbelroot.de/wp-content/uploads/2023/01/Displaying-bound-data-inside-the-WPF-ListView-640px.png" alt="Displaying bound data inside the ListView" class="wp-image-14135" title="Displaying bound data inside the ListView"/></a><figcaption class="wp-element-caption">Displaying bound data inside the WPF ListView</figcaption></figure>



<p>After specifying some kind of model to actually display inside the ListView, we now go ahead and tell the ListView about its datasource. The ListView will then know: &#8222;Where can I fetch my data from? What is my data?&#8220;.</p>



<h3 class="wp-block-heading">Preventing a known error</h3>



<p>But before, we need to clean up the old code, otherwise we could run into an error like this:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p><em>System.InvalidOperationException: &#8222;The items collection must be empty before using itemssource&#8220;</em></p>
</blockquote>



<p>To avoid this error, you need to remove the manually added items inside our XAML code. You can&#8217;t like say: &#8222;Hey, fetch the items from this datasource here&#8220; AND put them in manually by XAML. Please go ahead and remove this marked area from the XAML code:</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2023/01/Removing-the-hardcoded-XAML-Items-from-the-ListView.png"><img loading="lazy" decoding="async" width="526" height="201" src="https://robbelroot.de/wp-content/uploads/2023/01/Removing-the-hardcoded-XAML-Items-from-the-ListView.png" alt="Removing the hardcoded XAML Items from the ListView" class="wp-image-14130" title="Removing the hardcoded XAML Items from the ListView"/></a><figcaption class="wp-element-caption">Removing the hardcoded XAML Items from the ListView</figcaption></figure>



<h3 class="wp-block-heading">Giving the ListView its datasource</h3>



<p>In the next step, we will actually care for some items being displayed. For this, we will <strong>use the DataSource property of the ListView</strong> and bind to some property from our ViewModel. Don&#8217;t be scared, if you don&#8217;t know what a ViewModel is! Maybe this is another chance, to refer to <strong><a href="https://robbelroot.de/blog/mvvm-csharp/" target="_blank" rel="noreferrer noopener">my blog post considering the MVVM pattern</a></strong>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;ListView ItemsSource="{Binding MusicArtists}">
&lt;!-- rest of the listview.. -->
&lt;/ListView></pre>



<p>We won&#8217;t create a separate ViewModel right now, for this simple example, we will use the MainWindow itself. This is pretty much easier at this point and it won&#8217;t push you too far away from the actual topic. Never the less, we still need a bindable property inside our MainWindow, which we are going to create, now.</p>



<h3 class="wp-block-heading">Creating a bindable property</h3>



<p>To create the announced property, just build some normal property inside your MainWindow (for example) called &#8222;MusicArtists&#8220;. This could look like the following code, keep in mind, that we should use something which implements the &#8222;INotifyCollectionChanged&#8220; interface. But yes, I hear you, this should be a topic for another blog post, so just take it like that for now.</p>



<p>Next, create that property called &#8222;MusicArtists&#8220; of type of &#8222;ObservableCollection&#8220;, with the generic type argument &#8222;MusicArtist&#8220;. We could also just make it like readonly, but those are fine tuning things, which aren&#8217;t necessary right now.</p>



<p>The last important step is, to say: &#8222;Hey, MainWindow, your DataContext is actually.. you!&#8220;. Then the MainWindow will know where to find the corresponding &#8222;MusicArtists&#8220;, which we are going to bind very soon from the XAML side. Take a look at the following example XAML code:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="MainWindow.xaml.cs" data-enlighter-group="datasource-init">using System;
using System.Collections.ObjectModel;
using System.Windows;
using WpfListViewExampleCs.Models;

namespace WpfListViewExampleCs;

/// &lt;summary>
/// Interaction logic for MainWindow.xaml
/// &lt;/summary>
public partial class MainWindow : Window
{

    public ObservableCollection&lt;MusicArtist> MusicArtists { get; set; }

    public MainWindow()
    {
        InitializeComponent();
        MusicArtists = new ObservableCollection&lt;MusicArtist>();
        DataContext = this;
    }
}</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="MainWindow.xaml.vb" data-enlighter-group="datasource-init">Imports System
Imports System.Collections.ObjectModel
Imports System.Windows
Imports WpfListViewExampleCs.Models

Namespace WpfListViewExampleCs

    Public Partial Class MainWindow
        Inherits Window

        Public Property MusicArtists As ObservableCollection(Of MusicArtist)

        Public Sub New()
            InitializeComponent()
            MusicArtists = New ObservableCollection(Of MusicArtist)()
            DataContext = Me
        End Sub

    End Class

End Namespace</pre>



<h3 class="wp-block-heading">Filling up some data</h3>



<p>Currently, the ListView wouldn&#8217;t be able to display much, because there just aren&#8217;t no items inside the &#8222;MusicArtists&#8220;, yet. So in the next step we are going to fill up some items, to make the ListView be able to display those. This is the reason, why we added a quick constructor inside the &#8222;MusicArtist&#8220; class: Creating quick example artists.</p>



<p>Let&#8217;s re-create the MainWindow constructor for this:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="MainWindow.xaml.cs" data-enlighter-group="filling-data">// rest of the MainWindow code behind file

    public MainWindow()
    {
        InitializeComponent();
        MusicArtists = new ObservableCollection&lt;MusicArtist>();
        MusicArtists.Add(new MusicArtist("Jimmy", "Hendrix", new DateTime(1942, 11, 27)));
        MusicArtists.Add(new MusicArtist("Michael", "Jackson", new DateTime(1958, 8, 29)));
        MusicArtists.Add(new MusicArtist("The", "Beatles", new DateTime(1960, 1, 1)));
        DataContext = this;
    }

// rest of the MainWindow code behind file</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="MainWindow.xaml.vb" data-enlighter-group="filling-data">' rest of the MainWindow code behind file

Public Sub New()
    InitializeComponent()
    MusicArtists = New ObservableCollection(Of MusicArtist)()
    MusicArtists.Add(New MusicArtist("Jimmy", "Hendrix", New DateTime(1942, 11, 27)))
    MusicArtists.Add(New MusicArtist("Michael", "Jackson", New DateTime(1958, 8, 29)))
    MusicArtists.Add(New MusicArtist("The", "Beatles", New DateTime(1960, 1, 1)))
    DataContext = Me
End Sub

' rest of the MainWindow code behind file</pre>



<h3 class="wp-block-heading">But it&#8217;s displaying rubbish now?</h3>



<p>If you are looking at the current results, you would pretty much only see rubbish. If you didn&#8217;t look until now, please watch the following screenshot, then you probably will. The items are more or less being displayed, but there&#8217;s only something like &#8222;Blabla.Bla.MusicArtist&#8220; written.</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2023/01/Bound-ListView-Items-without-Column-Bindings-1.png"><img loading="lazy" decoding="async" width="454" height="178" src="https://robbelroot.de/wp-content/uploads/2023/01/Bound-ListView-Items-without-Column-Bindings-1.png" alt="Bound ListView Items displaying rubbish without Column Bindings" class="wp-image-14159" title="Bound ListView Items displaying rubbish without Column Bindings"/></a><figcaption class="wp-element-caption">Bound ListView Items displaying rubbish without Column Bindings</figcaption></figure>



<p>The reason for that is, that the corresponding ListViewItem actually knows: &#8222;Hey, there is something called MusicArtist&#8220;. But it&#8217;s also asking itself: &#8222;well, how am I supposed to display this? Property A, B, C, WHAT!?&#8220;. In the end it thinks: &#8222;You know what, dev? I&#8217;m just going to display THAT over here&#8220;.</p>



<p>By &#8222;that&#8220; I&#8217;m meaning the complete path to the corresponding class instance type. We are having a class called &#8222;MusicArtist&#8220; inside our project called &#8222;WpfListViewExampleCs&#8220; AND inside the &#8222;Models&#8220; namespace. So this is basically why that &#8222;rubbish&#8220; comes out of it, it displays the complete namespace to class path!</p>



<h3 class="wp-block-heading">Overriding that &#8222;default-rubbish&#8220;</h3>



<p>Before we are going to use the final solution for this, I first want to explain and show you, how you could potentially solve it at this point. Please keep in mind, that this solution depends on your usecase! Usually, if you want to display like &#8222;multiple columns&#8220;, you would actually need – well – multiple columns.</p>



<p>In the image from above, you are seeing the <strong><a href="https://learn.microsoft.com/en-us/dotnet/api/system.object.tostring?view=net-9.0" target="_blank" rel="noreferrer noopener">default object class &#8222;ToString&#8220;</a></strong> output. To change it, you can just override it in your derived class – being the &#8222;MusicArtist&#8220;-class like in a second. Hint: I&#8217;m using the newer, shorter version of the overriding functionality. This means, no body parantheses and an arrow (for C#), as well as string interpolation (for both languages).</p>



<p>You could add this to the &#8222;MusicArtist&#8220; class:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="MusicArtist.cs" data-enlighter-group="override-musicartist-tostring">// rest of the MusicArtist class

public override string ToString()
    => $"{FirstName} {LastName}";

// rest of the MusicArtist class</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="MusicArtist.vb" data-enlighter-group="override-musicartist-tostring">' rest of the MusicArtist class

Public Overrides Function ToString() As String
    Return $"{FirstName} {LastName}"
End Function

' rest of the MusicArtist class</pre>



<p>So the current / new output would look like this:</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2023/01/Bound-ListView-Items-with-overriden-ToString-Function.png"><img loading="lazy" decoding="async" width="354" height="210" src="https://robbelroot.de/wp-content/uploads/2023/01/Bound-ListView-Items-with-overriden-ToString-Function.png" alt="Bound ListView Items with overriden ToString Function" class="wp-image-14168" title="Bound ListView Items with overriden ToString Function"/></a><figcaption class="wp-element-caption">Bound ListView Items with overriden ToString Function</figcaption></figure>



<h2 class="wp-block-heading">Using the ListViews &#8222;View&#8220; Property creating columns</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2023/01/Creating-columns-using-the-ListView-View-Property-640px.png"><img loading="lazy" decoding="async" width="640" height="360" src="https://robbelroot.de/wp-content/uploads/2023/01/Creating-columns-using-the-ListView-View-Property-640px.png" alt="Creating columns using the ListView View Property" class="wp-image-14218" title="Creating columns using the ListView View Property"/></a><figcaption class="wp-element-caption">Creating columns using the ListView View Property</figcaption></figure>



<p>After cleaning up that &#8222;namespace and class&#8220;-mess we saw in the last section, we now can go the last step (I think). Currently, our ListView is only displaying some kind of single dimensioned data, which is not thaaaat bad, but I guess bad for multi-dimensioned data? What if I wanted to have some sort of separated columns for first and last name? This is exactly, where the so called &#8222;View&#8220;-property of the WPF ListView comes into play!</p>



<h3 class="wp-block-heading">But why a ListView, why no DataGrid instead?</h3>



<p>Sure, you could ask yourself: &#8222;Why would I use a ListView, when I could use a DataGrid instead?&#8220;. The answer is: &#8222;It depends&#8220;, yeah, I hate those answers as well.. Usually there&#8217;s one rule floating around in the internet: Use a DataGrid, if you need editing, use a ListView otherwise – bang. I would pretty much love going deeper into this topic, but well, I think this post is already too big, isn&#8217;t it?</p>



<h3 class="wp-block-heading">Specifying a &#8222;View&#8220;-template</h3>



<p>To start creating those columns we talked about, we need to specify some sort of template for the View property. But it&#8217;s not a usual &#8222;DataTemplate&#8220;, it&#8217;s something more special, lemme give you an example. The basic view provided from WPF itself, is something called a &#8222;GridView&#8220;. I mean, the name pretty much suggests what it is. If needed, you could create something similiar for your own needs, but again, I think this would blow up this post even more..</p>



<div style="box-sizing:border-box;margin:36px 0;border-radius:12px;overflow:hidden;background:#1a1a2e;position:relative;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;">
  <div style="position:absolute;inset:0;background-image:linear-gradient(rgba(255,255,255,0.03) 1px,transparent 1px),linear-gradient(90deg,rgba(255,255,255,0.03) 1px,transparent 1px);background-size:40px 40px;pointer-events:none;"></div>
  <div style="position:absolute;top:-60px;right:-60px;width:240px;height:240px;background:radial-gradient(circle,rgba(230,126,34,0.18) 0%,transparent 70%);pointer-events:none;"></div>
  <div style="position:relative;padding:32px 28px;">
    <div style="display:inline-block;font-size:11px;font-weight:700;letter-spacing:2.5px;text-transform:uppercase;color:#e67e22;margin-bottom:14px;border:1px solid rgba(230,126,34,0.35);border-radius:4px;padding:3px 10px;">Need a WPF developer?</div>
    <p style="margin:0 0 10px 0;font-size:clamp(17px,4vw,21px);font-weight:700;color:#ffffff;line-height:1.35;">Got a WPF project that needs professional hands?</p>
    <p style="margin:0 0 24px 0;font-size:14px;color:rgba(255,255,255,0.65);line-height:1.7;max-width:540px;">I build WPF applications with clean MVVM architecture in C# and VB.NET — professionally and efficiently. Just send me a message.</p>
    <div style="display:flex;flex-wrap:wrap;gap:12px;align-items:center;">
      <a href="https://robbelroot.de/contact/" style="display:inline-block;padding:12px 24px;background:#e67e22;color:#ffffff;font-size:14px;font-weight:700;text-decoration:none;border-radius:7px;white-space:nowrap;" onmouseover="this.style.background='#cf6d17'" onmouseout="this.style.background='#e67e22'">→ Get in touch</a>
    </div>
  </div>
</div>



<p>So let&#8217;s stick with the basic GridView for now. Back into your MVVM prepared ListView, we can now provide a GridView template for our ListView. Watch out for that nice string format functionality, where I specified a custom format for that birthday binding. Basically all you have to do is, providing the columns you want, with a header of your choice. Then you need to tell the ListViewItem, where it should pull the data from (being the DisplayMemberPath-Binding).</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">            &lt;ListView ItemsSource="{Binding MusicArtists}">
                &lt;ListView.View>
                    &lt;GridView>
                        &lt;GridView.Columns>
                            &lt;GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding FirstName}" />
                            &lt;GridViewColumn Header="LastName" DisplayMemberBinding="{Binding LastName}" />
                            &lt;GridViewColumn Header="Birthday" DisplayMemberBinding="{Binding Birthday, StringFormat={}{0:dd.MM.yyyy}}" />
                        &lt;/GridView.Columns>
                    &lt;/GridView>
                &lt;/ListView.View>
            &lt;/ListView></pre>



<p>And finally, you end up having this nice thing here:</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2023/01/WPF-ListView-with-specified-View-Property-as-GridView.png"><img loading="lazy" decoding="async" width="505" height="268" src="https://robbelroot.de/wp-content/uploads/2023/01/WPF-ListView-with-specified-View-Property-as-GridView.png" alt="WPF ListView with specified View Property as GridView" class="wp-image-14226" title="WPF ListView with specified View Property as GridView"/></a><figcaption class="wp-element-caption">WPF ListView with specified View Property as GridView</figcaption></figure>



<h3 class="wp-block-heading">Adding, removing, etc. the MVVM way – preparations</h3>



<p>Because it&#8217;s a bit harder, I saved this topic (and other) for the end of this blog post. You actually need some sort of &#8222;ICommand&#8220; implementation, which we won&#8217;t cover here, but I will give you the example code needed. There will be another blog post about those commands in english, currently, it&#8217;s only available in german, sorry.</p>



<h3 class="wp-block-heading">A DelegateCommand base class</h3>



<p>Please go ahead and <strong>create another folder inside your project</strong>, called &#8222;<strong>Utils</strong>&#8222;. We will deposit a small class over there, which helps us with the buttons in MVVM style. The class I&#8217;m talking about looks like this, it&#8217;s a basic implementation of a delegated action (with check) for the <strong><a href="https://learn.microsoft.com/en-US/dotnet/api/system.windows.input.icommand?view=net-9.0" target="_blank" rel="noreferrer noopener">ICommand-interface</a></strong>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="DelegateCommand.cs" data-enlighter-group="delegate-command">using System.Windows.Input;
using System;

namespace Utils
{
    public class DelegateCommand : ICommand
    {
        private Action&lt;object?> _action;

        private Func&lt;object?, bool>? _canExecute;

        public DelegateCommand(Action&lt;object?> action)
        {
            _action = action;
        }

        public DelegateCommand(Action&lt;object?> action, Func&lt;object?, bool> canExecute)
        {
            _action = action;
            _canExecute = canExecute;
        }

        public void Execute(object? parameter)
        {
            _action(parameter);
        }

        public bool CanExecute(object? parameter)
        {
            if (_canExecute == null)
                return true;
            return _canExecute(parameter);
        }

        public void RaiseCanExecuteChanged()
        {
            CanExecuteChanged?.Invoke(this, EventArgs.Empty);
        }

        public event EventHandler? CanExecuteChanged;
    }
}</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="DelegateCommand.vb" data-enlighter-group="delegate-command">Namespace Utils
    Public Class DelegateCommand
        Implements ICommand
        Private _action As Action(Of Object)
        Private _canExecute As Func(Of Object, Boolean)
        Sub New(action As Action(Of Object))
            _action = action
        End Sub
        Sub New(action As Action(Of Object), canExecute As Func(Of Object, Boolean))
            _action = action
            _canExecute = canExecute
        End Sub
        Public Sub Execute(parameter As Object) Implements ICommand.Execute
            ' führt unsere von außen mitgegebene Aktion aus und übergibt auch den Parameter
            _action(parameter)
        End Sub
        Public Function CanExecute(parameter As Object) As Boolean Implements ICommand.CanExecute
            If _canExecute Is Nothing Then
                Return True
            End If
            Return _canExecute(parameter)
        End Function
        Public Event CanExecuteChanged As EventHandler Implements ICommand.CanExecuteChanged
    End Class
End Namespace</pre>



<h3 class="wp-block-heading">A PropertyChangedBase class</h3>



<p>Without going into too much detail, please copy this class and put it into the &#8222;Utils&#8220; folder as well. There is <strong><a href="https://robbelroot.de/blog/der-ultimative-inotifypropertychanged-guide/" target="_blank" rel="noreferrer noopener">a blog post for the INotifyPropertyChanged-interface</a></strong>, but <strong>currently it&#8217;s only in german</strong>. Maybe you could use a browser-translator for now, but I will add the english version soon. So now, please take this class:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="PropertyChangedBase.cs" data-enlighter-group="property-changed-base">using System.Runtime.CompilerServices;
using System.ComponentModel;

namespace Utils;

public abstract class PropertyChangedBase : INotifyPropertyChanged
{
    protected void NotifyOfPropertyChange([CallerMemberName] string? propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler? PropertyChanged;
}</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="PropertyChangedBase.vb" data-enlighter-group="property-changed-base">Imports System.ComponentModel
Imports System.Runtime.CompilerServices

Namespace Utils

    Public Class PropertyChangedBase
        Implements INotifyPropertyChanged

        Protected Sub NotifyOfPropertyChange(&lt;CallerMemberName> Optional propertyName As String = Nothing)
            RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
        End Sub

        Public Event PropertyChanged As PropertyChangedEventHandler Implements INotifyPropertyChanged.PropertyChanged

    End Class

End Namespace</pre>



<h3 class="wp-block-heading">Corresponding buttons</h3>



<p>Please add something like a StackPanel and 3 Buttons inside of it. There will be one button for adding and one for removing an item at a specific position. The third button will be for removing the selected item. At this point, we will also those commands, please have a look at the following XAML. Please pardon me for not making this visually appealing, it&#8217;s only for its functional purpose.</p>



<p>Keep in mind, that you also need to create 3 new properties of type of &#8222;DelegateCommand&#8220;, 1 for each command, being:</p>



<ul class="wp-block-list">
<li>AddItemCommand</li>



<li>RemoveItemCommand</li>



<li>RemoveItemAtIndexCommand</li>
</ul>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">        &lt;GroupBox Header="Music stars" Padding="8" Width="400" VerticalAlignment="Center" HorizontalAlignment="Center">
            &lt;StackPanel>
                &lt;ListView SelectedItem="{Binding SelectedMusicArtist}" ItemsSource="{Binding MusicArtists}">
                    &lt;ListView.View>
                        &lt;GridView>
                            &lt;GridView.Columns>
                                &lt;GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding FirstName}" />
                                &lt;GridViewColumn Header="LastName" DisplayMemberBinding="{Binding LastName}" />
                                &lt;GridViewColumn Header="Birthday" DisplayMemberBinding="{Binding Birthday, StringFormat={}{0:dd.MM.yyyy}}" />
                            &lt;/GridView.Columns>
                        &lt;/GridView>
                    &lt;/ListView.View>
                &lt;/ListView>
                &lt;StackPanel Orientation="Horizontal" Margin="0 12 0 0" HorizontalAlignment="Right">
                    &lt;Button Command="{Binding AddItemCommand}" Content="Add item" Margin="0 0 8 0" />
                    &lt;Button Command="{Binding RemoveItemCommand}" Content="Remove item" Margin="0 0 8 0" />
                    &lt;Button Command="{Binding RemoveItemAtIndexCommand}" Content="Remove item at index" />
                &lt;/StackPanel>
            &lt;/StackPanel>
        &lt;/GroupBox></pre>



<p>The &#8222;new&#8220; UI now looks like this:</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2023/01/WPF-ListView-MVVM-based-UI-1.png"><img loading="lazy" decoding="async" width="477" height="250" src="https://robbelroot.de/wp-content/uploads/2023/01/WPF-ListView-MVVM-based-UI-1.png" alt="MVVM based UI" class="wp-image-14269" title="MVVM based UI"/></a><figcaption class="wp-element-caption">MVVM based UI</figcaption></figure>



<h3 class="wp-block-heading">The MainViewModel</h3>



<p>Now create a new folder called &#8222;ViewModels&#8220; and a &#8222;MainViewModel&#8220; class inside. Let the &#8222;MainViewModel&#8220; class inherit from the &#8222;PropertyChangedBase&#8220; you copied from the previous paragraph. The view (code behind) file should now be abandoned as it&#8217;s getting too much otherwise.</p>



<p>Over here we fully implemented the &#8222;SelectedMusicArtist&#8220;-property to actually tell the &#8222;RemoveItemCommand&#8220;: &#8222;Hey, here was something selected, check if you can be executed now&#8220;. We&#8217;re  subscribing to the &#8222;CollectionChanged&#8220; event of the ObservableCollection as well. This way, we can also refresh the commands state, when the collection is changed.</p>



<p>In the next steps, we are going to instantiate the commands and therefore telling them, what to do and when.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="MainViewModel.cs" data-enlighter-group="the-main-view-model">using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using Utils;
using WpfListViewExampleCs.Models;

namespace WpfListViewExampleCs.ViewModels;

public class MainViewModel : PropertyChangedBase
{

    public ObservableCollection&lt;MusicArtist> MusicArtists { get; set; }

    private MusicArtist? _selectedMusicArtist;

    public MusicArtist? SelectedMusicArtist
    {
        get => _selectedMusicArtist;
        set
        {
            if (_selectedMusicArtist == value)
                return;
            _selectedMusicArtist = value;
            RemoveItemCommand.RaiseCanExecuteChanged();
        }
    }

    public DelegateCommand AddItemCommand { get; set; }

    public DelegateCommand RemoveItemCommand { get; set; }

    public DelegateCommand RemoveItemAtIndexCommand { get; set; }

    public MainViewModel()
    {
        MusicArtists = new ObservableCollection&lt;MusicArtist>();
        MusicArtists.CollectionChanged += MusicArtists_CollectionChanged;
        MusicArtists.Add(new MusicArtist("Jimmy", "Hendrix", new DateTime(1942, 11, 27)));
        MusicArtists.Add(new MusicArtist("Michael", "Jackson", new DateTime(1958, 8, 29)));
        MusicArtists.Add(new MusicArtist("The", "Beatles", new DateTime(1960, 1, 1)));
    }

    private void MusicArtists_CollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
    {
        RemoveItemCommand.RaiseCanExecuteChanged();
        RemoveItemAtIndexCommand.RaiseCanExecuteChanged();
    }

}
</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="MainWindow.xaml.vb" data-enlighter-group="the-main-view-model">Imports System
Imports System.Collections.ObjectModel
Imports System.Collections.Specialized
Imports Utils
Imports WpfListViewExampleCs.Models

Namespace WpfListViewExampleCs.ViewModels

    Public Class MainViewModel
        Inherits PropertyChangedBase

        Public Property MusicArtists As ObservableCollection(Of MusicArtist)

        Private _selectedMusicArtist As MusicArtist?

        Public Property SelectedMusicArtist As MusicArtist?
            Get
                Return _selectedMusicArtist
            End Get
            Set(ByVal value As MusicArtist?)
                If _selectedMusicArtist = value Then Return
                _selectedMusicArtist = value
                RemoveItemCommand.RaiseCanExecuteChanged()
            End Set
        End Property

        Public Property AddItemCommand As DelegateCommand

        Public Property RemoveItemCommand As DelegateCommand

        Public Property RemoveItemAtIndexCommand As DelegateCommand

        Public Sub New()
            MusicArtists = New ObservableCollection(Of MusicArtist)()
            AddHandler MusicArtists.CollectionChanged, AddressOf MusicArtists_CollectionChanged
            MusicArtists.Add(New MusicArtist("Jimmy", "Hendrix", New DateTime(1942, 11, 27)))
            MusicArtists.Add(New MusicArtist("Michael", "Jackson", New DateTime(1958, 8, 29)))
            MusicArtists.Add(New MusicArtist("The", "Beatles", New DateTime(1960, 1, 1)))
        End Sub

        Private Sub MusicArtists_CollectionChanged(ByVal sender As Object?, ByVal e As NotifyCollectionChangedEventArgs)
            RemoveItemCommand.RaiseCanExecuteChanged()
            RemoveItemAtIndexCommand.RaiseCanExecuteChanged()
        End Sub

    End Class

End Namespace</pre>



<h3 class="wp-block-heading">Modern alternative (.NET 6+): CommunityToolkit.Mvvm</h3>



<p>Writing <code>DelegateCommand</code> and <code>PropertyChangedBase</code> manually works perfectly fine &#x1f449; and understanding how they work is exactly why we did it this way. But in 2026, you would typically reach for the <strong><a href="https://learn.microsoft.com/en-us/dotnet/communitytoolkit/mvvm/" target="_blank" rel="noreferrer noopener">CommunityToolkit.Mvvm</a></strong> NuGet package instead.</p>



<p>It ships source generators like <code>[ObservableProperty]</code> and <code>[RelayCommand]</code>, which produce the same boilerplate automatically at compile time, meaning less code to write and maintain.</p>



<p>Compare the two approaches with these small code snippets:</p>



<p>&#x274c; Manual (what we did above)</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">private MusicArtist? _selectedMusicArtist;
public MusicArtist? SelectedMusicArtist {
    get => _selectedMusicArtist;
    set { _selectedMusicArtist = value; NotifyOfPropertyChange(); }
}</pre>



<p>&#x2705; CommunityToolkit.Mvvm</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">[ObservableProperty]
private MusicArtist? _selectedMusicArtist;</pre>



<p>Well worth a look once you are comfortable with the concepts above!</p>



<h3 class="wp-block-heading">Assign a ViewModel to the MainWindow</h3>



<p>To be able to this all work, make sure, you set an appropriate ViewModel for your MainWindow. Otherwise, the binding system wouldn&#8217;t know, where to search for your bindings. This is done by adding this code to your MainWindow XAML file:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;!-- create a namespace mapping at the top of your file -->
xmlns:vm="clr-namespace:WpfListViewExampleCs.ViewModels"

&lt;!-- Set the DataContext MainViewModel instance by XAML -->
&lt;Window.DataContext>
    &lt;vm:MainViewModel />
&lt;/Window.DataContext></pre>



<h3 class="wp-block-heading">How to add WPF ListView items with MVVM</h3>



<p>After you got our little helper class from above inside your &#8222;Utils&#8220; folder and everything else ready, we can now continue. Adding items is now kinda different, because our items are actually bound and not just simple strings. Our commands wants at least to know, what it should do, when executed. We can pass a function, to let the command determine, if it can be executed. I mean, we don&#8217;t want a &#8222;remove selected item&#8220; to happen, when there&#8217;s no item, right?</p>



<p>The following code is needed for the actual functionality: Instantiate the command inside the constructor (don&#8217;t forget to take the command property from above!).</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="MainViewModel.cs" data-enlighter-group="main-view-model-add-item">    // other properties

    public DelegateCommand AddItemCommand { get; set; }

    public MainViewModel()
    {
        AddItemCommand = new DelegateCommand(AddItem);

        // list instantiation etc..
    }

    // the delegate function to be executed on click
    private void AddItem(object? parameter)
    {
        // adding miley cyrus, as we have no dynamic dialog, as it's not a part of this here..
        MusicArtists.Add(new MusicArtist("Miley", "Cyrus", new DateTime(1992, 11, 23)));
    }</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="MainViewModel.vb" data-enlighter-group="main-view-model-add-item">    ' other properties
   
    Public Property AddItemCommand As DelegateCommand

    Sub New()
        AddItemCommand = new DelegateCommand(AddItem);

        ' list instantiation etc..
    End Sub

    Private Sub AddItem(parameter As Object)
        MusicArtists.Add(New MusicArtist("Miley", "Cyrus", New DateTime(1992, 11, 23)))
    End Sub</pre>



<h3 class="wp-block-heading">Removing the selected item MVVM style</h3>



<p>The important thing here is, that we can only remove a selected item, if there is actually one selected. Please check the &#8222;SelectedMusicArtist&#8220;-property from the above MainViewModel for this. If the selected item (therefore the SelectedMusicArtist) changes, we tell the command to refresh.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="MainViewModel.cs" data-enlighter-group="main-view-model-remove-item">    // other properties

    public DelegateCommand RemoveItemCommand { get; set; }

    public MainViewModel()
    {
        RemoveItemCommand = new DelegateCommand(RemoveItem, CanRemoveItem);

        // list instantiation etc..
    }

    private void RemoveItem(object? parameter)
    {
        // we should know that SelectedMusicArtist isn't null, therefore the !
        MusicArtists.Remove(SelectedMusicArtist!);
    }

    private bool CanRemoveItem(object? parameter)
    {
        return SelectedMusicArtist != null;
    }</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="MainViewModel.vb" data-enlighter-group="main-view-model-remove-item">    ' other properties
   
    Public Property RemoveItemCommand As DelegateCommand

    Sub New()
        RemoveItemCommand = new DelegateCommand(RemoveItem, CanRemoveItem);

        ' list instantiation etc..
    End Sub

    Private Sub RemoveItem(parameter As Object)
        MusicArtists.Remove(SelectedMusicArtist)
    End Sub

    Private Function CanRemoveItem(parameter As Object) As Boolean
        Return SelectedMusicArtist IsNot Nothing
    End Function</pre>



<h3 class="wp-block-heading">Deleting an item by index, MVVM like</h3>



<p>In the last example, we do pretty much the same, but with some hardcoded index, just as an example!</p>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="MainViewModel.cs" data-enlighter-group="main-view-model-remove-item-index">    // other properties

    public DelegateCommand RemoveItemAtIndexCommand { get; set; }

    public MainViewModel()
    {
        RemoveItemAtIndexCommand = new DelegateCommand(RemoveItemAtIndex, CanRemoveItemAtIndex);

        // list instantiation etc..
    }

    private void RemoveItemAtIndex(object? parameter)
    {
        // we need at least one item inside
        MusicArtists.RemoveAt(0);
    }

    private bool CanRemoveItemAtIndex(object? parameter)
    {
        return MusicArtists.Count > 0;
    }</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="MainViewModel.vb" data-enlighter-group="main-view-model-remove-item-index">    ' other properties
   
    Public Property RemoveItemAtIndexCommand As DelegateCommand

    Sub New()
        RemoveItemAtIndexCommand = new DelegateCommand(RemoveItemAtIndex, CanRemoveItemAtIndex);

        ' list instantiation etc..
    End Sub

    Private Sub RemoveItemAtIndex(parameter As Object)
        MusicArtists.RemoveAt(0)
    End Sub

    Private Function CanRemoveItemAtIndex(parameter As Object) As Boolean
        MusicArtists.Count > 0
    End Function</pre>



<h2 class="wp-block-heading">Conclusion – WPF ListView Control</h2>



<p>If you want to have a control, which is able to display things in like a list, being able to select one or more items out of it, the ListView is possibly your choice. When thinking about: &#8222;Should I use a ListView or a DataGrid&#8220;, you should choose the ListView, if editing is not a concern. As mostly always, you have the option to go for the code behind, or the MVVM approach, I would personally always choose the MVVM one. It seems much cleaner for my personal preferences and doesn&#8217;t mix up UI with business code. Feel free to leave any comment on how to improve this article, or if I may explained something wrong.</p>



<div style="box-sizing:border-box;margin:36px 0;border-radius:12px;overflow:hidden;background:#1a1a2e;position:relative;font-family:-apple-system,BlinkMacSystemFont,'Segoe UI',Roboto,sans-serif;">
  <div style="position:absolute;inset:0;background-image:linear-gradient(rgba(255,255,255,0.03) 1px,transparent 1px),linear-gradient(90deg,rgba(255,255,255,0.03) 1px,transparent 1px);background-size:40px 40px;pointer-events:none;"></div>
  <div style="position:absolute;top:-60px;right:-60px;width:240px;height:240px;background:radial-gradient(circle,rgba(230,126,34,0.18) 0%,transparent 70%);pointer-events:none;"></div>
  <div style="position:relative;padding:32px 28px;">
    <div style="display:inline-block;font-size:11px;font-weight:700;letter-spacing:2.5px;text-transform:uppercase;color:#e67e22;margin-bottom:14px;border:1px solid rgba(230,126,34,0.35);border-radius:4px;padding:3px 10px;">Need a WPF developer?</div>
    <p style="margin:0 0 10px 0;font-size:clamp(17px,4vw,21px);font-weight:700;color:#ffffff;line-height:1.35;">Got a WPF project that needs professional hands?</p>
    <p style="margin:0 0 24px 0;font-size:14px;color:rgba(255,255,255,0.65);line-height:1.7;max-width:540px;">I build WPF applications with clean MVVM architecture in C# and VB.NET — professionally and efficiently. Just send me a message.</p>
    <div style="display:flex;flex-wrap:wrap;gap:12px;align-items:center;">
      <a href="https://robbelroot.de/contact/" style="display:inline-block;padding:12px 24px;background:#e67e22;color:#ffffff;font-size:14px;font-weight:700;text-decoration:none;border-radius:7px;white-space:nowrap;" onmouseover="this.style.background='#cf6d17'" onmouseout="this.style.background='#e67e22'">→ Get in touch</a>
    </div>
  </div>
</div>



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



<div class="schema-faq wp-block-yoast-faq-block"><div class="schema-faq-section" id="faq-question-1774441748497"><strong class="schema-faq-question">What is the difference between a WPF ListView and a DataGrid?</strong> <p class="schema-faq-answer">A ListView is best for displaying read-only data in list or grid format, while a DataGrid supports inline editing, sorting, and cell-level interaction. Use a ListView when you only need to display and select items, use a DataGrid when users need to edit data directly in the table.</p> </div> <div class="schema-faq-section" id="faq-question-1774441768936"><strong class="schema-faq-question">How do I add columns to a WPF ListView?</strong> <p class="schema-faq-answer">Set the ListView&#8217;s View property to a GridView, then define GridViewColumn elements with Header and DisplayMemberBinding. Each column binds to a property of your data model, allowing multi-column display.</p> </div> <div class="schema-faq-section" id="faq-question-1774441780175"><strong class="schema-faq-question">Can I use the WPF ListView with MVVM and data binding?</strong> <p class="schema-faq-answer">Yes, bind the ItemsSource property to an ObservableCollection in your ViewModel. Use ICommand implementations (like DelegateCommand or RelayCommand) for add/remove operations, and bind SelectedItem for selection tracking.</p> </div> </div>



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



<p>If you need quick and ready to use examples, just go ahead and download the ones you need. Maybe I&#8217;ll add some more later.</p>



<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 wp-element-button" href="https://robbelroot.de/file-download/?dlid=6a6afb78-5d9f-4104-98af-8b46cdb3b68a" target="_blank" rel="noreferrer noopener">WpfListViewExampleCs.zip</a></div>



<div class="wp-block-button"><a class="wp-block-button__link wp-element-button" href="https://robbelroot.de/file-download/?dlid=eae7bc5f-d92c-4b01-b60d-c4e4e362e055" target="_blank" rel="noreferrer noopener">WpfListViewExampleCsMvvm.zip</a></div>
</div>
<p>Der Beitrag <a href="https://robbelroot.de/blog/the-wpf-listview-control-the-complete-guide/">The WPF ListView Control – the complete Guide in 2026</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://robbelroot.de/blog/the-wpf-listview-control-the-complete-guide/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>VB NET Tutorial 2 &#8211; Erste Ausgaben auf der Konsole</title>
		<link>https://robbelroot.de/blog/vb-net-tutorial-2-erste-ausgaben-auf-der-konsole/</link>
					<comments>https://robbelroot.de/blog/vb-net-tutorial-2-erste-ausgaben-auf-der-konsole/#comments</comments>
		
		<dc:creator><![CDATA[Robert Skibbe]]></dc:creator>
		<pubDate>Thu, 03 Feb 2022 22:45:03 +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[ausgabe]]></category>
		<category><![CDATA[console]]></category>
		<category><![CDATA[datentyp]]></category>
		<category><![CDATA[datentypen]]></category>
		<category><![CDATA[erweitert]]></category>
		<category><![CDATA[konsole]]></category>
		<category><![CDATA[output]]></category>
		<category><![CDATA[teil]]></category>
		<category><![CDATA[teil 2]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[tutorial 2]]></category>
		<category><![CDATA[variable]]></category>
		<category><![CDATA[variablen]]></category>
		<category><![CDATA[vb]]></category>
		<category><![CDATA[vb.net]]></category>
		<category><![CDATA[vbnet]]></category>
		<category><![CDATA[visual basic]]></category>
		<category><![CDATA[write]]></category>
		<category><![CDATA[writeline]]></category>
		<guid isPermaLink="false">https://robbelroot.de/?p=8243</guid>

					<description><![CDATA[<p>VB NET Tutorial 2 &#8211; Ausgaben &#38; mehr Datentypen Willkommen im VB NET Tutorial 2, wo wir weiter in die Welt der Programmierung wagen. Nachdem wir uns im letzten Tutorial mit dem Einstieg in die Variablen beschäftigt haben, geht es hier weiter. Eigentlich wollte ich hier auch schon mit den &#8230;</p>
<p>Der Beitrag <a href="https://robbelroot.de/blog/vb-net-tutorial-2-erste-ausgaben-auf-der-konsole/">VB NET Tutorial 2 &#8211; Erste Ausgaben auf der Konsole</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-Tutorial-2-Erweiterte-Variablen-und-Ausgaben.jpg"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-2-Erweiterte-Variablen-und-Ausgaben.jpg" alt="VB NET Tutorial 2 - Erweiterte Variablen und Ausgaben" class="wp-image-8246" title="VB NET Tutorial 2 - Erweiterte Variablen und Ausgaben"/></a><figcaption>VB NET Tutorial 2 &#8211; Erweiterte Variablen und Ausgaben</figcaption></figure>






<h2 class="wp-block-heading" id="vb-net-tutorial-2-ausgaben-mehr-datentypen">VB NET Tutorial 2 &#8211; Ausgaben &amp; mehr Datentypen</h2>



<p>Willkommen im <strong>VB NET Tutorial 2</strong>, wo wir <strong>weiter in </strong>die Welt der <strong>Programmierung </strong>wagen.</p>



<p><strong>Nachdem </strong>wir uns <strong>im letzten Tutorial mit </strong>dem <strong>Einstieg </strong>in die <strong>Variablen </strong>beschäftigt haben, geht es hier weiter.</p>



<p class="info-banner"><strong>Eigentlich wollte </strong>ich <strong>hier </strong>auch schon <strong>mit </strong>den <strong>Variablen weiter machen</strong>, aber ich glaube, das <strong>wäre </strong>dann <strong>zu viel </strong>gewesen. Daher <strong>fokussieren </strong>wir <strong>uns </strong>lieber erstmal <strong>auf </strong>die <strong>Konsole und </strong>die <strong>Verkettung von Strings</strong>. <strong>Dafür müssen </strong>wir natürlich auch noch ein <strong>neues Projekt </strong>aufsetzen, etc.</p>



<style>
.info-banner {
  background: #fff9bb;
  padding: 2em;
  border-radius: 0.5em;
}
</style>



<p><strong>Bisher </strong>haben wir auch <strong>leider </strong>noch <strong>nicht </strong>wirklich was von unseren <strong>Daten gesehen</strong>.</p>



<p>Wir <strong>definierten</strong>, <strong>deklarierten </strong>und <strong>initialisierten</strong>, aber <strong>haben </strong>davon <strong>nicht viel mitbekommen</strong>.</p>



<p>Der <strong>Code </strong>im Visual Studio, bzw. in Programmen <strong>verläuft </strong>generell <strong>linear</strong>.</p>



<p><strong>Zeilenweise wurden </strong>unsere kleinen <strong>Anweisungen </strong>wie z. B. die Folgende <strong>ausgeführt</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="">Dim alter As Integer = 30</pre>



<p>Dies <strong>funktionierte </strong>natürlich auch <strong>im Hintergrund</strong> alles so <strong>wie gewollt</strong>.</p>



<p>Der <strong>Speicher wurde </strong>in der Größe eines &#8222;Integers&#8220; <strong>reserviert und </strong>die <strong>Variable wurde </strong>für uns <strong>ansprechbar</strong>.</p>



<p><strong>Dazu konnten </strong>wir den Namen (alter in diesem Fall) verwenden und die <strong>Variable bei Bedarf ändern</strong>.</p>



<p>Das <strong>ging kinderleicht mit </strong>einer zusätzlichen Zeile, also mit <strong>einer Anweisung </strong>wie Diese hier:</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="">alter = 33</pre>



<p><strong>Dabei </strong>müssen wir <strong>natürlich </strong>auf den <strong>korrekten Datentyp </strong>achten, da es sonst Probleme gibt.</p>



<p>Wir <strong>können </strong>also <strong>keinem String plötzlich</strong> eine <strong>Zahl zuweisen</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="">Dim name = "RobbelRoot"
' geht nicht!!
name = 33</pre>



<h2 class="wp-block-heading" id="wie-du-siehst-siehst-du-nichts-erste-ausgaben">Wie Du siehst, siehst Du nichts – erste Ausgaben</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-2-Erste-Ausgaben-in-der-Konsole.jpg"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-2-Erste-Ausgaben-in-der-Konsole.jpg" alt="VB NET Tutorial 2 - Erste Ausgaben in der Konsole" class="wp-image-8256" title="VB NET Tutorial 2 - Erste Ausgaben in der Konsole"/></a><figcaption>VB NET Tutorial 2 &#8211; Erste Ausgaben in der Konsole</figcaption></figure>



<p><strong>Bisher </strong>war es leider <strong>nicht möglich</strong>, die <strong>Variablen </strong>wirklich <strong>zu verfolgen</strong>.</p>



<p><strong>Wie in </strong>der <strong>Programmierung normal</strong>, <strong>laufen </strong>die <strong>Codezeilen </strong>von <strong>oben nach unten </strong>ab.</p>



<p><strong>Nach </strong>dem die <strong>Ausführung des Programms </strong>beendet ist, <strong>war es </strong>das dann <strong>einfach</strong>.</p>



<p><strong>Visuell bekommen </strong>wir dadurch &#8222;leider&#8220; <strong>kein Feedback </strong>und stehen im Dunkeln.</p>



<p><strong>Später werden </strong>wir <strong>uns </strong>natürlich noch mit dem &#8222;<strong>Anschauen</strong>&#8220; <strong>der Werte </strong>innerhalb von Visual Studio <strong>beschäftigen</strong>.</p>



<p>Dabei <strong>handelt </strong>es sich <strong>um das </strong>sogenannte &#8222;<strong>Debugging</strong>&#8222;, wo man die Werte dann auch <strong>Schritt für Schritt verfolgen</strong> kann.</p>



<h3 class="wp-block-heading" id="die-altbekannte-konsole-konsolenanwendung">Die altbekannte Konsole/Konsolenanwendung</h3>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-2-Konsolen-Beispiel.png"><img loading="lazy" decoding="async" width="699" height="358" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-2-Konsolen-Beispiel.png" alt="VB NET Tutorial 2 - Konsolen-Beispiel" class="wp-image-8270" title="VB NET Tutorial 2 - Konsolen-Beispiel"/></a><figcaption>VB NET Tutorial 2 &#8211; Konsolen-Beispiel</figcaption></figure>



<p>Im <strong>heutigen Tutorial werden </strong>wir diese <strong>visuelle Darstellung </strong>aber <strong>durch die </strong>altbekannte <strong>Konsole realisieren</strong>.</p>



<p><strong>Auch wenn </strong>die <strong>Konsole </strong>im ersten Schritt ein wenig <strong>altbacken erscheint</strong>, <strong>findet </strong>Sie <strong>heutzutage </strong>durchaus <strong>Verwendung</strong>.</p>



<p><strong>Besonders </strong>viele <strong>bekannte Frameworks verwenden </strong>die sogenannten <strong>CLI</strong>&#8211; (Command Line Interface) Anwendungen.</p>



<p>Dort <strong>stellen </strong>Sie eine <strong>Hilfe zur Generierung von Projektvorlagen</strong>, oder ähnlichen Dingen <strong>dar</strong>.</p>



<p>Auch <strong>viele Konfigurationen </strong>im IT-Bereich <strong>werden statt mit </strong>einer &#8222;richtigen&#8220; <strong>GUI</strong>, mit einer Konsolenanwendung durchgeführt.</p>



<p>Sie <strong>sind </strong>also – <strong>auch heute noch </strong>– alles Andere als wegzudenken.</p>



<h3 class="wp-block-heading" id="grafische-oberflachen-noch-nicht">Grafische Oberflächen – noch nicht!</h3>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-2-Noch-keine-grafische-Oberflaeche.jpg"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-2-Noch-keine-grafische-Oberflaeche.jpg" alt="VB NET Tutorial 2 - Noch keine grafische Oberfläche" class="wp-image-8272" title="VB NET Tutorial 2 - Noch keine grafische Oberfläche"/></a><figcaption>VB NET Tutorial 2 &#8211; Noch keine grafische Oberfläche</figcaption></figure>



<p>Ich <strong>habe mich </strong>daher <strong>auch hier </strong>dazu <strong>entschieden</strong>, eine typische <strong>grafische Oberfläche</strong>, <strong>nach hinten </strong>zu schieben.</p>



<p><strong>Auch wenn </strong>ich die sogenannte <strong>GUI</strong> (graphical user interface) <strong>durchaus </strong>auch <strong>für Anfänger </strong>als <strong>spannend </strong>empfinde.</p>



<p><strong>Besonders </strong>das <strong>Zusammenklicken </strong>und das &#8222;<strong>Erfahren am Tun</strong>&#8220; ist dort hiflreich.</p>



<p>Man <strong>klickt</strong> einen <strong>Button und </strong>es <strong>passiert irgendwas</strong>, man <strong>hat Textboxen </strong>und <strong>kann </strong>etwas <strong>eintragen</strong>, etc.</p>



<p><strong>Dennoch kann </strong>es eben auch <strong>zu Verwirrung führen</strong>, da wir <strong>dort </strong>dann von Dingen wie <strong>Steuerelementen</strong>, <strong>Ereignissen</strong>, <strong>etc</strong>. sprechen.</p>



<p>Ich <strong>muss zugeben</strong>, <strong>dass mich </strong>die Vorgänge an der <strong>Konsole </strong>in meiner Zeit als Lernender <strong>genervt hat</strong>.</p>



<p>Man <strong>konnte </strong>eben <strong>nicht viel sehen </strong>und der geschriebene <strong>Code erschien </strong>irgendwie so <strong>unnahbar</strong>.</p>



<p><strong>Lasse Dich </strong>davon aber <strong>nicht abschrecken</strong>, es <strong>wird </strong>Dir <strong>nachher helfen</strong>, alles besser zu verstehen.</p>



<h2 class="wp-block-heading" id="visual-studio-offnen-ein-neues-projekt">Visual Studio öffnen – ein neues Projekt</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-2-Neues-Visual-Studio-Projekt.jpg"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-2-Neues-Visual-Studio-Projekt.jpg" alt="VB NET Tutorial 2 - Neues Visual Studio Projekt" class="wp-image-8275"/></a><figcaption>Neues Visual Studio Projekt anlegen</figcaption></figure>



<p><strong>Nun </strong>ist es <strong>an </strong>der <strong>Zeit </strong>einmal ein <strong>neues Projekt </strong>im Visual Studio <strong>anzulegen</strong>.</p>



<p>Im <strong>vorherigen Tutorial </strong>waren wir ja <strong>eher </strong>mehr <strong>theoretisch </strong>veranlagt, dass wollen wir <strong>nun ändern</strong>.</p>



<p><strong>Öffne </strong>also nun <strong>Visual Studio</strong> und <strong>wähle rechts </strong>den Punkt &#8222;<strong>Neues Projekt erstellen</strong>&#8222;:</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/Visual-Studio-neues-Projekt-anlegen-Startbildschirm.png"><img loading="lazy" decoding="async" width="1029" height="682" src="https://robbelroot.de/wp-content/uploads/2022/02/Visual-Studio-neues-Projekt-anlegen-Startbildschirm.png" alt="Visual Studio neues Projekt anlegen - Startbildschirm" class="wp-image-8277" title="Visual Studio neues Projekt anlegen - Startbildschirm"/></a><figcaption>Visual Studio neues Projekt anlegen &#8211; Startbildschirm</figcaption></figure>



<h3 class="wp-block-heading" id="projekt-typ-wahlen">Projekt-Typ wählen</h3>



<p><strong>Nachdem </strong>Du diesen <strong>Menüpunkt ausgewählt </strong>hast, kommst Du in den <strong>nächsten Bildschirm</strong>.</p>



<p><strong>Dort </strong>kannst Du dann den <strong>Projekt-Typ auswählen</strong>:</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/Visual-Studio-Projekt-anlegen-Nach-Projektvorlage-suchen.png"><img loading="lazy" decoding="async" width="1025" height="680" src="https://robbelroot.de/wp-content/uploads/2022/02/Visual-Studio-Projekt-anlegen-Nach-Projektvorlage-suchen.png" alt="Visual Studio Projekt anlegen - Nach Projektvorlage suchen" class="wp-image-8280" title="Visual Studio Projekt anlegen - Nach Projektvorlage suchen"/></a><figcaption>Visual Studio Projekt anlegen &#8211; Nach Projektvorlage suchen</figcaption></figure>



<p><strong>Wähle </strong>dort den <strong>Projekt-Typ</strong> &#8222;<strong>Konsolenanwendung </strong>(Visual Basic)&#8220;.</p>



<p><strong>Falls </strong>Du diesen Projekt-Typ links <strong>nicht finden </strong>kannst, <strong>verwende </strong>dafür die <strong>Suche </strong>oben rechts.</p>



<h3 class="wp-block-heading" id="namen-fur-projekt-festlegen">Namen für Projekt festlegen</h3>



<p><strong>Sobald </strong>Du den <strong>Projekttyp ausgewählt </strong>hast <strong>und weiter </strong>gegangen bist, kannst Du einen <strong>Namen festlegen</strong>.</p>



<p><strong>Später gehen </strong>wir noch <strong>auf </strong>Punkte wie &#8222;Name der <strong>Projektmappe</strong>&#8220; ein, allerdings <strong>reicht </strong>der <strong>Projektname</strong> aktuell <strong>aus</strong>.</p>



<figure class="wp-block-image size-full is-resized"><a href="https://robbelroot.de/wp-content/uploads/2022/02/Visual-Studio-Projekt-anlegen-Projektname-festlegen.png"><img loading="lazy" decoding="async" src="https://robbelroot.de/wp-content/uploads/2022/02/Visual-Studio-Projekt-anlegen-Projektname-festlegen.png" alt="Visual Studio Projekt anlegen - Projektname festlegen" class="wp-image-8283" width="664" height="394" title="Visual Studio Projekt anlegen - Projektname festlegen"/></a><figcaption>Visual Studio Projekt anlegen &#8211; Projektname festlegen</figcaption></figure>



<p><strong>Beachte </strong>für den Anfang <strong>sonst nur </strong>den Pfad namens &#8222;<strong>Ort</strong>&#8222;, <strong>damit </strong>Du Deine <strong>Projekte </strong>auch <strong>wiederfindest</strong>!</p>



<p><strong>Wähle dann einen </strong>für Dich und für Dein Projekt <strong>sinnvollen Namen </strong>und fahre fort.</p>



<h3 class="wp-block-heading" id="zielframework-wahlen">Zielframework wählen</h3>



<p>Im <strong>nächsten</strong> <strong>Schritt wählen </strong>wir mit unserem aktuellen Stand auch <strong>einfach nur </strong>das <strong>standardmäßig </strong>ausgewählte <strong>Zielframework</strong>.</p>



<p>Mach es Dir also <strong>nicht schwer </strong>und <strong>nutze</strong> das <strong>schon ausgewählte Framework </strong>(in meinem Fall .NET Core 3.1).</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/Visual-Studio-Projekt-anlegen-Zielframework-auswaehlen.png"><img loading="lazy" decoding="async" width="623" height="230" src="https://robbelroot.de/wp-content/uploads/2022/02/Visual-Studio-Projekt-anlegen-Zielframework-auswaehlen.png" alt="Visual Studio Projekt anlegen - Zielframework auswählen" class="wp-image-8286" title="Visual Studio Projekt anlegen - Zielframework auswählen"/></a><figcaption>Visual Studio Projekt anlegen &#8211; Zielframework auswählen</figcaption></figure>



<h3 class="wp-block-heading" id="ein-leeres-read-to-go-projekt">Ein leeres &#8222;Read to go&#8220;-Projekt</h3>



<p><strong>Danach </strong>befinden wir uns <strong>im </strong>Bildschirm des <strong>neu angelegten</strong>, leeren <strong>Projekts</strong>.</p>



<p>Ich <strong>habe Dir </strong>im Bild <strong>markiert</strong>, <strong>worauf </strong>Du in den nächsten Zeilen Deine <strong>Aufmerksamkeit lenken </strong>solltest.</p>



<p><strong>Alles Andere würde </strong>Dich vermutlich nur <strong>ablenken und </strong>erstmal nur <strong>unnötig </strong>verwirren, oder <strong>überfordern</strong>.</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/Visual-Studio-Projekt-anlegen-In-einem-neuen-Konsolenanwendungs-Projekt.png"><img loading="lazy" decoding="async" width="1280" height="850" src="https://robbelroot.de/wp-content/uploads/2022/02/Visual-Studio-Projekt-anlegen-In-einem-neuen-Konsolenanwendungs-Projekt.png" alt="Visual Studio Projekt anlegen - In einem neuen Konsolenanwendungs-Projekt" class="wp-image-8288" title="Visual Studio Projekt anlegen - In einem neuen Konsolenanwendungs-Projekt"/></a></figure>



<p>Visual Studio Projekt anlegen &#8211; In einem neuen Konsolenanwendungs-Projekt</p>



<p><strong>Falls </strong>Du <strong>allerdings doch schon </strong>ein paar <strong>Zusatzinformationen </strong>haben möchtest, okeydokey!</p>



<h3 class="wp-block-heading" id="projektmappen-explorer">Projektmappen-Explorer</h3>



<p><strong>Rechts oben </strong>siehst Du den <strong>Projektmappen-Explorer</strong>, dort können wir in einer &#8222;Mappe&#8220;, unsere <strong>Projekte </strong>organisieren.</p>



<p><strong>Wie das </strong>schon <strong>suggeriert</strong>, <strong>kann </strong>ein &#8222;Projekt&#8220;, bzw. eine <strong>Projektmappe </strong>also <strong>verschiedene Projekte</strong> beinhalten.</p>



<p>Dabei <strong>muss </strong>es sich also <strong>nicht immer nur </strong>um 1 <strong>Projekt </strong>wie die Konsolenanwendung handeln.</p>



<h3 class="wp-block-heading" id="git-anderungen">Git-Änderungen</h3>



<p>Wenn wir <strong>in der Nähe des Explorers</strong> auf das <strong>nächste Tab </strong>gehen, können wir zum Beispiel die <strong>Git-Änderungen</strong> schauen.</p>



<p><strong>Das </strong>ist (für den jetzigen Stand) einfach eine <strong>Möglichkeit</strong>, die <strong>Änderungen </strong>unseres <strong>Quellcodes </strong>sinnvoll zu <strong>verwalten</strong>.</p>



<h3 class="wp-block-heading" id="klassenansicht">Klassenansicht</h3>



<p>Als <strong>nächstes Tab</strong>, <strong>kommt </strong>die <strong>Klassenansicht</strong>, wo wir einfach praktisch eine <strong>andere Ansicht </strong>über die <strong>Klassen-Struktur </strong>bekommen.</p>



<p><strong>Auch das </strong>wird <strong>später </strong>sehr wahrscheinlich <strong>mehr einleuchten </strong>als jetzt!</p>



<h3 class="wp-block-heading" id="eigenschaftsfenster">Eigenschaftsfenster</h3>



<p><strong>Darunter </strong>befindet sich das <strong>Eigenschafts-Fenster</strong>, Welches uns <strong>je nach </strong>vorher ausgewähltem <strong>Element </strong>gewisse <strong>Eigenschaften anzeigt</strong>.</p>



<p><strong>Wähle ich </strong>zum Beispiel das <strong>Projekt </strong>im Explorer <strong>aus </strong>(in unserem Fall VBTutorial 2b), <strong>kann ich </strong>hier <strong>weitere Eigenschaften </strong>dazu <strong>sehen</strong>.</p>



<p>Über die <strong>anderen Bereiche</strong>, sprechen wir dann wirklich <strong>später</strong>!</p>



<h2 class="wp-block-heading" id="das-programm-ausfuhren-vb-net-tutorial-2">Das Programm ausführen – VB NET Tutorial 2</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-2-Programm-ausfuehren.png"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-2-Programm-ausfuehren.png" alt="VB NET Tutorial 2 - Programm ausführen" class="wp-image-8305" title="VB NET Tutorial 2 - Programm ausführen"/></a><figcaption>VB NET Tutorial 2 &#8211; Programm ausführen</figcaption></figure>



<p><strong>Bevor </strong>wir nun <strong>mit </strong>dem <strong>Coden fortfahren</strong>, kannst Du Dir ja schonmal <strong>anschauen</strong>, <strong>wie </strong>man das <strong>Programm ausführt</strong>.</p>



<p>Damit nun ein wenig Action ins Spiel kommt, <strong>klicke</strong> <strong>mit </strong>Deiner <strong>Maus </strong>einfach mal <strong>oben </strong>in der Mitte <strong>auf </strong>den &#8222;<strong>Play</strong>&#8222;-Knopf.</p>



<p><strong>Alternativ </strong>kannst Du auch den <strong>Hotkey </strong>&#8222;F5&#8220; <strong>drücken</strong>, Welchen Du <strong>vermutlich </strong>noch <strong>sehr oft </strong>benötigen wirst.</p>



<p><strong>Präge Dir </strong>den <strong>Hotkey </strong>deswegen am besten auch so schnell es geht <strong>ein</strong>!</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/Visual-Studio-Projekt-bzw-Programm-starten.png"><img loading="lazy" decoding="async" width="649" height="268" src="https://robbelroot.de/wp-content/uploads/2022/02/Visual-Studio-Projekt-bzw-Programm-starten.png" alt="Visual Studio Projekt bzw Programm starten" class="wp-image-8295" title="Visual Studio Projekt bzw Programm starten"/></a><figcaption>Visual Studio Projekt bzw Programm starten</figcaption></figure>



<p><strong>Damit </strong>wird das Programm im &#8222;<strong>Debug</strong>&#8222;-Modus <strong>gestartet und </strong>– ups, <strong>verschwindet </strong>gleich wieder..</p>



<p><strong>Wenn </strong>Deine <strong>Augen schnell </strong>sind, <strong>siehst </strong>Du vermutlich kurz ein <strong>schwarzes Fenster </strong>aufblinken.</p>



<p>Das ist, bzw. <strong>war </strong>dann die <strong>Konsole</strong>, Welche den <strong>Befehl tatsächlich ausgeführt </strong>hatte, eine Zeile auf Ihr auszugeben.</p>



<p><strong>Lass mich </strong>Dir kurz ein wenig den <strong>Ablauf erklären</strong>..</p>



<p><strong>Wenn </strong>ein <strong>Programm gestartet </strong>wird, <strong>wird nach </strong>einem <strong>Einstiegspunkt gesucht</strong>, also wo das Ganze Programm &#8222;los geht&#8220;.</p>



<p>In <strong>unserem Fall </strong>hier ist das die <strong><a href="https://docs.microsoft.com/de-de/dotnet/csharp/methods" target="_blank" rel="noreferrer noopener">&#8222;Main&#8220;-Methode</a></strong>, Welche sich <strong>im </strong>&#8222;<strong>Program</strong>&#8222;-Modul befindet und <strong>nur eine Zeile </strong>enthält.</p>



<p><strong>Dabei </strong>handelt es sich <strong>um </strong>eine <strong><a href="https://docs.microsoft.com/de-de/dotnet/api/system.console.writeline?view=net-6.0" target="_blank" rel="noreferrer noopener">&#8222;WriteLine&#8220;-Anweisung</a></strong>, <strong>Welche </strong>den gewählten <strong>Text </strong>&#8222;Hello World&#8220; auf der Konsole <strong>ausgibt</strong>.</p>



<p><strong>Nachdem </strong>die <strong>Konsole mit </strong>der <strong>Ausgabe fertig </strong>ist, gibt es <strong>keine weiteren Anweisungen </strong>mehr im Programm.</p>



<p><strong>Somit </strong>wurden <strong>alle</strong> <strong>Arbeiten erledigt und </strong>das <strong>Programm</strong>, bzw. die Konsole wird wieder <strong>geschlossen</strong>.</p>



<p><strong>Gleich schauen </strong>wir uns <strong>an</strong>, <strong>wie </strong>man dieses Verhalten <strong>verhindern </strong>kann.</p>



<h2 class="wp-block-heading" id="die-erste-ausgabe-auf-der-konsole-vb-net-tutorial-2">Die erste Ausgabe auf der Konsole – VB NET Tutorial 2</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-2-Erste-Ausgabe.png"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-2-Erste-Ausgabe.png" alt="VB NET Tutorial 2 - Erste Ausgabe" class="wp-image-8319" title="VB NET Tutorial 2 - Erste Ausgabe"/></a><figcaption>VB NET Tutorial 2 &#8211; Erste Ausgabe</figcaption></figure>



<p><strong>Sorgen </strong>wir <strong>nun </strong>im nächsten Schritt einmal <strong>darum</strong>, <strong>dass </strong>das <strong>Programm </strong>sich <strong>nicht </strong>immer sofort <strong>beendet</strong>.</p>



<p><strong>Damit </strong>haben wir dann <strong>endlich </strong>die <strong>Chance</strong>, die <strong>Ausgabe </strong>aktiv <strong>zu verfolgen</strong>, ohne &#8222;The Flash&#8220; sein zu müssen.</p>



<p>Im <strong>Endeffekt </strong>ist das <strong>auch </strong>gar <strong>nicht schwer</strong>, wir <strong>müssen </strong>der <strong>Konsole </strong>nur irgendwie &#8222;Hey, <strong>warte </strong>mal&#8220; <strong>sagen</strong>.</p>



<p><strong>Dafür </strong>haben wir für den Anfang <strong>2 einfache</strong> und sinnvolle <strong>Möglichkeiten</strong>:</p>



<ul class="wp-block-list"><li>Die <strong><a href="https://docs.microsoft.com/de-de/dotnet/api/system.console.readkey?view=net-6.0" target="_blank" rel="noreferrer noopener">&#8222;Console.ReadKey&#8220;-Methode</a></strong></li><li>oder die seitens UI bekanntere <strong><a href="https://docs.microsoft.com/de-de/dotnet/api/system.console.readline?view=net-6.0" target="_blank" rel="noreferrer noopener">&#8222;Console.ReadLine&#8220;-Methode</a></strong></li></ul>



<p><strong>Die </strong>&#8222;<strong>ReadKey</strong>&#8222;-Methode liest – wie der Name vermuten lässt – <strong>einen </strong>bestimmten &#8222;<strong>Key</strong>&#8222;.</p>



<p><strong>Dabei </strong>handelt es sich <strong>um </strong>einen <strong>Tastendruck</strong> = <strong>Tippen </strong>von z. B. &#8222;<strong>b</strong>&#8222;.</p>



<p>Die <strong>Konsole pausiert </strong>nach Start des Befehls <strong>solange</strong>, <strong>bis </strong>es einen gewissen <strong>Input </strong>von uns <strong>bekommt</strong>.</p>



<p>Bei <strong>Variante 1</strong> ist es wie gesagt ein <strong>einziger </strong>&#8222;<strong>Key</strong>&#8220; und <strong>bei </strong>der <strong>nächsten </strong>Variante bis Sie <strong>ein </strong>&#8222;<strong>Enter</strong>&#8220; bekommt.</p>



<h3 class="wp-block-heading" id="code-anpassen-ausgabe">Code anpassen – Ausgabe</h3>



<p><strong>Wenn </strong>Du <strong>nun also </strong>bereits wie oben beschrieben ein <strong>Projekt angelegt </strong>hast, geht&#8217;s nun weiter.</p>



<p>Wir <strong>werden nun eine der </strong>genannten <strong>Methoden verwenden</strong>, um das Beenden der Konsole zu verhindern.</p>



<p><strong>Passe </strong>den <strong>Code </strong>also nun so <strong>an</strong>, dass der &#8222;Console.ReadLine&#8220;-Befehl seinen Platz findet:</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="">Module Program

    Sub Main(args As String())
        Console.WriteLine("Dies ist unsere Ausgabe!")
        Console.ReadLine()
    End Sub

End Module</pre>



<p><strong>Dadurch </strong>dürftest Du nun die <strong>Ausgabe </strong>des Textes &#8222;Dies ist unsere Ausgabe!&#8220; <strong>sehen</strong>:</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/Ausgabe-auf-der-Konsole-und-ReadLine.png"><img loading="lazy" decoding="async" width="488" height="211" src="https://robbelroot.de/wp-content/uploads/2022/02/Ausgabe-auf-der-Konsole-und-ReadLine.png" alt="Ausgabe auf der Konsole und ReadLine" class="wp-image-8321" title="Ausgabe auf der Konsole und ReadLine"/></a><figcaption>Ausgabe auf der Konsole und ReadLine</figcaption></figure>



<h3 class="wp-block-heading" id="programm-schliessen">Programm schließen</h3>



<p>Diese <strong>Ausgabe bleibt </strong>nun solange stehen, <strong>bis </strong>Du das <strong>Programm</strong>:</p>



<ul class="wp-block-list"><li><strong>entweder durch </strong>das &#8222;<strong>X</strong>&#8220; oben rechts</li><li><strong>durch </strong>drücken einer <strong>Eingabe </strong>von z. B. &#8222;abc&#8220; und &#8222;<strong>Enter</strong>&#8220; beendest</li><li><strong>alternativ </strong>kannst Du auch den <strong>Stop-Knopf</strong> <strong>im </strong>Visual <strong>Studio </strong>benutzen</li></ul>



<p><strong>Nun </strong>bist Du natürlich <strong>herzlich eingeladen</strong>, einmal die <strong>Definitionen </strong>von Variablen aus Tutorial 1 <strong>zu wiederholen</strong>.</p>



<p>Diese, oder <strong>neue Definitionen</strong>, bzw. deren dahinterstehende Werte werden wir <strong>gleich einmal ausgeben</strong>.</p>



<p><strong>Am einfachsten </strong>ist es am Anfang <strong>mit Strings</strong>, warum wirst Du gleich noch erfahren.</p>



<h3 class="wp-block-heading" id="strings-definieren-einladung-wiederholung">Strings definieren – Einladung &amp; Wiederholung</h3>



<p><strong>Definiere </strong>daher einfach mal <strong>2-3 Strings</strong> und <strong>dann </strong>sehen wir <strong>weiter</strong>.</p>



<p>Ich <strong>habe </strong>hier <strong>bewusst </strong>nochmal die <strong>3 Möglichkeiten</strong> zur Definition verwendet.</p>



<p><strong>Zur Erinnerung</strong>, die <strong>Möglichkeiten </strong>zur Definition <strong>waren</strong>:</p>



<ul class="wp-block-list"><li><strong>Deklaration </strong>(mit anschließender Zuweisung bei Bedarf)</li><li><strong>Initialisierung</strong></li><li><strong>Initialisierung </strong>mit <strong>Typinferenz</strong></li></ul>



<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="">' Variante 1
Dim vorname As String
vorname = "Robert"

' Variante 2
Dim youtubeKanal As String = "RobbelRoot"

' Variante 3
Dim befehl = "Download"</pre>



<h3 class="wp-block-heading" id="die-definierten-variablen-ausgeben">Die definierten Variablen ausgeben</h3>



<p><strong>Nun können </strong>wir auch einmal die definierten <strong>Variablen</strong>, statt der geschriebenen Texte <strong>ausgeben lassen</strong>.</p>



<p>Dafür können wir der &#8222;Console.WriteLine&#8220;-Methode einfach unsere Variablen übergeben:</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 befehl = "Download"
Console.WriteLine(befehl)
Console.ReadLine()</pre>



<p><strong>Nun dürfest </strong>Du auch die &#8222;befehl&#8220;-Variable <strong>ausgegeben bekommen</strong>.</p>



<p><strong>Schön wäre </strong>es auch, <strong>wenn man </strong>nun so <strong>etwas wie </strong>&#8222;Hey, in der Variable steht: &lt;InhaltDerVariable&gt;&#8220; <strong>ausgeben könnte</strong>.</p>



<p>Das <strong>geht </strong>zum Glück auch <strong>ganz einfach mit </strong>dem <strong>verketten von Strings</strong>.</p>



<p>Dabei <strong>spielt </strong>es <strong>keine Rolle</strong>, <strong>ob </strong>wir diesen <strong>String selbst schreiben</strong>, also im Sinne von &#8222;Das ist ein String&#8220; (mit Anführungszeichen).</p>



<p><strong>Oder ob </strong>wir den String wie oben <strong>vorher in </strong>einer <strong>Variable gespeichert </strong>haben.</p>



<h3 class="wp-block-heading" id="strings-miteinander-verketten">Strings miteinander verketten</h3>



<p><strong>Durch </strong>die <strong>Verkettung </strong>von Strings <strong>können </strong>wir <strong>beliebig viele Strings </strong>aneinander <strong>reihen</strong>.</p>



<p>Das <strong>funktioniert </strong>in Kombination <strong>mit </strong>dem <strong>Verkettungsoperator </strong>– <strong>in Visual Basic</strong> ist das das &#8222;<strong>&amp;</strong>&#8222;-Zeichen.</p>



<p>In <strong>anderen Sprachen </strong>ist es <strong>häufig </strong>das &#8222;<strong>+</strong>&#8222;-Zeichen, <strong>nur </strong>mal so <strong>als Anmerkung</strong>.</p>



<p><strong>Hier </strong>hast Du ein <strong>Beispiel </strong>für die <strong>Ausgabe und </strong>die <strong>Verkettung </strong>von 2 Strings:</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="">Dim befehl = "Download"
Console.WriteLine("Hey, in der Variable steht: " &amp; befehl)</pre>



<p><strong>Dadurch </strong>wird nun <strong>diese Ausgabe </strong>auf der Konsole zustande kommen:</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/Zwei-Strings-verkettet-auf-der-Konsole-ausgeben.png"><img loading="lazy" decoding="async" width="463" height="222" src="https://robbelroot.de/wp-content/uploads/2022/02/Zwei-Strings-verkettet-auf-der-Konsole-ausgeben.png" alt="Zwei Strings verkettet auf der Konsole ausgeben" class="wp-image-8342" title="Zwei Strings verkettet auf der Konsole ausgeben"/></a><figcaption>Zwei Strings verkettet auf der Konsole ausgeben</figcaption></figure>



<p><strong>Beachte </strong>hierbei <strong>wie gesagt</strong>, <strong>dass </strong>es sich <strong>hier </strong>einfach <strong>nur </strong>um <strong>2 Strings</strong> handelt, <strong>Welche </strong>wir <strong>verkettet </strong>haben.</p>



<p><strong>Links </strong>ist ein <strong>String</strong>, <strong>rechts </strong>ist ein <strong>String </strong>– ganz <strong>easy</strong>, Punkt!</p>



<p>Du <strong>könntest </strong>stattdessen <strong>auch 5 Strings verketten</strong>, kein Thema, <strong>wobei </strong>ich hier <strong>sogar </strong>die Variable <strong>mehrfach genutzt </strong>habe:</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 befehl = "Download"
Console.WriteLine("Das ist ein " &amp; befehl &amp; ", plus ein " &amp; befehl &amp; " mit Strings!")</pre>



<p>Diese <strong>Verkettung funktioniert auch </strong>nicht nur <strong>bei </strong>der Ausgabe selbst, wir können die <strong>Variable </strong>auch so neu <strong>zuweisen</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="">Dim befehl = "Download"
befehl = "Dies ist ein " &amp; befehl</pre>



<p><strong>Falls </strong>wir die <strong>String-Variable nur</strong> &#8222;<strong>erweitern</strong>&#8220; möchten, also einen <strong>String anfügen </strong>wollen, <strong>geht </strong>das auch <strong>kürzer</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="">Dim befehl = "Download"
befehl &amp;= " erweitert um das hier!"</pre>



<p><strong>Das </strong>&#8222;und-gleich&#8220; (&amp;=) <strong>könnte man </strong>hier <strong>als </strong>&#8222;<strong>erweitere um</strong>&#8220; <strong>übersetzen</strong>, cool oder!?</p>



<p><strong>Letztendlich </strong>sind also diese <strong>beiden Anweisungen </strong>hier praktisch das <strong>Gleiche</strong>:</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="">befehl = befehl &amp; " erweitert um das hier!"
befehl &amp;= " erweitert um das hier!"</pre>



<p>So, ich <strong>denke das war es </strong>an dieser Stelle <strong>für </strong>das <strong>heutige </strong>Tutorial, <strong>sonst </strong>dürfte es <strong>zu viel</strong> werden.</p>



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



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-1-Fazit.jpg"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-1-Fazit.jpg" alt="VB NET Tutorial 2 - Fazit" class="wp-image-8236"/></a></figure>



<p>Im <strong>heutigen Tutorial </strong>haben wir uns mit der <strong>ersten Ausgabe von Daten </strong>beschäftigt.</p>



<p>Diese <strong>Daten waren </strong>in erster Linie <strong>Strings</strong>, <strong>Welche </strong>wir <strong>als Literale</strong>, <strong>oder </strong>als <strong>Variablen </strong>geschrieben haben.</p>



<p>Zur <strong>Erinnerung</strong>: <strong>Literale sind </strong>einfach <strong>gewisse Schreibweisen</strong>, von Denen man <strong>ableiten </strong>kann, <strong>was</strong> der dahinter stehende <strong>Wert ist</strong>.</p>



<p>Ein <strong>Beispiel wären </strong>die <strong>Anführungszeichen </strong>im Quellcode, <strong>woran </strong>man <strong>erkennt</strong>, <strong>dass </strong>es sich um <strong>einen String </strong>handelt.</p>



<p><strong>Anschließend </strong>habe wir <strong>verschiedene Verkettungsoperationen</strong> durchgeführt, um die <strong>Ausgabe </strong>ein wenig <strong>dynamischer </strong>zu gestalten.</p>



<p><strong>Damit </strong>das Ganze überhaupt <strong>möglich </strong>war, <strong>haben </strong>wir natürlich auch noch ein <strong>neues Visual Studio</strong> <strong>Projekt aufgesetzt</strong>.</p>



<h2 class="wp-block-heading" id="downloads">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/vbnet-tutorial-2-download" target="_blank" rel="noreferrer noopener">VBTutorial2_DE.zip</a></div>
</div>



<h2 class="wp-block-heading" id="weiterfuhrende-links">Weiterführende Links</h2>



<ul class="wp-block-list"><li><a href="https://robbelroot.de/blog/vb-net-tutorial-3-eingaben-entgegennehmen-verarbeiten/" target="_blank" rel="noreferrer noopener"><strong>weiter zu Tutorial 3 &#8211; Eingaben verarbeiten</strong></a></li><li><strong><a href="https://robbelroot.de/blog/vbnet-timer-beispiel-code-im-intervall-ausfuehren/" target="_blank" rel="noreferrer noopener">VB NET Timer Beispiel</a></strong></li><li><strong><a href="https://robbelroot.de/blog/tutorials-vbnet-1-variablen/" target="_blank" rel="noreferrer noopener">zurück zu Tutorial 1 &#8211; Variablen</a></strong></li></ul>
<p>Der Beitrag <a href="https://robbelroot.de/blog/vb-net-tutorial-2-erste-ausgaben-auf-der-konsole/">VB NET Tutorial 2 &#8211; Erste Ausgaben auf der Konsole</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://robbelroot.de/blog/vb-net-tutorial-2-erste-ausgaben-auf-der-konsole/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>VB NET Tutorial 1: Variablen</title>
		<link>https://robbelroot.de/blog/tutorials-vbnet-1-variablen/</link>
					<comments>https://robbelroot.de/blog/tutorials-vbnet-1-variablen/#comments</comments>
		
		<dc:creator><![CDATA[Robert Skibbe]]></dc:creator>
		<pubDate>Tue, 01 Feb 2022 15:00:00 +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[deklaration]]></category>
		<category><![CDATA[deklarieren]]></category>
		<category><![CDATA[grundlage]]></category>
		<category><![CDATA[grundlagen]]></category>
		<category><![CDATA[initialisieren]]></category>
		<category><![CDATA[initialisierung]]></category>
		<category><![CDATA[kurs]]></category>
		<category><![CDATA[lernen]]></category>
		<category><![CDATA[nachhilfe]]></category>
		<category><![CDATA[speichern]]></category>
		<category><![CDATA[teil 1]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[unterricht]]></category>
		<category><![CDATA[variable]]></category>
		<category><![CDATA[variablen]]></category>
		<category><![CDATA[vb]]></category>
		<category><![CDATA[vbnet]]></category>
		<category><![CDATA[visual basic net]]></category>
		<category><![CDATA[webinar]]></category>
		<category><![CDATA[zwischenspeicher]]></category>
		<guid isPermaLink="false">http://robbelroot.de/?p=540</guid>

					<description><![CDATA[<p>Variablen – Die digitalen Schubladen der Programmierung Willkommen zum VB NET Tutorial 1, Deinem ersten Visual Basic NET Tutorial (jedenfalls aus meiner Tutorial-Reihe)! In diesem ersten Beitrag werde ich versuchen, Dir den Einstieg in die Programmierung mit Visual Basic NET zu ebnen. Das erste Visual Basic NET (oder kurz VB.NET) &#8230;</p>
<p>Der Beitrag <a href="https://robbelroot.de/blog/tutorials-vbnet-1-variablen/">VB NET Tutorial 1: Variablen</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-Tutorial-1-Variablen-programmieren-lernen.jpg"><img loading="lazy" decoding="async" width="1280" height="720" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-1-Variablen-programmieren-lernen.jpg" alt="VB NET Tutorial 1 - Variablen - programmieren lernen" class="wp-image-8034" title="VB NET Tutorial 1 - Variablen - programmieren lernen"/></a><figcaption class="wp-element-caption">VB NET Tutorial 1 – Variablen</figcaption></figure>






<h2 class="wp-block-heading" id="variablen-die-digitalen-schubladen-der-programmierung">Variablen – Die digitalen Schubladen der Programmierung</h2>



<p>Willkommen zum <strong>VB NET Tutorial 1</strong>, Deinem <strong>ersten Visual Basic NET Tutorial</strong> (jedenfalls aus meiner Tutorial-Reihe)! In diesem ersten Beitrag werde ich versuchen, Dir den Einstieg in die Programmierung mit Visual Basic NET zu ebnen. Das erste Visual Basic NET (oder kurz VB.NET) Tutorial wird sich um das wohl gängigste Anfänger-Thema drehen.</p>



<p class="info-banner">Stelle sicher, <strong>dass </strong>Du die <strong><a href="https://robbelroot.de/preparations-first-steps/">Vorbereitungen und ersten Schritte</a></strong> <strong>abgeschlossen </strong>hast, bevor Du durchstartest! <strong>Ich werde </strong>vermutlich außerdem <strong>an jedem</strong> Tutorial-<strong>Ende </strong>ein <strong>downloadbares Beispielprojekt </strong>bereitstellen. Das <strong>kannst </strong>Du <strong>ganz einfach</strong> mit Visual Studio <strong>öffnen </strong>und <strong>brauchst </strong>zum Probieren <strong>nicht alles abtippen</strong>. <strong>Öffne </strong>ein beliebiges <strong>Projekt</strong>, <strong>indem Du </strong>die jeweilige <strong>&#8222;.sln&#8220;-Datei</strong> innerhalb des Download-Ordners <strong>öffnest</strong>. Das jeweilige <strong>Programm </strong>kannst Du <strong>mit </strong>&#8222;<strong>F5</strong>&#8222;, <strong>oder </strong>dem &#8222;Play&#8220;-<strong>Knopf </strong>starten. <strong>Beachte</strong>, <strong>dass </strong>es sich besonders <strong>bei </strong>den <strong>ersten Programmen meistens </strong>um <strong>Konsolenanwendungen </strong>handeln wird. Diese <strong>werden dadurch ausgezeichnet</strong>, dass Sie <strong>meist ohne </strong>die typische <strong>grafische Oberfläche </strong>auskommen. <strong>Ebenso werden </strong>Sie <strong>beendet</strong>, <strong>sobald </strong>Sie <strong>alle Codezeilen </strong>linear <strong>ausgeführt </strong>haben. <strong>Daher </strong>wirst Du <strong>eventuell nicht unbedingt </strong>etwas in der Ausgabe im Fenster <strong>sehen </strong>können.</p>



<style>
.info-banner {
  background: #fff9bb;
  padding: 2em;
  border-radius: 0.5em;
}
</style>



<h2 class="wp-block-heading" id="das-vb-net-tutorial-1-im-videoformat-auf-youtube">Das VB NET Tutorial 1 im Videoformat auf YouTube</h2>



<p><strong>Natürlich </strong>hast Du <strong>auch </strong>die <strong>Möglichkeit</strong>, dieses <strong>Tutorial auf YouTube anzusehen</strong>.</p>



<p>Ich <strong>denke</strong>, <strong>dass </strong>ich <strong>bald </strong>eventuell mal <strong>eine Neuauflage </strong>des Videos <strong>machen </strong>werde.</p>



<p>Der <strong>wesentlich überarbeitete Inhalt</strong> hier, <strong>stimmt </strong>leider <strong>nicht mehr mit</strong> dem <strong>Video </strong>überein.</p>



<p><strong>Mittlerweile </strong>ist das Video immerhin ein bisschen <strong>in die Jahre gekommen</strong>, aber natürlich <strong>trotzdem </strong>noch <strong>informativ</strong>.</p>



<p><strong>Bis </strong>ich das <strong>Video </strong>allerdings <strong>erneuert </strong>habe, <strong>würde </strong>ich Dir diesen <strong>Beitrag </strong>hier eher <strong>ans Herz legen</strong>!</p>


<div class="async-youtube" data-embed="Ex3zaPBNnf4" data-alt="">
    <div class="play-button"></div>      
  </div>



<h2 class="wp-block-heading" id="vb-net-tutorial-1-variablen-ein-alter-bekannter-aus-dem-matheunterricht-oder">VB NET Tutorial 1: Variablen – Ein alter Bekannter aus dem Matheunterricht, oder!?</h2>



<p>Im <strong>ersten Teil</strong> der <strong>Visual Basic NET Tutorial-Reihe</strong>, <strong>lernst</strong> Du die Verwendung von <strong>Variablen kennen</strong>.</p>



<p><strong>Variablen an sich</strong> werden Dir <strong>vermutlich </strong>bereits ein <strong>Begriff sein</strong>, da Sie schon <strong>in </strong>der <strong>Schulzeit </strong>vorkamen.</p>



<p>Auch <strong>deren Verwendung in</strong> der <strong>Programmierung funktioniert analog </strong>zu Jenen aus dem Matheunterricht.</p>



<p><strong>Letztendlich dienen </strong>Sie <strong>als Ablage</strong> <strong>für </strong>einen gewissen <strong>Wert</strong>, Welcher dynamisch verändert werden kann.</p>



<p><strong>In </strong>der <strong>Programmierung </strong>verwendet man Variablen, <strong>um </strong>darin <strong>Daten zu speichern</strong>.</p>



<p><strong>Variablen können </strong>&#8222;<strong>statisch</strong>&#8220; gesehen, wie &#8222;x = 5&#8220; <strong>definiert </strong>werden, <strong>oder </strong>z. B. das <strong>Ergebnis gewisser Prozesse </strong>sein.</p>



<p>Dabei <strong>spreche </strong>ich <strong>von </strong>dem <strong>Wert</strong>, <strong>der </strong>letztendlich &#8222;<strong>hinter</strong>&#8220; der <strong>Variable steckt</strong>.</p>



<h2 class="wp-block-heading" id="unterschiedliche-wert-arten-sogenannte-datentypen">Unterschiedliche Wert-Arten &#8211; sogenannte Datentypen</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-1-Datentypen-von-Variablen.jpg"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-1-Datentypen-von-Variablen.jpg" alt="VB NET Tutorial 1 Datentypen von Variablen" class="wp-image-8082" title="VB NET Tutorial 1 Datentypen von Variablen"/></a><figcaption class="wp-element-caption">VB NET Tutorial 1 Datentypen von Variablen</figcaption></figure>



<p><strong>Wie </strong>Du Dir <strong>sicherlich vorstellen kannst</strong>, wäre es ziemlich <strong>schwer</strong>, in der <strong>Programmierung </strong>nur <strong>mit Zahlen auszukommen</strong>.</p>



<p>Daher <strong>können Variablen </strong>glücklicherweise ganz <strong>viele </strong>verschiedene <strong>Werte</strong>, <strong>speichern</strong>.</p>



<p>Diese <strong>Werte können von</strong> sehr <strong>unterschiedlicher Natur</strong>, bzw. sogenannten Datentypen <strong>sein</strong>.</p>



<p><strong>Seien </strong>es <strong>beispielsweise Zahlen</strong>, ein <strong>Geburtsdatum</strong>, einzelne <strong>Zeichen</strong>, <strong>oder </strong>gar <strong>ganze Texte</strong>.</p>



<p>Wäre <strong>blöd</strong>, <strong>wenn </strong>wir dem <strong>Nutzer </strong>sonst<strong> sagen müssten</strong>:</p>



<blockquote class="wp-block-quote is-layout-flow wp-block-quote-is-layout-flow">
<p>„Nein Herr/Frau Mustermann, Sie können keine Zahlen eingeben, unser Programm unterstützt nur Wörter!</p>
</blockquote>



<h3 class="wp-block-heading" id="ware-ja-sonst-langweilig">Wäre ja sonst langweilig..</h3>



<p>Du <strong>kannst </strong>Dir die <strong>dementsprechenden Blicke </strong>der Kunden mit Sicherheit <strong>vorstellen</strong>.</p>



<p><strong>Letztendlich zurecht</strong>, denn <strong>obige Situation</strong> wäre vermutlich <strong>ein Armutszeugnis</strong> an Programm.</p>



<p>So <strong>wäre </strong>ein <strong>CRM</strong>-Programm <strong>und </strong>dessen <strong>Funktionen </strong>natürlich <strong>unmöglich </strong>vorstellbar.</p>



<p><strong>Wobei </strong>die <strong>Rechnungsstellung </strong>so <strong>vermutlich Spaß </strong>machen würde – also so <strong>nur mit Zahlen</strong> &#x1f61b;!</p>



<p>Eine <strong>Variable ist</strong> somit also <strong>ein Hilfsmittel</strong>, Welches uns erlaubt, <strong>mit Daten zu arbeiten</strong>.</p>



<p><strong>Um </strong>mit <strong>Variablen </strong>zu <strong>arbeiten</strong>, <strong>können </strong>wir Diese <strong>mit </strong>einer Art &#8222;<strong>Namen</strong>&#8220; <strong>ansprechen</strong>.</p>



<p>Man <strong>muss natürlich </strong>auch irgendwie sagen können: &#8222;Hey, <strong>du bekommst </strong>den <strong>Wert</strong>..&#8220;, oder &#8222;<strong>Gib </strong>mir mal bitte <strong>deinen Wert</strong>..&#8220;.</p>



<p>Aber <strong>genug davon</strong>, gehen wir über zum <strong>nächsten Punkt</strong>: Konkrete Beispiele.</p>



<h2 class="wp-block-heading" id="vb-net-variablen-let-s-go">VB NET Variablen &#8211; Let&#8217;s Go!</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-1-Variablen-definieren.jpg"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-1-Variablen-definieren.jpg" alt="VB NET Tutorial 1 - Variablen definieren" class="wp-image-8120" title="VB NET Tutorial 1 - Variablen definieren"/></a><figcaption class="wp-element-caption">VB NET Tutorial 1 &#8211; Variablen definieren</figcaption></figure>



<p><strong>Nach außen wirken </strong>die <strong>Zuweisungen </strong>von Variablen also <strong>erstmal </strong>alle <strong>gleich</strong>.</p>



<p><strong>Schließlich sagt </strong>man <strong>meistens </strong>einfach nur, <strong>dass ein </strong>irgendwie benanntes &#8222;<strong>Ding</strong>&#8222;, einen gewissen <strong>Wert bekommt</strong>.</p>



<p><strong>Wie oben </strong>schon <strong>erwähnt</strong>, <strong>kann </strong>es sich dabei z. B. um die <strong>typischen Zahlen </strong>aus dem Matheunterricht handeln.</p>



<p><strong>Für </strong>viele <strong>Anfänger wird </strong>es jedoch <strong>neu sein</strong>, dass <strong>Variablen auch </strong>&#8222;<strong>komplexe Daten&#8220; </strong>speichern können.</p>



<p><strong>Dabei handelt es </strong>sich eventuell <strong>um </strong>so etwas wie eine <strong>Lieferung</strong>, eine <strong>Rechnung</strong>, oder gar einen <strong>Kunden</strong>.</p>



<p><strong>Natürlich setzen </strong>sich diese sogenannten <strong>Objekte </strong>auch wiederum <strong>auch aus </strong>den bekannten <strong>simplen Werten </strong>wie Zeichen, etc. <strong>zusammen</strong>.</p>



<p><strong>Darüber </strong>werden wir <strong>allerdings </strong>erst in einem <strong>späteren </strong>Teil dieser Tutorial-Reihe sprechen.</p>



<h3 class="wp-block-heading" id="konkrete-beispiele">Konkrete Beispiele</h3>



<p><strong>Nehmen wir </strong>als erstes Beispiel <strong>den </strong>in Deutschland wohl <strong>bekanntesten Namen</strong>, also einen Text, wie z. B. <strong>&#8222;Max Mustermann&#8220;</strong>.</p>



<p><strong>Okay</strong>, zugegebenermaßen &#8222;<strong>Müller</strong>&#8222;, etc. wäre <strong>auch passend gewesen</strong>, aber <strong>bleiben </strong>wir einmal <strong>bei </strong>Herr <strong>Mustermann</strong>.</p>



<p>In der Programmierung nennt man diese <strong>Zeichenfolge</strong> – also eine tatsächliche Abfolge von Zeichen – einen <strong>String</strong>.</p>



<p>Als <strong>nächstes Beispiel </strong>könnten wir <strong>Jahreszahlen</strong>, <strong>wie </strong>z. B. <strong>2020</strong>, <strong>2021</strong>, oder <strong>2022 </strong>nehmen.</p>



<p>Es gibt neben den stinknormalen Ganzzahlen noch weitere Arten, ich <strong>würde </strong>Dich <strong>zu diesem Zeitpunkt aber ungern </strong>überfordern.</p>



<p>Wir <strong>belassen es daher </strong>an dieser Stelle einfach <strong>dabei</strong>, <strong>dass </strong>es sich hier um <strong>einen </strong>&#8222;<strong><a href="https://de.wikipedia.org/wiki/Integer" target="_blank" rel="noreferrer noopener">Integer</a></strong>&#8220; handelt.</p>



<p>Eine <strong>kleine Zusatzinformation </strong>wäre noch, dass man natürlich <strong>auch negative Zahlen wie &#8222;-5</strong>&#8220; verwenden könnte.</p>



<h3 class="wp-block-heading" id="der-erste-codeder-quellcode-zur-festlegung-zweier-variablen-mit-den-oben-genannten-werten-wurde-so-aussehen">Der erste Code – VB NET Tutorial 1</h3>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-1-Der-erste-Code.jpg"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-1-Der-erste-Code.jpg" alt="VB NET Tutorial 1 - Der erste Code" class="wp-image-8239" title="VB NET Tutorial 1 - Der erste Code"/></a><figcaption class="wp-element-caption">VB NET Tutorial 1 &#8211; Der erste Code</figcaption></figure>



<p><strong>Kommen </strong>wir <strong>nun zu </strong>unseren <strong>ersten Codezeilen</strong>, wo wir <strong>einfach </strong>mal <strong>2 </strong>unterschiedliche <strong>Variablen definieren</strong>.</p>



<p>Der <strong>Quellcode zur Festlegung</strong> zweier Variablen <strong>mit </strong>den <strong>oben genannten Werten</strong>, würde so aussehen:</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 vollerName As String = "Max Mustermann"
Dim jahresZahl As Integer = 2022</pre>



<p>Ich <strong>werde auch </strong>gleich <strong>noch auf </strong>die <strong>unterschiedlichen Methoden zur Definition</strong> eingehen.</p>



<p><strong>Zuerst schauen </strong>wir uns einmal an, <strong>was </strong>der <strong>Code </strong>im Detail macht und was dort für &#8222;Schlüsselworte&#8220; sind.</p>



<p>Grundsätzlich könnte man den <strong>obigen Code </strong>erstmal <strong>als folgendes</strong> <strong>Muster </strong>beschreiben:</p>



<ul class="wp-block-list">
<li><strong>Deklaration</strong></li>



<li><strong>Dimensionierung</strong></li>



<li><strong>Wertzuweisung</strong></li>
</ul>



<p><strong>Wenn </strong>man sich eine <strong>obige Zeile </strong>ansieht, kann es dort vor allem <strong>als Anfänger </strong>schnell <strong>zur Verwirrung </strong>kommen.</p>



<h3 class="wp-block-heading" id="lass-dich-nicht-verwirren">Lass Dich nicht verwirren</h3>



<p><strong>Die Reihenfolge </strong>die ich <strong>beschrieben </strong>habe, <strong>stimmt </strong>ggf. <strong>nicht auf </strong>den <strong>ersten Blick</strong> mit dem Code überein.</p>



<p>Man <strong>könnten </strong>ja <strong>meinen</strong>, <strong>dass </strong>das Schlüsselwort &#8222;<strong>Dim</strong>&#8220; <strong>für </strong>die <strong>Dimensionierung </strong>zuständig ist.</p>



<p><strong>Würde </strong>ja zumindest allgemein <strong>sprach-technisch Sinn ergeben</strong> und so war es auch mal.</p>



<p>Zumindest <strong>galt </strong>das <strong>damals </strong>in VB6 <strong>für </strong>&#8222;<strong>Arrays</strong>&#8220; (deutsch auch Felder/Datenfelder genannt).</p>



<p><strong>Diese müssen Dir </strong>aber natürlich zum jetzigen Zeitpunkt <strong>noch nichts sagen</strong>, daher <strong>legen </strong>wir Sie <strong>erstmal beiseite</strong>.</p>



<p>Um das mit dem &#8222;Dim&#8220; <strong>zu verstehen</strong>, <strong>muss </strong>man die <strong>Geschichte von VB NET</strong> (Visual Basic .NET) betrachten.</p>



<h2 class="wp-block-heading" id="ein-blick-zuruck-zu-nur-visual-basic">Ein Blick zurück zu (nur) Visual Basic</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-1-Es-war-einmal-VB6.jpg"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-1-Es-war-einmal-VB6.jpg" alt="VB NET Tutorial 1 - Es war einmal VB6" class="wp-image-8153" title="VB NET Tutorial 1 - Es war einmal VB6"/></a><figcaption class="wp-element-caption">VB NET Tutorial 1 &#8211; Es war einmal VB6</figcaption></figure>



<p><strong>Kurz erklärt, entspringt </strong>die &#8222;<strong>NET</strong>&#8222;-Variante, Welche sich auf das <strong><a href="https://de.wikipedia.org/wiki/.Net-Framework" target="_blank" rel="noreferrer noopener">NET Framework</a></strong> bezieht, <strong>aus </strong>der vorherigen VB- (nur <strong>Visual Basic</strong>) Version.</p>



<p>Dort <strong>war </strong>die &#8222;<strong>Dim</strong>&#8222;-<strong>Anweisung für </strong>die <strong>Speicherreservierung </strong>(Dimensionierung) <strong>von Arrays</strong> zuständig.</p>



<p><strong>In Visual Basic NET</strong> wurde dieser Befehl sozusagen <strong>recycelt und dient </strong>von nun an <strong>der Deklaration von Variablen</strong>.</p>



<p><strong>Im Endeffekt kündigt </strong>es also im ersten Schritt <strong>nur an</strong>: &#8222;Hey, da kommt was, was XY heißt&#8220; – mehr nicht!</p>



<p><strong>Merke </strong>Dir also: <strong>Möchte </strong>ich eine <strong>Variable definieren</strong>, <strong>verwende ich </strong>das Schlüsselwort &#8222;<strong>Dim</strong>&#8222;.</p>



<p>Die <strong>Dimensionierung</strong>, <strong>also </strong>die tatsächliche <strong>Speicherreservierung findet </strong>erst <strong>statt</strong>, <strong>sobald </strong>der <strong>Datentyp klar</strong> ist.</p>



<p><strong>Datentypen werden durch </strong>die sogenannte <strong><a href="https://docs.microsoft.com/de-de/dotnet/visual-basic/language-reference/statements/as-clause" target="_blank" rel="noreferrer noopener">&#8222;As&#8220;-Klausel</a></strong> <strong>angegeben</strong>, wodurch dann Speicher reserviert werden kann.</p>



<p>Klar, <strong>im Endeffekt </strong>kann man ja <strong>nur </strong>für etwas <strong>reservieren</strong>, <strong>von </strong>dem man <strong>weiß</strong>, <strong>was es ist</strong>!</p>



<h3 class="wp-block-heading" id="ubung-macht-den-meister">Übung macht den Meister!</h3>



<p><strong>Auch wenn </strong>Du an dieser Stelle eventuell <strong>noch nicht durchblickst</strong>, vertraue mir, das <strong>kommt noch</strong>.</p>



<p><strong>Häufig </strong>– und <strong>so ging es mir</strong> auch <strong>am Anfang </strong>– <strong>werden </strong>die <strong>Sachen </strong>erst <strong>bei </strong>aktiver <strong>Verwendung klarer</strong>.</p>



<p>Es <strong>kommt </strong>also <strong>manchmal einfach mit </strong>der <strong>Erfahrung</strong>, die Dinge zu verstehen.</p>



<p><strong>Besonders wenn </strong>gewisse Dinge <strong>erstmal ins Muskelgedächtnis übergegangen </strong>sind ist das hilfreich.</p>



<p>Also <strong>dann</strong>, <strong>wenn </strong>Du den <strong>Zustand erreichen </strong>konntest, über <strong>einzelne Konstrukte </strong>beim Schreiben <strong>nicht mehr nachdenken </strong>zu <strong>müssen</strong>.</p>



<h2 class="wp-block-heading" id="arten-der-definition">Arten der Definition</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/Variablen-Definieren.jpg"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/Variablen-Definieren.jpg" alt="Variablen Definieren – Arten der Definition" class="wp-image-8241" title="Variablen Definieren – Arten der Definition"/></a><figcaption class="wp-element-caption">Variablen Definieren – Arten der Definition</figcaption></figure>



<p><strong>Oben </strong>haben wir nun die <strong>ersten beiden Beispiele</strong> <strong>für </strong>die <strong>Definition </strong>von Variablen gesehen.</p>



<p><strong>Danach </strong>gab es noch ein <strong>paar wichtige</strong>, <strong>rudimentäre Zusatzinformationen </strong>zum Hintergrund von <strong>Visual Basic NET</strong>.</p>



<p><strong>Lass </strong>uns <strong>nun </strong>mal <strong>schauen</strong>, <strong>Welche weiteren Definitionsmöglichkeiten </strong>wir für Variablen haben.</p>



<p>Diese <strong>Definitionsmöglichkeiten </strong>müssten wir <strong>für </strong>das <strong>bessere Verständnis </strong>vermutlich <strong>aufschlüsseln</strong>.</p>



<p><strong>Sicherlich </strong>wird dies <strong>zu einem späteren Zeitpunkt</strong> z. B. noch beim Thema Klassen <strong>erweitert</strong>.</p>



<p><strong>Zum jetzigen Zeitpunkt belassen wir es</strong> aber <strong>bei </strong>den Variablen-Basics.</p>



<h3 class="wp-block-heading" id="variante-1-die-simple-deklaration">Variante 1 – Die simple Deklaration</h3>



<p>Im <strong>obigen Text </strong>habe ich schon <strong>einmal angedeutet</strong>, <strong>dass </strong>die obige <strong>Definition </strong>der Variablen von <strong>3 Bausteinen</strong> <strong>geprägt </strong>ist.</p>



<p>Es <strong>geht allerdings </strong>für eine simple sogenannte Deklaration <strong>auch einfacher</strong>.</p>



<p>Dies <strong>ist dann </strong>mehr oder weniger <strong>nur </strong>eine <strong>Ankündigung </strong>der Variable, <strong>ohne </strong>jegliche <strong>Wertzuweisung</strong>.</p>



<p>Eine <strong>Wertzuweisung während </strong>der <strong>Deklaration bezeichnet </strong>man <strong>auch </strong>leicht <strong>anders</strong>, aber gleich mehr dazu.</p>



<p>Eine <strong>wirklich simple Deklaration </strong>a la &#8222;Hey, <strong>es gibt</strong> da das <strong>Ding namens XY </strong>sieht&#8220; sieht daher so aus:</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 vorname As String</pre>



<p><strong>Mehr </strong>ist dafür <strong>nicht von Nöten</strong>, nur:</p>



<ul class="wp-block-list">
<li>Das <strong>Dim</strong>-Schlüsselwort</li>



<li><strong>Name </strong>der Variable</li>



<li><strong>As </strong>&lt;Datentyp&gt; Klausel</li>
</ul>



<p>Man <strong>könnte </strong>den <strong>Code auch einfach </strong>wie folgt ins Deutsche <strong>übersetzen</strong>:</p>



<p><strong>Lege </strong>eine Zeichenketten-<strong>Variable namens </strong>&#8222;<strong>vorname</strong>&#8220; <strong>fest </strong>– Punkt.</p>



<h3 class="wp-block-heading" id="variante-2-die-initialisierung">Variante 2 – Die Initialisierung</h3>



<p>Das <strong>nächste Beispiel wird </strong>sich <strong>durch </strong>einen wie oben bereits gezeigten <strong>Mini-Schritt unterscheiden</strong>.</p>



<p><strong>Auch wenn </strong>wir mit den vorherigen Beispielen <strong>schon </strong>erfolgreich <strong>Variablen deklarieren </strong>konnten, sind dies eben <strong>nur Deklarationen</strong>.</p>



<p><strong>Wenn </strong>wir die <strong>Variablen </strong>nun <strong>direkt </strong>bei der Deklaration <strong>mit</strong> <strong>Werten versehen </strong>möchten, können wir das relativ <strong>einfach erledigen</strong>.</p>



<p><strong>In </strong>der <strong>Programmierung nennt man das </strong>für gewöhnlich <strong>Initialisierung</strong>.</p>



<p>Ich <strong>deklariere also </strong>eine <strong>Variable und weise </strong>Ihr im selben Schritt bereits einen <strong>Wert zu</strong>.</p>



<p><strong>Hier </strong>siehst Du noch einmal <strong>2 Beispiele</strong> <strong>zur Initialisierung</strong> von Variablen:</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 youtubeKanal As String = "RobbelRoot"
Dim alter As Integer = 30</pre>



<p><strong>Neben </strong>dem angesprochenen &#8222;<strong>Lege .. fest als ..</strong>&#8220; <strong>kommt </strong>nun also noch <strong>etwas hinzu</strong>.</p>



<p><strong>Wieder </strong>ist dies ein alter <strong>Bekannter aus </strong>dem <strong>Matheunterricht </strong>namens &#8222;<strong>Gleichheitszeichen</strong>&#8222;.</p>



<p><strong>Hinter </strong>dem <strong>Gleichheitszeichen muss </strong>dann nur noch <strong>ein </strong>zum Datentyp <strong>passender Wert </strong>folgen.</p>



<p><strong>Schließlich </strong>ist eine <strong>Zahl etwas anderes als </strong>ein &#8222;<strong>b</strong>&#8220; <strong>und </strong>auch ein <strong>Geburtsdatum unterscheidet </strong>sich zum Beispiel.</p>



<p>Ein <strong>Geburtsdatum wäre </strong>ja <strong>sogar </strong>eine <strong>Mischung aus Zeichen </strong>und <strong>Zahlen</strong>.</p>



<p><strong>Dieser Wert </strong>wird in diesem Kontext <strong>auch </strong>gerne <strong>als </strong>sogenanntes &#8222;<strong>Literal</strong>&#8220; bezeichnet.</p>



<h3 class="wp-block-heading" id="variante-3-die-kombination-aus-beiden">Variante 3 – Die Kombination aus beiden</h3>



<p>Die <strong>letzte Variante zur Definition </strong>von Variablen <strong>ist </strong>die <strong>Kombination der beiden </strong>vorherigen <strong>Möglichkeiten</strong>.</p>



<p><strong>Dabei verwenden </strong>wir wie gewohnt die &#8222;<strong>Dim</strong>&#8222;-Anweisung, <strong>jedoch lassen</strong> <strong>wir </strong>in diesem Fall die &#8222;<strong>As</strong>&#8222;-Klausel <strong>weg</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="">Dim preis = 149</pre>



<p>In <strong>diesem Fall wird </strong>der <strong>Datentyp anhand </strong>des zugewiesenen <strong>Wertes ermittelt </strong>– toll, oder!?</p>



<h4 class="wp-block-heading" id="die-sogenannte-typinferenz">Die sogenannte Typinferenz</h4>



<p>Diese &#8222;Funktionalität&#8220; <strong>nennt man Typinferenz</strong>, da der <strong>Typ von </strong>dem zugewiesenen <strong>Wert inferiert </strong>wird.</p>



<p><strong>Inferieren heißt </strong>im Endeffekt nichts anderes als: etwas mitbringen, mit etwas beitragen, <strong>logisch fordern</strong>.</p>



<p><strong>Ich </strong>persönlich <strong>finde </strong>die Erfassung von <strong>Datentypen via Typinferenz</strong> sehr <strong>angenehm </strong>und effizient.</p>



<p>Es <strong>sei </strong>jedoch <strong>angemerkt</strong>, <strong>dass </strong>es auch <strong>einige Entwickler </strong>gibt, Welche <strong>die Typinferenz verteufeln</strong>.</p>



<p><strong>Diese Entwickler sind </strong>der <strong>Meinung</strong>, <strong>dass </strong>diese <strong>non-explizite</strong> Weise der <strong>Typangabe unsauber </strong>ist.</p>



<p><strong>Obwohl </strong>wir <strong>noch nicht beim Thema </strong>&#8222;<strong>verschiedene Zahlen</strong>-Datentypen&#8220; angekommen sind, mache ich mal ein Beispiel.</p>



<p><strong>Lass Dich </strong>aber <strong>nicht </strong>davon <strong>verwirren</strong>, <strong>da es </strong>vermutlich im zweiten Teil zu den Variablen <strong>angesprochen wird</strong>.</p>



<h4 class="wp-block-heading" id="warum-manche-entwickler-die-typinferenz-ablehnen">Warum manche Entwickler die Typinferenz ablehnen</h4>



<p><strong>Unter anderem dieses Beispiel könnte </strong>die <strong>Ablehnung </strong>mancher Entwickler <strong>gegenüber </strong>der <strong>Typinferenz erklären</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="">Dim unbekannteZahl = 5</pre>



<p><strong>Wenn </strong>Du diesen <strong>Code </strong>einmal <strong>in </strong>Deine <strong>Programmierumgebung </strong>namens Visual Studio eingibst, <strong>wirst </strong>Du <strong>folgendes sehen</strong>:</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Beispiel-fuer-ungenaue-Typinferenz.png"><img loading="lazy" decoding="async" width="394" height="74" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Beispiel-fuer-ungenaue-Typinferenz.png" alt="VB NET Beispiel für ungenaue Typinferenz" class="wp-image-8177" title="VB NET Beispiel für ungenaue Typinferenz"/></a><figcaption class="wp-element-caption">VB NET Beispiel für ungenaue Typinferenz</figcaption></figure>



<p>Es <strong>wird hier automatisch </strong>der Datentyp &#8222;<strong>Integer&#8220; suggeriert</strong>, was aber <strong>nicht zwangsweise stimmen </strong>muss.</p>



<p><strong>Wenn </strong>ich zum Beispiel im Programm <strong>mit Dezimalzahlen </strong>arbeiten <strong>möchte</strong>, <strong>wäre </strong>dies hier schon <strong>nicht mehr </strong>&#8222;möglich&#8220;.</p>



<p><strong>Zumindest dieses </strong>kleine <strong>Problem </strong>könnte man schnell <strong>lösen</strong>, <strong>wenn </strong>man das &#8222;<strong>richtige</strong>&#8220; <strong>Literal </strong>verwendet:</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Beispiel-fuer-ungenaue-Typinferenz-mit-Double.png"><img loading="lazy" decoding="async" width="400" height="70" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Beispiel-fuer-ungenaue-Typinferenz-mit-Double.png" alt="VB NET Beispiel für ungenaue Typinferenz mit Double" class="wp-image-8183" title="VB NET Beispiel für ungenaue Typinferenz mit Double"/></a><figcaption class="wp-element-caption">VB NET Beispiel für ungenaue Typinferenz mit Double</figcaption></figure>



<p><strong>Dadurch </strong>würde ich <strong>nun </strong>die <strong>Möglichkeit </strong>haben, <strong>mit Dezimalzahlen zu arbeiten</strong>, aber da <strong>gibt </strong>es <strong>leider </strong>noch <strong>mehr</strong>..</p>



<p><strong>Daher </strong>gehen wir <strong>an</strong> <strong>dieser Stelle </strong>nochmal <strong>zurück zu </strong>dem <strong>Integer</strong>, <strong>also </strong>der einfachen <strong>Ganzzahl</strong>.</p>



<p><strong>Wie </strong>wir <strong>im nächsten Tutorial</strong>-Teil zu den Variablen noch lernen werden, <strong>gibt es </strong>dort <strong>Unterschiede</strong>.</p>



<p>Man <strong>könnte </strong>– ohne zu viel zu spoilern – <strong>sagen</strong>, <strong>dass </strong>es <strong>unterschiedliche Größen</strong>, bzw. Bereiche gibt.</p>



<h4 class="wp-block-heading" id="er-weiss-nicht-sollte-wissen-was-er-tut">Er <s>weiß nicht</s> sollte wissen was er tut..</h4>



<p><strong>Das typische </strong>&#8222;<strong>Byte</strong>&#8222;, wovon viele irgendwo mal gehört haben, <strong>kann </strong>eine <strong>weniger große Zahl</strong>, <strong>als Andere </strong>Zahlen-<strong>Datentypen </strong>speichern.</p>



<p><strong>Im Vergleich dazu </strong>hätten wir auch noch die <strong>riesige Zahl </strong>namens &#8222;<strong>Long</strong>&#8222;, <strong>Welche </strong>wirklich (wirklich) <strong>große Zahlen speichern </strong>kann.</p>



<p>Also erneut: <strong>Mit </strong>dem <strong>obigen Beispiel </strong>des &#8222;Integers&#8220; <strong>suggerieren </strong>wir einfach <strong>eine gewisse Größe</strong>.</p>



<p><strong>Solange man </strong>das <strong>weiß </strong>ist <strong>alles gut</strong>, <strong>wenn </strong>man es eventuell <strong>übersieht</strong>, kann es <strong>blöd </strong>werden.</p>



<p><strong>Ansonsten kann man </strong>dann wie erwähnt noch <strong>durch </strong>gewisse <strong>Literale nachhelfen</strong>.</p>



<p><strong>Letztendlich bestimmt das </strong>entwickelnde Software-<strong>Team </strong>nachher <strong>die Konventionen</strong>.</p>



<p><strong>Oft bekommt man </strong>durch die Arbeit als Entwickler auch gewisse &#8222;<strong>Schreib-Regeln</strong>&#8220; vorgesetzt.</p>



<p>Dies <strong>kann durch </strong>das <strong>Team</strong>, den <strong>Chef</strong>, <strong>oder </strong>z. B. durch den <strong>Kunden geschehen</strong>.</p>



<h2 class="wp-block-heading" id="baumchen-wechsle-dich">Bäumchen wechsle dich!</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-1-Variablen-aendern-Baeumchen-wechsle-dich.jpg"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/VB-NET-Tutorial-1-Variablen-aendern-Baeumchen-wechsle-dich.jpg" alt="VB NET Tutorial 1 - Variablen ändern - Bäumchen wechsle dich" class="wp-image-8233" title="VB NET Tutorial 1 - Variablen ändern - Bäumchen wechsle dich"/></a><figcaption class="wp-element-caption">VB NET Tutorial 1 &#8211; Variablen ändern &#8211; Bäumchen wechsle dich</figcaption></figure>



<p><strong>In </strong>den <strong>bisherigen Beispielen </strong>haben wir uns <strong>größtenteils mit </strong>der <strong>Definition </strong>von Variablen beschäftigt.</p>



<p><strong>Wer </strong>sich <strong>aber </strong>die <strong>Bezeichnung </strong>&#8222;Variable&#8220; selbst einmal <strong>anschaut</strong>, <strong>wird </strong>etwas <strong>feststellen</strong>.</p>



<p>Das <strong>Nomen </strong>&#8222;<strong>Variable</strong>&#8220; steht <strong>nicht umsonst </strong>in <strong>Verwandtschaft mit </strong>dem Adjektiv &#8222;<strong>variabel</strong>&#8222;.</p>



<p><strong>Der </strong>Inhalt, bzw. der <strong>Wert </strong>hinter der Variable <strong>ist </strong>– richtig vermutet – <strong>nicht statisch</strong>.</p>



<p>Wir <strong>können </strong>den <strong>Wert </strong>also <strong>zur Laufzeit </strong>des Programmes dynamisch <strong>anpassen</strong>.</p>



<h3 class="wp-block-heading" id="weitere-arten-von-variablen">Weitere &#8222;Arten&#8220; von &#8222;Variablen&#8220;</h3>



<p><strong>Später </strong>werden wir auch noch <strong>weitere Arten</strong> von Variablen <strong>kennenlernen</strong>.</p>



<p>Diese <strong>sind im Gegensatz </strong>dazu <strong>nicht variabel</strong>, sprich man kann Sie nicht ändern.</p>



<p><strong>Welche Vorteile und </strong>eventuell auch <strong>Nachteile </strong>daraus entstehen können, <strong>schauen wir </strong>uns <strong>später </strong>an.</p>



<p>Ein <strong>kleiner Spoiler </strong>wäre, dass es sich <strong>dabei </strong>z. B. um <strong>Konstanten </strong>handelt.</p>



<p><strong>Wie </strong>der <strong>Name </strong>schon <strong>vermuten lässt</strong>, sind <strong>Diese </strong>eben halt – <strong>konstant</strong>, sprich nicht veränderbar!</p>



<p><strong>Zurück </strong>zur <strong>Variable</strong>, <strong>Welche </strong>eigentlich <strong>für </strong>das Wort &#8222;<strong>veränderbar</strong>&#8220; <strong>stehen </strong>könnte.</p>



<h3 class="wp-block-heading" id="wert-einer-variablen-andern">Wert einer Variablen ändern</h3>



<p><strong>Initialisieren wir </strong>also wie neu gelernt eine <strong>Variable via Typinferenz</strong>.</p>



<p><strong>Zur Wiederholung</strong>: Wir <strong>leiten </strong>den <strong>Ziel-Datentyp aus</strong> dem zugewiesenen <strong>Wert ab</strong>.</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="">Dim vorname = "Robert"</pre>



<p>Ich <strong>habe </strong>hier also das &#8222;<strong>Dim</strong>&#8222;-Schlüsselwort <strong>verwendet und </strong>anschließend einen <strong>Namen </strong>(vorname) <strong>festgelegt</strong>.</p>



<p><strong>Im gleichen Schritt </strong>haben wir der Variable einen <strong>Wert zugewiesen</strong>, sprich initialisiert.</p>



<p><strong>Durch </strong>die <strong>Angabe eines Strings</strong>, also einer <strong>Zeichenfolge </strong>mit dem Inhalt &#8222;Robert&#8220;, <strong>kennt </strong>die <strong>Variable ihren Datentyp</strong>.</p>



<p>In <strong>diesem Fall bekommt </strong>die <strong>Variable </strong>ihren Datentyp &#8222;<strong>String</strong>&#8220; also <strong>über </strong>die <strong>Typinferenz</strong>.</p>



<p><strong>Um </strong>diese <strong>Variable </strong>nun ganz einfach <strong>im nächsten Schritt zu ändern</strong>, machen wir <strong>praktisch </strong>das <strong>Gleiche </strong>nochmal.</p>



<p>Nur <strong>dieses Mal verwenden </strong>wir <strong>nicht </strong>das &#8222;Dim&#8220;-Schlüsselwort, <strong>da </strong>diese <strong>Variable </strong>in der nächsten Zeile <strong>bereits </strong>&#8222;<strong>bekannt</strong>&#8220; ist.</p>



<p><strong>Durch die Bezeichnung</strong> der Variable (vorname), <strong>können wir Diese </strong>nun ganz einfach <strong>ansprechen</strong>.</p>



<p><strong>Dann führen </strong>wir einfach eine <strong>erneute Zuweisung </strong>eines Wertes <strong>durch</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="">Dim vorname = "Robert"
vorname = "Max"</pre>



<h3 class="wp-block-heading" id="auf-den-richtigen-typ-kommt-s-an">Auf den richtigen Typ kommt&#8217;s an</h3>



<p><strong>Hierbei </strong>gilt es natürlich zu <strong>beachten</strong>, <strong>dass </strong>die <strong>Variable einen </strong>zum Deklarationstyp <strong>passenden Wert </strong>bekommt.</p>



<p>Ich <strong>könnte natürlich </strong>jetzt <strong>nicht </strong>hingehen und <strong>einer </strong>&#8222;<strong>String</strong>&#8222;-Variable <strong>plötzlich </strong>eine <strong>Zahl zuweisen</strong>.</p>



<p>Es <strong>würde verständlicherweise </strong>einen <strong>Fehler </strong>in unserer Programmierumgebung <strong>anzeigen</strong>.</p>



<p>Das <strong>gleiche Problem </strong>hätten wir <strong>auch bei </strong>einem <strong>eigens erstellten Datentyps </strong>wie einem &#8222;Kunden&#8220;.</p>



<p>Auch dazu werden wir <strong>später </strong>im <strong>Kapitel </strong>&#8222;<strong>Klassen</strong>&#8220; noch einiges lernen!</p>



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



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



<p><strong>Am Ende </strong>des ersten VB NET Tutorials <strong>angekommen</strong>, ist es <strong>Zeit für </strong>ein kleines <strong>Fazit</strong>:</p>



<p><strong>Variablen dienen </strong>uns in <strong>der Programmierung </strong>also <strong>als eine Art Zwischenspeicher </strong>für Werte.</p>



<p><strong>Mit </strong>diesen <strong>Werten können wir dann </strong>in verschiedenen Situationen (<strong>namentlich angesprochen</strong>) <strong>arbeiten</strong>.</p>



<p>Der <strong>Wert hinter </strong>den <strong>Variablen kann </strong>zur Laufzeit des Programms <strong>dynamisch geändert werden</strong>. </p>



<p><strong>Variablen werden üblicherweise mit </strong>einem gewissen <strong>Datentyp </strong>definiert, damit &#8222;man&#8220; weiß, womit man arbeitet.</p>



<p><strong>Deshalb muss </strong>man auch bei der <strong>nachträglichen </strong>Änderung des Wertes <strong>auf </strong>den <strong>Datentyp achten</strong>.</p>



<p>In <strong>späteren Tutorials </strong>werden wir uns <strong>natürlich auch </strong>mit <strong>dynamischen Datentypen </strong>beschäftigen.</p>



<p>Man <strong>weiß nicht </strong>unbedingt <strong>in jedem Moment </strong>den <strong>exakten Datentyp</strong>, <strong>daher </strong>muss <strong>oft </strong>auch ein wenig <strong>Flexibilität her</strong>.</p>



<p>Es gibt <strong>3 gängige Arten </strong>Variablen <strong>zu definieren</strong>:</p>



<ul class="wp-block-list">
<li><strong>Deklaration </strong>= Ankündigung mit Datentyp</li>



<li><strong>Initialisierung </strong>= Ankündigung mit Datentyp &amp; Wertzuweisung</li>



<li><strong>Init. durch Typinferenz </strong>= Ankündigung ohne expliziten Datentyp, jedoch via Inferenz</li>
</ul>



<h2 class="wp-block-heading" id="im-nachsten-tutorial">Im nächsten Tutorial</h2>



<p>Das <strong>nächste Tutorial wird </strong>ein <strong>zweiter Beitrag</strong> zum Thema <strong>Variablen </strong>sein.</p>



<p><strong>Dort </strong>werde ich <strong>noch mehr auf</strong> die unterschiedlichen <strong>Typen</strong>, deren <strong>Verwendung und </strong>weitere <strong>Tipps eingehen</strong>.</p>



<p><strong>Ebenso konnten </strong>wir <strong>bisher nicht </strong>wirklich <strong>viel </strong>von den Variablen <strong>sehen</strong>, <strong>da </strong>Sie mehr oder weniger &#8222;<strong>geschluckt</strong>&#8220; wurden.</p>



<p><strong>Daher werde ich zeigen</strong>, <strong>wie </strong>man die Variablen dann wirklich visuell darstellen, also &#8222;<strong>ausgeben</strong>&#8220; kann.</p>



<p><strong>Dadurch </strong>werden Sie <strong>für </strong>den <strong>Endnutzer </strong>und letztendlich uns <strong>sichtbar</strong>, wenn auch erstmal nicht schön.</p>



<p>Das <strong>haben </strong>sogenannte <strong>Konsolenanwendungen </strong>aber so <strong>an sich</strong>, gell!?</p>



<h2 class="wp-block-heading" id="downloads">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 wp-element-button" href="https://bit.ly/vbnet-tutorial-1-download" target="_blank" rel="noreferrer noopener">VBTutorial1_DE.zip</a></div>
</div>



<h2 class="wp-block-heading" id="weiterfuhrende-links">Weiterführende Links</h2>



<ul class="wp-block-list">
<li><a href="https://robbelroot.de/blog/vb-net-tutorial-2-erste-ausgaben-auf-der-konsole/" target="_blank" rel="noreferrer noopener"><strong>weiter zu Tutorial 2</strong> <strong>– erste Ausgaben</strong></a></li>



<li><strong><a href="https://robbelroot.de/blog/vbnet-inputbox-beispiel/" target="_blank" rel="noreferrer noopener">VB NET Inputbox – Simple Eingaben entgegennehmen</a></strong></li>



<li><strong><a href="https://robbelroot.de/blog/vbnet-timer-beispiel-code-im-intervall-ausfuehren/" target="_blank" rel="noreferrer noopener">Timer Beispiel in VB NET</a></strong></li>
</ul>
<p>Der Beitrag <a href="https://robbelroot.de/blog/tutorials-vbnet-1-variablen/">VB NET Tutorial 1: Variablen</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://robbelroot.de/blog/tutorials-vbnet-1-variablen/feed/</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
			</item>
		<item>
		<title>&#x1f575;&#xfe0f; Hacking a NET Application</title>
		<link>https://robbelroot.de/blog/hacking-a-net-application/</link>
					<comments>https://robbelroot.de/blog/hacking-a-net-application/#comments</comments>
		
		<dc:creator><![CDATA[Robert Skibbe]]></dc:creator>
		<pubDate>Wed, 12 Jan 2022 22:02:00 +0000</pubDate>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[.net]]></category>
		<category><![CDATA[app]]></category>
		<category><![CDATA[application]]></category>
		<category><![CDATA[avoid]]></category>
		<category><![CDATA[c#]]></category>
		<category><![CDATA[credentials]]></category>
		<category><![CDATA[dangerous]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[developer]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[ftp]]></category>
		<category><![CDATA[guide]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[hacked]]></category>
		<category><![CDATA[hacker]]></category>
		<category><![CDATA[hacking]]></category>
		<category><![CDATA[hardcode]]></category>
		<category><![CDATA[hardcoding]]></category>
		<category><![CDATA[hd]]></category>
		<category><![CDATA[help]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[password]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[security]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[user]]></category>
		<category><![CDATA[vbnet]]></category>
		<category><![CDATA[video]]></category>
		<category><![CDATA[vulnerable]]></category>
		<guid isPermaLink="false">https://robbelroot.de/?p=7320</guid>

					<description><![CDATA[<p>Hacking User &#38; Password information from a NET Assembly So you are interested in hacking a simple NET Application with like all the nerdy stuff!? Then i&#8217;m glad to tell you, that you are very welcome, go ahead and let&#8217;s hack! This example will work for C# as well, even &#8230;</p>
<p>Der Beitrag <a href="https://robbelroot.de/blog/hacking-a-net-application/">&#x1f575;&#xfe0f; Hacking a NET Application</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/01/Hacking-User-Pass-Thumbnail.png"><img loading="lazy" decoding="async" width="1920" height="1080" src="https://robbelroot.de/wp-content/uploads/2022/01/Hacking-User-Pass-Thumbnail.png" alt="" class="wp-image-7324"/></a></figure>






<h2 class="wp-block-heading" id="hacking-user-password-information-from-a-net-assembly">Hacking User &amp; Password information from a NET Assembly</h2>



<p>So you are interested in <strong>hacking a simple NET Application</strong> with like <strong>all </strong>the <strong>nerdy stuff</strong>!?</p>



<p>Then i&#8217;m glad to tell you, that you are <strong>very welcome</strong>, <strong>go </strong>ahead and <strong>let&#8217;s hack</strong>!</p>



<p>This example <strong>will work for C# as well</strong>, even it is presented with a Visual Basic NET Application.</p>



<p>In the <strong>first few lines </strong>i will <strong>write </strong>a bit <strong>about </strong>my <strong>own experience </strong>with hacking, that i experienced &#8222;on&#8220; myself. </p>



<p><strong>After that </strong>i <strong>will show </strong>you some kind of <strong>basic example </strong>application, which is like our victim.</p>



<p><strong>In </strong>the<strong> last step </strong>i will explain, <strong>which tool </strong>you would need to inspect those specific files.</p>



<p>You will also learn how to <strong>find the information</strong> you need inside those tools.</p>



<h2 class="wp-block-heading" id="liking-humour-videos-heres-the-youtube-version">Liking humour &amp; videos? Heres the YouTube version</h2>



<p><strong>If</strong> you <strong>like watching videos </strong>and <strong>having </strong>like <strong>actual </strong>live <strong>examples </strong>– i&#8217;ve also got you covered!</p>



<p>Just <strong>visit <a href="https://youtube.com/RobbelRoot" target="_blank" rel="noreferrer noopener">my YouTube channel</a></strong> <strong>or click </strong>the following <strong>video </strong>to load and watch it:</p>


<div class="async-youtube" data-embed="tdJTEg7swlI" data-alt="">
    <div class="play-button"></div>      
  </div>



<h2 class="wp-block-heading" id="why-knowing-how-to-hack-some-things-is-important">Why knowing how to hack some things is important</h2>



<p><strong>Even if you&#8217;re not </strong>that in the dark sitting <strong>hacker person</strong>, yet <strong>knowing </strong>about hacking <strong>is </strong>very <strong>important </strong>in terms of programming.</p>



<p>Why – you ask? Because <strong>actively hacking</strong> has <strong>obviously </strong>something really important <strong>to do with the opposite</strong>.</p>



<p><strong>Knowing how </strong>attacker actually <strong>attacks</strong>, will <strong>make </strong>you <strong>stronger in defending </strong>yourself.</p>



<p>So <strong>if </strong>you <strong>understand </strong>the <strong>concepts</strong>, you <strong>have </strong>a (better) <strong>chance to </strong>actually <strong>defend </strong>against them – right!?</p>



<p>You can see, that the old and good saying &#8222;<strong>wisdom is power</strong>&#8220; <strong>isn&#8217;t</strong> that <strong>bad </strong>at all.</p>



<p>This also <strong>counts for programming</strong> or software development as well.</p>



<h3 class="wp-block-heading" id="sometimes-learning-attacking-turns-to-defending">Sometimes learning attacking turns to defending</h3>



<p><strong>Defending against hackers </strong>in a <strong>possible matter</strong>, is like one of the basic essences.</p>



<p>I mean <strong>who wants </strong>to build <strong>application </strong>basically everyone could get like <strong>for free</strong>, or <strong>even </strong>script <strong>kiddies can </strong>crack.</p>



<p><strong>Sure there </strong>are <strong>unavoidable </strong>situations or better said <strong>different aspects</strong>, <strong>where </strong>you <strong>can&#8217;t protect</strong> yourself, or your app.</p>



<p>I <strong>had to </strong>hardly <strong>learn myself</strong>, that those aspects <strong>aren&#8217;t the real key</strong> of importance, too!</p>



<p>This is <strong>where </strong>we are about to switch to <strong>my own </strong>little <strong>story behind </strong>that whole thing.</p>



<h2 class="wp-block-heading" id="my-own-background-and-contact-points-of-hacking">My own background and contact points of hacking</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/Hacking-a-NET-Application-My-Story.png"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/01/Hacking-a-NET-Application-My-Story.png" alt="My Story- Hacking a NET Application" class="wp-image-7341" title="My Story- Hacking a NET Application"/></a><figcaption>My Story – Hacking a NET Application</figcaption></figure>



<p><strong>Even if i don&#8217;t </strong>really <strong>know </strong>to <strong>where </strong>like i should actually <strong>start </strong>telling you, i will try to find some starting point.</p>



<p><strong>One of the things</strong>, which <strong>stuck in my mind</strong> till today – and it&#8217;s like 15 years ago – <strong>is the following</strong> thingy.</p>



<p><strong>I was a</strong> little <strong>gamer </strong>bro, who was <strong>playing </strong>his current <strong>MMO </strong>called &#8222;Silkroad Online&#8220; like insane.</p>



<p><strong>Everyone who knows </strong>about the so called &#8222;<strong>china grinders</strong>&#8220; knows what i&#8217;m talking about..</p>



<p><strong>Those games </strong>are the <strong>definition </strong>of &#8222;spend all your time&#8220;, because they are so <strong>grinding based</strong>, hence the name..</p>



<h3 class="wp-block-heading" id="a-whole-load-of-work-gone">A whole load of work – gone</h3>



<p>You are <strong>essentially slaying like 5000</strong> monsters <strong>to get </strong>almost no drops and <strong>zero </strong>exp.</p>



<p>As you can imagine there were like many <strong>scammers </strong>and <strong>fakers </strong>which <strong>tried to scam or hack your account</strong>.</p>



<p>Despite this <strong>i liked the game</strong> and i <strong>can&#8217;t</strong> exactly <strong>tell why</strong>, i think it was the chinese and dragonball like style.</p>



<p>You <strong>had </strong>like this <strong>whole chinese world </strong>built <strong>around </strong>the <strong>silkroad lore</strong> (and i really love &#8222;the&#8220; asian lore).</p>



<p><strong>Then </strong>there was this <strong>dragonball </strong>like aspect, where you <strong>can shoot your own</strong> &#8222;<strong>kamehameha</strong>&#8220; which kept me for long enough.</p>



<p><strong>In the end</strong> i <strong>spent hours </strong>and hours <strong>fighting </strong>those <strong>monsters for </strong>like almost <strong>nothing and </strong>even <strong>wasted </strong>some *cough* <strong>bucks</strong>.</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/Hacking-a-NET-Application-Speeding-things-up-with-a-bot.png"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/01/Hacking-a-NET-Application-Speeding-things-up-with-a-bot.png" alt="Speeding things up with a bot – Hacking a NET Application" class="wp-image-7352" title="Speeding things up with a bot – Hacking a NET Application"/></a><figcaption>Speeding things up with a bot – Hacking a NET Application</figcaption></figure>



<p><strong>As </strong>i <strong>always </strong>had this &#8222;hmm, i gotta <strong>find a solution</strong> for that&#8220;-mentality, i also <strong>tried to</strong> do this time.</p>



<p><strong>After all </strong>the <strong>outcome wasn&#8217;t</strong> that <strong>good </strong>in the end, but well, i think that&#8217;s the price you gotta pay, huh!?</p>



<h3 class="wp-block-heading" id="searching-for-help">Searching for help</h3>



<p>So i <strong>started searching</strong> for like helpers with leveling and <strong>found different leveling services</strong>, but they <strong>were </strong>mostly very <strong>expensive</strong>..</p>



<p><strong>Not much time after </strong>that, i <strong>found </strong>something called &#8222;<strong>bot</strong>&#8222;, which was basically just a software.</p>



<p><strong>Those </strong>were/are <strong>tools </strong>which will <strong>take </strong>the <strong>leveling aspect</strong> in their hand and basically <strong>tell your </strong>ingame <strong>character what to do</strong> – so you don&#8217;t have to.</p>



<p><strong>There was a free bot</strong> which i downloaded and installed and <strong>boom </strong>– the <strong>problem happened</strong>.</p>



<p>It <strong>did its level job pretty well</strong>, <strong>but had </strong>like an integrated <strong>trojan </strong>aspect, which <strong>spied on </strong>my <strong>credentials </strong>and <strong>transferred </strong>my <strong>data </strong>to its owner.</p>



<p>So not <strong>many days after</strong> the installation, one of my <strong>accounts was gone</strong> – yeah, i had multiple&#8230;</p>



<p><strong>This is the whole story why i started learning programming and hacking</strong> – i wanted to create my own bot to compensate my lost time and my lost account!</p>



<p><strong>Back to </strong>the main <strong>topic </strong>– hacking a NET application!</p>



<h2 class="wp-block-heading" id="how-i-started-programming-like-logins-ew">How i started programming like logins – ew</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/Login-Screens-how-to-hack-a-net-application.png"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/01/Login-Screens-how-to-hack-a-net-application.png" alt="Login Screens - how to hack a net application" class="wp-image-7365" title="Login Screens - how to hack a net application"/></a><figcaption>Login Screens &#8211; how to hack a net application</figcaption></figure>



<p>So <strong>now the story begins</strong>, where <strong>i started programming</strong> at all and like <strong>login screens</strong>.</p>



<p>I think <strong>every beginner in programming did it like this</strong> at some point.</p>



<p><strong>On </strong>the <strong>one side </strong>you are just <strong>making your first steps</strong> and on the <strong>other side </strong>you just want to <strong>make it work</strong>.</p>



<p>You <strong>don&#8217;t have much knowledge</strong> – if any at all – about this persistence thingy called <strong>database</strong>, <strong>nor </strong>the <strong>security </strong>thingy called <strong>cryptography</strong>, etc.</p>



<p><strong>This makes you </strong>– especially at the beginning – <strong>think</strong>, that you <strong>can just do </strong>this thing, which is known as <strong>hardcoding</strong>.</p>



<p>You <strong>just put variables inside </strong>the code (or something similar) <strong>with fixed</strong> values like:</p>



<pre class="wp-block-code"><code>variableA = "ValueB"</code></pre>



<p><strong>Then </strong>you can go ahead and <strong>easily compare </strong>this <strong>value in </strong>terms of an <strong>authentication screen </strong>like:</p>



<pre class="wp-block-code"><code>user = "User"
password = "password"
if enteredUser = user and enteredPassword = password then...</code></pre>



<p><strong>So far</strong> this doesn&#8217;t seem like anything <strong>hacky</strong>, but <strong>read </strong>a few <strong>more </strong>lines and <strong>let me explain</strong>!</p>



<p><strong>One big problem </strong>is, that even <strong>if you think</strong> that <strong>only </strong>&#8222;<strong>noobs</strong>&#8220; <strong>do this</strong>, <strong>i&#8217;ve seen too many agencies</strong> and similar small firms did that as well.</p>



<p>I mean it&#8217;s <strong>one thing</strong> if you&#8217;re <strong>doing </strong>this <strong>on </strong>your <strong>own</strong>, <strong>local computer</strong>, for yourself <strong>with </strong>no <strong>customer data </strong>involved.</p>



<p>But i really think it&#8217;s <strong>something different when </strong>&#8222;<strong>companies</strong>&#8220; do things like this, <strong>knowing that </strong>it&#8217;s totally <strong>wrong</strong>!</p>



<p><strong>Especially in my work as a freelancer &amp; teacher</strong> i learned, that small business i supported, sometimes persisted on like &#8222;nah we will do it like this, it&#8217;s okay&#8220;.</p>



<p>Some <strong>other excuses</strong> i hear a lot are like &#8222;It&#8217;s <strong>only a small customer</strong>, that&#8217;s okay&#8220;, or &#8222;there&#8217;s <strong>no budget left</strong>, just do it&#8220;!</p>



<p><strong>Even </strong>if this <strong>involved software</strong> for other customers which <strong>got their software</strong> like <strong>delegated </strong>to freelancers.</p>



<h2 class="wp-block-heading" id="creating-the-sample-application-our-victim">Creating the sample application – our victim</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/Login-Dialog-example-hardcoded-credentials-in-NET-application.png"><img loading="lazy" decoding="async" width="1280" height="720" src="https://robbelroot.de/wp-content/uploads/2022/01/Login-Dialog-example-hardcoded-credentials-in-NET-application.png" alt="Login Dialog example - hardcoded credentials in NET application" class="wp-image-7379" title="Login Dialog example - hardcoded credentials in NET application"/></a><figcaption>Login Dialog example &#8211; hardcoded credentials in NET application</figcaption></figure>



<p>So now <strong>let&#8217;s jump to the hacking action</strong> and see what malicous things can actually happen if you&#8217;re doing it like above yourself.</p>



<p>You can <strong>go ahead </strong>now and <strong>create a new windows forms application</strong> (dotnet Core 3.1 in the example above).</p>



<p>The <strong>first Form </strong>will be the &#8222;<strong>frmLogin</strong>&#8222;, which is – as you&#8217;ve may guessed – the <strong>form where the login will happen</strong>.</p>



<h3 class="wp-block-heading" id="basic-login-form-controls">Basic login form controls</h3>



<p><strong>Add </strong>the usual login <strong>controls like 2 textboxes for the user and the password</strong>, as well as the <strong>button to start the login</strong> process.</p>



<p><strong>Name the button</strong> – like you should always do – for example &#8222;btnLogin&#8220; and put <strong>the following code</strong> inside the handler:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="VB.NET" data-enlighter-group="btn-login-sub">Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click
    Dim loginFailed = Not CheckLogin(tbUser.Text, tbPassword.Text)
    If loginFailed Then
        MessageBox.Show("Login failed!")
        Return
    End If
    successfullySubmitted = True
    Dim mainForm = New frmMain()
    mainForm.Show()
    Close()
End Sub</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="btn-login-sub">private void btnLogin_Click(object sender, EventArgs e)
{
    var loginFailed = !CheckLogin(tbUser.Text, tbPassword.Text);
    if (loginFailed)
    {
        MessageBox.Show("Login failed!");
        Return;
    }
    successfullySubmitted = true;
    var mainForm = new frmMain();
    mainForm.Show();
    Close();
}</pre>



<p>The <strong>code will call the check &#8222;Check-Login&#8220;-function</strong> which we will declare in a second <strong>and invert </strong>the <strong>result</strong>.</p>



<p>We are <strong>doing this to</strong> use this &#8222;<strong>early return</strong>&#8222;-block in the next few lines, to display a <strong>short message</strong> and well – <strong>return </strong>early.</p>



<p><strong>Then </strong>we are <strong>setting a flag to true</strong>, to avoid closing the form, but ignore this for now, you can see it later with the complete code.</p>



<h3 class="wp-block-heading" id="the-protected-content-the-main-form">The protected content – the main form</h3>



<p><strong>Next </strong>we are <strong>creating a new</strong> &#8222;<strong>frmMain</strong>&#8220; instance <strong>and show it</strong> in the next step.</p>



<p><strong>In </strong>the <strong>last step</strong> we are <strong>closing </strong>the <strong>current form</strong>, so we are basically <strong>closing the login form</strong>, to only <strong>leave </strong>the <strong>main </strong>form open.</p>



<p><strong>Right now</strong> is the time to <strong>define </strong>the &#8222;<strong>CheckLogin</strong>&#8222;-<strong>function</strong>, before we continue adding the main form:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="VB.NET" data-enlighter-group="checklogin-function">Private Function CheckLogin(user As String, password As String) As Boolean
    Dim credentialsMatching = user = "User" AndAlso password = "password"
    Return credentialsMatching
End Function</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="checklogin-function">private bool CheckLogin(string user, string password)
{
    var credentialsMatching = user == "User" &amp;&amp; password == "password";
    returnn credentialsMatching;
}</pre>



<p>Now <strong>create </strong>an <strong>additional form </strong>and call it like &#8222;frmMain&#8220;, you can<strong> leave the form as it is</strong>.</p>



<p>You <strong>could add </strong>something like <strong>code to handle the closing</strong> of the app for you, <strong>but </strong>this <strong>isn&#8217;t necessary</strong>.</p>



<p><strong>Maybe </strong>you want to <strong>download </strong>the <strong>complete project</strong> down below, so you can see everything in total.</p>



<p>Currently the app <strong>should look something like this</strong>:</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/Hacking-NET-Application-Example-App.gif"><img loading="lazy" decoding="async" width="885" height="446" src="https://robbelroot.de/wp-content/uploads/2022/01/Hacking-NET-Application-Example-App.gif" alt="Hacking NET Application Example App" class="wp-image-7392" title="Hacking NET Application Example App"/></a><figcaption>NET Application Example App hacking</figcaption></figure>



<h2 class="wp-block-heading" id="hacking-our-sample-application">Hacking our sample application</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/Hacking-our-NET-sample-application-extracting-hardcoded-credentials.png"><img loading="lazy" decoding="async" width="1280" height="960" src="https://robbelroot.de/wp-content/uploads/2022/01/Hacking-our-NET-sample-application-extracting-hardcoded-credentials.png" alt="Hacking our NET sample application - extracting hardcoded credentials" class="wp-image-7396" title="Hacking our NET sample application - extracting hardcoded credentials"/></a><figcaption>Hacking our NET sample application &#8211; extracting hardcoded credentials</figcaption></figure>



<p><strong>To </strong>actually <strong>start hacking </strong>our little <strong>NET application</strong>, you <strong>need </strong>a <strong>tool</strong>, which is actually <strong>provided by </strong>the <strong>NET </strong>Framework itself.</p>



<p>So there is <strong>no big magic</strong>, <strong>or </strong>big action like &#8222;yeah you need to download x, y, z.. blabla..&#8220;.</p>



<p><strong>Everything </strong>you need, is <strong>mostly already in place</strong>, <strong>if </strong>you <strong>installed Visual Studio</strong>.</p>



<h3 class="wp-block-heading" id="the-needed-tool-ildasm">The needed tool – &#8222;ildasm&#8220;</h3>



<p>The <strong>tool needed is called</strong> &#8222;<strong>IL-Disassembler</strong>&#8220; or in short &#8222;<strong>ildasm</strong>&#8220; and resides in the &#8222;C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools&#8220; folder.</p>



<p><strong>Maybe </strong>you will <strong>find </strong>the tool <strong>in </strong>a slightly <strong>different location</strong>, <strong>if </strong>you have a <strong>different version </strong>of Visual Studio, etc.</p>



<p><strong>Double click the tool</strong> to open it and the <strong>following screen </strong>will appear:</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/IL-Disassembler-ildasm-screenshot.png"><img loading="lazy" decoding="async" width="389" height="282" src="https://robbelroot.de/wp-content/uploads/2022/01/IL-Disassembler-ildasm-screenshot.png" alt="IL-Disassembler ildasm screenshot" class="wp-image-7403" title="IL-Disassembler ildasm screenshot"/></a><figcaption>IL-Disassembler ildasm screenshot</figcaption></figure>



<p>Then <strong>go ahead and select your</strong> &#8222;&lt;YourApplicationName&gt;.<strong>dll</strong>&#8222;-<strong>file </strong>with the <strong>&#8222;File-&gt;Open&#8220; menu item</strong> (sorry, i only have the german version).</p>



<p>You can <strong>find this file inside the debug folder of your app </strong>(don&#8217;t forget to start debugging at least once, so the files are created!).</p>



<p>Just a <strong>quick side note</strong>: This will <strong>also work for </strong>the <strong>release </strong>&#8222;.dll&#8220;-file!</p>



<p><strong>Expand the nodes as shown</strong> in the screenshot, then you will find the function we are looking for.</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/IL-Disassembler-ildasm-viewing-the-checklogin-function.png"><img loading="lazy" decoding="async" width="389" height="597" src="https://robbelroot.de/wp-content/uploads/2022/01/IL-Disassembler-ildasm-viewing-the-checklogin-function.png" alt="IL-Disassembler ildasm viewing the checklogin function" class="wp-image-7411" title="IL-Disassembler ildasm viewing the checklogin function"/></a><figcaption>IL-Disassembler ildasm viewing the checklogin function</figcaption></figure>



<p>If you <strong>finally double click that &#8222;CheckLogin&#8220;-function</strong>, a <strong>dialog will</strong> appear, where you can <strong>inspect the IL code</strong> for this function.</p>



<p><strong>There </strong>you will <strong>see </strong>you <strong>hardcoded credentials</strong> – <strong>congratulations, you hacked the program</strong>!</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/Finished-hacking-the-net-application-by-revealing-hardcoded-credentials.png"><img loading="lazy" decoding="async" width="624" height="394" src="https://robbelroot.de/wp-content/uploads/2022/01/Finished-hacking-the-net-application-by-revealing-hardcoded-credentials.png" alt="Finished hacking the net application by revealing hardcoded credentials" class="wp-image-7414" title="Finished hacking the net application by revealing hardcoded credentials"/></a><figcaption>Finished hacking the net application by revealing hardcoded credentials</figcaption></figure>



<h2 class="wp-block-heading" id="downloads">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/hacking-a-dot-net-application-download" target="_blank" rel="noreferrer noopener">SimpleHackExample.zip</a></div>
</div>



<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-webshop/" target="_blank" rel="noreferrer noopener">Hacking a Webshop</a></strong></li><li><strong><a href="https://robbelroot.de/blog/converting-davinci-resolve-markers-to-youtube-chapters/" target="_blank" rel="noreferrer noopener">Converting DaVinci Resolve markers to YouTube chapters</a></strong></li></ul>
<p>Der Beitrag <a href="https://robbelroot.de/blog/hacking-a-net-application/">&#x1f575;&#xfe0f; Hacking a NET Application</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://robbelroot.de/blog/hacking-a-net-application/feed/</wfw:commentRss>
			<slash:comments>7</slash:comments>
		
		
			</item>
		<item>
		<title>Converting DaVinci Resolve markers to YouTube chapters</title>
		<link>https://robbelroot.de/blog/converting-davinci-resolve-markers-to-youtube-chapters/</link>
					<comments>https://robbelroot.de/blog/converting-davinci-resolve-markers-to-youtube-chapters/#comments</comments>
		
		<dc:creator><![CDATA[Robert Skibbe]]></dc:creator>
		<pubDate>Sun, 09 Jan 2022 18:43:08 +0000</pubDate>
				<category><![CDATA[Allgemein]]></category>
		<category><![CDATA[automate]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[chapter]]></category>
		<category><![CDATA[chapters]]></category>
		<category><![CDATA[converter]]></category>
		<category><![CDATA[create]]></category>
		<category><![CDATA[davinci resolve]]></category>
		<category><![CDATA[editing]]></category>
		<category><![CDATA[generate]]></category>
		<category><![CDATA[marker]]></category>
		<category><![CDATA[markers]]></category>
		<category><![CDATA[tool]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[videoediting]]></category>
		<category><![CDATA[youtube]]></category>
		<guid isPermaLink="false">https://robbelroot.de/?p=7275</guid>

					<description><![CDATA[<p>So you want to start converting DaVinci Resolve markers to YouTube chapters – fully automated? Here you will find 2 free tools to do so &#x1f609;! Just paste the content of the &#8222;.edl&#8220;-file into the left textbox and the magic will happen. Alternatively, you can simply drag and drop the &#8230;</p>
<p>Der Beitrag <a href="https://robbelroot.de/blog/converting-davinci-resolve-markers-to-youtube-chapters/">Converting DaVinci Resolve markers to YouTube chapters</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/01/DaVinci-Resolve-Marker-zu-YouTube-Kapitel-umwandeln-1.png"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/01/DaVinci-Resolve-Marker-zu-YouTube-Kapitel-umwandeln-1.png" alt="Converting DaVinci Resolve markers to YouTube chapters" class="wp-image-7237" title="Converting DaVinci Resolve markers to YouTube chapters"/></a><figcaption>Converting DaVinci Resolve markers to YouTube chapters</figcaption></figure>






<p>So you want to start <strong>converting DaVinci Resolve markers to YouTube chapters</strong> – fully automated?</p>



<p><strong>Here </strong>you will find <strong>2 free tools</strong> to do so &#x1f609;!</p>



<p><strong>Just paste </strong>the <strong>content </strong>of the &#8222;.edl&#8220;-file <strong>into </strong>the <strong>left textbox </strong>and the magic will happen.</p>



<p><strong>Alternatively</strong>, you can simply <strong>drag and drop</strong> the <strong>file into </strong>the <strong>field </strong>and <strong>save </strong>yourself <strong>opening </strong>the <strong>file</strong>.</p>


<section class="dcc-wrapper">
  <div class="dcc-title">Marker to YouTube-chapter converter</div>
  <div class="dcc-box-row">
    <div class="dcc-box-column">
      <div class="dcc-box-title">.edl contents</div>
      <div class="dcc-box-wrapper dcc-left-box-wrapper">
        <textarea id="edlContent" class="dcc-box" placeholder="Drag & drop the .edl file here, or paste the text-contents"></textarea>
      </div>
      <div class="dcc-box-menu">
        <button onclick="pasteEdlContent()">Paste</button>
      </div>
    </div>
    <div class="dcc-box-column">
      <div class="dcc-box-title">YouTube chapters</div>
      <div class="dcc-box-wrapper dcc-right-box-wrapper">
        <textarea id="chapters" class="dcc-box" readonly placeholder="Your chapters will appear here after processing successfully"></textarea>
      </div>
      <div class="dcc-box-menu">
        <button onclick="copyChapters()">Copy</button>
      </div>
    </div>
  </div>
  <hr class="dcc-dl-separator">
  <div class="dcc-dl-wrapper">
    <a href="/downloads/tools/MarkersToChaptersConverter.zip">
      <div class="dcc-dl-inner">
        <!-- <div style="border-right:1px solid black;margin: 0.3em 0;margin-right:0.5em;"></div> -->
        <div style="color:black;font-weight:bold;">Download app</div>
      </div>
    </a>
  </div>
</section>



<h2 class="wp-block-heading" id="background-to-davinci-resolve-markers-youtube-chapters">Background to DaVinci Resolve markers &amp; YouTube chapters</h2>



<p><strong>Ultimately</strong>, you can <strong>export </strong>so-called &#8222;<strong>editing decision lists</strong>&#8220; <strong>from </strong>your <strong>DaVinci Resolve</strong> timeline.</p>



<p><strong>These arise </strong>or can be created <strong>by setting </strong>&#8222;<strong>markers</strong>&#8222;, i.e. small <strong>bookmarks</strong>, in <strong>DaVinci Resolve</strong>.</p>



<p>It is <strong>best </strong>to <strong>imagine </strong>it <strong>like </strong>a <strong>book </strong>in which you attach <strong>small bookmarks to </strong>certain pages so that you can <strong>remember </strong>this point <strong>later</strong>.</p>



<p>Each of <strong>these bookmarks </strong>&#8211; <strong>called </strong>&#8222;<strong>markers</strong>&#8220; in DaVinci Resolve slang &#8211; you <strong>can </strong>also <strong>add </strong>text, i.e. a kind of <strong>title</strong>.</p>



<p>It wasn&#8217;t that long <strong>ago </strong>&#8211; <strong>2 days</strong> lol to be precise &#8211; <strong>when I faced the problem</strong> myself for the first time.</p>



<p>So I went there <strong>more or less usual</strong>, <strong>made </strong>my <strong>videos </strong>and <strong>set </strong>the <strong>markers </strong>in DaVinci Resolve.</p>



<p><strong>As mentioned</strong>, I did this <strong>so </strong>that I could <strong>quickly find </strong>the <strong>important points </strong>while I was working on it.</p>



<p><strong>Later </strong>I <strong>realized </strong>that I could just as easily <strong>use these passages </strong>for my YouTube chapters.</p>



<p><strong>Of course </strong>I <strong>didn&#8217;t feel</strong> like <strong>copying </strong>everything out <strong>by hand</strong>, so I looked for a tool.</p>



<p><strong>Unfortunately </strong>I <strong>couldn&#8217;t find a tool quickly</strong>, but luckily you are a programmer and can <strong>build one yourself </strong>&#x1f60e;.</p>


<div class="async-youtube" data-embed="jGXuR29Q0xs" data-alt="">
    <div class="play-button"></div>      
  </div>



<h2 class="wp-block-heading" id="extension-1-converting-multi-color-markers-to-youtube-chapters">Extension 1 – Converting multi color markers to YouTube chapters</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/DaVinci-Resolve-convert-multi-color-markers-to-YouTube-chapters.png"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/01/DaVinci-Resolve-convert-multi-color-markers-to-YouTube-chapters.png" alt="DaVinci Resolve convert multi color markers to YouTube chapters" class="wp-image-7290" title="DaVinci Resolve convert multi color markers to YouTube chapters"/></a><figcaption>DaVinci Resolve convert multi color markers to YouTube chapters</figcaption></figure>



<h3 class="wp-block-heading" id="supporting-multiple-colors">Supporting multiple colors</h3>



<p><strong>Of course</strong>, a few <strong>small ideas </strong>came to mind with this tool <strong>after </strong>it was <strong>finished</strong>.</p>



<p><strong>Currently </strong>the <strong>converter </strong>can process <strong>only one marker color</strong>, which may not be sufficient for some (a few?) applications.</p>



<p>It <strong>doesn&#8217;t</strong> <strong>matter which colors </strong>are <strong>present </strong>in the code, as the <strong>tool always takes the color </strong>from the <strong>first </strong>&#8222;dataset&#8220; as the main color.</p>



<p><strong>In theory</strong>, it <strong>wouldn&#8217;t be</strong> really <strong>difficult </strong>to add multiple colors <strong>since </strong>I was <strong>looking for extensibility </strong>in the code as always.</p>



<p><strong>However</strong>, I <strong>still </strong>have to <strong>think about how </strong>to implement this as a surface, as I <strong>want to keep it intuitive</strong>.</p>



<h2 class="wp-block-heading" id="extension-2-windows-tool-for-eleminating-manual-work">Extension 2 – Windows Tool for eleminating manual work</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/DaVinci-Resolve-convert-edi-files-on-creation-to-youtube-chapters.png"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/01/DaVinci-Resolve-convert-edi-files-on-creation-to-youtube-chapters.png" alt="DaVinci Resolve convert edl files on creation to youtube chapters" class="wp-image-7296" title="DaVinci Resolve convert edl files on creation to youtube chapters"/></a><figcaption>DaVinci Resolve convert edl files on creation to youtube chapters</figcaption></figure>



<h3 class="wp-block-heading" id="converting-edl-files-on-creation">Converting EDL files on creation</h3>



<p><strong>Programmers </strong>are <strong>known to be </strong><s>lazy</s>, creative / <strong>innovative</strong>, so I thought about something.</p>



<p><strong>Especially people </strong>who <strong>upload videos </strong>every day have this <strong>recurring task </strong>in front of them.</p>



<p><strong>Filming</strong>, <strong>cutting</strong>, setting DaVinci Resolve <strong>markers</strong>, <strong>exporting</strong>, etc. every day &#8211; <strong>sigh</strong>.</p>



<p><strong>Then </strong>going to <strong>my </strong>* wink * <strong>homepage </strong>and <strong>use </strong>the YouTube <strong>chapter converter</strong>, so:</p>



<ul class="wp-block-list"><li>Open download folder</li><li>Visit my page/open the tab</li><li>Drop file into webbrowser</li><li>copy chapters</li><li>Open the Youtube video</li><li>Paste chapters..</li></ul>



<p>Anyone who knows me knows that I&#8217;m <strong>not a big fan of recurring</strong> &#8222;monkey work&#8220;, so the <strong>following idea came </strong>up:</p>



<p><strong>How about </strong>a <strong>small Windows tool</strong>, which can <strong>recognize </strong>the <strong>creation of </strong>an &#8222;.edl&#8220; <strong>file </strong>within a folder?</p>



<p>You <strong>could </strong>of course <strong>make </strong>this <strong>folder configurable </strong>and thus adapt it to your needs.</p>



<p><strong>One should</strong> also <strong>be able </strong>to <strong>switch off </strong>the tool <strong>without closing </strong>it, of course.</p>



<p>Then the <strong>tool could </strong>go there and, if it <strong>detects</strong> the <strong>creation </strong>of a <strong>file</strong>, take this file and <strong>process </strong>it <strong>directly</strong>.</p>



<p><strong>After </strong>the <strong>successful processing</strong>, the YouTube <strong>chapters could </strong>be <strong>placed on </strong>the <strong>clipboard immediately </strong>&#8211; automatically!</p>



<p><strong>No sooner </strong>said than done &#8211; I <strong>implemented</strong> the <strong>tool quickly </strong>and this is what it looks like (I did without an overly beautiful design, sorry ..):</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/DaVinci-Resolve-Marker-in-YouTube-Kapitel-umwandeln-Screenshot.png"><img loading="lazy" decoding="async" width="371" height="181" src="https://robbelroot.de/wp-content/uploads/2022/01/DaVinci-Resolve-Marker-in-YouTube-Kapitel-umwandeln-Screenshot.png" alt="Converting DaVinci Resolve markers to YouTube chapters Screenshot" class="wp-image-7266" title="Converting DaVinci Resolve markers to YouTube chapters Screenshot"/></a><figcaption>Converting DaVinci Resolve markers to YouTube chapters Screenshot</figcaption></figure>



<h2 class="wp-block-heading" id="downloads">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/davinci-resolve-markers-to-youtube-chapter-converter-download" target="_blank" rel="noreferrer noopener"><strong>MarkersToChaptersConverter.zip</strong></a></div>
</div>



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



<ul class="wp-block-list"><li><strong><a href="https://robbelroot.de/blog/hacking-a-net-application/" target="_blank" rel="noreferrer noopener">Hacking a NET Application</a></strong></li><li><strong><a href="https://robbelroot.de/blog/creating-a-vb-net-datagridview-filter-functionality/" target="_blank" rel="noreferrer noopener">Filtering a VB NET DataGridView</a></strong></li><li><strong><a href="https://robbelroot.de/blog/vb-net-removing-items-from-listboxes/" target="_blank" rel="noreferrer noopener">Removing items from a ListBox</a></strong></li></ul>
<p>Der Beitrag <a href="https://robbelroot.de/blog/converting-davinci-resolve-markers-to-youtube-chapters/">Converting DaVinci Resolve markers to YouTube chapters</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://robbelroot.de/blog/converting-davinci-resolve-markers-to-youtube-chapters/feed/</wfw:commentRss>
			<slash:comments>5</slash:comments>
		
		
			</item>
		<item>
		<title>DaVinci Resolve Marker in YouTube-Kapitel umwandeln</title>
		<link>https://robbelroot.de/blog/davinci-resolve-marker-in-youtube-kapitel-umwandeln/</link>
					<comments>https://robbelroot.de/blog/davinci-resolve-marker-in-youtube-kapitel-umwandeln/#comments</comments>
		
		<dc:creator><![CDATA[Robert Skibbe]]></dc:creator>
		<pubDate>Fri, 07 Jan 2022 18:48:36 +0000</pubDate>
				<category><![CDATA[Allgemein]]></category>
		<category><![CDATA[Downloads]]></category>
		<category><![CDATA[Tools]]></category>
		<category><![CDATA[automation]]></category>
		<category><![CDATA[automatisieren]]></category>
		<category><![CDATA[automatisierung]]></category>
		<category><![CDATA[chapter]]></category>
		<category><![CDATA[chapters]]></category>
		<category><![CDATA[converter]]></category>
		<category><![CDATA[davinci resolve]]></category>
		<category><![CDATA[editing]]></category>
		<category><![CDATA[generieren]]></category>
		<category><![CDATA[kapitel]]></category>
		<category><![CDATA[konverter]]></category>
		<category><![CDATA[marker]]></category>
		<category><![CDATA[tool]]></category>
		<category><![CDATA[tools]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[umwandeln]]></category>
		<category><![CDATA[videobearbeitung]]></category>
		<category><![CDATA[videoediting]]></category>
		<category><![CDATA[youtube]]></category>
		<guid isPermaLink="false">https://robbelroot.de/?p=7194</guid>

					<description><![CDATA[<p>Du möchtest DaVinci Resolve Marker in YouTube-Kapitel umwandeln und hast keine Lust selbst Hand anzulegen!? Dann findest Du hier zwei Tools – in Form eines Webinterfaces und einer kleinen App – um dies automatisiert erledigen zu lassen &#x1f609;! Füge einfach den Inhalt der &#8222;.edl&#8222;-Datei in das linke Textfeld ein und &#8230;</p>
<p>Der Beitrag <a href="https://robbelroot.de/blog/davinci-resolve-marker-in-youtube-kapitel-umwandeln/">DaVinci Resolve Marker in YouTube-Kapitel umwandeln</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/01/DaVinci-Resolve-Marker-zu-YouTube-Kapitel-umwandeln-1.png"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/01/DaVinci-Resolve-Marker-zu-YouTube-Kapitel-umwandeln-1.png" alt="DaVinci Resolve Marker zu YouTube-Kapitel umwandeln" class="wp-image-7237" title="DaVinci Resolve Marker zu YouTube-Kapitel umwandeln"/></a><figcaption class="wp-element-caption">DaVinci Resolve Marker zu YouTube-Kapitel umwandeln</figcaption></figure>






<p>Du möchtest <strong>DaVinci Resolve Marker in YouTube-Kapitel umwandeln</strong> und hast keine Lust selbst Hand anzulegen!?</p>



<p>Dann <strong>findest </strong>Du hier <strong>zwei Tools</strong> – in Form eines Webinterfaces und einer kleinen App – um dies automatisiert erledigen zu lassen &#x1f609;!</p>



<p><strong>Füge </strong>einfach den <strong>Inhalt </strong>der &#8222;.<strong>edl</strong>&#8222;-Datei <strong>in </strong>das <strong>linke Textfeld </strong>ein und schon passiert die Magie.</p>



<p><strong>Alternativ </strong>kannst Du die <strong>Datei </strong>auch einfach <strong>via </strong>&#8222;<strong>Drag &amp; Drop</strong>&#8220; in das Feld ziehen und <strong>sparst </strong>Dir so das <strong>Öffnen </strong>der Datei.</p>


<section class="dcc-wrapper">
  <div class="dcc-title">Marker to YouTube-chapter converter</div>
  <div class="dcc-box-row">
    <div class="dcc-box-column">
      <div class="dcc-box-title">.edl contents</div>
      <div class="dcc-box-wrapper dcc-left-box-wrapper">
        <textarea id="edlContent" class="dcc-box" placeholder="Drag & drop the .edl file here, or paste the text-contents"></textarea>
      </div>
      <div class="dcc-box-menu">
        <button onclick="pasteEdlContent()">Paste</button>
      </div>
    </div>
    <div class="dcc-box-column">
      <div class="dcc-box-title">YouTube chapters</div>
      <div class="dcc-box-wrapper dcc-right-box-wrapper">
        <textarea id="chapters" class="dcc-box" readonly placeholder="Your chapters will appear here after processing successfully"></textarea>
      </div>
      <div class="dcc-box-menu">
        <button onclick="copyChapters()">Copy</button>
      </div>
    </div>
  </div>
  <hr class="dcc-dl-separator">
  <div class="dcc-dl-wrapper">
    <a href="/downloads/tools/MarkersToChaptersConverter.zip">
      <div class="dcc-dl-inner">
        <!-- <div style="border-right:1px solid black;margin: 0.3em 0;margin-right:0.5em;"></div> -->
        <div style="color:black;font-weight:bold;">Download app</div>
      </div>
    </a>
  </div>
</section>



<h2 class="wp-block-heading" id="hintergrund-zu-davinci-resolve-markern-youtube-kapiteln">Hintergrund zu DaVinci Resolve Markern &amp; YouTube-Kapiteln</h2>



<p>Letztendlich <strong>kannst </strong>Du sogenannte <strong>Editierentscheidungslisten aus </strong>Deiner <strong>DaVinci Resolve Timeline exportieren</strong>.</p>



<p><strong>Diese entstehen</strong>, bzw. können <strong>dadurch </strong>erstellt werden, <strong>dass </strong>Du in DaVinci Resolve &#8222;<strong>Marker</strong>&#8222;, also kleine Lesezeichen <strong>setzt</strong>.</p>



<p><strong>Stell</strong>&#8218; es Dir am besten <strong>wie </strong>ein <strong>Buch </strong>vor, in dem Du auf <strong>gewissen Seiten </strong>kleine <strong>Lesezeichen </strong>einhängst, um Dich <strong>später </strong>an diese Stelle zu <strong>erinnern</strong>.</p>



<p><strong>Jedes </strong>dieser <strong>Lesezeichen </strong>– im <strong>DaVinci Resolve</strong> Slang &#8222;<strong>Marker</strong>&#8220; genannt – kannst Du zusätzlich <strong>mit </strong>Text, also einer Art <strong>Titel versehen</strong>.</p>



<p>Es ist gar <strong>nicht allzu lang her</strong> – um genau zu sein 2 Tage lol – wo ich das erste Mal <strong>selbst vor dem Problem</strong> stand.</p>



<p>Ich ging also wie mehr oder weniger gewohnt hin, <strong>machte </strong>meine <strong>Videos </strong>und <strong>setzte </strong>die <strong>Marker in DaVinci Resolve</strong>.</p>



<p>Das habe ich wie erwähnt deshalb gemacht, damit ich eben <strong>während </strong>meiner <strong>Bearbeitung schnell zu </strong>den <strong>wichtigen Stellen </strong>finde.</p>



<p><strong>Nachträglich</strong> habe ich dann <strong>gemerkt</strong>, <strong>dass </strong>ich diese Stellen auch ebenso gut <strong>für </strong>meine <strong>YouTube-Kapitel</strong> nutzen könnte.</p>



<p><strong>Natürlich </strong>hatte ich <strong>wenig</strong> <strong>Lust </strong>alles <strong>händisch </strong>herauszukopieren, <strong>also </strong>suchte ich nach einem <strong>Hilfsmittel</strong>.</p>



<p>Leider habe ich <strong>kein Tool </strong>auf die Schnelle gefunden, aber <strong>zum Glück </strong>ist man ja <strong>Programmierer </strong>und kann sich <strong>selbst </strong>eins<strong> bauen </strong>&#x1f60e;.</p>



<h2 class="wp-block-heading" id="erweiterung-1-mehrfarbige-marker-in-youtube-kapitel-umwandeln">Erweiterung 1 – Mehrfarbige Marker in YouTube-Kapitel umwandeln</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/DaVinci-Resolve-mehrfarbige-Marker-zu-YouTube-Kapitel-umwandeln.png"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/01/DaVinci-Resolve-mehrfarbige-Marker-zu-YouTube-Kapitel-umwandeln.png" alt="DaVinci Resolve mehrfarbige Marker zu YouTube-Kapitel umwandeln" class="wp-image-7233" title="DaVinci Resolve mehrfarbige Marker zu YouTube-Kapitel umwandeln"/></a><figcaption class="wp-element-caption">DaVinci Resolve mehrfarbige Marker zu YouTube-Kapitel umwandeln</figcaption></figure>



<h3 class="wp-block-heading" id="unterstutzung-mehrerer-farben">Unterstützung mehrerer Farben</h3>



<p><strong>Natürlich k</strong>amen mir auch bei <strong>diesem Tool </strong>nach der Fertigstellung noch einige <strong>kleine Ideen</strong> in den Kopf.</p>



<p><strong>Aktuell </strong>kann der Konverter z. B. <strong>nur eine </strong>Marker-<strong>Farbe verarbeiten</strong>, was für einige (wenige?) Einsatzfälle <strong>vielleicht</strong> nicht ausreicht.</p>



<p><strong>Aktuell</strong> ist es <strong>egal</strong>, <strong>welche Farben </strong>im Code vorhanden sind, da das <strong>Tool immer </strong>die Farbe aus dem <strong>ersten </strong>&#8222;<strong>Datensatz</strong>&#8220; als Haupt-Farbe nimmt.</p>



<p><strong>Theoretisch </strong>wäre es <strong>nicht </strong>wirklich <strong>schwer</strong>, <strong>mehrere Farben </strong>hinzuzufügen, da ich im Code wie immer auf Erweiterbarkeit geachtet habe.</p>



<p>Ich <strong>muss </strong>mir allerdings <strong>noch überlegen</strong>, <strong>wie </strong>ich das als <strong>Oberfläche realisiere</strong>, da ich es weiterhin intuitiv halten möchte.</p>



<h2 class="wp-block-heading" id="erweiterung-2-windows-tool-zur-eliminierung-manueller-arbeit">Erweiterung 2 – Windows Tool zur Eliminierung manueller Arbeit</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/DaVinci-Resolve-edi-Datei-bei-Erstellung-in-YouTube-Kapitel-umwandeln.png"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/01/DaVinci-Resolve-edi-Datei-bei-Erstellung-in-YouTube-Kapitel-umwandeln.png" alt="DaVinci Resolve edi-Datei bei Erstellung in YouTube-Kapitel umwandeln" class="wp-image-7243" title="DaVinci Resolve edi-Datei bei Erstellung in YouTube-Kapitel umwandeln"/></a><figcaption class="wp-element-caption">DaVinci Resolve edi-Datei bei Erstellung in YouTube-Kapitel umwandeln</figcaption></figure>



<h3 class="wp-block-heading" id="edl-dateien-bereits-beim-erstellen-umwandeln">EDL-Dateien bereits beim Erstellen umwandeln</h3>



<p><strong>Programmierer </strong>sind ja <strong>bekanntlich </strong><s>faul</s> <strong>kreativ</strong>/innovativ, <strong>also </strong>habe ich mir <strong>noch was </strong>überlegt.</p>



<p><strong>Besonders </strong>Menschen die <strong>täglich Videos </strong>hochladen, haben diese <strong>immer wiederkehrende Aufgabe </strong>vor sich.</p>



<p>Jeden Tag <strong>Material </strong>filmen, <strong>schneiden</strong>, DaVinci Resolve <strong>Marker setzen</strong>, <strong>exportieren</strong>, etc. – seufz.</p>



<p><strong>Danach </strong>auf meine *zwinker* Homepage gehen und den <strong>YouTube-Kapitel Konverter</strong> nutzen, also:</p>



<ul class="wp-block-list">
<li>Download-Ordner öffnen</li>



<li>Meine Seite öffnen / Tab öffnen</li>



<li>Datei in den Webbrowser droppen</li>



<li>Kapitel kopieren</li>



<li>YouTube-Video öffnen</li>



<li>Kapitel einfügen..</li>
</ul>



<p>Wer mich kennt weiß, dass ich <strong>kein großer Fan von wiederkehrender </strong>&#8222;Monkey-Work&#8220; ist, also kam <strong>folgende Idee</strong>:</p>



<p><strong>Wie wäre es mit </strong>einem kleinen <strong>Windows-Tool</strong>, <strong>Welches </strong>die <strong>Erstellung </strong>einer <strong>&#8222;.edl&#8220;-Datei</strong> innerhalb eines Ordners <strong>erkennen </strong>kann?</p>



<p><strong>Diesen Ordner </strong>könnte man natürlich <strong>konfigurierbar </strong>machen und somit <strong>an </strong>seine <strong>Bedürfnisse anpassen</strong>.</p>



<p><strong>Ebenso </strong>sollte man die <strong>Möglichkeit </strong>haben, dass <strong>Tool</strong> <strong>ohne</strong> <strong>schließen abzuschalten</strong>, klar..</p>



<p><strong>Danach könnte </strong>das <strong>Tool </strong>hingehen und sich bei erkannter <strong>Erstellung einer Datei</strong>, diese Datei <strong>nehmen und </strong>direkt <strong>verarbeiten</strong>.</p>



<p><strong>Nach </strong>der erfolgreichen <strong>Verarbeitung </strong>könnten die <strong>YouTube-Kapitel</strong> also <strong>sofort in </strong>die <strong>Zwischenablage </strong>gelegt werden – <strong>automatisch</strong>!</p>



<p>Gesagt, getan – habe das <strong>Tool </strong>fix <strong>umgesetzt </strong>und <strong>so sieht </strong>es <strong>aus </strong>(ich habe auf übermäßig <strong>schönes Design verzichtet</strong>, sorry..):</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/01/DaVinci-Resolve-Marker-in-YouTube-Kapitel-umwandeln-Screenshot.png"><img loading="lazy" decoding="async" width="371" height="181" src="https://robbelroot.de/wp-content/uploads/2022/01/DaVinci-Resolve-Marker-in-YouTube-Kapitel-umwandeln-Screenshot.png" alt="DaVinci Resolve Marker in YouTube-Kapitel umwandeln Screenshot" class="wp-image-7266" title="DaVinci Resolve Marker in YouTube-Kapitel umwandeln Screenshot"/></a><figcaption class="wp-element-caption">DaVinci Resolve Marker in YouTube-Kapitel umwandeln Screenshot</figcaption></figure>



<h2 class="wp-block-heading" id="downloads">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 wp-element-button" href="https://bit.ly/davinci-resolve-markers-to-youtube-chapter-converter-herunterladen" target="_blank" rel="noreferrer noopener">MarkersToChaptersConverter.zip</a></div>
</div>



<h2 class="wp-block-heading" id="weiterfuhrende-links">Weiterführende Links</h2>



<ul class="wp-block-list">
<li><strong><a href="https://robbelroot.de/blog/dein-vb-net-plugin-system-im-eigenbau-das-ultimative-beispiel/" target="_blank" rel="noreferrer noopener">Erstelle dein eigenes VB NET Plugin-System</a></strong></li>



<li><strong><a href="https://robbelroot.de/blog/vb-net-webbrowser-alternative/" target="_blank" rel="noreferrer noopener">Ein alternatives Webbrowser Steuerelement</a></strong></li>



<li><strong><a href="https://robbelroot.de/blog/vbnet-formular-automatisch-ausfuellen-winforms/" target="_blank" rel="noreferrer noopener">VB NET Formulare automatisch ausfüllen</a></strong></li>
</ul>
<p>Der Beitrag <a href="https://robbelroot.de/blog/davinci-resolve-marker-in-youtube-kapitel-umwandeln/">DaVinci Resolve Marker in YouTube-Kapitel umwandeln</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://robbelroot.de/blog/davinci-resolve-marker-in-youtube-kapitel-umwandeln/feed/</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
			</item>
		<item>
		<title>VB.NET data binding Tutorial</title>
		<link>https://robbelroot.de/blog/vbnet-data-binding-tutorial/</link>
					<comments>https://robbelroot.de/blog/vbnet-data-binding-tutorial/#comments</comments>
		
		<dc:creator><![CDATA[Robert Skibbe]]></dc:creator>
		<pubDate>Wed, 28 Jul 2021 19:06:27 +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[anleitung]]></category>
		<category><![CDATA[basic]]></category>
		<category><![CDATA[binden]]></category>
		<category><![CDATA[binding]]></category>
		<category><![CDATA[bindung]]></category>
		<category><![CDATA[checkbox]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[databinding]]></category>
		<category><![CDATA[daten]]></category>
		<category><![CDATA[datenbindung]]></category>
		<category><![CDATA[hilfe]]></category>
		<category><![CDATA[label]]></category>
		<category><![CDATA[textbox]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[vb]]></category>
		<category><![CDATA[vb.net]]></category>
		<category><![CDATA[vbnet]]></category>
		<category><![CDATA[visual]]></category>
		<category><![CDATA[visual basic]]></category>
		<guid isPermaLink="false">https://robbelroot.de/?p=4859</guid>

					<description><![CDATA[<p>VB.NET data binding Tutorial Lerne in diesem VB.NET data binding Tutorial, wie Du Steuerelemente und Daten sauber trennst, indem Du Daten an die Steuerelemente bindest. Besonders seit der WPF (Windows Presentation Foundation) konnte sich ein geordnetes Verhältnis zwischen Daten und dessen Darstellung etablieren. Eventuell hast Du noch später Interesse an &#8230;</p>
<p>Der Beitrag <a href="https://robbelroot.de/blog/vbnet-data-binding-tutorial/">VB.NET data binding Tutorial</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-DataBinding-Tutorial.png"><img loading="lazy" decoding="async" width="1024" height="536" src="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-DataBinding-Tutorial-1024x536.png" alt="VB.NET DataBinding Tutorial" class="wp-image-4861" title="VB.NET DataBinding Tutorial" srcset="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-DataBinding-Tutorial-1024x536.png 1024w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-DataBinding-Tutorial-300x157.png 300w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-DataBinding-Tutorial-768x402.png 768w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-DataBinding-Tutorial-700x366.png 700w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-DataBinding-Tutorial-332x174.png 332w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-DataBinding-Tutorial.png 1200w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a><figcaption>VB.NET DataBinding Tutorial</figcaption></figure>






<h2 class="wp-block-heading" id="vb-net-data-binding-tutorial">VB.NET data binding Tutorial</h2>



<p><strong>Lerne </strong>in diesem <strong>VB.NET data binding Tutorial</strong>, wie Du <strong>Steuerelemente </strong>und <strong>Daten </strong>sauber trennst, <strong>indem </strong>Du Daten an die Steuerelemente <strong>bindest</strong>.</p>



<p>Besonders <strong>seit </strong>der <strong>WPF </strong>(Windows Presentation Foundation) <strong>konnte</strong> sich ein geordnetes Verhältnis zwischen <strong>Daten und </strong>dessen <strong>Darstellung </strong>etablieren.</p>



<p><strong>Eventuell </strong>hast Du noch später <strong>Interesse </strong>an diesen Beiträgen: <strong><a href="https://robbelroot.de/blog/wpf-datatemplate/" target="_blank" rel="noreferrer noopener">WPF DataTemplate</a></strong>, <strong><a href="https://robbelroot.de/blog/wpf-stackpanel/" target="_blank" rel="noreferrer noopener">WPF StackPanel</a></strong>, <strong><a href="https://robbelroot.de/blog/mahapps-metro/" target="_blank" rel="noreferrer noopener">MahApps Metro</a></strong>.</p>



<h2 class="wp-block-heading" id="drum-binde-sich-wer-nicht-ewig-prufen-will">Drum binde sich, wer nicht ewig prüfen will</h2>



<p>Das <strong>sagte </strong>der deutsche Immunbiologe und Aphoristiker <strong>Gerhard Uhlenbruck</strong>.</p>



<p><strong>Wie recht </strong>er <strong>auch in </strong>der <strong>Programmierung </strong>mit dieser Aussage haben sollte, war Ihm denke ich nicht bewusst.</p>



<p>Die <strong>Bindung von Daten </strong>an entsprechende <strong>Steuerelemente </strong>bringt verschiedene <strong>Vorteile </strong>mit sich, wovon ich Einige gleich erklären werden.</p>



<h2 class="wp-block-heading" id="damals-war-alles-besser-winforms-zeiten">Damals war alles besser – WinForms-Zeiten</h2>



<p>Es <strong>gibt vermutlich </strong>immer wieder <strong>Verfechter </strong>der Aussage &#8222;damals war alles besser&#8220;, <strong>jedoch ist </strong>dem durchaus <strong>nicht immer so</strong>.</p>



<p>&#8222;<strong>Damals</strong>&#8220; zu WinForms-Zeiten wurde leider <strong>wie </strong>ich es zu <strong>oft erlebt </strong>habe <strong>nicht </strong>&#8222;<strong>sauber</strong>&#8220; mit Daten <strong>umgegangen</strong>.</p>



<p><strong>Einerseits </strong>bei den <strong>Datentypen</strong>, aber wegen vermeintlicher &#8222;Einfachheit&#8220; <strong>auch </strong>oft mit beabsichtigtem <strong>Verzicht auf Datenbindungen</strong>.</p>



<h2 class="wp-block-heading" id="eine-kleine-wiederholung-vb-net-data-binding-tutorial">Eine kleine Wiederholung –  VB.NET data binding Tutorial</h2>



<p>Im Zuge einer <strong>kleinen </strong>W<strong>i</strong>ederholung aus meinem &#8222;<strong><a href="https://robbelroot.de/blog/observablecollection/" target="_blank" rel="noreferrer noopener">ObservableCollection</a></strong>&#8220; Beitrag, möchte ich hier <strong>noch einmal </strong>ein kleines <strong>Beispiel aufgreifen</strong>.</p>



<p>Bei diesem Beispiel <strong>handelt es </strong>sich <strong>um </strong>die Darstellung von simplen <strong><a href="https://docs.microsoft.com/de-de/dotnet/api/microsoft.visualbasic.strings?view=net-5.0" target="_blank" rel="noreferrer noopener">Strings</a></strong> innerhalb einer <strong><a href="https://docs.microsoft.com/de-de/dotnet/api/system.windows.forms.listbox?view=net-5.0" target="_blank" rel="noreferrer noopener">ListBox</a></strong>.</p>



<p>Stelle Dir eine <strong>simple Auflistung </strong>von Strings vor, wo <strong>Elemente eingefügt</strong>, <strong>bearbeitet </strong>und auch <strong>entfernt </strong>werden können.</p>



<p>Eigentlich <strong>nichts </strong>wirklich <strong>Komplexes</strong>, <strong>schaue Dir </strong>allerdings einmal kurz das folgende <strong>Bild an</strong>.</p>



<p>In <strong>diesem Beispiel </strong>ist die <strong>ListBox </strong>tatsächlich <strong>an </strong>eine <strong>Liste </strong>als Datenquelle <strong>gebunden</strong>, <strong>jedoch </strong>gibt es <strong>Probleme</strong>.</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2021/07/DataBinding-mit-normaler-Liste.gif"><img loading="lazy" decoding="async" width="231" height="195" src="https://robbelroot.de/wp-content/uploads/2021/07/DataBinding-mit-normaler-Liste.gif" alt="DataBinding mit normaler Liste" class="wp-image-4838"/></a><figcaption>DataBinding mit normaler Liste</figcaption></figure>



<h3 class="wp-block-heading" id="darstellungsprobleme">Darstellungsprobleme</h3>



<p><strong>Wie </strong>Du <strong>im Bild</strong> erkennen kannst, <strong>gibt </strong>es <strong>Probleme </strong>mit der <strong>Darstellung</strong>, <strong>obwohl </strong>die <strong>Liste </strong>im Hintergrund Elemente beinhaltet.</p>



<p>Das sich <strong>Elemente in </strong>der <strong>Auflistung </strong>befinden, <strong>zeigt </strong>die beabsichtigt eingefügte <strong>MessageBox </strong>nach kurzer Verzögerung <strong>an</strong>.</p>



<p>Die <strong>nicht angezeigten</strong>, jedoch im Hintergrund <strong>befindlichen Elemente </strong>werden <strong>nicht dargestellt</strong>, <strong>weil </strong>die &#8222;ListBox&#8220; <strong>nichts </strong>von dem Hinzufügen <strong>mitbekommt</strong>.</p>



<p>Sie <strong>merkt </strong>also <strong>nicht</strong>, <strong>dass </strong>die sich dahinter befindliche <strong>Auflistung </strong>tatsächlich <strong>ändert</strong>, also Elemente <strong>hinzugefügt</strong>, oder auch <strong>gelöscht </strong>werden.</p>



<h3 class="wp-block-heading" id="datenbindung-mit-benachrichtigungsmechanismus-vb-net-data-binding-tutorial">Datenbindung mit Benachrichtigungsmechanismus –  VB.NET data binding Tutorial </h3>



<p>Das <strong>beschriebene Problem </strong>können wir mit einer Art <strong>Benachrichtigungsmechanismus </strong>lösen.</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2021/07/Indirekte-Modifikation-der-ListBox-durch-DatenQuelle.png"><img loading="lazy" decoding="async" width="646" height="385" src="https://robbelroot.de/wp-content/uploads/2021/07/Indirekte-Modifikation-der-ListBox-durch-DatenQuelle.png" alt="Indirekte Modifikation der ListBox durch DatenQuelle" class="wp-image-4830" title="Indirekte Modifikation der ListBox durch DatenQuelle" srcset="https://robbelroot.de/wp-content/uploads/2021/07/Indirekte-Modifikation-der-ListBox-durch-DatenQuelle.png 646w, https://robbelroot.de/wp-content/uploads/2021/07/Indirekte-Modifikation-der-ListBox-durch-DatenQuelle-300x179.png 300w, https://robbelroot.de/wp-content/uploads/2021/07/Indirekte-Modifikation-der-ListBox-durch-DatenQuelle-332x198.png 332w" sizes="auto, (max-width: 646px) 100vw, 646px" /></a><figcaption>Indirekte Modifikation der ListBox durch DatenQuelle</figcaption></figure>



<p><strong>Dabei wird </strong>die <strong>Veränderung </strong>der Datenquelle <strong>durch </strong>z. B. <strong>Ereignisse </strong>an die Abonnenten dieser Ereignisse <strong>kommuniziert</strong>.</p>



<p>Diesen <strong>Mechanismus </strong>können wir <strong>durch </strong>die <strong>Implementierung </strong>der &#8222;<strong>IRaiseItemChangedEvents</strong>&#8222;-Schnittstelle erreichen.</p>



<h3 class="wp-block-heading" id="vorhandene-bordmittel-nutzen">Vorhandene Bordmittel nutzen</h3>



<p><strong>Statt </strong>diese <strong>Schnittstelle </strong>selbst zu <strong>implementieren </strong>und somit eine eigene Klasse zu erstellen, <strong>können </strong>wir <strong>auch </strong>auf die <strong>bestehenden Klassen </strong>zurückgreifen.</p>



<p>Zum <strong>Glück </strong>gibt uns das .NET-<strong>Framework </strong>für <strong>WinForms</strong>-Anwendungen z. B. die &#8222;BindingList&#8220;-Klasse an die Hand.</p>



<p><strong>Für </strong>modernere <strong>WPF</strong>&#8211;<strong>Anwendungen </strong>(Windows Presentation Foundation) können wir die <strong>vorhandene </strong>&#8222;<strong>ObservableCollection</strong>&#8222;-Klasse verwenden.</p>



<p><strong>Nach </strong>diesem kleinen <strong>Rückblick </strong>werden wir unseren Blick <strong>nun </strong>in Richtung diverser <strong>Beispiele </strong>richten.</p>



<h2 class="wp-block-heading" id="databinding-voraussetzungen">Voraussetzungen –  VB.NET data binding Tutorial </h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB.NET-data-binding-Tutorial-Voraussetzungen.jpg"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/VB.NET-data-binding-Tutorial-Voraussetzungen.jpg" alt="VB.NET data binding Tutorial - Voraussetzungen" class="wp-image-9406" title="VB.NET data binding Tutorial - Voraussetzungen"/></a><figcaption>VB.NET data binding Tutorial &#8211; Voraussetzungen</figcaption></figure>



<pre class="wp-block-verse"><strong>Damit </strong>viele, bzw. gar alle der nächsten <strong>Beispiele funktionieren</strong>, musst Du bei der verwendeten Klasse die "<strong>INotifyPropertyChanged</strong>"-<strong>Schnittstelle implementieren</strong>.</pre>



<h3 class="wp-block-heading" id="implementierung-inotifypropertychanged-schnittstelle">Implementierung INotifyPropertyChanged-Schnittstelle</h3>



<p>Eine <strong>beispielhafte Implementierung </strong>der &#8220; <strong>INotifyPropertyChanged</strong>&#8222;-Schnittstelle kann wie gleich folgend aussehen.</p>



<p><strong>Ausgehend </strong>davon, dass wir eine <strong>Eigenschaft namens </strong>&#8222;TextBoxText&#8220; in der Form haben:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="VB.NET" data-enlighter-group="property-changed-helper">Private _textBoxtext As String

Public Property TextBoxText As String
    Get
        Return _textBoxtext
    End Get
    Set(value As String)
        If _textBoxtext = value Then
            Return
        End If
        _textBoxtext = value
        ' raise something like "hey, i changed my value!"
    End Set
End Property</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="property-changed-helper">private string _textBoxText;

public string TextBoxText
{
    get
    {
        return _textBoxText;
    }
    set
    {
        if (_textBoxText == value)
            return;
        _textBoxText = value;
        // raise something like "hey, i changed my value!"
    }
}</pre>



<p><strong>definieren </strong>wir zuerst einen <strong>privaten Member </strong>namens &#8222;<strong>_textBoxtext</strong>&#8222;, Welcher als <strong>Hintergrund</strong>&#8211;<strong>Feld für </strong>die <strong>Eigenschaft </strong>dient.</p>



<h3 class="wp-block-heading" id="eigenschaft-definieren">Eigenschaft definieren</h3>



<p><strong>Danach </strong>erstellen wir eine <strong>Eigenschaft</strong>, die dieses Feld verwendet, <strong>wobei </strong>der <strong>Setter </strong>hier das <strong>Interessante </strong>ist.</p>



<p>Der <strong>Setter überprüft </strong>zuerst, <strong>ob </strong>sich der aktuelle <strong>Wert </strong>des Feldes tatsächlich vom übergebenen Wert <strong>unterscheidet</strong>.</p>



<p><strong>Wenn </strong>die <strong>Werte </strong>sowieso schon die &#8222;<strong>Gleichen</strong>&#8220; <strong>sind</strong>, <strong>verlassen </strong>wir den <strong>Setter </strong>durch &#8222;Early-Return&#8220;.</p>



<p><strong>Da </strong>die <strong>Werte </strong>sich <strong>unterscheiden</strong>, <strong>wenn </strong>wir <strong>nicht </strong>vorher schon <strong>per Return</strong> rausgesprungen sind, <strong>setzen </strong>wir dann den <strong>neuen Wert</strong>.</p>



<h3 class="wp-block-heading" id="helper-methode-vb-net-data-binding-tutorial">Helper-Methode –  VB.NET data binding Tutorial </h3>



<p><strong>Nun </strong>kommen wir zum <strong>besonderen Part </strong>der <strong>Eigenschaft</strong>, denn <strong>jetzt kommunizieren </strong>wir die tatsächlichen <strong>Änderungen </strong>der Eigenschaft <strong>an </strong>eventuell <strong>Interessierte</strong>.</p>



<p><strong>Um </strong>dazu jedoch <strong>nicht </strong>für jede einzelne Property zig Zeilen <strong>Code zu wiederholen</strong>, <strong>habe </strong>ich mir eine kleine <strong>Helper-Methode geschrieben</strong>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="VB.NET" data-enlighter-group="property-changed-helper-method">Private Sub NotifyOfPropertyChange(&lt;CallerMemberName> Optional propertyName As String = Nothing)
    If propertyName Is Nothing Then
        Return
    End If
    RaiseEvent PropertyChanged(Me, New PropertyChangedEventArgs(propertyName))
End Sub</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="property-changed-helper-method">private void NotifyOfPropertyChange([CallerMemberName] string propertyName = null)
{
    if (propertyName == null)
        return;
    PropertyChanged(this, new PropertyChangedEventArgs(propertyName))
}</pre>



<h3 class="wp-block-heading" id="property-changed-auslösen">Änderungen der Eigenschaft kommunizieren</h3>



<p><strong>Ersetze </strong>den vorhin bereits <strong>angeführten Kommentar </strong>nun <strong>durch </strong>einen Aufruf der <strong>Helper-Methode</strong>.</p>



<p><strong>Beachte </strong>dabei, dass Du <strong>durch </strong>die tolle Funktionalität des &#8222;<strong>CallerMemberName</strong>&#8222;-Attributs des Parameters keinen Namen angeben musst.</p>



<p>Der <strong>Name </strong>der <strong>Eigenschaft wird </strong>hier im Setter durch dieses Attribut <strong>automatisch befüllt</strong>, lasse Dir den Wert ggf. einmal in verschiedenen Fällen ausgeben.</p>



<p><strong>Diese </strong>kleine <strong>Methode </strong>hat mir <strong>selbst schon </strong>sehr <strong>oft </strong>die Arbeit bei <strong>Datenbindungen </strong>wie in dem <strong>VB.NET data binding Tutorial</strong> erleichtert.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="VB.NET" data-enlighter-group="property-changed-auslösen">Private _textBoxtext As String

Public Property TextBoxText As String
    Get
        Return _textBoxtext
    End Get
    Set(value As String)
        If _textBoxtext = value Then
            Return
        End If
        _textBoxtext = value
        NotifyOfPropertyChange()
    End Set
End Property</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="property-changed-auslösen">private string _textBoxText;

public string TextBoxText
{
    get
    {
        return _textBoxText;
    }
    set
    {
        if (_textBoxText == value)
            return;
        _textBoxText = value;
        NotifyOfPropertyChange();
    }
}</pre>



<h3 class="wp-block-heading" id="wann-anderungen-kommuniziert-werden">Wann Änderungen kommuniziert werden </h3>



<pre class="wp-block-verse"><strong>Beachte</strong>, <strong>dass </strong>die <strong>TextBox </strong>die <strong>Änderungen </strong>selbst erst an die Eigenschaft kommuniziert, <strong>wenn </strong>Du die <strong>TextBox verlässt</strong>! Dies <strong>kannst Du </strong>anderweitig <strong>konfigurieren</strong>, <strong>indem </strong>Du <strong>weitere Parameter </strong>der <strong>Add</strong>-Methode definierst.</pre>



<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="">Add("Text", Me, "TextBoxText", False, DataSourceUpdateMode.OnPropertyChanged)</pre>



<p><strong>Genauer genommen </strong>geht es hier um den <strong>letzten Parameter </strong>namens &#8222;<strong>updateMode</strong>&#8222;, Welche wir nun einmal auf den Wert 1 (<strong>DataSourceUpdateMode.OnPropertyChanged</strong>) stellen.</p>



<h3 class="wp-block-heading" id="weitere-abhangige-eigenschaften">Weitere abhängige Eigenschaften</h3>



<p><strong>Vielleicht</strong> hast Du auch andere Eigenschaften, also zum <strong>Beispiel </strong>eine <strong>Eigenschaft </strong>wie &#8222;<strong>FullName</strong>&#8222;, Welche vom &#8222;<strong>First</strong>-&#8222;, sowie vom &#8222;<strong>LastName</strong>&#8220; abhängig ist.</p>



<p><strong>Dann </strong>kannst Du einen <strong>weiteren Aufruf </strong>der Helper-Methode <strong>mit </strong>der abhängigen <strong>Eigenschaft durchführen</strong>:</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="">NotifyOfPropertyChange("&lt;TheDependentProperty>")</pre>



<h2 class="wp-block-heading" id="winforms-beispiele-vb-net-data-binding-tutorial">WinForms Beispiele – VB.NET data binding Tutorial</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB.NET-data-binding-Tutorial-Winforms-Beispiele.jpg"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/VB.NET-data-binding-Tutorial-Winforms-Beispiele.jpg" alt="VB.NET data binding Tutorial - Winforms Beispiele" class="wp-image-9408" title="VB.NET data binding Tutorial - Winforms Beispiele"/></a><figcaption>VB.NET data binding Tutorial &#8211; Winforms Beispiele</figcaption></figure>



<p>In <strong>diesem Abschnitt </strong>versuche ich mit der Darstellung <strong>diverser Beispiele </strong>das <strong>Thema Datenbindung unter WinForms </strong>ein wenig näher zu bringen.</p>



<p><strong>Keine Sorge</strong>, in diesem <strong>VB.NET data binding Tutorial</strong> werde ich auch noch auf <strong>Beispiele </strong>für <strong>WPF </strong>eingehen, falls Du eher <strong>daran interessiert </strong>bist.</p>



<p><strong>Neben </strong>den bereits <strong>angeführten </strong>Auflistungen gibt es natürlich auch <strong>weitere Datenbindungen wie </strong>z. B. an einen <strong>Text</strong>.</p>



<p>Finde hier alle gängigen WinForms-Beispiele für das <strong>VB.NET data binding Tutorial</strong>.</p>



<h3 class="wp-block-heading" id="textbox-text-an-string-binden">TextBox Text an String binden</h3>



<p>Im <strong>ersten Beispiel </strong>realisieren wir eine kleine <strong>Datenbindung</strong>, Welche den eingegebenen <strong>Text </strong>einer <strong>TextBox </strong>innerhalb eines <strong>Labels widerspiegelt</strong>.</p>



<p>Als <strong>Eigenschaft </strong>verwende ich dafür das <strong><a href="#databinding-voraussetzungen">oben angeführte Beispiel</a></strong> aus den Voraussetzungen.</p>



<p><strong>Sage nun </strong>der <strong>TextBox </strong>durch folgenden Code, <strong>dass </strong>Sie Ihren <strong>Text</strong>, <strong>an</strong> die &#8222;TextBoxText&#8220;-<strong>Eigenschaft</strong> <strong>binden </strong>soll:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="VB.NET" data-enlighter-group="textbox-binding">tbTextBoxBinding.DataBindings.Add("Text", Me, "TextBoxText")</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="textbox-binding">tbTextBoxBinding.DataBindings.Add("Text", this, "TextBoxText");</pre>



<p><strong>Nun </strong>wird <strong>bereits </strong>die <strong>TextBox </strong>in der Lage sein, <strong>Änderungen </strong>an der Eigenschaft in ihrem Text <strong>widerzuspiegeln</strong>.</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="">TextBoxText = "My New Text"</pre>



<p><strong>Was </strong>allerdings <strong>noch fehlt</strong>, <strong>ist </strong>das oben angesprochene <strong>Label</strong>, Welches die <strong>Zeichenfolge ebenfalls darstellen </strong>soll.</p>



<p><strong>Dafür rufen </strong>wir auch einfach nur die &#8222;Add&#8220;-<strong>Methode </strong>der <strong>DataBindings</strong>-Eigenschaft des Labels auf:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="VB.NET" data-enlighter-group="label-binding">lblTextBoxBinding.DataBindings.Add("Text", Me, "TextBoxText")</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="label-binding">lblTextBoxBinding.DataBindings.Add("Text", this, "TextBoxText");</pre>



<p>Und <strong>schon </strong>ist das <strong>Label bereit</strong>, die <strong>Änderungen </strong>der &#8222;TextBoxText&#8220;-Eigenschaft ebenfalls <strong>darzustellen</strong>.</p>



<h3 class="wp-block-heading" id="checkbox-an-boolean-binden">CheckBox an Boolean binden</h3>



<p>Als <strong>nächstes </strong>beschäftigen wir uns mit der <strong>Datenbindung </strong>eines <strong>Booleans an </strong>eine <strong>CheckBox</strong>.</p>



<p><strong>Ziehe dafür </strong>auch hier ein <strong>Label und </strong>dieses mal eine <strong>CheckBox auf </strong>die <strong>Form</strong>, rufe danach <strong>folgende Anweisungen </strong>z. B. im Load-Ereignis der Form auf.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="VB.NET" data-enlighter-group="checkbox-binding">chbCheckBoxBinding.DataBindings.Add("Checked", Me, "CheckBoxBool", False, DataSourceUpdateMode.OnPropertyChanged)
lblCheckBoxBinding.DataBindings.Add("Text", Me, "CheckBoxBool", False, DataSourceUpdateMode.OnPropertyChanged)</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="checkbox-binding">chbCheckBoxBinding.DataBindings.Add("Checked", Me, "CheckBoxBool", False, DataSourceUpdateMode.OnPropertyChanged)
lblCheckBoxBinding.DataBindings.Add("Text", Me, "CheckBoxBool", False, DataSourceUpdateMode.OnPropertyChanged)</pre>



<h3 class="wp-block-heading" id="listbox-an-simple-auflistung-binden">ListBox an simple Auflistung binden</h3>



<p>Hier ein <strong>einfaches Beispiel</strong>, wie Du eine <strong>einfache Auflistung </strong>(Integer, String, ..) an eine <strong>ListBox binden </strong>kannst.</p>



<p><strong>Erstelle </strong>z. B. eine <strong>Eigenschaft </strong>namens &#8222;<strong>StringList</strong>&#8220; vom <strong>Typ BindingList</strong>, Welche gleich die einzelnen Items beinhalten wird.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="VB.NET" data-enlighter-group="string-list-binding">Public Property StringList As BindingList(Of String)</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="string-list-binding">public BindingList&lt;string> StringList { get;set; }</pre>



<p>Für das <strong>Download-Beispiel</strong> habe ich die folgende<strong> Methode </strong>definiert und im <strong>Load-Ereignishandler</strong> der Form <strong>aufgerufen</strong>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="VB.NET" data-enlighter-group="prepare-listbox-binding-method">Private Sub PrepareListBoxBinding()
    StringList = New BindingList(Of String)()
    For i = 1 To 3
        StringList.Add($"Item {i}")
    Next
    lbSimpleStringBinding.DataSource = StringList
End Sub</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="prepare-listbox-binding-method">private void PrepareListBoxBinding()
{
    StringList = new BindingList&lt;string>();
    for (var i = 0; i &lt;= 3; i++)
    {
        StringList.Add($"Item {i}");
    }
    lbSimpleStringBinding.DataSource = StringList;
}</pre>



<h3 class="wp-block-heading" id="datagridview-datenbindung-mit-objekten">DataGridView Datenbindung mit Objekten</h3>



<p><strong>Ähnlich wie </strong>mit der <strong>ListBox </strong>kann man <strong>auch bei </strong>einem <strong>DataGridView </strong>verfahren, das <strong>heißt </strong>man <strong>legt </strong>die <strong>DataSource </strong>fest <strong>und konfiguriert </strong>noch ein wenig.</p>



<p><strong>Standardmäßig generiert </strong>das <strong>DataGridView Spalten </strong>bei <strong>Datenbindungen automatisch</strong>, <strong>leider </strong>hat das dann <strong>auch ggf</strong>. <strong>ungewollte </strong>Spalten zur Folge.</p>



<p>Wir <strong>können </strong>dieses <strong>Verhalten deaktivieren</strong>, <strong>indem </strong>wir die &#8222;<strong>AutoGenerateColumns</strong>&#8222;-Eigenschaft <strong>auf False </strong>stellen <strong>und </strong>die <strong>Spalten </strong>z. B. im Designer <strong>selbst bestimmen</strong>.</p>



<pre class="wp-block-verse">Achtung! Die "<strong>AutoGenerateColumns</strong>"-Eigenschaft ist leider <strong>nicht in </strong>den <strong>Designer</strong>-<strong>Eigenschaften </strong>des <strong>DataGrids </strong>aufzufinden. <strong>Daher </strong>müssen wir Diese <strong>über </strong>den <strong>Code setzen</strong>.</pre>



<p>Gehe dazu <strong>im Designer auf </strong>die <strong>3 Punkte</strong> <strong>der &#8222;Columns&#8220;</strong>-Eigenschaft und klicke <strong>dann </strong>im erscheinenden Fenster <strong>links </strong>auf &#8222;<strong>Hinzufügen</strong>&#8222;.</p>



<p><strong>Gebe </strong>der <strong>Spalte </strong>dann einen <strong>eindeutigen Namen </strong>und <strong>lege </strong>den <strong>Typ </strong>sowie den <strong>Header</strong>&#8211;<strong>Text fest</strong>.</p>



<p>Wenn Du <strong>dann </strong>auf <strong>Hinzufügen und </strong>danach auf <strong>schließen </strong>klickst, kannst Du <strong>weitere Einstellungen </strong>der Spalte festlegen.</p>



<p>Die <strong>wichtigste Einstellung </strong>ist wohl der &#8222;DataPropertyName&#8220;, Dieser bestimmt, <strong>welche </strong>der <strong>Eigenschaften </strong>der gebundenen Elemente <strong>in </strong>dieser<strong> Spalte dargestellt </strong>werden.</p>



<h2 class="wp-block-heading" id="wpf-beispiele-vb-net-data-binding-tutorial">WPF Beispiele – VB.NET data binding Tutorial</h2>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2022/02/VB.NET-data-binding-Tutorial-WPF-Beispiele.jpg"><img loading="lazy" decoding="async" width="1200" height="628" src="https://robbelroot.de/wp-content/uploads/2022/02/VB.NET-data-binding-Tutorial-WPF-Beispiele.jpg" alt="VB.NET data binding Tutorial - WPF Beispiele" class="wp-image-9411" title="VB.NET data binding Tutorial - WPF Beispiele"/></a><figcaption>VB.NET data binding Tutorial &#8211; WPF Beispiele</figcaption></figure>



<p>Hier <strong>folgen </strong>nun einige <strong>WPF-Beispiele</strong> des  <strong>VB.NET data binding Tutorial</strong>, einen <strong>Vorgeschmack </strong>siehst Du im <strong>animierten Bild </strong>hier drunter:</p>



<figure class="wp-block-image size-full"><a href="https://robbelroot.de/wp-content/uploads/2021/07/WPF-DataBinding-Example.gif"><img loading="lazy" decoding="async" width="164" height="216" src="https://robbelroot.de/wp-content/uploads/2021/07/WPF-DataBinding-Example.gif" alt="WPF DataBinding Example" class="wp-image-4936" title="WPF DataBinding Example"/></a><figcaption>WPF DataBinding Example</figcaption></figure>



<h3 class="wp-block-heading" id="textbox-und-textblock-an-string-binden">TextBox und TextBlock an String binden</h3>



<p>Im ersten <strong>XAML Code</strong>, <strong>erstellen </strong>wir eine <strong>GroupBox mit </strong>einem <strong>StackPanel </strong>und den dann zu bindenden Controls.</p>



<p><strong>Darunter </strong>eine <strong>TextBox</strong>, um den <strong>Text einzugeben und </strong>der <strong>TextBlock</strong>, der die <strong>Daten sofort </strong>bei Änderung <strong>anzeigt</strong>.</p>



<p><strong>Wie </strong>auch <strong>im WinForms-Beispiel </strong>müssen wir den <strong>Zeitpunkt zum updaten </strong>der Daten konfigurieren.</p>



<p>Bei <strong>WPF</strong> machen wir das mit der &#8222;<strong>UpdateSourceTrigger</strong>&#8222;-Eigenschaft und dem Wert &#8222;<strong>PropertyChanged</strong>&#8222;.</p>



<p><strong>Auch hier </strong>kannst Du die <strong><a href="#property-changed-auslösen">Eigenschaft von oben</a></strong> verwenden.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;GroupBox Header="TextBox">
    &lt;StackPanel>
        &lt;TextBox Text="{Binding TextBoxText, UpdateSourceTrigger=PropertyChanged}" />
        &lt;TextBlock Text="{Binding TextBoxText}" />
    &lt;/StackPanel>
&lt;/GroupBox></pre>



<h3 class="wp-block-heading" id="checkbox-an-boolean-binden">CheckBox an Boolean binden</h3>



<p>Um eine <strong>CheckBox </strong>an einen <strong>Boolean </strong>zu <strong>binden</strong>, <strong>benötigst </strong>Du diesen hier drunter geschrieben <strong>XAML-Code</strong>.</p>



<p>Die &#8222;<strong>IsChecked</strong>&#8222;-<strong>Eigenschaft </strong>der CheckBox <strong>wird </strong>hier <strong>an </strong>die <strong>Boolean</strong>-Eigenschaft <strong>der Form </strong>namens &#8222;CheckBoxBool&#8220; <strong>gebunden</strong>.</p>



<p>Um <strong>Feedback </strong>zu <strong>erhalten </strong>und zu sehen, <strong>wann </strong>die <strong>Änderungen </strong>an die Eigenschaft <strong>kommuniziert </strong>werden, <strong>stelle </strong>ich das Ganze noch <strong>in </strong>einem <strong>TextBlock dar</strong>.</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;GroupBox Header="CheckBox">
    &lt;StackPanel>
        &lt;CheckBox IsChecked="{Binding CheckBoxBool}" />
        &lt;TextBlock Text="{Binding CheckBoxBool}" />
    &lt;/StackPanel>
&lt;/GroupBox></pre>



<h3 class="wp-block-heading" id="observablecollection-an-listbox-und-datagrid-binden">ObservableCollection an ListBox und DataGrid binden</h3>



<p><strong>Für </strong>die <strong>listen</strong>-basierten <strong>Beispiele </strong>wirst Du eine Auflistung wie Diese hier brauchen:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="VB.NET" data-enlighter-group="observable-customers-collection">Public Property Customers As ObservableCollection(Of Customer)</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="observable-customers-collection">public Customers As ObservableCollection&lt;Customer> { get; set; }</pre>



<p>Diese <strong>können </strong>wir dann wie folgt <strong>vorbereiten </strong>(am besten <strong>im Konstruktor aufrufen</strong>):</p>



<pre class="EnlighterJSRAW" data-enlighter-language="visualbasic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="VB.NET" data-enlighter-group="prepare-customers-observablecollection">Private Sub PrepareCustomers()
    Customers = New ObservableCollection(Of Customer)
    Customers.Add(New Customer("John", "Doe"))
    Customers.Add(New Customer("Maximilian", "Doe"))
    Customers.Add(New Customer("Robert", "Doe"))
End Sub</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="csharp" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="C#" data-enlighter-group="prepare-customers-observablecollection">private void PrepareCustomers()
    Customers = New ObservableCollection(Of Customer);
    Customers.Add(new Customer("John", "Doe"));
    Customers.Add(new Customer("Maximilian", "Doe"));
    Customers.Add(new Customer("Robert", "Doe"));
End Sub</pre>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;GroupBox Header="ListBox" Height="80">
    &lt;ScrollViewer VerticalScrollBarVisibility="Auto">
        &lt;ListBox ItemsSource="{Binding Customers }" />
    &lt;/ScrollViewer>
&lt;/GroupBox></pre>



<p><strong>Hier </strong>siehst Du einen beispielhaften <strong>Code</strong>, um die <strong>Customers als Datenquelle </strong>für eine ListBox zu verwenden:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;GroupBox Header="DataGrid" Height="80">
    &lt;DataGrid ItemsSource="{Binding Customers }" AutoGenerateColumns="False">
        &lt;DataGrid.Columns>
            &lt;DataGridTextColumn Binding="{Binding FirstName}" />
            &lt;DataGridTextColumn Binding="{Binding LastName}" />
        &lt;/DataGrid.Columns>
    &lt;/DataGrid>
&lt;/GroupBox></pre>



<p>Und <strong>hier </strong>wäre der passende <strong>Code für </strong>das <strong>DataGrid</strong>:</p>



<pre class="EnlighterJSRAW" data-enlighter-language="xml" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">&lt;GroupBox Header="DataGrid" Height="80">
    &lt;DataGrid ItemsSource="{Binding Customers }" AutoGenerateColumns="False">
        &lt;DataGrid.Columns>
            &lt;DataGridTextColumn Binding="{Binding FirstName}" />
            &lt;DataGridTextColumn Binding="{Binding LastName}" />
        &lt;/DataGrid.Columns>
    &lt;/DataGrid>
&lt;/GroupBox></pre>



<h2 class="wp-block-heading" id="downloads-vb-net-data-binding-tutorial">Downloads –  VB.NET data binding Tutorial</h2>



<p>Hier findest Du alle Downloads zum <strong>VB.NET data binding Tutorial</strong>:</p>



<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="/downloads/vbnet/WinFormsDataBindingExampleVB.zip" target="_blank" rel="noreferrer noopener">WinFormsDataBindingExampleVB.zip</a></div>



<div class="wp-block-button"><a class="wp-block-button__link" href="/downloads/cs/WinFormsDataBindingExampleCS.zip" target="_blank" rel="noreferrer noopener">WinFormsDataBindingExampleCS.zip</a></div>



<div class="wp-block-button"><a class="wp-block-button__link" href="/downloads/vbnet/WpfDatabindingExampleVB.zip" target="_blank" rel="noreferrer noopener">WpfDatabindingExampleVB.zip</a></div>



<div class="wp-block-button"><a class="wp-block-button__link" href="/downloads/cs/WpfDatabindingExampleCS.zip" target="_blank" rel="noreferrer noopener">WpfDatabindingExampleCS.zip</a></div>
</div>



<p></p>
<p>Der Beitrag <a href="https://robbelroot.de/blog/vbnet-data-binding-tutorial/">VB.NET data binding Tutorial</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://robbelroot.de/blog/vbnet-data-binding-tutorial/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>VB.NET Game – Dein Eigenes TicTacToe Spiel</title>
		<link>https://robbelroot.de/blog/vbnet-game-dein-eigenes-tictactoe-spiel/</link>
					<comments>https://robbelroot.de/blog/vbnet-game-dein-eigenes-tictactoe-spiel/#comments</comments>
		
		<dc:creator><![CDATA[Robert Skibbe]]></dc:creator>
		<pubDate>Thu, 15 Jul 2021 02:02:10 +0000</pubDate>
				<category><![CDATA[Allgemein]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Visual Basic .NET]]></category>
		<category><![CDATA[Visual Basic .NET Problemlösungen]]></category>
		<category><![CDATA[2d]]></category>
		<category><![CDATA[beitrag]]></category>
		<category><![CDATA[custom]]></category>
		<category><![CDATA[eigenes]]></category>
		<category><![CDATA[game]]></category>
		<category><![CDATA[post]]></category>
		<category><![CDATA[spiel]]></category>
		<category><![CDATA[spielen]]></category>
		<category><![CDATA[tictactoe]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[vb.net]]></category>
		<category><![CDATA[vbnet]]></category>
		<guid isPermaLink="false">https://robbelroot.de/?p=3959</guid>

					<description><![CDATA[<p>VB.NET Game erstellen Du möchtest Dein eigenes VB.NET Game entwickeln und dies sauber und mit Spaß umsetzen? Dann bist Du mit deiner Recherche genau auf den richtigen Beitrag meiner Seite gestoßen! Vielleicht interessieren Dich später auch noch die folgenden Beiträge: VB.NET Slot Machine, AddHandler-Anweisung, Sleep-Timer. Vorschau auf das Spiel Hier &#8230;</p>
<p>Der Beitrag <a href="https://robbelroot.de/blog/vbnet-game-dein-eigenes-tictactoe-spiel/">VB.NET Game – Dein Eigenes TicTacToe Spiel</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></description>
										<content:encoded><![CDATA[





<h2 class="wp-block-heading">VB.NET Game erstellen</h2>



<p>Du <strong>möchtest </strong>Dein eigenes <strong>VB.NET Game</strong> entwickeln und dies <strong>sauber und </strong>mit <strong>Spaß </strong>umsetzen?</p>



<p><strong>Dann </strong>bist Du mit deiner Recherche <strong>genau </strong>auf den <strong>richtigen Beitrag</strong> meiner Seite gestoßen!</p>



<p><strong>Vielleicht </strong>interessieren Dich später <strong>auch </strong>noch die <strong>folgenden Beiträge</strong>: <strong><a href="/blog/vbnet-slot-machine-einarmiger-bandit" target="_blank" rel="noreferrer noopener">VB.NET Slot Machine</a></strong>, <strong><a href="/blog/vbnet-addhandler" target="_blank" rel="noreferrer noopener">AddHandler-Anweisung</a></strong>, <strong><a href="/blog/vbnet-sleep-timer" target="_blank" rel="noreferrer noopener">Sleep-Timer</a></strong>.</p>



<h2 class="wp-block-heading">Vorschau auf das Spiel</h2>



<p>Hier siehst Du im Sinne einer <strong>Vorschau </strong>ein <strong>kleines Bild </strong>von dem <strong>fertigen Spiel</strong>.</p>



<figure class="wp-block-image size-large"><a href="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-Game-TicTacToe.png"><img loading="lazy" decoding="async" width="646" height="487" src="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-Game-TicTacToe.png" alt="VB.NET Game TicTacToe" class="wp-image-3999" title="VB.NET Game TicTacToe" srcset="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-Game-TicTacToe.png 646w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-Game-TicTacToe-300x226.png 300w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-Game-TicTacToe-332x250.png 332w" sizes="auto, (max-width: 646px) 100vw, 646px" /></a><figcaption>VB.NET Game TicTacToe</figcaption></figure>



<h2 class="wp-block-heading">Vorwort – VB.NET Game</h2>



<p>Ich <strong>möchte </strong>an dieser Stelle direkt <strong>anführen</strong>, <strong>dass </strong>ich dieses <strong>Beispiel </strong>hier so <strong>einfach und nachvollziehbar </strong>wie möglich gestalten möchte.</p>



<p><strong>Daher </strong>werden wir hier <strong>für </strong>den <strong>Anfang bei </strong>einem <strong>simplen und </strong>jedem <strong>bekannten Spiel </strong>bleiben.</p>



<p>In <strong>späteren Beiträgen </strong>werde ich aber sehr sehr wahrscheinlich noch <strong>andere </strong>kleine <strong>Spiele vorstellen </strong>und <strong>erläutern</strong>.</p>



<p>Es wird hier also leider <strong>nicht das nächste Call of Duty</strong> erstellt &#x1f61b;!</p>



<p>Ich werden hier ausschließlich <strong>vorhandene Steuerelemente </strong>und verwende auch <strong>kein GDI+</strong>.</p>



<p>Das <strong>kommt eventuell noch </strong>in einer verbesserten Version, also <strong>für </strong>eventuelle <strong>fortgeschrittenere </strong>Personen.</p>



<h2 class="wp-block-heading">Ein eigenes Spiel – Der Traum vieler Entwickler</h2>



<p><strong>Viele Entwickler </strong>– so auch ich damals – <strong>finden </strong>ihren <strong>Weg </strong>teilweise gerade <strong>durch </strong>das Interesse am <strong>Gaming zur Softwareentwicklung</strong>.</p>



<p><strong>Manchmal </strong>haben Sie dabei auch <strong>direkt eigene Pläne</strong>, <strong>vielleicht </strong>sogar <strong>schon </strong>ein <strong>konkretes Ziel</strong>: &#8222;Ich <strong>möchte </strong>mein <strong>eigenes Spiel </strong>entwickeln!&#8220;.</p>



<p><strong>Häufiger Stand </strong>dabei ist <strong>leider</strong>: Keine <strong>Vorerfahrung</strong>, mangelnde <strong>Grammatik </strong>(man weiß nicht wonach man suchen muss) und vor allem so <strong>mancher Code </strong>der im <strong>Netz kreucht </strong>und fleucht.</p>



<h2 class="wp-block-heading">Der Blick zurück</h2>



<p><strong>Mannoman</strong>, <strong>wenn ich </strong>so an meine eigenen Anfänge der .NET Entwicklung <strong>zurückdenke</strong>, dann denke ich <strong>vor</strong> <strong>allem an </strong>mein <strong>Interesse</strong>, einen <strong>eigenen Bot </strong>zu bauen.</p>



<p>Ich <strong>war damals </strong>zugegebenermaßen <strong>so dämlich</strong>, mir eine <strong>Fremdsoftware von </strong>einem <strong>dubiosen Anbieter </strong>herunterzuladen und <strong>zu verwenden</strong>.</p>



<p>Diese <strong>führte </strong>dann leider <strong>dazu</strong>, <strong>dass ich </strong>meinen <strong>Account </strong>von einem Spiel was ich zu der Zeit gespielt habe, <strong>verloren </strong>habe – ich <strong>wurde gehackt</strong>.</p>



<p>Bei diesem <strong>Spiel handelte </strong>es sich <strong>um </strong>ein typisches <strong>China</strong>&#8211;<strong>Grinder</strong>, sprich <strong>viele Stunden </strong>mit <strong>leveln </strong>zu verbringen <strong>und wenige </strong>bis kaum <strong>Erfolge</strong>, bzw. Level-Ups!</p>



<h2 class="wp-block-heading">Die Hürden – VB.NET Game</h2>



<p><strong>Wie </strong>ich <strong>oben </strong>schon <strong>angesprochen</strong> habe, hat man <strong>vor allem am Anfang </strong>viele <strong>Hürden </strong>zu bekämpfen <strong>und </strong>die <strong>vielen Argumente </strong>aus Foren <strong>wie </strong>&#8222;Lerne die Basics&#8220; <strong>will man </strong>natürlich <strong>nicht hören</strong>.</p>



<p>Natürlich sind diese Argumente mehr als berechtigt und durchaus sinnvoll, aber man möchte dies wie erwähnt nicht hören.</p>



<p><strong>Ich selbst hatte </strong>dort schon immer einen <strong>Sturkopf und wenn </strong>ich nun einmal ein <strong>Spiel bauen wollte</strong>, <strong>dann wollte ich </strong>halt ein Spiel bauen..</p>



<p><strong>Dann </strong>bekam man auch z. B. noch <strong>weitere Kritik </strong>zu hören, dass <strong>VB.NET nicht dafür geeignet</strong> sei, ein Spiel zu programmieren.</p>



<p><strong>GDI+</strong> wäre <strong>zu langsam </strong>und Prozessor-lastig und und und..</p>



<p>Die <strong>traurige Wahrheit </strong>ist, dass diese <strong>Aussagen erstmal </strong>soweit alle <strong>im Kern stimmen </strong>– im Kern!</p>



<p><strong>Wer allerdings </strong>seine <strong>ersten Erfahrungen mit kleinen Spielen </strong>wie TicTacToe, Schiffe versenken, oder ähnlichen Spielen <strong>sammeln möchte </strong>– mein Gott, &#8222;Do It!&#8220;.</p>



<p>Du <strong>wirst vermutlich </strong>nur <strong>nicht </strong>das <strong>grafische Wunder </strong>schlechthin mit VB.NET, GDI+ und Co. bauen können, das <strong>muss </strong>Dir <strong>klar sein</strong>.</p>



<h2 class="wp-block-heading">Los geht&#8217;s TicTacToe – VB.NET Game</h2>



<p>Jetzt beginnen wir mit unserem kleinen TicTacToe-Game und fangen beim Begrüßungsbildschirm an.</p>



<h3 class="wp-block-heading">Begrüßungsbildschirm</h3>



<p>Wie man es <strong>aus Spielen kennt</strong>, sieht man <strong>am Anfang </strong>einen kleinen <strong>Begrüßungsbildschirm</strong>, Welchen <strong>auch wir </strong>fix <strong>einbauen </strong>werden.</p>



<p id="projekt-modus-fuer-herunterfahren"><strong>Stelle </strong>zuerst in den <strong>Projekteigenschaften </strong>(Projekt-&gt;Eigenschaften-&gt;Anwendung) den <strong>Modus für </strong>das <strong>Herunterfahren auf</strong> &#8222;beim <strong>schließen des letzten Formulars</strong>&#8222;.</p>



<figure class="wp-block-image size-large"><a href="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-Game-TicTacToe-Begruessungsbildschirm.png"><img loading="lazy" decoding="async" width="644" height="484" src="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-Game-TicTacToe-Begruessungsbildschirm.png" alt="VB.NET Game TicTacToe Begrüßungsbildschirm" class="wp-image-3979" title="VB.NET Game TicTacToe Begrüßungsbildschirm" srcset="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-Game-TicTacToe-Begruessungsbildschirm.png 644w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-Game-TicTacToe-Begruessungsbildschirm-300x225.png 300w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-Game-TicTacToe-Begruessungsbildschirm-332x250.png 332w" sizes="auto, (max-width: 644px) 100vw, 644px" /></a><figcaption>VB.NET Game TicTacToe Begrüßungsbildschirm</figcaption></figure>



<h4 class="wp-block-heading">Form-Eigenschaften</h4>



<p>Dafür <strong>ändern </strong>wir die <strong>Eigenschaften </strong>der aktuellen <strong>Hauptform </strong>(meist Form) auf:</p>



<ul class="wp-block-list"><li><strong>Name</strong>: frmWelcome</li><li><strong>DoubleBuffered</strong>: True</li><li><strong>StartPosition</strong>: CenterScreen</li><li><strong>Size</strong>: 640; 480</li><li><strong>FormBorderStyle</strong>: None</li><li><strong>BackgroundImage</strong>: bg.jpg</li></ul>



<p>Das <strong>Hintergrundbild </strong>findest Du als <strong>Bestandteil des Beispielprojekts </strong>in den <strong>Downloads weiter unten</strong>.</p>



<p>Füge das <strong>Hintergrundbild als Ressource </strong>unter <strong>Projekt</strong>-&gt;<strong>Eigenschaften</strong>-&gt;<strong>Ressourcen </strong>durch z. B. <strong>Drag &amp; Drop</strong> hinzu.</p>



<p><strong>Danach erscheint </strong>das <strong>Bild </strong>auch <strong>im Ressourcen</strong>-Ordner im <strong>Projektmappen</strong>&#8211;<strong>Explorer</strong>.</p>



<h4 class="wp-block-heading">Titel-Label</h4>



<p>Füge <strong>als nächstes </strong>ein <strong>Label für </strong>den <strong>Titel </strong>hinzu und stelle die <strong>Eigenschaften </strong>wie es Dir passt ein, für mein Beispiel jedoch <strong>wie folgt</strong>:</p>



<ul class="wp-block-list"><li><strong>Name</strong>: lblTitle</li><li><strong>BackColor</strong>: Transparent</li><li><strong>Font</strong>: Arial Black; 48pt; style=Bold</li><li><strong>ForeColor</strong>: White</li><li><strong>Text</strong>: TicTacToe</li></ul>



<p>Und <strong>positioniere </strong>das <strong>Label </strong>nach deinem Belieben <strong>auf </strong>der <strong>Form</strong>.</p>



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



<p>Nimm im <strong>nächsten Schritt 2 Buttons</strong> zur Hand und stelle diese Eigenschaften ein:</p>



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



<p>Ein <strong>Knopf zum Starten </strong>des Spiels:</p>



<ul class="wp-block-list"><li><strong>Name</strong>: btnStartGame</li><li><strong>BackColor</strong>: LimeGreen</li><li><strong>FlatAppearance</strong>: BorderColor Black, BorderSize 3</li><li><strong>FlatStyle</strong>: Flat</li><li><strong>Font</strong>: Arial; 12pt; style=Bold</li><li><strong>Size</strong>: 196; 55</li><li><strong>Text</strong>: Start Game</li></ul>



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



<p>Der <strong>Knopf zum Beenden </strong>des Spiels (vor Beginn).</p>



<p>Hat <strong>soweit </strong>die <strong>gleichen Eigenschaften </strong>wie der andere Button, nur eventuell eine andere <strong>Farbe </strong>inkl. <strong>Text</strong>.</p>



<ul class="wp-block-list"><li><strong>Name</strong>: btnExit</li><li><strong>BackColor</strong>: IndianRed</li><li><strong>Text</strong>: Exit</li></ul>



<h4 class="wp-block-heading">Die Button-Handler </h4>



<p>Gehen wir <strong>nun z. B. durch</strong> einen <strong>Doppelklick </strong>auf die Buttons, <strong>in </strong>die <strong>Handler der Buttons </strong>und fügen folgenden Code ein:</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 frmWelcome

    Private Sub btnStartGame_Click(sender As Object, e As EventArgs) Handles btnStartGame.Click
        Dim frmTicTacToe = New frmTicTacToe()
        frmTicTacToe.Show()
        Close()
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Application.Exit()
    End Sub

End Class</pre>



<p>Beim <strong>Klicken </strong>des <strong>Start</strong>-Buttons wird die <strong>aktuelle</strong>, also die <strong>Begrüßungsform geschlossen </strong>und <strong>anschließend </strong>die <strong>andere </strong>Form <strong>angezeigt</strong>.</p>



<p><strong>Denke </strong>hier <strong>an </strong>den <strong><a href="#projekt-modus-fuer-herunterfahren">obigen Hinweis</a></strong>, <strong>sonst </strong>wird sich hier das gesamte <strong>Programm schließen</strong>.</p>



<p><strong>Danach </strong>wird eine neue <strong>Instanz </strong>der <strong>TicTacToe-Form</strong> erstellt und <strong>angezeigt</strong> und schon geht es los.</p>



<p>Der Schließen-Button macht nichts anderes als – wer hätte es gedacht – die <strong>Anwendung </strong>zu <strong>schließen</strong>.</p>



<p>Man <strong>könnte </strong>hier <strong>auch </strong>einfach <strong>Close </strong>allein <strong>verwenden</strong>, da wir <strong>vorher kein </strong>anderes <strong>Formular angezeigt </strong>haben.</p>



<h3 class="wp-block-heading">TicTacToe VB.NET Game Formular</h3>



<p><strong>Nun </strong>geht es mit dem <strong><a href="https://docs.microsoft.com/de-de/dotnet/api/system.windows.forms.form?view=net-5.0" target="_blank" rel="noreferrer noopener">Formular</a> für </strong>das <strong>Spiel </strong>selbst <strong>und </strong>der <strong>Logik </strong>dahinter weiter.</p>



<p><strong>Füge </strong>dazu ein <strong>weiteres Formular </strong>hinzu und setze <strong>folgende Eigenschaften</strong>:</p>



<ul class="wp-block-list"><li><strong>Name</strong>: frmTicTacToe</li><li><strong>StartPosition</strong>: CenterScreen</li><li><strong>Text</strong>: TicTacToeExample</li><li><strong>Size</strong>: 640; 480</li><li><strong>FormBorderStyle</strong>: None</li><li><strong>DoubleBuffered</strong>: True</li></ul>



<p>Danach brauchst Du ein <strong><a href="https://docs.microsoft.com/de-de/dotnet/api/system.windows.forms.splitcontainer?view=net-5.0" target="_blank" rel="noreferrer noopener">SplitContainer</a></strong>, Welcher das <strong>Formular </strong>in 2 Bereiche <strong>einteilt</strong>:</p>



<ul class="wp-block-list"><li><strong>Name</strong>: splcGame</li><li><strong>Dock</strong>: Fill</li><li><strong>FixedPanel</strong>: Panel1</li><li><strong>IsSplitterFixed</strong>: True</li><li><strong>Size</strong>: 640; 480</li><li><strong>SplitterDistance</strong>: 156</li><li><strong>SplitterWidth</strong>: 1</li></ul>



<p>In den <strong>rechten Bereich </strong>des <strong>SplitContainers</strong> fügst Du nun ein <strong><a href="https://docs.microsoft.com/de-de/dotnet/api/system.windows.forms.tablelayoutpanel?view=net-5.0" target="_blank" rel="noreferrer noopener">TableLayoutPanel</a></strong> hinzu:</p>



<ul class="wp-block-list"><li><strong>Name</strong>: tlpGame</li><li><strong>BackgroundImage</strong>: game_bg</li><li><strong>BackgroundImageLayout</strong>: Stretch</li><li><strong>ColumnCount</strong>: 3</li><li><strong>RowCount</strong>: 3</li><li><strong>Dock</strong>: Fill</li></ul>



<p>Den <strong>linken Bereich </strong>des <strong>SplitContainers</strong>, also das <strong>Panel1</strong> bekommt folgende Eigenschaften:</p>



<ul class="wp-block-list"><li><strong>BackgroundImage</strong>: sidebar</li></ul>



<p><strong>Danach </strong>füge <strong>4 Labels</strong> und <strong>2 Buttons</strong> hinzu, wovon <strong>2 Labels als Anzeige</strong> dienen (<strong>Rest-Zeit</strong> und <strong>welcher Spieler dran</strong> ist) und <strong>2 als Beschreibung</strong> dafür.</p>



<p>Die <strong>beiden Buttons </strong>sind letztendlich für das <strong>Schließen </strong>der <strong>Anwendung und </strong>dem <strong>Zurückkehren </strong>in das <strong>Hauptmenü</strong> zuständig.</p>



<h2 class="wp-block-heading">Code des Spiels – VB.NET Game</h2>



<h3 class="wp-block-heading">Eigenschaften</h3>



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



<p>Eine Instanz der <strong><a href="https://docs.microsoft.com/de-de/dotnet/api/system.random?view=net-5.0" target="_blank" rel="noreferrer noopener">Random-Klasse</a></strong> <strong>um </strong>uns das zufällige <strong>Auslosen </strong>des <strong>Start-Spielers</strong> zu ermöglichen.</p>



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



<p>Ein <strong>Objekt aus </strong>den <strong>Ressourcen</strong>, <strong>einmalig abgerufen </strong>um den <strong>Vergleich </strong>und das <strong>Setzen </strong>der <strong>Bilder </strong>zu ermöglichen.</p>



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



<p><strong>Analog </strong>zum Bild hier <strong>drüber</strong>..</p>



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



<p>Ein Boolean, Welcher den aktuellen <strong>an der Reihe </strong>befindlichen <strong>Spieler widerspiegelt</strong>.</p>



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



<p>Auf <strong>PlayerTurn basierender boolischer Wert</strong>, Welcher angibt <strong>ob Spieler </strong>1 gerade <strong>dran </strong>ist.</p>



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



<p><strong>Analog </strong>zum Wert <strong>hier drüber</strong>, nur für Spieler 2.</p>



<h3 class="wp-block-heading">Konstruktor</h3>



<h4 class="wp-block-heading">new()</h4>



<p>Der <strong>Konstruktor instanziiert die Random-Klasse und setzt </strong>einmalig die <strong>Bilder</strong>, Welche <strong>später für </strong>die <strong>Pictureboxen </strong>(Spiele-Token) verwendet werden.</p>



<p><strong>Zum Schluss </strong>laden wir die <strong>Pictureboxen in </strong>einer <strong>Schleife</strong>, um nicht 9 mal das Gleiche Prozedere im Designer <strong>machen </strong>zu <strong>müssen</strong>.</p>



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



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



<p><strong>Generiert alle </strong>9 <strong>Pictureboxen </strong>und <strong>fügt </strong>Sie dem TableLayoutPanel <strong>hinzu</strong>.</p>



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



<p><strong>Erstellt </strong>eine <strong>Picturebox mit </strong>den gewünschten <strong>Styles und </strong>dem benötigten <strong>Klick</strong>&#8211;<strong>Ereignishandler</strong>.</p>



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



<p>Wenn eine Picturebox geklickt wird, wird die <strong>Picturebox </strong>mit <strong>Typecasting ausgemacht</strong>.</p>



<p><strong>Danach </strong>wird überprüft, <strong>ob </strong>das <strong>Bild </strong>der Picturebox <strong>schon gesetzt </strong>wurde, <strong>wenn ja</strong>, wird die <strong>Sub </strong>durch <strong>Early-Return</strong> <strong>verlassen</strong>.</p>



<p>Danach <strong>wird </strong>das <strong>Bild </strong>(also das X, oder der Kreis) vom jeweiligen Spieler <strong>gesetzt</strong>.</p>



<p><strong>Zum Schluss </strong>wird geprüft, <strong>ob </strong>ein <strong>Spieler gewonnen </strong>hat und <strong>wenn ja</strong>, gefragt ob man <strong>noch </strong>ein <strong>Match spielen </strong>möchte.</p>



<p><strong>Alternativ wird </strong>nach einem <strong>Unentschieden </strong>geschaut <strong>und </strong>zu guter Letzt die Runde ansonsten <strong>einfach an </strong>den nächsten <strong>Spieler abgegeben</strong>.</p>



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



<p><strong>Je </strong>nachdem <strong>welcher Spieler </strong>dran ist, wird eine <strong>weitere Hilfsmethode </strong>zum <strong>Setzen </strong>des jeweiligen <strong>Spieler</strong>&#8211;<strong>Bildes </strong>aufgerufen.</p>



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



<p><strong>Setzt </strong>das <strong>Bild </strong>des <strong>Spielers 1</strong> in die Picturebox.</p>



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



<p><strong>Anlog </strong>zur vorherigen Methode, nur <strong>für Spieler 2</strong>.</p>



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



<p><strong>Überprüft </strong>mit weiteren <strong>Hilfsmethoden</strong>, ob ein <strong>Spieler gewonnen </strong>hat.</p>



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



<p>Sehr <strong>hilfreiche Methode</strong>, um <strong>jeweils </strong>die <strong>Pictureboxen </strong>zu <strong>überprüfen</strong>, <strong>ob </strong>Diese <strong>alle </strong>das <strong>gleiche Bild </strong>haben und <strong>bei dreien </strong>dementsprechend <strong>gewonnen </strong>wurde.</p>



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



<p><strong>Fragt </strong>den Nutzer <strong>nach </strong>einem &#8222;<strong>Rematch</strong>&#8220; und <strong>startet </strong>das <strong>Spiel </strong>bei &#8222;Ja&#8220; <strong>neu</strong>, oder <strong>schließt </strong>das <strong>Formular</strong>.</p>



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



<p><strong>Fragt </strong>den Nutzer <strong>nach </strong>einem &#8222;<strong>Unentschieden-Rematch</strong>&#8220; und verfährt <strong>analog </strong>der <strong>obigen Methode</strong>.</p>



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



<p><strong>Startet </strong>ein <strong>neues Spiel</strong>, indem die <strong>Bilder zurückgesetzt </strong>werden und einen neuen <strong>zufälligen Start</strong>&#8211;<strong>Spieler </strong>wählt.</p>



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



<p>Setzt die Bilder zurück.</p>



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



<p><strong>Funktion</strong>, Welche <strong>überprüft</strong>, ob <strong>alle Spiel-Tokens</strong> (alle Bilder) gesetzt wurden.</p>



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



<p><strong>Wechselt </strong>das &#8222;wer ist gerade dran&#8220;-Flag, <strong>updatet </strong>das <strong>Label</strong>, Welche den <strong>aktuellen Spieler </strong>darstellt und startet den <strong>Countdown neu</strong>.</p>



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



<p>Setzt den Label-Text, Welcher den aktuellen Spieler anzeigt.</p>



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



<p><strong>Setzt </strong>das <strong>Countdown-Label</strong> und startet den <strong>Countdown-Timer</strong>.</p>



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



<p>Der &#8222;Form-Load&#8220;-<strong>Ereignishandler</strong>, Welcher das <strong>Spiel initial startet</strong>.</p>



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



<p>Diese <strong>Sub wählt</strong> einen <strong>zufälligen Spieler</strong>, Welcher mit seinem Zug startet.</p>



<p>Dabei generieren wir eine <strong>neue zufällige Zahl</strong> und <strong>prüfen </strong>mit dem Modulus-Operator (Wert 2), <strong>ob </strong>Diese eine <strong>gerade </strong>Zahl <strong>ist</strong>.</p>



<p><strong>Wenn </strong>die <strong>Zahl gerade </strong>ist, dann <strong>beginnt </strong>der <strong>Player 1</strong>, <strong>sonst Player 2</strong>.</p>



<p>Danach wird auch hier <strong>dargestellt welcher Player </strong>dran ist und der <strong>Countdown</strong> <strong>gestartet</strong>.</p>



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



<p>Der <strong>Tick-Ereignishandler</strong> des Timers, Welcher <strong>jede Sekunde </strong>nach Start <strong>tickt</strong>.</p>



<p>Solange der <strong>Timer noch nicht </strong>das <strong>Ende </strong>erreicht hat (größer als 0 ist) wird <strong>heruntergezählt und </strong>das Time-<strong>Label aktualisiert</strong>.</p>



<p><strong>Wenn </strong>das <strong>Ende erreicht </strong>ist, dann wird der <strong>Timer gestoppt </strong>und der <strong>Spieler gewechselt </strong>inkl. <strong>Timer neugestartet</strong>.</p>



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



<p>Setzt den <strong>aktuellen </strong>Zeit-<strong>Wert in </strong>das Time-<strong>Label </strong>und <strong>zeigt </strong>somit die <strong>verbleibende Zeit für </strong>die <strong>Runde</strong>.</p>



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



<p>Soll den <strong>Timer sauber beenden</strong>, falls Dieser zur Zeit des &#8222;<strong>Form-Closing</strong>&#8222;-Events noch läuft.</p>



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



<p>Der <strong>Klick</strong>&#8211;<strong>Handler </strong>für den &#8222;Zurück-Button&#8220;, Welcher die <strong>Start-Form</strong> erneut <strong>anzeigt </strong>und die <strong>aktuelle </strong>Form <strong>schließt</strong>.</p>



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



<p><strong>Beendet </strong>die <strong>Anwendung</strong>.</p>



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



<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 frmWelcome

    Private Sub btnStartGame_Click(sender As Object, e As EventArgs) Handles btnStartGame.Click
        Dim frmTicTacToe = New frmTicTacToe()
        frmTicTacToe.Show()
        Close()
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Application.Exit()
    End Sub

End Class

Public Class frmTicTacToe

    Private Property Random As Random

    Private Property XImage As Bitmap

    Private Property CircleImage As Bitmap

    Public Property PlayerTurn As Boolean

    Public ReadOnly Property IsPlayerOnesTurn
        Get
            Return PlayerTurn
        End Get
    End Property

    Public ReadOnly Property IsPlayerTwosTurn
        Get
            Return Not PlayerTurn
        End Get
    End Property

    Sub New()
        InitializeComponent()
        Random = New Random()
        XImage = My.Resources.x
        CircleImage = My.Resources.circle
        InitializePictureBoxes()
    End Sub

    Private Sub InitializePictureBoxes()
        For i = 1 To 9
            Dim pb = CreatePictureBox()
            tlpGame.Controls.Add(pb)
        Next
    End Sub

    Private Function CreatePictureBox() As PictureBox
        Dim pb = New PictureBox()
        With pb
            .BackColor = Color.Transparent
            .Dock = DockStyle.Fill
            .Margin = New Padding(0)
            .BorderStyle = BorderStyle.FixedSingle
            AddHandler pb.Click, AddressOf PictureBox_Click
        End With
        Return pb
    End Function

    Private Sub PictureBox_Click(sender As Object, e As EventArgs)
        Dim pb = CType(sender, PictureBox)

        Dim tokenAlreadySet = pb.BackgroundImage IsNot Nothing
        If tokenAlreadySet Then
            Return
        End If

        SetPlayerToken(pb)

        If HasPlayerWon() Then
            AskForRematch()
        ElseIf AllTokensPlaced() Then
            AskForDrawRematch()
        Else
            ChangePlayerTurn()
        End If
    End Sub

    Private Sub SetPlayerToken(pb As PictureBox)
        If IsPlayerOnesTurn Then
            SetPlayerOneToken(pb)
        Else
            SetPlayerTwoToken(pb)
        End If
    End Sub

    Private Sub SetPlayerOneToken(pb As PictureBox)
        pb.BackgroundImage = XImage
    End Sub

    Private Sub SetPlayerTwoToken(pb As PictureBox)
        pb.BackgroundImage = CircleImage
    End Sub

    Private Function HasPlayerWon() As Boolean
        Dim horizontalMatch = CheckHorizontalMatch()
        If horizontalMatch Then
            Return True
        End If

        Dim verticalMatch = CheckVerticalMatch()
        If verticalMatch Then
            Return True
        End If

        Dim diagonalMatch = CheckDiagonalMatch()
        If diagonalMatch Then
            Return True
        End If

        Return False
    End Function

    Private Function CheckHorizontalMatch() As Boolean
        Return DoPictureBoxesMatch(1, 2, 3) OrElse
            DoPictureBoxesMatch(4, 5, 6) OrElse
            DoPictureBoxesMatch(7, 8, 9)
    End Function

    Private Function CheckVerticalMatch() As Boolean
        Return DoPictureBoxesMatch(1, 4, 7) OrElse
            DoPictureBoxesMatch(2, 5, 8) OrElse
            DoPictureBoxesMatch(3, 6, 9)
    End Function

    Private Function CheckDiagonalMatch() As Boolean
        Return DoPictureBoxesMatch(1, 5, 9) OrElse
            DoPictureBoxesMatch(7, 5, 3)
    End Function

    Private Function DoPictureBoxesMatch(ParamArray pictureBoxNumbers As Integer()) As Boolean
        Dim images = New List(Of PictureBox)
        For Each number In pictureBoxNumbers
            Dim pbIndex = number - 1
            Dim pb = CType(tlpGame.Controls(pbIndex), PictureBox)
            images.Add(pb)
        Next
        If IsPlayerOnesTurn Then
            Return images.All(Function(x) x.BackgroundImage Is XImage)
        Else
            Return images.All(Function(x) x.BackgroundImage Is CircleImage)
        End If
    End Function

    Private Sub AskForRematch()
        Dim playerNumber = IIf(IsPlayerOnesTurn, 1, 2)
        Dim msg = $"Player {playerNumber} won, do you want to restart?"
        Dim wantsToRestart = MessageBox.Show(msg, "Player won", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes
        If wantsToRestart Then
            Restart()
        Else
            Close()
        End If
    End Sub

    Private Sub AskForDrawRematch()
        Dim msg = $"It's a draw, do you want to restart?"
        Dim wantsToRestart = MessageBox.Show(msg, "Draw!", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes
        If wantsToRestart Then
            Restart()
        Else
            Close()
        End If
    End Sub

    Private Sub Restart()
        ResetImages()
        ChooseRandomStartingPlayer()
    End Sub

    Private Sub ResetImages()
        For Each pb In tlpGame.Controls.OfType(Of PictureBox)
            pb.BackgroundImage = Nothing
        Next
    End Sub

    Private Function AllTokensPlaced() As Boolean
        Dim countPlaced = 0
        For Each pb In tlpGame.Controls.OfType(Of PictureBox)
            Dim tokenSet = pb.BackgroundImage IsNot Nothing
            If tokenSet Then
                countPlaced += 1
            End If
        Next
        ' or with LINQ:
        ' Dim countPlaced = tlpGame.Controls.OfType(Of PictureBox).Where(Function(pb) pb.BackgroundImage IsNot Nothing)
        Return countPlaced = tlpGame.Controls.Count
    End Function

    Private Sub ChangePlayerTurn()
        PlayerTurn = Not PlayerTurn
        UpdatePlayerTurnLabel()
        StartCountDown()
    End Sub

    Private Sub UpdatePlayerTurnLabel()
        Dim playerNumber = 0
        If IsPlayerOnesTurn Then
            playerNumber = 1
        Else
            playerNumber = 2
        End If
        lblPlayerTurnValue.Text = $"Player {playerNumber}"
    End Sub

    Private Sub StartCountDown()
        lblTimeValue.Text = "10"
        tmrCountDown.Start()
    End Sub

    Private Sub frmTicTacToe_Load(sender As Object, e As EventArgs) Handles Me.Load
        ChooseRandomStartingPlayer()
    End Sub

    Private Sub ChooseRandomStartingPlayer()
        Dim rand = Random.Next(100)
        Dim isEven = rand Mod 2 = 0
        PlayerTurn = isEven
        UpdatePlayerTurnLabel()
        StartCountDown()
    End Sub

    Private Sub tmrCountDown_Tick(sender As Object, e As EventArgs) Handles tmrCountDown.Tick
        Dim time = Convert.ToByte(lblTimeValue.Text)
        If time > 0 Then
            time -= 1
            UpdateTimeLabel(time)
            Return
        End If
        tmrCountDown.Stop()
        ChangePlayerTurn()
        StartCountDown()
    End Sub

    Private Sub UpdateTimeLabel(time As Integer)
        lblTimeValue.Text = time.ToString()
    End Sub

    Private Sub frmTicTacToe_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
        tmrCountDown.Stop()
    End Sub

    Private Sub btnBack_Click(sender As Object, e As EventArgs) Handles btnBack.Click
        Dim frmWelcome = New frmWelcome()
        frmWelcome.Show()
        Close()
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Application.Exit()
    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="/downloads/vbnet/TicTacToeExample.zip" target="_blank" rel="noreferrer noopener">TicTacToeExample.zip</a></div>
</div>
<p>Der Beitrag <a href="https://robbelroot.de/blog/vbnet-game-dein-eigenes-tictactoe-spiel/">VB.NET Game – Dein Eigenes TicTacToe Spiel</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://robbelroot.de/blog/vbnet-game-dein-eigenes-tictactoe-spiel/feed/</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
			</item>
		<item>
		<title>VB.NET Form öffnen</title>
		<link>https://robbelroot.de/blog/vbnet-form-oeffnen/</link>
					<comments>https://robbelroot.de/blog/vbnet-form-oeffnen/#comments</comments>
		
		<dc:creator><![CDATA[Robert Skibbe]]></dc:creator>
		<pubDate>Sat, 03 Jul 2021 01:49:11 +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[anzeigen]]></category>
		<category><![CDATA[darstellen]]></category>
		<category><![CDATA[dialog]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[help]]></category>
		<category><![CDATA[hilfe]]></category>
		<category><![CDATA[howto]]></category>
		<category><![CDATA[instance]]></category>
		<category><![CDATA[instanz]]></category>
		<category><![CDATA[öffnen]]></category>
		<category><![CDATA[open]]></category>
		<category><![CDATA[show]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[vb.net]]></category>
		<category><![CDATA[vbnet]]></category>
		<category><![CDATA[zweite form]]></category>
		<guid isPermaLink="false">https://robbelroot.de/?p=2825</guid>

					<description><![CDATA[<p>VB.NET Form öffnen In VB.NET eine Form zu öffnen kann vor allem für Neulinge ziemlich doof laufen, jedoch dazu gleich mehr! Eine zweite Form zu öffnen, oder allgemein eine versteckte Form wieder anzuzeigen gehört glaube ich heute zu den Standards in jeder Anwendung. Wenn Du dir z. B. das konkrete &#8230;</p>
<p>Der Beitrag <a href="https://robbelroot.de/blog/vbnet-form-oeffnen/">VB.NET Form öffnen</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-Form-oeffnen.png"><img loading="lazy" decoding="async" width="1024" height="536" src="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-Form-oeffnen-1024x536.png" alt="VB.NET Form öffnen" class="wp-image-2829" title="VB.NET Form öffnen" srcset="https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-Form-oeffnen-1024x536.png 1024w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-Form-oeffnen-300x157.png 300w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-Form-oeffnen-768x402.png 768w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-Form-oeffnen-700x366.png 700w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-Form-oeffnen-332x174.png 332w, https://robbelroot.de/wp-content/uploads/2021/07/VB.NET-Form-oeffnen.png 1200w" sizes="auto, (max-width: 1024px) 100vw, 1024px" /></a><figcaption class="wp-element-caption">VB.NET Form öffnen</figcaption></figure>






<h2 class="wp-block-heading">VB.NET Form öffnen</h2>



<p>In <strong>VB.NET</strong> eine <strong>Form </strong>zu <strong>öffnen </strong>kann vor allem für Neulinge ziemlich doof laufen, jedoch dazu gleich mehr!</p>



<p><strong>Eine zweite Form </strong>zu <strong>öffnen</strong>, <strong>oder </strong>allgemein eine <strong>versteckte Form wieder anzuzeigen gehört </strong>glaube ich heute <strong>zu </strong>den <strong>Standards </strong>in jeder Anwendung.</p>



<p><strong>Wenn </strong>Du dir <strong>z. B.</strong> das <strong>konkrete Beispiel mit </strong>einer <strong>Adress</strong>&#8211;<strong>Eingabe anschauen </strong>möchtest, <strong>kannst Du</strong> auch <a href="/blog/vbnet-input-dialog/" target="_blank" rel="noreferrer noopener"><strong>in diesen Beitrag hier abtauchen</strong></a>, <strong>allerdings</strong> ist da schon <strong>viel </strong>drum <strong>herum</strong>, <strong>daher </strong>ist <strong>Dieser hier </strong>definitiv <strong>einfacher</strong>.</p>



<h2 class="wp-block-heading">Wie man es nicht machen sollte!</h2>



<p>Bevor wir jedoch in die Richtung &#8222;wie sollte man es machen&#8220; gehen, schauen wir uns erst einmal an, wie man es eben nicht tun sollte! <strong>Viele Codes</strong> die Du online finden wirst, oder schon gefunden hast, <strong>werden</strong> ungefähr <strong>so aussehen</strong> – bitte nicht:</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 btnShowSecondFormWrong_Click(sender As Object, e As EventArgs) Handles btnShowSecondFormWrong.Click
        ' DONT DO THIS!
        Form2.Show()
    End Sub

End Class</pre>



<p>Diese <strong>besonders für Einsteiger einfache Variante</strong>, prägt sich bei vielen .NET Entwicklern am Anfang ihrer Karriere Aufgrund der simplen Handhabung ein.</p>



<p><strong>Später jedoch</strong>, <strong>stoßen </strong>Sie damit irgendwann berechtigterweise <strong>auf Kritik</strong> und eventuellen Problemen, denn <strong>rein logisch </strong>betrachtet: <strong>Wie </strong>willst Du eine Form <strong>anzeigen</strong>, die Du <strong>nie erstellt </strong>hast? In erster Linie ist es also eher ein Verständnis/Logik-Problem des Programmierers.</p>



<p><strong>Lasse diesen Kritiker</strong>, bzw. Besserwisser dieses Mal <strong>mich sein </strong>&#x1f913;, denn <strong>je eher Du </strong>dieses Code-Schnipsel <strong>vergisst</strong>, <strong>desto besser </strong>für Dich und Deine Nerven!</p>



<h3 class="wp-block-heading">C# macht es richtig</h3>



<p>Ein <strong>tolles Vorbild </strong>ist in diesem Beispiel <strong>C#</strong>, <strong>obwohl </strong>sich <strong>viele Funktionalitäten</strong> in den Sprachen <strong>ähneln</strong>, da Sie ja <strong>beide auf </strong>dem <strong>.NET Framework</strong> <strong>basieren</strong>, <strong>gibt </strong>es einige <strong>Unterschiede</strong>.</p>



<p><strong>Unter anderem wird VB.NET</strong> <strong>deswegen </strong>auch so oft harsch <strong>kritisiert</strong>, <strong>aber gut </strong>das <strong>gehört </strong>hier jetzt <strong>nicht </strong>weiter <strong>hin</strong>..</p>



<p><strong>In C#</strong> ist es <strong>zum Beispiel</strong> gar <strong>nicht </strong>erst <strong>möglich </strong>so einen Quatsch wie oben zu machen, <strong>denn </strong>der <strong>C#-Compiler</strong> <strong>fragt </strong>sich, <strong>welches Objekt</strong> er verwenden soll:</p>



<figure class="wp-block-image size-large"><a href="https://robbelroot.de/wp-content/uploads/2021/07/CSharp-Compiler-Fehler-CS0120-benoetigtes-Objekt.png"><img loading="lazy" decoding="async" width="747" height="191" src="https://robbelroot.de/wp-content/uploads/2021/07/CSharp-Compiler-Fehler-CS0120-benoetigtes-Objekt.png" alt="CSharp Compiler Fehler CS0120 benötigtes Objekt" class="wp-image-2836" title="CSharp Compiler Fehler CS0120 benötigtes Objekt" srcset="https://robbelroot.de/wp-content/uploads/2021/07/CSharp-Compiler-Fehler-CS0120-benoetigtes-Objekt.png 747w, https://robbelroot.de/wp-content/uploads/2021/07/CSharp-Compiler-Fehler-CS0120-benoetigtes-Objekt-300x77.png 300w, https://robbelroot.de/wp-content/uploads/2021/07/CSharp-Compiler-Fehler-CS0120-benoetigtes-Objekt-700x179.png 700w, https://robbelroot.de/wp-content/uploads/2021/07/CSharp-Compiler-Fehler-CS0120-benoetigtes-Objekt-332x85.png 332w" sizes="auto, (max-width: 747px) 100vw, 747px" /></a><figcaption class="wp-element-caption">CSharp Compiler Fehler CS0120 benötigtes Objekt</figcaption></figure>



<h3 class="wp-block-heading">Warum so in VB.NET möglich?</h3>



<p><strong>Letztendlich </strong>könnte man sich <strong>fragen</strong>, <strong>warum </strong>das Ganze denn dann <strong>möglich </strong>ist, <strong>wenn </strong>es doch so <strong>unsauber </strong>ist.</p>



<p>Die <strong>Antwort liegt bei </strong>der <strong>Altlast </strong>von VB.NET, <strong>bekannt unter </strong>dem Kürzel &#8222;VB6&#8220; und dem <strong>Namen </strong>&#8222;<strong><a href="https://de.wikipedia.org/wiki/Visual_Basic_Classic" target="_blank" rel="noreferrer noopener">Visual Basic Classic</a></strong>&#8222;.</p>



<p>Eine <strong>derartige Herangehensweise funktioniert</strong>, <strong>weil </strong>VB.NET an dieser Stelle <strong>eine implizite Instanz </strong>der Klasse – wenn nötig – <strong>erstellt</strong>.</p>



<h3 class="wp-block-heading">Wieso viele Entwickler es trotzdem tun &#x1f612;</h3>



<p><strong>Immer wieder </strong>habe ich <strong>in Vergangenheit beobachtet</strong>, <strong>wie </strong>einige meiner <strong>Schüler</strong>, <strong>aber auch Entwickler </strong>aus <strong>Unternehmen</strong>, mit denen ich <strong>kooperiert </strong>habe, es <strong>trotzdem weiterhin </strong>so <strong>gemacht </strong>haben.</p>



<p><strong>Was bei Schülern</strong>, sprich <strong>am Anfang stehenden Entwicklern</strong>, meiner Meinung nach <strong>noch entschuldigt </strong>werden kann, <strong>sieht bei angestellten </strong>Entwicklern denke ich <strong>anders aus</strong>.</p>



<p><strong>Besonders </strong>aus<strong> VB6 </strong>kommenden &#8222;<strong>Entwicklern</strong>&#8220; <strong>schwören </strong>häufig <strong>nicht von </strong>dieser <strong>Methode ab</strong>, <strong>da </strong>Sie ja <strong>so einfach </strong>ist und &#8222;..es <strong>hat ja </strong>schließlich immer <strong>funktioniert</strong>&#8222;..</p>



<h2 class="wp-block-heading">Code – VB.NET Form öffnen aber richtig!</h2>



<p><strong>Wer sich </strong>verständlicherweise nun <strong>fragt</strong>: &#8222;Nunja, <strong>wie öffne ich </strong>denn dann eine <strong>neue Form </strong>in <strong>VB.NET</strong>?&#8220;, <strong>oder </strong>&#8222;<strong>Wie zeige </strong>ich eine <strong>versteckte Form </strong>in VB.NET an?&#8220;, <strong>wird hier </strong>nun <strong>fündig</strong>.</p>



<p><strong>Da </strong>es <strong>sich </strong>bei <strong>VB.NET</strong> um eine <strong>objektorientierte Sprache</strong> handelt, in der man natürlich <strong>meistens mit </strong>irgendwelchen <strong>Objekten </strong>– vorzugsweise <strong>Instanzen von Klassen</strong> – arbeitet, <strong>sieht </strong>es <strong>auch bei </strong>den <strong>Formen nicht anders </strong>aus.</p>



<p><strong>Letztendlich </strong>muss man <strong>nur </strong>eine <strong>Instanz </strong>einer Form <strong>erstellen </strong>und <strong>anschließend </strong>deren <strong><a href="https://docs.microsoft.com/de-de/dotnet/api/system.windows.forms.form.show?view=net-5.0" target="_blank" rel="noreferrer noopener">Show</a></strong>-, bzw. <strong><a href="https://docs.microsoft.com/de-de/dotnet/api/system.windows.forms.form.showdialog?view=net-5.0" target="_blank" rel="noreferrer noopener">ShowDialog</a></strong>-Methode aufrufen.</p>



<h3 class="wp-block-heading">VB.NET Form öffnen</h3>



<p>Wir <strong>nehmen </strong>also unser <strong>kleines Beispiel von vorhin</strong> <strong>und bauen es </strong>fix wie folgt <strong>um</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 btnShowSecondForm_Click(sender As Object, e As EventArgs) Handles btnShowSecondForm.Click
        Dim instance = New Form2()
        instance.Show()
    End Sub

End Class</pre>



<h3 class="wp-block-heading">Achtung – Offene Formen</h3>



<p><strong>Beachte </strong>jedoch, <strong>dass bei mehreren Klicks</strong>, dementsprechend <strong>mehrere und </strong>somit <strong>unabhängige Instanzen </strong>der Form <strong>geöffnet werden</strong>.</p>



<p><strong>Wenn </strong>Du <strong>Diese dann </strong>im zweiten Formular mit dem &#8222;Hide&#8220;-Button <strong>versteckst</strong>, <strong>bleiben Diese </strong>dennoch im Hintergrund <strong>offen</strong> – die Formen <strong>verbrauchen also Ressourcen </strong>im Hintergrund!</p>



<p><strong>Um </strong>das <strong>Problem </strong>genauer <strong>darzustellen</strong>, <strong>habe ich </strong>Dir eine <strong><a href="https://docs.microsoft.com/de-de/dotnet/api/system.diagnostics.debug.writeline?view=net-5.0" target="_blank" rel="noreferrer noopener">Debug.WriteLine</a></strong>-Zeile <strong>eingebaut</strong>, die dies anzeigt.</p>



<p>Ich <strong>denke dort liegt auch</strong> das <strong>hauptsächliche Problem </strong>von vielen Entwicklern, <strong>also das </strong>der <strong>jeweilige Entwickler noch nicht </strong>so <strong>weit </strong>ist und <strong>nicht mit </strong>dem ganzen <strong>Umfang </strong>der Materie <strong>klarkommt</strong>.</p>



<p>Damit <strong>meine ich </strong>die <strong>korrekte Arbeit mit </strong>verschiedenen <strong>Instanzen </strong>und das <strong>jede </strong>dieser Instanzen auch <strong>eigene Events </strong>auslösen kann, <strong>verwaltet werden </strong>möchte und auch <strong>eigene</strong> <strong>Gültigkeitsbereiche</strong> hat.</p>



<p><strong>Man muss </strong>hier also darauf <strong>achten</strong>, <strong>dass Formen auch </strong>bei <strong>Bedarf </strong>wieder <strong>sauber geschlossen </strong>werden und <strong>nicht </strong>wie gezeigt <strong>im Hintergrund </strong>vor sich hin <strong>dümpeln</strong>.</p>



<h3 class="wp-block-heading">Zweites Beispiel – Mit Zwischenspeicher</h3>



<p><strong>In diesem Teil</strong> zeige ich Dir, <strong>wie </strong>Du die <strong>Form nach Erstellung </strong>in einer formularweiten Variable/Eigenschaft <strong>cachen </strong>– also praktisch <strong>zwischenspeichern </strong>– <strong>kannst</strong>.</p>



<p>Das <strong>ermöglicht </strong>dir den <strong>späteren Zugriff </strong>auf die Form, z. B. wie im Sample <strong>durch weitere Buttons</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

    Public WithEvents CachedInstance As Form2

    Private Sub btnShowFormAndCache_Click(sender As Object, e As EventArgs) Handles btnShowFormAndCache.Click
        CachedInstance = New Form2()
        CachedInstance.Show()
    End Sub

    Private Sub btnShowCachedForm_Click(sender As Object, e As EventArgs) Handles btnShowCachedForm.Click
        If CachedInstance IsNot Nothing Then
            CachedInstance.Show()
        End If
    End Sub

    Private Sub btnHideCachedForm_Click(sender As Object, e As EventArgs) Handles btnHideCachedForm.Click
        If CachedInstance IsNot Nothing Then
            CachedInstance.Hide()
        End If
    End Sub

    Private Sub btnCloseCachedForm_Click(sender As Object, e As EventArgs) Handles btnCloseCachedForm.Click
        If CachedInstance IsNot Nothing Then
            CachedInstance.Close()
        End If
    End Sub

End Class</pre>



<h2 class="wp-block-heading">Kompletter Code – VB.NET Form öffnen</h2>



<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 btnShowSecondFormWrong_Click(sender As Object, e As EventArgs) Handles btnShowSecondFormWrong.Click
        Form2.Show()
    End Sub

    Private Sub btnShowSecondForm_Click(sender As Object, e As EventArgs) Handles btnShowSecondForm.Click
        Dim instance = New Form2()
        instance.Show()
    End Sub


    Public WithEvents CachedInstance As Form2

    Private Sub btnShowFormAndCache_Click(sender As Object, e As EventArgs) Handles btnShowFormAndCache.Click
        CachedInstance = New Form2()
        CachedInstance.Show()
    End Sub

    Private Sub btnShowCachedForm_Click(sender As Object, e As EventArgs) Handles btnShowCachedForm.Click
        If CachedInstance IsNot Nothing Then
            CachedInstance.Show()
        End If
    End Sub
    Private Sub btnHideCachedForm_Click(sender As Object, e As EventArgs) Handles btnHideCachedForm.Click
        If CachedInstance IsNot Nothing Then
            CachedInstance.Hide()
        End If
    End Sub

    Private Sub btnCloseCachedForm_Click(sender As Object, e As EventArgs) Handles btnCloseCachedForm.Click
        If CachedInstance IsNot Nothing Then
            CachedInstance.Close()
        End If
    End Sub

End Class

Public Class Form2

    Private Sub btnHide_Click(sender As Object, e As EventArgs) Handles btnHide.Click
        Hide()
        Debug.WriteLine(Application.OpenForms.Count, "Open")
    End Sub

    Private Sub btnClose_Click(sender As Object, e As EventArgs) Handles btnClose.Click
        Close()
    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 wp-element-button" href="/downloads/vbnet/OpenFormExample.zip" target="_blank" rel="noreferrer noopener">OpenFormExample.zip</a></div>
</div>
<p>Der Beitrag <a href="https://robbelroot.de/blog/vbnet-form-oeffnen/">VB.NET Form öffnen</a> erschien zuerst auf <a href="https://robbelroot.de">Robert Skibbe</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://robbelroot.de/blog/vbnet-form-oeffnen/feed/</wfw:commentRss>
			<slash:comments>8</slash:comments>
		
		
			</item>
	</channel>
</rss>
