Moderated by Solitaire
 
Visual Basic / VB.Net Resources & Tutorials   C# Resources & Links


Hi!

by (no login)

Though as I said in the QB forum, I'm not going to be sticking around here long. Maybe if it were more active :-/

-/\lipha

Posted on Jun 20, 2013, 3:28 PM

Respond to this message   

Return to Index


Show current time based on the Timer property

by Solitaire (no login)

This is the VB version of the QB program I posted on the main forum:

http://www.network54.com/Forum/648955/message/1358478540/Show+Time

Note the similarities and differences between the 2 versions. The output is nearly identical when you compare them side by side. The VB version is done as a Console application



Module Module1
    'adapted from QB

    Sub Main()
        Dim tim, hours, minutes, seconds As Integer
        Dim hrs, min, sec, ap, mytime As String

        Do
            Console.Clear()
            tim = Convert.ToInt32(Timer)
            Console.WriteLine("TIMER is now:  " & tim)
            Console.WriteLine(vbCrLf & "Press any key to stop...")
            Console.WriteLine(vbCrLf & vbCrLf & vbCrLf)
            hours = Int(tim \ 3600)
            minutes = Int((tim - (hours * 3600)) \ 60)
            seconds = Int(tim - (hours * 3600) - (minutes * 60))
            If hours <= 12 Then
                ap = " AM "
            Else
                ap = " PM "
                hours = hours - 12
            End If
            hrs = Trim(Str(hours))
            If hours < 10 Then hrs = " " & hrs
            min = Trim(Str(minutes))
            If minutes < 10 Then min = "0" & min
            sec = Trim(Str(seconds))
            If seconds < 10 Then sec = "0" & sec
            mytime = hrs + ":" + min + ":" & sec & ap
            Console.WriteLine("Time is now:  " & mytime)
            Threading.Thread.Sleep(250)
        Loop Until Console.KeyAvailable
        Console.WriteLine(vbCrLf & vbCrLf)
    End Sub

End Module

Posted on Jan 18, 2013, 5:47 PM

Respond to this message   

Return to Index


how to register Intero.MessengerAPI.dll after creating setup with inno of any application

by (no login)

hi,

how to register .net dlls( Intero.MessengerAPI.dll)into registry.
regsvr32 it is used to register the dlls into registery.I have used it
i have tested in one machine.in that machine dont have .net.
in command prompt i have write(regsvr32 interop.messengerAPI.dll)
& preesing enter then following error will be displayed.
interop.MessengerAPI.dll was loaded, but the DllRegisterServer entry point was not found The file can not be registered.
How to avoid this error! How to register .net assembly into registry without .net in that machine.

Thanks,

Posted on Jul 6, 2011, 11:24 PM

Respond to this message   

Return to Index


Re: how to register Intero.MessengerAPI.dll after creating setup with inno of any application

by (Login MCalkins)
R

jayawant has asked this question on 2 other forums:

http://www.codeguru.com/forum/showthread.php?s=a2fdad1fdd25149b24f8fe69634c09ff&p=2022947#post2022947

http://forums.asp.net/t/1696472.aspx/1?how+to+create+setup+file+with+inno+including+interop+MessengerAPI+dll+of+any+application

Also, someone seems to have asked a similar question:

http://forums.fanatic.net.nz/index.php?showtopic=16212

Regards,
Michael

Posted on Jul 9, 2011, 10:14 PM

Respond to this message   

Return to Index


how to avoid( Error HRESULT E_FAIL has been returned from a call to a COM Component)

by (no login)

hi,

i have create a setup file with inno software including interop.MessengerAPI.dll of chatting

application(with windows live messenger).I have created a setup file with inno of that chatting

application.but installed that setup it is work in only my pc it is not work in anather

pc.because this errors are generated ( Error HRESULT E_FAIL has been returned from a call to a

COM Component).How to avoid or remove this error.?

Thanks,

Posted on Jul 6, 2011, 11:22 PM

Respond to this message   

Return to Index


Why .NET?

by (Login MCalkins)
R

What is wrong with the Win32 API (aka Windows API), so that .NET is necessary or better?

Considering the existence of Wine, are .NET applications really more portable than Win32 applications?

Or is it that somebody at MS just decided one day: Hey, let's compete with Java, and deprecate our own API in the process?

Regards,
Michael

Posted on Jun 20, 2011, 4:30 PM

Respond to this message   

Return to Index


* Why NOT .NET? What would you call it? DOT NUT? :-)

by (Login burger2227)
R

Posted on Jun 21, 2011, 2:50 PM

Respond to this message   

Return to Index


Re: Why .NET?

by (Login rpgfan3233)

This is probably going to be far more information than you need, but it's here just the same.

> What is wrong with the Win32 API (aka Windows API), so that .NET is necessary or better?

The .NET runtime is a sort of "safety layer" using a JIT compiler similar to that of Java. This safety layer allows one to avoid worrying about whether something was safely created and just code. The layer takes care of everything else such as handling out-of-memory issues (by crashing the program only rather than the whole system). This is essentially what is meant by "managed code" - you don't NEED to manage the details. However, you can handle such "exceptions" in at least some of the .NET languages, Visual C++ and C# being two that I have in mind. Often displaying a simple error message detailing what happened is preferable to a user receiving a cryptic error message due to a sudden program crash.

.NET is also its own object-oriented library, an ability that the Windows API could only emulate at the time it was created. Underneath it all, the idea is the same, but .NET allows for a cleaner way of doing things.

Microsoft's developer division once created another object-oriented library known as MFC. This was essentially an object-oriented enhancement of the Windows API, but in the end, you still needed to know SOMETHING about the Windows API to create a decent application.

Then there's DLL hell... Remember the days when you didn't have MSVBVM50.DLL, and a program complained about that? Well, .NET was supposed to fix that, too. How? I haven't a clue. Perhaps it can't do it by itself, hence the creation of WinSxS (Windows Side-by-Side), which knows about all of the assemblies installed in the "global assembly cache" (GAC) and of course the ones in the current directory.

A related problem involves using functions written in one language (C/C++) in a different language (J++, VB, etc.). Well, it turns out that because .NET is a gigantic library that each language shares in common, there's less of a need to reinvent the wheel because there's a good chance that the .NET library already has what is needed, meaning that custom functions aren't supposed to be as necessary. Of course, that's not true, but Microsoft at least tried.

As for the specific issue you mentioned, the Windows API* is a lower-level C API while .NET is a higher-level object-oriented API. You can create and show a window in just a few lines when using .NET, but it takes several lines to do the same thing using the Windows API. Then you need to make sure to "listen" to events to be able to properly close the process, etc., etc. In the end, using .NET just makes sense because it saves development time.

I'm waiting to see what Microsoft has up its sleeve next because the .NET and Windows teams have not worked together in the past, leaving a gigantic fracture between the two (it's not fun to use a Windows API function in .NET code...), but they apparently will be working closely when creating Windows 8. Hopefully it all works out great.

* - "Win32 API" refers to the API for developing 32-bit Windows applications while "Win64 API" refers to the API for developing 64-bit Windows applications; the term "Windows API" is an umbrella term that refers to both.

Posted on Jun 25, 2011, 1:02 AM

Respond to this message   

Return to Index


As always, ChronoKitsune, I appreciate your response.

by (Login MCalkins)
R

>The layer takes care of everything else such as handling out-of-memory issues (by crashing the program only rather than the whole system).

But then, that should be the case with any user mode application. An access violation should result in a protection fault, which would be handled by the OS, resulting in the application being killed. (I guess, unless it had its own exception handler for it.)

P.S. I misread. For some reason, I thought you meant wild pointers/access violation. But still, a program crash is about the worst that could happen anyway, I would think. I guess a user mode program could hang the system by causing a deadlock...

>Often displaying a simple error message detailing what happened is preferable to a user receiving a cryptic error message due to a sudden program crash.

:-P. I'll take any error message over no error message. On my Windows 2000 computer, Age of Empires tended to crash with no error message at all. I don't remember if Dr Watson ran or not (I think it did), but, if it did, it didn't say anything. When AoE crashes on XP, At least I get that annoying little box asking if I want to send an error report. Ha ha. Why? Would Microsoft really patch a game that's more than a decade old, and that few people play anymore? (Actually, I have the latest patch for it, and I think it crashes more with it. I could save and restore games with the unpatched version. After the patch, it crashes any time I try to restore a game. But, I get to press "." to find idle villagers...)

>Microsoft's developer division once created another object-oriented library known as MFC.

I attended a Microsoft promotional event in San Antonio about the time of the Vista launch (circa 2007?). I remember one of the guys mentioned MFC, and I had thought it was part of .NET. I didn't know anything else about it.

>Remember the days when you didn't have MSVBVM50.DLL,

Well, not that specific .DLL, but yeah. I remember I used to use two programs from the same people, SpywareBlaster and SpywareGuard. (I still use SpywareBlaster, but not SG.) Back on Windows 98SE (or maybe 95?), each one required a file that the other one came with, if I remember right. One required VBRun6.dll, or something like that, and the other required mscomctl.ocx, or something like that. Install them both, and you had the files.

Also, my mom liked a card game called Arachnid, and it used a specific "cards.dll". Well Windows already had a different "cards.dll" for Freecell, etc. I wrote a batch file that would copy the Arachnid one to the System folder any time she wanted to play Arachnid, and would copy the normal one to the System folder afterwards.

>A related problem involves using functions written in one language (C/C++) in a different language (J++, VB, etc.).

And again, I thought that was already possible. A properly written library should be usable from any other language, as long as it uses a standard calling calling convention (cdecl, pascal, stdcall, etc.), I would have thought. Or are some languages just too incompatible? I guess that's what "wrappers" are for.

>This is essentially what is meant by "managed code" - you don't NEED to manage the details.

>As for the specific issue you mentioned, the Windows API* is a lower-level C API while .NET is a higher-level object-oriented API. You can create and show a window in just a few lines when using .NET, but it takes several lines to do the same thing using the Windows API. Then you need to make sure to "listen" to events to be able to properly close the process, etc., etc. In the end, using .NET just makes sense because it saves development time.

I guess that's what it boils down to. I'm still a procedural guy. If someday I get the hang of object oriented programming, I'll probably understand why people like it so much. But until then, I'll be satisfied with procedural programming.

I know what you mean by it taking a lot of coding to do anything with the API (and even more so in assembly than in C, as you and I both know). That's probably why I have stuck to console application programming so far. If OOP makes it easier, than I guess that is a good thing.

I guess I'm at the point where I'm still trying to learn how Windows works. Therefore, I don't want to ignore the details, I want to immerse myself in them, because one of the goals is to learn Windows. I guess if your goal is to write applications as efficiently as possible, then you don't want to have to worry about the details.

>.NET is also its own object-oriented library, an ability that the Windows API could only emulate at the time it was created. Underneath it all, the idea is the same, but .NET allows for a cleaner way of doing things.

I don't know how C++ uses the API. My understanding of the API (which is admittedly very limited) is from an entirely procedural understanding. I know it is basically a C interface, but it works well enough from assembly. I don't know if C++ programs see it differently. It was my understanding that they could use it in an OOP way (I guess by treating handles as pointers to objects, or something like that?), even though Windows itself is written in C. But from what you just said, it sounds like there is something deficient in that approach.

>"Win32 API" refers to the API for developing 32-bit Windows applications while "Win64 API" refers to the API for developing 64-bit Windows applications; the term "Windows API" is an umbrella term that refers to both.

Yes. I'm not keen on writing 64 bit applications, though. I don't see the need, as WOW64, or whatever, should allow Win32 apps to run sufficiently well. I have yet to own a computer running a 64 bit version of Windows. Of course, Windows API used to mean the Win16 API... :-D.

Part of the reason I made this post is that I sometimes get annoyed that I keep finding information for .NET when I search trying to find Windows information.

Also, the .NET framework, at least for XP and 2000, is something you have to install, and .NET framework 2, 3, and 3.5 are very large packages. 4 is quite a bit smaller, but still larger than the Java RE. Frankly, I would rather not need to install the framework (and even the JRE for that matter), but I do anyway, because there will be those one or two neat programs that require it, but could have been written without it...

I guess if people want to use .NET, that's up to them.

As far as schools teaching .NET, I would rather schools teach the API and cross platform libraries, and Assembly, C, and C++...

Regards,
Michael

P.S.

I've heard that Visual Studio has an option to insert canary values on the stack to try to detect buffer overflows that could have overwritten the return address. I'm guessing that's not just .NET specific, but applies to traditional Visual C/C++ also... But really, a careful programmer shouldn't allow buffer overflows in the first place...

As to portability, you're a Linux and Wine user, if I recall correctly. Are you able to run .NET programs on Linux? Do you write .NET programs?

P.S.

I must have been getting MFC mixed up with WPF and WFC, or FCL, or something... Too many "foundations"... :-). I guess MFC is what is used by C++ programs for OO access to the API...

P.S.

It's not just because I'm trying to learn Windows, but also, I guess it's part of my programming style. For example, I prefer Assembly to C, to a large extent, because of the fine control and micromanagement that it allows me. I guess C allows fine control also, but I am not familiar enough with C to avail myself of it.

Java and .NET both have automatic garbage collection. Is this really more efficient? If not, the word "lazy" pops into my mind. I guess what I might call "lazy" is someone else's "easier", resulting in greater productivity. I guess automatic garbage collection means that you don't have to manually deallocate objects (e.g. "delete" in C++)? So good programming practice in Java or .NET would be bad programming practice in C++, C, or Assembly.



    
This message has been edited by MCalkins on Jun 25, 2011 6:09 AM
This message has been edited by MCalkins on Jun 25, 2011 6:04 AM
This message has been edited by MCalkins on Jun 25, 2011 5:32 AM
This message has been edited by MCalkins on Jun 25, 2011 5:28 AM
This message has been edited by MCalkins on Jun 25, 2011 5:27 AM
This message has been edited by MCalkins on Jun 25, 2011 5:03 AM
This message has been edited by MCalkins on Jun 25, 2011 4:43 AM
This message has been edited by MCalkins on Jun 25, 2011 4:36 AM
This message has been edited by MCalkins on Jun 25, 2011 4:34 AM

Posted on Jun 25, 2011, 4:21 AM

Respond to this message   

Return to Index


Re: .NET vs non-.NET and garbage collection vs manual resource management

by (Login rpgfan3233)

> I've heard that Visual Studio has an option to insert canary values
> on the stack to try to detect buffer overflows that could have
> overwritten the return address. I'm guessing that's not just .NET
> specific, but applies to traditional Visual C/C++ also... But
> really, a careful programmer shouldn't allow buffer overflows in the
> first place...

Agreed. Buffer overflows shouldn't happen at all.

> As to portability, you're a Linux and Wine user, if I recall
> correctly. Are you able to run .NET programs on Linux? Do you write
> .NET programs?

.NET programs can run on Linux thanks to the work of the open-source
Mono project (see <http://www.mono-project.com/Main_Page>). You
can also compile some C# programs using the "mcs" compiler provided by
the project. In recent years, the Mono interpreter can be used to run
ASP.NET on top of the Apache HTTP server in a non-Windows environment
when the proper Apache module is installed and configured properly.
Additionally, the binfmt-support package in Debian (I'm fairly certain
it exists with similar names in other Linux distributions) allows one
to run non-ELF binaries (executables) almost seamlessly. For example,
when I run "./foo.exe", the Wine interpreter is automatically invoked
as "wine ./foo.exe". Note that even though I run a 64-bit Debian
system, Wine still only works with Win32 applications. If I have a
.NET application rather than a Windows application
(e.g. "./foobar.exe"), the Mono interpreter is automatically invoked
("mono ./foobar.exe").

I don't write .NET programs, but I once had an interest in the C#
programming language. As a matter of fact, I still have an interest,
though the amount of interest has decreased significantly due to the
fact that I am first and foremost a C programmer. As such, it is my
belief that portable code is a programmer's responsibility. C++ is a
bit higher-level than C and allows for greater possibilities with less
code, but its I/O facilities are somewhat slower and considerably more
expensive in the resulting file size. That being said, I do believe
that the right tool should be used for the right job. If I need
something done quickly, I use a Python script. If I need something
portable, I do my best to use a C program. If a rapid application
development cycle is necessary, but the code should still be portable,
C++ is usually a good choice. With plain ol' C and C++, the programs
should just work because they're compiled to machine code (albeit with
calls to library routines such as those found in msvcrt.dll). If
managed C++ is used, however, the .NET runtime takes over.

Where does that leave .NET? I don't like having a large layer between
me and the OS unless it's actually useful. C and C++ provide just
enough of a barrier to allow me to write reasonably safe AND portable
code, and Python is great for quick, yet powerful, scripts. I will
say that I feel SOME knowledge of the system's assembly programming
language is useful even to an application programmer, more
specifically for debugging purposes than anything else. I actually
wonder if inline ASM blocks work when writing managed C++ code... I
would think it would be a violation of the safety of managed code...
Then again, this is Microsoft we're talking about; they probably
figured out how to emulate it or something.

> Java and .NET both have automatic garbage collection. Is this really
> more efficient?

Well, the efficiency depends upon the implementation of the garbage
collector. I know that Python's garbage collector uses "reference
counting". Once it sees there are no more references, the object is
deallocated. I'm not sure what .NET's garbage collector uses, but
generally a garbage collector is difficult to program and even more
difficult to use effectively in one's program (such a practice is
often discouraged).

> I guess automatic garbage collection means that you don't have to
> manually deallocate objects (e.g. "delete" in C++)? So good
> programming practice in Java or .NET would be bad programming
> practice in C++, C, or Assembly.

Yes. In Java, .NET, Python and any other language/runtime that makes
use of a garbage collector, messing with the garbage collector at all
is generally considered "bad programming practice." Many people will
tell you to carefully consider WHY you're doing it. However, there is
nothing wrong with the idea of manual resource management as long as
you know what you're doing. I look at Python and know that most of
what I do will be safe and portable to systems that have Python
installed. I look at C and C++ (and assembly) and know that I'm
responsible for making the code safe. There exist garbage collection
libraries for C++, but I personally have never had an interest in
using them. They might not get in the way like a full-blown JIT
compiler/runtime, but they also provide me no reasonable benefit. In
C++, if I need a pointer, I'll use std::auto_ptr, which allows for a
bit of automatic resource management when using pointers, mainly to
avoid the pitfalls of pointers, but that's all. In the end, I still
prefer managing everything myself. It creates more complex code, but
it assures me that I'm doing things exactly the way I want.

I guess we're both just a couple of control freaks when it comes to
programming. :P



    
This message has been edited by rpgfan3233 on Jun 25, 2011 11:07 AM

Posted on Jun 25, 2011, 11:07 AM

Respond to this message   

Return to Index


* :-)

by (Login MCalkins)
R

thank you for your answer.



    
This message has been edited by MCalkins on Jun 25, 2011 11:54 AM

Posted on Jun 25, 2011, 11:53 AM

Respond to this message   

Return to Index


How do i

by Unseen Machine (no login)

In Vb 2008 express ( though i doubt it matters what version), how do i just do something. I cant figure out how to use MAIN. I have my subs working when i click a button or a menu item, but i dont know how to just code in vb. my code looks like this


Public Class Main()

Shared vaiables

* I want my code here, a DO loop with no break and my sub and function calls in it.

A sub for something

Another sub for something else

Yet another sub for something...etc


End Class


how do i go about having a sub that is my main? and how do i get it started when my program starts? For some reason, i cant find all that much BASIC about this language. I am finding C++ much easier going...

ANy ideas folks????

Posted on Aug 8, 2010, 6:26 PM

Respond to this message   

Return to Index


There is no main...all subs and functions.

by (Login burger2227)
R

You have tools or objects that are SUB calls according to the Event that occurs. Such as clicking a control or just having the mouse over something. You can make SUB procedures to execute code that used to be in the main program.

Call a SUB Main and use it to handle all of the other subs. VB is a mouse oriented way of programming. Nothing happens without the mouse interaction except for keyboard entries. The keyboard interaction is similar to an INKEY$ INPUT loop but the text box takes care of the loop.



    
This message has been edited by burger2227 on Aug 11, 2010 6:54 AM

Posted on Aug 11, 2010, 6:41 AM

Respond to this message   

Return to Index


Thanks Clippy..

by Unseen Machine (no login)

Thats the most annoying news ever, but i now have a work around.

Posted on Aug 15, 2010, 7:40 AM

Respond to this message   

Return to Index


Unseen Machine: You can create a Console application in VB Express 2008

by Solitaire (Login Solitaire1)
S

Here is sample code for a Console app:

--------------------------------------------------------------------
Module Module1
'Class-level variables go here
Dim loca As String = "here"

Sub Main()
'This is your main program
Dim count As Integer = 0
Do
count = count + 1
Call mysub()
Loop Until count = 3
Console.ReadLine()
End Sub

Sub mysub()
Console.WriteLine("Subprocedure goes " & loca)
End Sub

End Module
--------------------------------------------------------------------

If you create a Windows Forms app, then you get a series of event procedures. There is no main program. You could use a FormLoad event that will run code as soon as the application starts, and then calls on the subprocedures. Here is a rough equivalent of the above code:

---------------------------------------------------------------------
Public Class Form1
Dim loca As String = "here"

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim count As Integer = 0
Do
count = count + 1
Call mysub()
Loop Until count = 3
End Sub

Sub mysub()
MessageBox.Show("Subprocedure goes " & loca)
End Sub

End Class
-----------------------------------------------------------------------

Alternatively, you can just put the FormLoad code into a button, which will execute when the button is clicked. It runs the button's Click event procedure.



    
This message has been edited by Solitaire1 on Aug 30, 2010 11:14 AM

Posted on Aug 29, 2010, 11:53 AM

Respond to this message   

Return to Index


whats a console application, does that mean it will run in dos?

by Anonymous (no login)

Posted on Sep 15, 2010, 7:54 PM

Respond to this message   

Return to Index


A Console program in Visual Basic.NET

by Solitaire (Login Solitaire1)
S

does not use a GUI. It will run inside a black console window instead of inside a Windows Form. It is NOT a DOS program. It is part of the Visual Studio and requires the .NET framework in order to run.

Console applications use procedural commands instead of event-driven procedures. It's the closest you can get to resembling QBasic programming inside of the Visual Studio IDE. However, it's still an object-oriented language and uses methods and classes with the dot operator.

Posted on Sep 16, 2010, 11:01 AM

Respond to this message   

Return to Index


black console window

by Anonymous (no login)

can you change the color of the console window?

Posted on Sep 16, 2010, 7:11 PM

Respond to this message   

Return to Index


Change Console Background color

by Solitaire (no login)

You can change the color in VB code. You need to clear the console in order to change the entire background. It works similarly to the way you would do it in QBasic.

============================================================================
Module Module1

Sub Main()
Console.BackgroundColor = ConsoleColor.Blue
Console.Clear()
Console.WriteLine("Background color was changed")
Console.ForegroundColor = ConsoleColor.Yellow
Console.WriteLine()
Console.WriteLine("Foreground color was changed")
Console.ReadLine()
Console.BackgroundColor = ConsoleColor.Black
Console.ForegroundColor = ConsoleColor.Gray
Console.WriteLine("Default colors were restored")
Console.ReadLine()
Console.Clear()
End Sub

End Module
============================================================================



    
This message has been edited by Solitaire1 on Sep 20, 2010 7:41 AM

Posted on Sep 19, 2010, 12:55 PM

Respond to this message   

Return to Index


fascinating! I changed mine to yellow on blue

by Anonymous (no login)

Posted on Sep 19, 2010, 8:58 PM

Respond to this message   

Return to Index


Please use a name other than Anonymous.

by Solitaire (Login Solitaire1)
S

Also, if your message contains no text, we place an * in front of the title. That lets us know not to bother opening the message.



    
This message has been edited by Solitaire1 on Oct 19, 2010 12:47 PM

Posted on Sep 20, 2010, 7:41 AM

Respond to this message   

Return to Index


* OK

by Anonymus of Hungary (no login)

Posted on Oct 6, 2010, 6:20 AM

Respond to this message   

Return to Index


VB6 ColorBar program using RGB

by Solitaire (Login Solitaire1)
S

Years ago I wrote a simple VB6 ColorBar program with sliders that you can use for each of the R, G, or B values. It displays over 16 million colors.
The picture box instantly changes to show the resulting color. Labels also display the changing values for R, G, and B, as well as the changing Hexadecimal value.

Start a new VB6 project and place the following controls on the form:

4 labels named: lblRed, lblGreen, lblBlue, and lblHex

3 vertical scroll bars named: vsbRed, vsbGreen, and vsbBlue

1 picture box named: picColor

2 command buttons named: cmdClear, and cmdExit

Place each scroll bar below the matching label.
Place the Hex label to the right of the color labels.
Place the picture box below the Hex label.
Place the buttons below the picture box.

Change the Max value of each scroll bar to 255, Min to 0, LargeChange to 10, SmallChange to 1.

Labels' Autsize should be False. Captions should be: Red 0, Green 0, Blue 0, and Hexadecimal &&H000000&&

Change the size of each label so it displays the color name (or word) on top and the number just under it. When the program runs, it will just change the number.

Here is the code:

------------------------------------------------------------------------
Option Explicit
Dim mRedval As Integer, mGreenval As Integer, mBlueval As Integer

'Change event works by clicking on gray area or arrows.
'Change Event occurs as soon as mouse button is released.
'Scroll event occurs when user drags the thumb.

Private Sub vsbRed_Change()
mRedval = vsbRed.Value
lblRed.Caption = "Red" & vbCrLf & mRedval
picColor.BackColor = RGB(mRedval, mGreenval, mBlueval)
Call hexcode
End Sub

Private Sub vsbRed_Scroll()
mRedval = vsbRed.Value
lblRed.Caption = "Red" & vbCrLf & mRedval
picColor.BackColor = RGB(mRedval, mGreenval, mBlueval)
Call hexcode
End Sub

Private Sub vsbGreen_Change()
mGreenval = vsbGreen.Value
lblGreen.Caption = "Green" & vbCrLf & mGreenval
picColor.BackColor = RGB(mRedval, mGreenval, mBlueval)
Call hexcode
End Sub

Private Sub vsbGreen_Scroll()
mGreenval = vsbGreen.Value
lblGreen.Caption = "Green" & vbCrLf & mGreenval
picColor.BackColor = RGB(mRedval, mGreenval, mBlueval)
Call hexcode
End Sub

Private Sub vsbBlue_Change()
mBlueval = vsbBlue.Value
lblBlue.Caption = "Blue" & vbCrLf & mBlueval
picColor.BackColor = RGB(mRedval, mGreenval, mBlueval)
Call hexcode
End Sub

Private Sub vsbBlue_Scroll()
mBlueval = vsbBlue.Value
lblBlue.Caption = "Blue" & vbCrLf & mBlueval
picColor.BackColor = RGB(mRedval, mGreenval, mBlueval)
Call hexcode
End Sub

Private Sub cmdClear_Click()
vsbRed.Value = 0
vsbGreen.Value = 0
vsbBlue.Value = 0
vsbRed.SetFocus
End Sub

Sub hexcode()
Dim hexblue As String, hexgreen As String, hexred As String
Dim hexnum As String
hexblue = Hex(mBlueval)
hexgreen = Hex(mGreenval)
hexred = Hex(mRedval)
If mBlueval < 16 Then hexblue = "0" & hexblue
If mGreenval < 16 Then hexgreen = "0" & hexgreen
If mRedval < 16 Then hexred = "0" & hexred
hexnum = hexblue & hexgreen & hexred
lblHex.Caption = "Hexadecimal" & vbCrLf & "&&H" & hexnum & "&&"
End Sub

Private Sub cmdExit_Click()
End
End Sub





    
This message has been edited by Solitaire1 on Mar 19, 2010 1:17 PM

Posted on Mar 19, 2010, 1:10 PM

Respond to this message   

Return to Index


system beep on Keydown event

by unseenmachine (no login)

i get text from a user input box, but when i press the enter key to enter the value i always get a system beep. is this a setting or am i doing something wrong?

Posted on Mar 7, 2010, 2:40 PM

Respond to this message   

Return to Index


You need to get rid of enter or it will be included in the entry.

by (Login burger2227)
R

This example is from VB6 for a number entry in a text box.

Private Sub txtBonus_KeyPress(Index As Integer, KeyAscii As Integer)
If KeyAscii >= 48 And KeyAscii <= 57 Or KeyAscii = 8 Then
Exit Sub
ElseIf KeyAscii = 13 Then KeyAscii = 0
Else: Beep
'Unacceptable keystroke
KeyAscii = 0
End If
End Sub

Posted on Mar 7, 2010, 4:11 PM

Respond to this message   

Return to Index


I am in 2008 Express

by unseenmachine (no login)

Private Sub GuessBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles GuessBox.KeyDown
If e.KeyValue = Keys.Enter Then
If GuessBox.Text > "0" Then guess = Val(GuessBox.Text)
If guess < rndnum Then
high.ForeColor = Color.Black
low.ForeColor = Color.Red
ElseIf guess > rndnum Then
low.ForeColor = Color.Black
high.ForeColor = Color.Red
ElseIf guess = rndnum Then
low.ForeColor = Color.Black
high.ForeColor = Color.Black
MsgBox("Welldone, you got it", MsgBoxStyle.OkOnly, "YOU WON")
Call randomizer()
End If
GuessBox.Text = ""
GuessBox.Select()
End If
End Sub

Posted on Mar 7, 2010, 5:38 PM

Respond to this message   

Return to Index


* I gave up on Express 2005. Perhaps Solitaire will know.

by (Login burger2227)
R

Posted on Mar 8, 2010, 11:48 AM

Respond to this message   

Return to Index


Your code has a long way to go.

by Solitaire (Login Solitaire1)
S

To answer your immediate question, use the KeyPress event instead of the KeyDown event. Add the e.Handler = True statement to supress the beep:

-------------------------------------------------------------------------
Private Sub GuessBox_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles GuessBox.KeyPress
If e.KeyChar = Chr(13) Then
e.Handled = True
-------------------------------------------------------------------------
etc.

However, you are using legacy code from previous versions of BASIC which is now obsolete and you need to learn the new .NET code.

First of all, I did not see any of the variables declared with Dim. Do you have Option Explicit turned On? You should also have Option Strict turned On as well. Avoid using form-level variables when local variables should be used.

You did not indicate what high and low are. I'm assuming they are label controls.

The convention for naming controls is to use a 3-character prefix in lower case followed by a descriptive name beginning with an uppercase letter. The prefix for a TextBox is txt, a Button is btn, a Label is lbl. For example, your textbox should be named txtGuess.

I strongly suggest placing your code in a button, not in a textbox. Get the user's input in the textbox first. Then put the rest of your code in the Button's Click event.

Test the user's input using the TryParse() method. If the user enters a string instead of a number, or nothing at all, your program will crash unless you do this:

Dim guess As Integer, sgess As String
sgess = txtGuess.Text
Integer.TryParse(sgess, guess)

If the input was incorrect, the TryParse() method will return 0 instead of generating an error. It has two arguments: The first is the string/text to convert from, the second is the number to convert into. The number must be the same type as the Class -- it could be Integer or any other number type. Do not use the legacy Val() function, which only works with the type Double.

Use the MessageBox.Show() method instead of the legacy MsgBox() function.

Use the Random object instead of Randomize.

You can still use the KeyPress event of the textbox to move the focus to another control when the Enter key is pressed. For example, when the user enters a number and presses Enter, the cursor can then go to the Button:

If e.KeyChar = Chr(13) Then btnGuess.Focus()

Inside the button, you can do this:

Dim guess As Integer, sgess As String
sgess = txtGuess.Text
Integer.TryParse(sgess, guess)
If guess = 0 Then
MessageBox.Show("Please enter correct guess")
txtGuess.Focus()
Exit Sub
'etc

Try to redo your program as I suggested and post again if you have any more questions. You will need to post your entire program code so I can see what is going on.


Posted on Mar 12, 2010, 3:19 PM

Respond to this message   

Return to Index


* Welllllllllll....at least I got the KeyPress event right. lol.

by (Login burger2227)
R

Posted on Mar 12, 2010, 3:30 PM

Respond to this message   

Return to Index


I am new to VB -

by unseenmachine (no login)

I just followed the instructions that came in the help file and what i could find on the web.

here is the full code...



Public Class Form1

Dim rndmax As Integer, guess As Integer, rndnum As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If EasyToolStripMenuItem.Checked = False Then EasyToolStripMenuItem.Checked = True
rndmax = 500
Call randomizer()
maintxt.Text = "Guess a number from 1 to " + Str(rndmax)
End Sub

Function randomizer() As Integer
Randomize()
rndnum = Int(Rnd() * rndmax) + 1
Return rndnum
End Function

Private Sub MediumToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MediumToolStripMenuItem.Click
If MediumToolStripMenuItem.Checked = False Then
If EasyToolStripMenuItem.Checked = True Then EasyToolStripMenuItem.Checked = False
If HardToolStripMenuItem.Checked = True Then HardToolStripMenuItem.Checked = False
MediumToolStripMenuItem.Checked = True
rndmax = 1000
maintxt.Text = "Guess a number from 1 to " + Str(rndmax)
Call randomizer()
End If
End Sub

Private Sub EasyToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EasyToolStripMenuItem.Click
If EasyToolStripMenuItem.Checked = False Then
If MediumToolStripMenuItem.Checked = True Then MediumToolStripMenuItem.Checked = False
If HardToolStripMenuItem.Checked = True Then HardToolStripMenuItem.Checked = False
EasyToolStripMenuItem.Checked = True
rndmax = 500
maintxt.Text = "Guess a number from 1 to " + Str(rndmax)
Call randomizer()
End If
End Sub

Private Sub HardToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HardToolStripMenuItem.Click
If HardToolStripMenuItem.Checked = False Then
If EasyToolStripMenuItem.Checked = True Then EasyToolStripMenuItem.Checked = False
If MediumToolStripMenuItem.Checked = True Then MediumToolStripMenuItem.Checked = False
HardToolStripMenuItem.Checked = True
rndmax = 5000
maintxt.Text = "Guess a number from 1 to " + Str(rndmax)
Call randomizer()
End If
End Sub

Private Sub ToolStripMenuItem2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem2.Click
End
End Sub

Private Sub AboutGTNToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutGTNToolStripMenuItem.Click
MsgBox("Guess that number v.01 - Coded by UNSEEN MACHINE", MsgBoxStyle.OkOnly, "INFO")
End Sub

Private Sub gobtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gobtn.Click

If GuessBox.Text > "0" Then guess = Val(GuessBox.Text)
If guess < rndnum Then
high.ForeColor = Color.Black
low.ForeColor = Color.Red
ElseIf guess > rndnum Then
low.ForeColor = Color.Black
high.ForeColor = Color.Red
ElseIf guess = rndnum Then
low.ForeColor = Color.Black
high.ForeColor = Color.Black
MsgBox("Welldone, you got it", MsgBoxStyle.OkOnly, "YOU WON")
Call randomizer()
End If
GuessBox.Text = ""
GuessBox.Select()
End Sub

Private Sub GuessBox_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles GuessBox.KeyDown
If e.KeyValue = Keys.Enter Then
If GuessBox.Text > "0" Then guess = Val(GuessBox.Text)
If guess < rndnum Then
high.ForeColor = Color.Black
low.ForeColor = Color.Red
ElseIf guess > rndnum Then
low.ForeColor = Color.Black
high.ForeColor = Color.Red
ElseIf guess = rndnum Then
low.ForeColor = Color.Black
high.ForeColor = Color.Black
MsgBox("Welldone, you got it" & vbNewLine & "The number was " & rndnum, MsgBoxStyle.OkOnly, "YOU WON")
Call randomizer()
End If
GuessBox.Text = ""
GuessBox.Select()
End If
End Sub

Private Sub RestartToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RestartToolStripMenuItem.Click
Call randomizer()
End Sub
End Class


Posted on Mar 12, 2010, 4:24 PM

Respond to this message   

Return to Index


Revised code for guessing program.

by Solitaire (Login Solitaire1)
S

I wanted you to post the code after you revised it. But I see that it's overly complicated and I also see a lot more legacy code.

Here is a new sample you can try out for yourself. Just place one label, one textbox, and one button on your form, in that order. Change the names of the controls to lblGuess, txtGuess, and btnRestart. Change the text of the button to Restart. Then copy and paste the following code to the edit window, replacing anything else that's there:

---------------------------------------------------------------------------

Public Class Form1
Dim randnum As New Random()
Dim secret As Integer

Private Sub Form1_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
secret = randnum.Next(1, 101)
lblGuess.Text = "Enter a number from 1-100:"
End Sub

Private Sub txtGuess_KeyPress(ByVal sender As Object, ByVal _
e As System.Windows.Forms.KeyPressEventArgs) Handles txtGuess.KeyPress
If e.KeyChar = Chr(13) Then
Call Result()
e.Handled = True
End If
End Sub

Sub Result()
Dim guess As Integer
Integer.TryParse(txtGuess.Text, guess)
If guess > secret Then
lblGuess.Text = guess & " is too high.  Try again:"
ElseIf guess < secret Then
lblGuess.Text = guess & " is too low.  Try again:"
Else
lblGuess.Text = "Just Right!"
MessageBox.Show("Well done, you got it!" & _
vbNewLine & "The number was " & secret, "YOU WON")
End If
txtGuess.Clear()
txtGuess.Focus()
End Sub

Private Sub btnRestart_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnRestart.Click
txtGuess.Clear()
txtGuess.Focus()
lblGuess.Text = "Enter a number from 1-100:"
secret = randnum.Next(1, 101)
End Sub

End Class

--------------------------------------------------------------------------
Note: If the statement is too long to display without scrolling, you can use a space and underscore before pressing Enter to continue the statement on the next line. This is called the line continuation character, and the result is considered as a single statement. I did this in the sample above so that it can be copied without an error.

Posted on Mar 12, 2010, 5:51 PM

Respond to this message   

Return to Index


Thanks solitaire

by unseenmachine (no login)

I will have a go tomorrow, it is bedtime now. 2 am here, YAWN! Thanks for your help. John

Posted on Mar 12, 2010, 6:10 PM

Respond to this message   

Return to Index


Here are two more samples:

by Solitaire (Login Solitaire1)
S

This is a lot shorter. It makes use of the InputBox and a Do loop to repeat inside a single event until the user guesses the right number. Just place a single button on your form and name it btnGuess. Then copy and paste the following code in the edit window:


Public Class Form1

Private Sub btnGuess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuess.Click
Dim guess, count As Integer, sgess As String
Dim randnum As New Random()
Dim secret As Integer = randnum.Next(1, 101)
sgess = InputBox("Enter a number from 1-100")
count = 0
Do
count += 1
Integer.TryParse(sgess, guess)
If guess > secret Then
sgess = InputBox(guess & " is too high. Try again")
ElseIf guess < secret Then
sgess = InputBox(guess & " is too low. Try again")
Else
MessageBox.Show("Just right! " " You got the number in " & count & " tries.", "The number was " & secret)
End If
Loop Until guess = secret
End Sub

End Class

=============================================================================

This next one is even better. It only counts the tries if user enters a number and displays the count number with each repeat. If nothing is entered, it keeps the last valid guess. It also allows user to stop if a "0" is entered, and shows the random number at the end even if the user stopped before getting the guess right.

=============================================================================

Public Class Form1

Private Sub btnGuess_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuess.Click
Dim sgess As String, good As String = "", guess, oldguess As Integer, count As Integer = 0
Dim randnum As New Random()
Dim secret As Integer = randnum.Next(1, 101)
sgess = InputBox("Enter a number from 1-100, 0 to stop after try.")
Do
oldguess = guess
Integer.TryParse(sgess, guess)
If guess <> 0 Then count += 1
If guess = 0 Then guess = oldguess
If guess > secret Then
sgess = InputBox(guess & " is too high. Try again.", "Try #" & count)
ElseIf guess < secret Then
sgess = InputBox(guess & " is too low. Try again.", "Try #" & count)
Else
good = "Just right! You got the number in " & count & " tries."
End If
Loop Until guess = secret Or sgess = "0" 'stop without guessing
MessageBox.Show(good, "The number was " & secret)
End Sub

End Class



    
This message has been edited by Solitaire1 on Mar 13, 2010 11:02 AM
This message has been edited by Solitaire1 on Mar 13, 2010 11:01 AM
This message has been edited by Solitaire1 on Mar 13, 2010 10:48 AM

Posted on Mar 12, 2010, 6:25 PM

Respond to this message   

Return to Index


Adding mouse wheel scrolling to VB6 (link)

by (Login burger2227)
R

There is an Add in fix that allows you to scroll the code in VB6 below.

http://www.vbforums.com/showthread.php?t=439816

Read the instructions carefully! I placed the dll in the M$ download into my C:\Windows\System32 folder where it belongs. Use System folder if there is no System32 folder.

Ted



    
This message has been edited by burger2227 on Feb 19, 2010 10:30 AM

Posted on Feb 18, 2010, 5:44 PM

Respond to this message   

Return to Index


Has anyone tried to run VB 6 on Windows 7?

by Solitaire (Login Solitaire1)
S

A while ago, someone on the MSDN forum complained that he couldn't run Visual Basic 6 on Windows 7. Since the topic was not about VB.NET, it was dismissed and the OP sent elsewhere, so I never found out what the problem was.

I'm wondering if that was just an isolated incident, or if it's really an incompatibility issue. I don't have Windows 7 and everyone else I know with Windows 7 had long ago abandoned VB6. So can anyone here check it out and report back at this thread?



    
This message has been edited by Solitaire1 on Feb 5, 2010 3:04 PM

Posted on Feb 5, 2010, 3:03 PM

Respond to this message   

Return to Index


Apparently it still works for some reason. I thought M$ would kill it!

by (Login burger2227)
R

Only problem is that VB6 is only 32 bit. But 64 bit should still be able to run it.



    
This message has been edited by burger2227 on Feb 10, 2010 2:19 PM

Posted on Feb 8, 2010, 10:50 PM

Respond to this message   

Return to Index


I just installed VB6 on Win7 32-bit, working fine so far.

by Dav (no login)

IDE working & compiling projects. Compiled .EXE run fine.

This is all under Windows 7 32-bit.

- Dav

Posted on Mar 15, 2010, 5:00 AM

Respond to this message   

Return to Index


* Did you see the link to adding mouse scrolling of code?

by (Login burger2227)
R

Posted on Mar 16, 2010, 12:00 PM

Respond to this message   

Return to Index


* Yes. Thanks for sharing that.

by Dav (no login)

Posted on Mar 18, 2010, 4:36 AM

Respond to this message   

Return to Index

 Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement