Connecting Tech Pros Worldwide Forums | Help | Site Map

My first program in VB

Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#1: Aug 25 '08
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..
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Load()
  2.  
  3. Dim intext As String
  4. intext = Text1.Text
  5.  
  6. Dim outtext As String
  7. outtext = Text2.Text
  8. if (option1.Value=true) and (option4.Value=true)
  9. outtext = intext
  10. End If
  11.  
  12.  
  13.  
  14.  
  15.  
  16.  
  17. 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.

Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#2: Aug 25 '08

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?
vdraceil's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: tamil nadu, INDIA
Posts: 236
#3: Aug 26 '08

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.
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#4: Sep 2 '08

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
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#5: Sep 2 '08

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
#6: Sep 2 '08

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
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#7: Sep 2 '08

re: My first program in VB


Please reply fast. I'm stuck with first program itself.
vdraceil's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: tamil nadu, INDIA
Posts: 236
#8: Sep 2 '08

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.
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#9: Sep 2 '08

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?
vdraceil's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: tamil nadu, INDIA
Posts: 236
#10: Sep 2 '08

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)
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#11: Sep 2 '08

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.
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#12: Sep 2 '08

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.
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#13: Sep 2 '08

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.
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#14: Sep 2 '08

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?
vdraceil's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: tamil nadu, INDIA
Posts: 236
#15: Sep 2 '08

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.
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#16: Sep 2 '08

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.
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#17: Sep 2 '08

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
#18: Sep 2 '08

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

Expand|Select|Wrap|Line Numbers
  1.  
  2. Public Class Form1
  3.  
  4. ' declare two integers used to hold the in number and out number 
  5.  
  6.     Dim inno As Integer
  7.     Dim outno As Integer
  8.  
  9. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  10.  
  11. ' when the button is clicked and the IF statement matches change outno to a value of inno x2 
  12.  
  13.         inno = Text1.Text
  14.  
  15.         If "A" = "A" And "B" = "B" Then
  16.             outno = inno * 2
  17.  
  18. ' change text2.text to show outno
  19.  
  20.             Text2.Text = outno
  21.         End If
  22.  
  23.     End Sub
  24.  
  25. End Class
Needs Regular Fix
 
Join Date: Mar 2008
Posts: 283
#19: Sep 2 '08

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 -

Expand|Select|Wrap|Line Numbers
  1.  
  2.    Private inno As Integer
  3.    Private outno As Integer
  4.  
  5.  
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#20: Sep 3 '08

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:
Expand|Select|Wrap|Line Numbers
  1. Public outno As String
  2. Public inno As String
  3. Public Sub Text1_Change()
  4. inno = Text1.Text
  5. End Sub
  6.  
  7.  Public Sub Command1_Click()
  8.  
  9.  If Option1.Value = True And Option4.Value = True Then
  10.  outno = inno
  11.  End If
  12.  
  13.  If Option2.Value = True And Option5.Value = True Then
  14.  outno = inno
  15.  End If
  16.  
  17.  If Option3.Value = True And Option6.Value = True Then
  18.  outno = inno
  19.  End If
  20.  
  21.  If Option1.Value = True And Option6.Value = True Then
  22.  outno = hextobinary()
  23.  End If
  24.  
  25.  Text2.Text = outno
  26. End Sub
  27.  
  28. Public Sub Text2_Change()
  29.  Text2.Text = outno
  30. End Sub
  31. Function hextobinary()
  32. Dim i As Integer
  33. Dim store As Long
  34. Dim reminder As String
  35. Dim quot As Integer
  36. Dim temp As String
  37. For i = 0 To 9
  38.  If (inno(i) >= 0 And inno(i) <= 9) Then
  39.   store = store + inno(i) * pow(16, i)
  40.  
  41.   ElseIf (inno(i) = a) Then
  42.   inno(i) = 10
  43.   store = store + inno(i) * pow(16, i)
  44.   ElseIf (inno(i) = b) Then
  45.   inno(i) = 11
  46.   store = store + inno(i) * pow(16, i)
  47.   ElseIf (inno(i) = c) Then
  48.   inno(i) = 12
  49.   store = store + inno(i) * pow(16, i)
  50.   ElseIf (inno(i) = d) Then
  51.   inno(i) = 13
  52.   store = store + inno(i) * pow(16, i)
  53.   ElseIf (inno(i) = d) Then
  54.   inno(i) = 14
  55.   store = store + inno(i) * pow(16, i)
  56.   ElseIf (inno(i) = f) Then
  57.   inno(i) = 15
  58.   store = store + inno(i) * pow(16, i)
  59.  End If
  60.  Next i
  61.  
  62. If store! = 0 Then
  63. i = 0
  64.  Do
  65.   reminder(i) = store Mod 2
  66.   quot = store / 2
  67.   i = i + 1
  68.  While quot! = 0
  69. For j = 9 To 0
  70. outno(i) = reminder(j)
  71. i = i + 1
  72. j = j - 1
  73. Exit For
  74. Return
  75. 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....
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#21: Sep 3 '08

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?
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#22: Sep 3 '08

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
#23: Sep 3 '08

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
Expand|Select|Wrap|Line Numbers
  1.  
  2. For n = 1 To inno.Length
  3.  
  4.       value = (Mid(inno, n, 1))
  5.  
  6. ' whatever it is that you wish to do with this value
  7.  
  8. Next
  9.  
  10.  
to split it to an array -

Expand|Select|Wrap|Line Numbers
  1.  
  2. dim value(inno.length) as string
  3.  
  4. For n = 1 To inno.Length
  5.  
  6.       value(n) = (Mid(inno, n, 1))
  7.  
  8. Next
  9.  
  10.  
vdraceil's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: tamil nadu, INDIA
Posts: 236
#24: Sep 8 '08

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??
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#25: Sep 8 '08

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..
vdraceil's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: tamil nadu, INDIA
Posts: 236
#26: Sep 8 '08

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..
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#27: Sep 8 '08

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

Expand|Select|Wrap|Line Numbers
  1.  
  2. For n = 1 To inno.Length
  3.  
  4.       value = (Mid(inno, n, 1))
  5.  
  6. ' whatever it is that you wish to do with this value
  7.  
  8. Next
  9.  
  10.  
to split it to an array -

Expand|Select|Wrap|Line Numbers
  1.  
  2. dim value(inno.length) as string
  3.  
  4. For n = 1 To inno.Length
  5.  
  6.       value(n) = (Mid(inno, n, 1))
  7.  
  8. Next
  9.  
  10.  

Am unable to do inno.length..
The compiler is showing error.
Is it the wrong way or sth else..
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#28: Sep 9 '08

re: My first program in VB


Is pow function not there in VB? what do we do for exponential?
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#29: Sep 9 '08

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?
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#30: Sep 9 '08

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.
vdraceil's Avatar
Familiar Sight
 
Join Date: Jul 2007
Location: tamil nadu, INDIA
Posts: 236
#31: Sep 9 '08

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
#32: Sep 9 '08

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 ????
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#33: Sep 10 '08

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
Expand|Select|Wrap|Line Numbers
  1. len(inno)
; and got no error.
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#34: Sep 10 '08

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?
Expand|Select|Wrap|Line Numbers
  1. k = j - 1
  2. For i = 0 To j - 1
  3.   Text2.Text = reminder(k)
  4.   k = k - 1
  5. Next i
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#35: Sep 10 '08

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
Parul Bagadia's Avatar
Familiar Sight
 
Join Date: Mar 2008
Location: India
Posts: 187
#36: Sep 10 '08

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
#37: Nov 27 '08

re: My first program in VB


I have something to say about this code:
Expand|Select|Wrap|Line Numbers
  1. For i = 0 To 9
  2. If (inno(i) >= 0 And inno(i) <= 9) Then
  3.  
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:
Expand|Select|Wrap|Line Numbers
  1. For i = 0 To 9
  2. Text2.Text = Text2.Text & Array(i)
  3. Next
  4.  
Hope this helps you.
Reply