My first program in VB  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| |
Hi people..... this is my first program in VB.
Before this i have done programming in C,C++.
I read some of the chapters from Black book of VB; but still having problem in the initial part itself.
The program is I have to take a number in any of the forms; Octal, binary or hexadecimal convert it in the form required by user and display the result.
Am done with the logic of program; but having problems.. - Private Sub Form_Load()
-
-
Dim intext As String
-
intext = Text1.Text
-
-
Dim outtext As String
-
outtext = Text2.Text
-
if (option1.Value=true) and (option4.Value=true)
-
outtext = intext
-
End If
-
-
-
-
-
-
-
End Sub
First of all am not understanding why it creates so many procedures; every time i click something.......
And its showing error in if statement dont know why..
Is it because of option? i deleted some sub procedures above it; before that also it was showing an error.
Am using Microsoft Visual Basic.
Please tell me fast.
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB
Sorry folks, i troubled u i got the mistake; i had forgotten to write THEN.
But u can still tell me what's the use of different procedures; which we get when we click any of the controls?
I mean are they private? or public? Are they excluded from other code?
What if we dont make them?
|  | Familiar Sight | | Join Date: Jul 2007 Location: tamil nadu, INDIA
Posts: 236
| | | re: My first program in VB Quote:
Originally Posted by Parul Bagadia Sorry folks, i troubled u i got the mistake; i had forgotten to write THEN.
But u can still tell me what's the use of different procedures; which we get when we click any of the controls?
I mean are they private? or public? Are they excluded from other code?
What if we dont make them? In visual basic,when you just double-click a control,it automatically enters the code window creating the procedure for the basic event of the control you clicked..for example,if you double click command button,it creates a procedure Command1_Click,because click is the most usual event one will perform on a command button.if you want some other event you can choose in the code window.
This feature makes the task of developers easier.If you dont want to write anything inside the created procedure,just leave it or if you still feel annoying delete it.(Try not to double click any of the unnecessary controls in design time).
Since you are a beginner,you may feel this annoying,but once you get involved with visual basic,you'd really love this feature-it saves your time!
Remember visual basic does an event driven programming.
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB
It's the same program; where i got to convert a number from one number system to another..... m a bit confused regarding scope of different variables.
Here is my program that i wrote in Microsoft Visual Basic 6.0; for now its having only 3 options and that too converting a number from one to same number system.. Not actually converting also.. but its not displaying anything in text2.
Here is the code.I dont know how will i copy the form of VB.
Public Sub Text1_Change()
Static inno As Integer
inno = Text1.Text
End Sub
Public Sub Text2_Change()
Static outno As Integer
outno = Text2.Text
End Sub
Public Sub Command1_Click()
If Option1.Value = True And Option4.Value = True Then
outno = inno
End If
If Option2.Value = True And Option5.Value = True Then
outno = inno
End If
If Option3.Value = True And Option6.Value = True Then
outno = inno
End If
'If Option1.Value = True And Option6.Value = True Then
'outno = inno
'End If
'Dim counter As Integer
'For counter = 0 To 9
End Sub
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB Quote:
Originally Posted by vdraceil In visual basic,when you just double-click a control,it automatically enters the code window creating the procedure for the basic event of the control you clicked..for example,if you double click command button,it creates a procedure Command1_Click,because click is the most usual event one will perform on a command button.if you want some other event you can choose in the code window.
This feature makes the task of developers easier.If you dont want to write anything inside the created procedure,just leave it or if you still feel annoying delete it.(Try not to double click any of the unnecessary controls in design time).
Since you are a beginner,you may feel this annoying,but once you get involved with visual basic,you'd really love this feature-it saves your time!
Remember visual basic does an event driven programming. Thanks.
But m not sure whether i understand the meaning of event driven programming.
| | Needs Regular Fix | | Join Date: Mar 2008
Posts: 283
| | | re: My first program in VB Quote:
Originally Posted by Parul Bagadia
Here is the code.I dont know how will i copy the form of VB.
Public Sub Text1_Change()
Static inno As Integer
inno = Text1.Text
End Sub
Public Sub Text2_Change()
Static outno As Integer
outno = Text2.Text
End Sub
Public Sub Command1_Click()
If Option1.Value = True And Option4.Value = True Then
outno = inno
End If
End Sub I have chopped your code a little for size but looking at it you do not seem to be calling the Text2_Change sub inorder to change the text so all the program does when you click on the command1 is set the value of outno
also you are declaring the integers whenever you call the text change subs which does not look correct
i think if i understand what you are trying to do you will need to have something like -
Public Sub Command1_Click()
If Option1.Value = True And Option4.Value = True Then
outno = inno
End If
Text2_Change()
End Sub
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB
Please reply fast. I'm stuck with first program itself.
|  | Familiar Sight | | Join Date: Jul 2007 Location: tamil nadu, INDIA
Posts: 236
| | | re: My first program in VB Quote:
Originally Posted by Parul Bagadia Thanks.
But m not sure whether i understand the meaning of event driven programming. vb reacts to every possible event occuring to any control in the form.if it is a command button it may be subjected to many possible events like click,double click,mouse move etc. And the users can make their own coding under any event they wish to monitor.
This kind of programming is event driven programming.
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB Quote:
Originally Posted by vdraceil vb reacts to every possible event occuring to any control in the form.if it is a command button it may be subjected to many possible events like click,double click,mouse move etc. And the users can make their own coding under any event they wish to monitor.
This kind of programming is event driven programming. thank you so much...
Can u plz tell me wats wrong with my code?
|  | Familiar Sight | | Join Date: Jul 2007 Location: tamil nadu, INDIA
Posts: 236
| | | re: My first program in VB Quote:
Originally Posted by Parul Bagadia Please reply fast. I'm stuck with first program itself. to display something in textbox2 u must have text2.text=some value(i mean text2.text must be on the left side-assignment is from right to left)..
Why use static variables and declare it inside text box change event?instead you can declare variables generally(outside all events-so that the variables become public to the entire form)
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB Quote:
Originally Posted by jg007 I have chopped your code a little for size but looking at it you do not seem to be calling the Text2_Change sub inorder to change the text so all the program does when you click on the command1 is set the value of outno
also you are declaring the integers whenever you call the text change subs which does not look correct
i think if i understand what you are trying to do you will need to have something like -
Public Sub Command1_Click()
If Option1.Value = True And Option4.Value = True Then
outno = inno
End If
Text2_Change()
End Sub Thanks budy.
May be because m new in VB, i didnt understand why do i hav to keep it blank the text2 event?
And wats wrong with integer?
I mean earlier i had used string and instead of text i had used text1.text but i was getting a error that i cannot assign it to an array... M not sure wether i got that.
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB Quote:
Originally Posted by vdraceil to display something in textbox2 u must have text2.text=some value(i mean text2.text must be on the left side-assignment is from right to left)..
Why use static variables and declare it inside text box change event?instead you can declare variables generally(outside all events-so that the variables become public to the entire form) But then that will be global if m not wrong... and its not a good programming practise to declare variables as global.... so m willing to avoid that habit from beginning itself......... otherwise later it causes problems.
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB Quote:
Originally Posted by vdraceil to display something in textbox2 u must have text2.text=some value(i mean text2.text must be on the left side-assignment is from right to left)..
Why use static variables and declare it inside text box change event?instead you can declare variables generally(outside all events-so that the variables become public to the entire form) I did text2.text=outno.. but still invain.
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB
I also used debugger but all that m gettin in here is this:
For all the text boxes, options, variables no value is getting displayed other than the message: expression not defined in context for inno and for rest ; out of context..
Why is it so?
|  | Familiar Sight | | Join Date: Jul 2007 Location: tamil nadu, INDIA
Posts: 236
| | | re: My first program in VB Quote:
Originally Posted by Parul Bagadia I also used debugger but all that m gettin in here is this:
For all the text boxes, options, variables no value is getting displayed other than the message: expression not defined in context for inno and for rest ; out of context..
Why is it so? Are you sure that you made the right calculations for converting numbers(you didnt show that in your code) and stored it in outno?
Better have a command button so that the user may click it after entering the number in the text box and selecting the conversion type option button. Finally include text2.text=outno in command button's click event.
Okay..just give me some time and i'll design the proj and attach it.
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB Quote:
Originally Posted by vdraceil Are you sure that you made the right calculations for converting numbers(you didnt show that in your code) and stored it in outno?
Better have a command button so that the user may click it after entering the number in the text box and selecting the conversion type option button. Finally include text2.text=outno in command button's click event.
Okay..just give me some time and i'll design the proj and attach it. I have done the same things as u said........ given command button for conversion; option buttons for selecting a no. system and has also provided text box for entering data.
I dont know how to display that here.
There doesnt come any option to copy the entire form...
If u tell me how to do that i can display the form design over here.
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB
can anybody plz tell me how do i attach my visual basic form over here?
So that u can understand the form design and whatever is goin wrong.
| | Needs Regular Fix | | Join Date: Mar 2008
Posts: 283
| | | re: My first program in VB Quote:
Originally Posted by Parul Bagadia Thanks budy.
May be because m new in VB, i didnt understand why do i hav to keep it blank the text2 event?
And wats wrong with integer?
I mean earlier i had used string and instead of text i had used text1.text but i was getting a error that i cannot assign it to an array... M not sure wether i got that. Sorry, when I had used text2_change() that was as a call to the subroutine as I had slightly miss read the code but it was not very clear, please can you advise
1. how the form is set up
2. what you want to happen when the command button is clicked , including how the display changes
I am really unclear what you are trying to achieve with this form but I have posted an example of some cut down code below that might help , again apologies if this is completely different to what you are trying to do!!
also please note that I have not included any error checking for this so if you have this code and enter a string instead of a number into the box it will throw an exception also I don't have VB as I use vb.net so the subroutines may look a little different -
-
Public Class Form1
-
-
' declare two integers used to hold the in number and out number
-
-
Dim inno As Integer
-
Dim outno As Integer
-
-
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
-
-
' when the button is clicked and the IF statement matches change outno to a value of inno x2
-
-
inno = Text1.Text
-
-
If "A" = "A" And "B" = "B" Then
-
outno = inno * 2
-
-
' change text2.text to show outno
-
-
Text2.Text = outno
-
End If
-
-
End Sub
-
-
End Class
| | Needs Regular Fix | | Join Date: Mar 2008
Posts: 283
| | | re: My first program in VB
sorry, my programming habits are probably not the best , I have just noticed the comment you made about the variables
I am not really fully sure how it works but possibly it may do what you want if you use - -
-
Private inno As Integer
-
Private outno As Integer
-
-
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB
Thanks a lot..
In my program two text boxes are there; one to insert the number and other to display the result. One command button; which when clicked will perform actual conversion. And two frames to chose the number to be converted is in which number system as well the number to be obtained should be in which no. system with in all 6 option buttons..
My code is now working for same system changes; where earlier it was not.
What i mean by same system changes is........Base of input as well as output is the same may be octal/binary/hexadecimal.
But now m getting some error while converting a no. from hexadecimal to binary.....
The code is as follows: - Public outno As String
-
Public inno As String
-
Public Sub Text1_Change()
-
inno = Text1.Text
-
End Sub
-
-
Public Sub Command1_Click()
-
-
If Option1.Value = True And Option4.Value = True Then
-
outno = inno
-
End If
-
-
If Option2.Value = True And Option5.Value = True Then
-
outno = inno
-
End If
-
-
If Option3.Value = True And Option6.Value = True Then
-
outno = inno
-
End If
-
-
If Option1.Value = True And Option6.Value = True Then
-
outno = hextobinary()
-
End If
-
-
Text2.Text = outno
-
End Sub
-
-
Public Sub Text2_Change()
-
Text2.Text = outno
-
End Sub
-
Function hextobinary()
-
Dim i As Integer
-
Dim store As Long
-
Dim reminder As String
-
Dim quot As Integer
-
Dim temp As String
-
For i = 0 To 9
-
If (inno(i) >= 0 And inno(i) <= 9) Then
-
store = store + inno(i) * pow(16, i)
-
-
ElseIf (inno(i) = a) Then
-
inno(i) = 10
-
store = store + inno(i) * pow(16, i)
-
ElseIf (inno(i) = b) Then
-
inno(i) = 11
-
store = store + inno(i) * pow(16, i)
-
ElseIf (inno(i) = c) Then
-
inno(i) = 12
-
store = store + inno(i) * pow(16, i)
-
ElseIf (inno(i) = d) Then
-
inno(i) = 13
-
store = store + inno(i) * pow(16, i)
-
ElseIf (inno(i) = d) Then
-
inno(i) = 14
-
store = store + inno(i) * pow(16, i)
-
ElseIf (inno(i) = f) Then
-
inno(i) = 15
-
store = store + inno(i) * pow(16, i)
-
End If
-
Next i
-
-
If store! = 0 Then
-
i = 0
-
Do
-
reminder(i) = store Mod 2
-
quot = store / 2
-
i = i + 1
-
While quot! = 0
-
For j = 9 To 0
-
outno(i) = reminder(j)
-
i = i + 1
-
j = j - 1
-
Exit For
-
Return
-
End Function
its showing some error as an array is expected.....
I changed the data type of inno with array but there is no such data-type....
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB
I understood, the error is because i have taken input in a number and accessing it as array in for loop.
But then can somebody tell me how do i get my number in an array?.... there is no such data-type.. or is there?
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB
In case of the function oct..it converts an expression to octal system number...can we have another number in some number system which can get converted in octal...
| | Needs Regular Fix | | Join Date: Mar 2008
Posts: 283
| | | re: My first program in VB
you seem to be accessing inno as an array ie: array(no of item)
if you are trying to select as specific character within the string / integer you will be better using the MID command I think
lol please bear in mind that there may be better ways of doing this and I have not tried to work out the conversion but here is some code to split the string -
-
For n = 1 To inno.Length
-
-
value = (Mid(inno, n, 1))
-
-
' whatever it is that you wish to do with this value
-
-
Next
-
-
to split it to an array - -
-
dim value(inno.length) as string
-
-
For n = 1 To inno.Length
-
-
value(n) = (Mid(inno, n, 1))
-
-
Next
-
-
|  | Familiar Sight | | Join Date: Jul 2007 Location: tamil nadu, INDIA
Posts: 236
| | | re: My first program in VB
i have designed a simple application which helps in base conversion..
but the problem is i dont know how to attach it here..
anyone knows how??
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB Quote:
Originally Posted by vdraceil i have designed a simple application which helps in base conversion..
but the problem is i dont know how to attach it here..
anyone knows how?? Even i have designed it. And even i dont know how ill post that here.......
Moreover i never asked for design..... M just gettin stuck somewhere in my code..
|  | Familiar Sight | | Join Date: Jul 2007 Location: tamil nadu, INDIA
Posts: 236
| | | re: My first program in VB Quote:
Originally Posted by Parul Bagadia Even i have designed it. And even i dont know how ill post that here.......
Moreover i never asked for design..... M just gettin stuck somewhere in my code.. As this is your first program in vb(and you have some difficulties with the coding),i thought you might need an example.. Do you need it? If yes i'll have to figure out a way..
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB Quote:
Originally Posted by jg007 you seem to be accessing inno as an array ie: array(no of item)
if you are trying to select as specific character within the string / integer you will be better using the MID command I think
lol please bear in mind that there may be better ways of doing this and I have not tried to work out the conversion but here is some code to split the string -
-
For n = 1 To inno.Length
-
-
value = (Mid(inno, n, 1))
-
-
' whatever it is that you wish to do with this value
-
-
Next
-
-
to split it to an array - -
-
dim value(inno.length) as string
-
-
For n = 1 To inno.Length
-
-
value(n) = (Mid(inno, n, 1))
-
-
Next
-
-
Am unable to do inno.length..
The compiler is showing error.
Is it the wrong way or sth else..
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB
Is pow function not there in VB? what do we do for exponential?
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB
I got how to do exponential thing........its just by using[b] '^['/B].
But now can someone tell me how we use not equal to sign in VB?
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB Quote:
Originally Posted by Parul Bagadia I got how to do exponential thing........its just by using[b] '^['/B].
But now can someone tell me how we use not equal to sign in VB? I got that to by writting the expression in'( )' and using <> sign; but now m not getting equal to sign.
|  | Familiar Sight | | Join Date: Jul 2007 Location: tamil nadu, INDIA
Posts: 236
| | | re: My first program in VB Quote:
Originally Posted by Parul Bagadia I got that to by writting the expression in'( )' and using <> sign; but now m not getting equal to sign. use NOT keyword..for example, 'if not a=b and not a=c then ...'
| | Needs Regular Fix | | Join Date: Mar 2008
Posts: 283
| | | re: My first program in VB Quote:
Originally Posted by Parul Bagadia Am unable to do inno.length..
The compiler is showing error.
Is it the wrong way or sth else.. what error message are you getting though ????
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB Quote:
Originally Posted by jg007 what error message are you getting though ???? I got an error as invalid qualifier. For that i used
; and got no error.
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB
Finally i have reached till converting my no. in desired base; but now the problem is my output is stored in an array of string and i dont know how will i print it in VB.
I used print statement; nothing happened, compiler didnt even show the error message, but nothing happened as well.
I want the output in text2.text(a text box), so i directly used a for loop.
Right now my desired number is in reminder....
But still its not working, data type of both text2.text and reminder which is an array is string.
Can someone spot what should be the changes? - k = j - 1
-
For i = 0 To j - 1
-
Text2.Text = reminder(k)
-
k = k - 1
-
Next i
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB
I used Debug.Print as well; but its printing the number in debugger.
This is quite important to me.
Someone if knows tell me how to print an array in text box
|  | Familiar Sight | | Join Date: Mar 2008 Location: India
Posts: 187
| | | re: My first program in VB
I even tried to convert an array in string using Join but; it was giving random answers...... I need to know how do we display an array in Textbox.. Can someone tell me?
| | Newbie | | Join Date: Nov 2008 Location: Finland
Posts: 1
| | | re: My first program in VB
I have something to say about this code: -
For i = 0 To 9
-
If (inno(i) >= 0 And inno(i) <= 9) Then
-
This was really weird, because you can't input anything else than a 10-digit number. I guess you need an array.
An array is created like this:
Dim numbers(0 To 9) As Integer
EDIT: Oh, and to answer your question, use something like this: -
For i = 0 To 9
-
Text2.Text = Text2.Text & Array(i)
-
Next
-
Hope this helps you.
|  | Similar Visual Basic 4 / 5 / 6 bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,501 network members.
|