Jump to content

Just started VB.net school subject.

Johanes
Go to solution Solved by Johanes,

NEW QUESTION!!!!

 

how to disable the dot key if the textbox already has a dot in it.. im using an if statement to check if the string in the textbox has a "dot" if it returns true i want it so that it just doesnt add another dot...

 

Private Sub DotKey_Click(sender As Object, e As EventArgs) Handles DotKey.Click
        If CalConsol.Text.Contains(".") = True Then
            DotKey.Dispose()
        End If
        CalConsol.Text = CalConsol.Text & "."
    End Sub

this is my current dot button code

 

 

UPDATE!!! nevermind my brain was being stupid >.>

 

Private Sub DotKey_Click(sender As Object, e As EventArgs) Handles DotKey.Click
        If CalConsol.Text.Contains(".") = False Then
            CalConsol.Text = CalConsol.Text & "."
        End If
    End Sub

just had to do this >.>

so back story...

 

So basically we just started the 2nd term of our school year.. and now our teacher wants us to use VB.net as our language for our Programming 2 Class

 

i have no idea what VB.net is.. our last language was python. but i am getting the general gist of it.

 

our teacher gave us an activity to make a basic calculator.. just like microsoft "standard calculator"

 

so im just here blindly coding away and is sick of copy pasting multiple lines of the same code handling button events from 0 - 9 and the operations etc.

 

so i was searching a way to make my code shorter.. then i found my salvation.

 

i found from a C# video tutorial about event handling so im like.. there has to be a similar way in VB.net

and succeeded..

Private Sub NumButtons(sender As Object, e As EventArgs) Handles Num0.Click, Num9.Click, Num8.Click, Num7.Click, Num6.Click, Num5.Click, Num4.Click, Num3.Click, Num2.Click, Num1.Click, DotKey.Click
        If CalConsol.Text = "0" Then
            CalConsol.Clear()
        End If

        Dim NumBut As Button = CType(sender, Button)
        CalConsol.Text = CalConsol.Text & NumBut.Text
    End Sub

basically im just using a small block of code to input the numbers to the textbox which is a godsend for me >.<

but now im afraid that since i have no idea how this code works. particularity this line

Dim NumBut As Button = CType(sender, Button)

i understand the Dim class basically declaring a variable. but i have no idea what the As Button = CType(sender, Button) does like.. how does it know to take the text from my key buttons.

 

some explanations would be appreciated thanks you ^_^

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Johanes said:

Dim NumBut As Button = CType(sender, Button)

Dim is a very normal standard declaration

NumBut : it's the name of the variable the code is declaring

As Button : it's declaring the type as a Button.

= : it tries to assign right away on the same line a value to that variable

CType(value,type) : is a converter asking in your case to convert "sender" to a type "Button" as it's what the variable requires.

 

Now sender is always of type object  but it gets triggered by a button press so sender is always in this case a "Button".

Link to comment
Share on other sites

Link to post
Share on other sites

VB is Visual Basic, personally I like to call it a stepping stone to a 'proper programming language' like C#.

Anyways, what this code does:

Dim NumBut As Button = CType(sender, Button)

Is make an instance of Dim called 'NumBut' and declare is as a Button. I am not really into VB, but I think the 'As Button' line is basically saying "delcare it, as if it is a button". Then the CType function is called and gets sent sender and button (which I think are default button variables).

To further explain the 'as button' part: a lot of languages are meant to be used in an 'object oriented' manner. Every instance of a class in your program is an object. With the 'as Button'  line you are saying that specific 'Dim'  object, is a button object. That may allow it different options inside the program

 

Not sure if VB support this, but what I recommend you do is go to the 'Private Sub NumButtons' line, click on that line, press the 'F9' key and it should put a breakpoint there (at least it does in other languages you use in Visual Studio). What a breakpoint does is break ('pause') when you get to that certain line and on the bottom left you should now see the variables you're working with and their values.

That way you can kind of inspect what kind of input you are receiving.

 

With F10 you can go through the code step by step. Doing this, you will create a better understanding of what you code is doing and what kind of steps is taking to give you a response.

 

If you have any more questions, I would love to assist as best as I can.

Happy coding!

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

2 minutes ago, minibois said:

VB is Visual Basic, personally I like to call it a stepping stone to a 'proper programming language' like C#.

Anyways, what this code does:


Dim NumBut As Button = CType(sender, Button)

Is make an instance of Dim called 'NumBut' and declare is as a Button. I am not really into VB, but I think the 'As Button' line is basically saying "delcare it, as if it is a button". Then the CType function is called and gets sent sender and button (which I think are default button variables).

To further explain the 'as button' part: a lot of languages are meant to be used in an 'object oriented' manner. Every instance of a class in your program is an object. With the 'as Button'  line you are saying that specific 'Dim'  object, is a button object. That may allow it different options inside the program

 

Not sure if VB support this, but what I recommend you do is go to the 'Private Sub NumButtons' line, click on that line, press the 'F9' key and it should put a breakpoint there (at least it does in other languages you use in Visual Studio). What a breakpoint does is break ('pause') when you get to that certain line and on the bottom left you should now see the variables you're working with and their values.

That way you can kind of inspect what kind of input you are receiving.

 

With F10 you can go through the code step by step. Doing this, you will create a better understanding of what you code is doing and what kind of steps is taking to give you a response.

 

If you have any more questions, I would love to assist as best as I can.

Happy coding!

thank you for the shortcut keys.. yeah still new to the Visual Studio IDE no idea how to actually debug my code and see what its doing xD

Link to comment
Share on other sites

Link to post
Share on other sites

7 minutes ago, Franck said:

Dim is a very normal standard declaration

NumBut : it's the name of the variable the code is declaring

As Button : it's declaring the type as a Button.

= : it tries to assign right away on the same line a value to that variable

CType(value,type) : is a converter asking in your case to convert "sender" to a type "Button" as it's what the variable requires.

 

Now sender is always of type object  but it gets triggered by a button press so sender is always in this case a "Button".

thanks for the explanation.. as usual i try to see if i can understand it xD

Link to comment
Share on other sites

Link to post
Share on other sites

Just now, Johanes said:

thank you for the shortcut keys.. yeah still new to the Visual Studio IDE no idea how to actually debug my code and see what its doing xD

Place the breakpoint with F9 where you feel like you want to know more about the code (like this function). Then run your program like usual and trigger this function (by pressing a number button for example). Now the breakpoint should get triggered, your program minimizes and you see your code. You can see the values of variables and step through the code with F10 to see what the code does, to give a response on the front-end.

"We're all in this together, might as well be friends" Tom, Toonami.

 

mini eLiXiVy: my open source 65% mechanical PCB, a build log, PCB anatomy and discussing open source licenses: https://linustechtips.com/topic/1366493-elixivy-a-65-mechanical-keyboard-build-log-pcb-anatomy-and-how-i-open-sourced-this-project/

 

mini_cardboard: a 4% keyboard build log and how keyboards workhttps://linustechtips.com/topic/1328547-mini_cardboard-a-4-keyboard-build-log-and-how-keyboards-work/

Link to comment
Share on other sites

Link to post
Share on other sites

21 minutes ago, Johanes said:

thanks for the explanation.. as usual i try to see if i can understand it xD

in C# there are couple ways to do it might be much more readable as it's C syntax. In no way this is the best way to achieve what you are trying to do. This is for example/understanding purpose.

// with "as/is" syntax
Button NumBut;
NumBut = sender as Button;

// Or using simple casting
Button NumBut;
NumBut = (Button)sender;

// these are the same as the example below
object sender = ""; // this is actually a string but the type is seen as object since it's declared as is.
string myValue;
myValue = (string)sender; // we know it's a string so we cast as a string so it can be sotred in the string variable

 

Link to comment
Share on other sites

Link to post
Share on other sites

NEW QUESTION!!!!

 

how to disable the dot key if the textbox already has a dot in it.. im using an if statement to check if the string in the textbox has a "dot" if it returns true i want it so that it just doesnt add another dot...

 

Private Sub DotKey_Click(sender As Object, e As EventArgs) Handles DotKey.Click
        If CalConsol.Text.Contains(".") = True Then
            DotKey.Dispose()
        End If
        CalConsol.Text = CalConsol.Text & "."
    End Sub

this is my current dot button code

 

 

UPDATE!!! nevermind my brain was being stupid >.>

 

Private Sub DotKey_Click(sender As Object, e As EventArgs) Handles DotKey.Click
        If CalConsol.Text.Contains(".") = False Then
            CalConsol.Text = CalConsol.Text & "."
        End If
    End Sub

just had to do this >.>

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×