How to add the label box value in vb6 - vb6

I want to add the lable box value in vb6
Label1 = 200
Label2 = 500
'Adding
Label3 = Label1 + Label2
'Showing output as
Label3 = 200500
I want add the 2 values
Expected Output
Label3 = 700
What was the problem in my code
Need code help

The two answers are correct, but neither of it explain to you why this happens. VB 6 (or 5 or 4 or 3) has a default property for the controls. In the case of the label, the default property is caption. Since caption is a string, and string can be concatenate using & or +, VB pickup the type and then it does the math (in this case, concat).
Label3= val(Label1) + val(Label2)
This works good.. Also you can use Cint or any other convert to number function.

long time haven't worked with VB6 but try
Label3.caption = val(Label1.caption) + val(Label2.caption)

Do something like this:-
textbox3.text = val(textbox1.text) + val(textbox2.text)

Related

time.sleep() not working as expected last

I have a canvas with 30 labels, the text is updated (always different) using a button. I have 2 questions:
1.-why the command time.sleep doesn't work as I expect?
i = 0
while len(words) >= 1:
if i = 1:
canvas.itemconfig(label1, text = "text")
time.sleep(3)
if i = 2:
canvas.itemconfig(label2, text = "text")
time.sleep(3)
if i = 3:
canvas.itemconfig(label3, text = "text")
time.sleep(3)
if i = 4:
canvas.itemconfig(label4, text = "text")
time.sleep(3)
if i = 5:
canvas.itemconfig(label5, text = "text")
time.sleep(3)
i+= 1
And so on until 30, I want to set the label and then wait 3 seconds to set the next label and so on, but the code waits 15 seconds and sets all the labels at the same time at the end.
2.-is there a way to refer to my labels using "for" to avoid writing 30 lines? All my labels are called label1, label2, label3... I have done this in other language with something like this
For i in range(1,31):
canvas.itemconfig(label[i], text = "text")
Sorry if I wrote something wrong since I did it in my phone
When you use sleep in tkinter, you're freezing the program so nothing happens. If you want to make a change after a given time, use the after method.
root.after(1000, myfunction) # call myfunction after 1 second
As for the label updates, you are correct. Creating a label array would make it easier.
With the current label names, you can add them all to a list then iterate the list.
lbllst = [label1, label2, label3, ......]
for lbl in lbllist:
canvas.itemconfig(lbl, text = "text")

Textwidth not showing correctly in millimeters vb6

I have a picture box and I print contents in it. I want to know the exact textwidth of the text in millimeters. But I get wrong value. here is my code
me.scalemode = vbmillimeters
picturebox.scalemode = vbmillimeters
picturebox.fontname = "Arial"
picturebox.fontsize = 12
debug.print textwidth("AB.C.D.E. FGHIJKLMN")
When i measure in the printout in paper it is 48 mm
but it shows 32.97mm
please help me where am wrong.
Thanks in advance
If you need the width of the text printed to the picture box, use:
PictureBox.textwidth("AB.C.D.E. FGHIJKLMN")
What you are actually doing: textwidth("AB.C.D.E. FGHIJKLMN") is mesuring the same text printed to the Form (Me).
Doing like this would be less error-prone:
Dim TextWidth as Single
With PictureBox
.ScaleMode = vbMillimeters
.FontName = "Arial"
.FontSize = 12
TextWidth = .TextWidth("AB.C.D.E. FGHIJKLMN")
End With
because if you are then switching to paper, you can also easily switch context:
With SelectedPrinter....

View Datagridview in Print Preview Window

I am creating an application in vb.net. I have a datagridview control in my VB form. I need to view it on printpreview window with the contents in it. I have other control like labels and textboxes in the form and I can view all in printpreview. In the case of Datagridview control, I have a working printpreview code which I got from net. My problem is, I need to change the x and y positions of datagridview control. With the following code, the datagridview control is displaying over the other controls. I dont know how to do it in this code. Please help me.
I need to change the x and y positions of DataGridView like given in the below code(50 and 225).
e.Graphics.DrawString(Label7.Text, Label7.Font, Brushes.Black, 50, 225)
The code I used to display gridview is given below.
Code :
Dim ColumnCount As Integer = DataGridView1.ColumnCount
Dim RowCount As Integer = DataGridView1.RowCount
Dim CellTopPos As Integer = PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Top
For Row = 0 To RowCount - 2
Dim CellLeftPos As Integer = PrintDocument1.PrinterSettings.DefaultPageSettings.Margins.Left
For Cell = 0 To ColumnCount - 1
Dim CellValue As String = DataGridView1.Rows(Row).Cells(Cell).Value.ToString()
Dim CellWidth = DataGridView1.Rows(Row).Cells(Cell).Size.Width + 10
Dim CellHeight = DataGridView1.Rows(Row).Cells(Cell).Size.Height
Dim Brush As New SolidBrush(Color.Black)
e.Graphics.DrawString(CellValue, New Font("arial", 9), Brush, CellLeftPos, CellTopPos)
e.Graphics.DrawRectangle(Pens.Black, CellLeftPos, CellTopPos, CellWidth, CellHeight)
CellLeftPos += CellWidth
Next
CellTopPos += DataGridView1.Rows(Row).Cells(0).Size.Height
Next
Use this class .. unfortunately there is characters limit so i can not able to post code
http://www.codeproject.com/Articles/18042/Another-DataGridView-Printer

vb6 use same button on 2 textbox

Hi our instructor asked us to create a calculator that only uses button
for the value input in, It should have enter value1 on 1st textbox and then the other value on the next textbox in vb6
is there a way to use the same button to enter a value on the next textbox?
lets say after you press button 3 it will show on textbox1
Text1.Text = "3"
my problem is it wont go to the next textbox after it show the number 3
I've already tried
If Text2.Setfoucs = True Then
Text2.Text = "3"
Else
Text1.Text = "3"
End If
Its giving me error.
I just wanted to use the same button on the second textbox
after it was used on the 1st textbox
I thought of using another bunch of buttons and set visible = true after the
1st button was pressed so that the nextone will be
Text2.Text = "3"
Im just a beginner on VB6 any suggestions will be greatly appreciated.
here is what the project looks like
http://i.imgur.com/ixK9s1U.png
setFocus is a function, not a variable, and it doesn't return a value, so you can't use it in an if clause.
Here is my suggestion to accomplish what you're trying to do:
Add a GotFocus event to each of your textboxes, that sets a variable. Like so:
Private selectedTxtBox As Integer
Private Sub Text1_GotFocus()
selectedTxtBox = 1
End Sub
Private Sub Text2_GotFocus()
selectedTxtBox = 2
End Sub
Then on your button, you can do:
If selectedTxtBox = 1 Then
Text1.Text = "3"
ElseIf selectedTxtBox = 2 Then
Text2.Text = "3"
End If

Change frame caption forecolor

I have a radio button within a frame (frame1). On frame 2, I have a number of checkboxes. This frame (frame2) becomes editable when the radio button is selected from frame1. How can I modify my code so the Caption of frame2's forecolor changes also?
I've tried adding the following to the logic already in place for enabling the frame, but doesn't work.
frame2.ForeColor = vbRed (should work?)
frame2.Caption = vbRed
I've also tried the Hex color code with no look.
Can anyone advise?
Im using a enum to assign the radio buttons.
(Found in global declaration)
Private Enum
ExampleRef optB1_blah = 1
etc...
optB5_blah = 4
End Enum
(This code is found in a function)
If Example(ExampleRef.optB5_radiobtnchoice).Value Then
'//bug fix -
frame2.ForeColor = vbRed
'//If Not statement with unrelated logic
If vblnShowErrors Then
Err.Raise 10000, VALIDATION, "error, you cant make this choice."
End If
blnDataFail = True
End If
End If
blnMinData = Not blnDataFail
End If
Fixed this, I was looking in the wrong function, long day! The frame.ForeColor = X syntax was fine.

Resources