vb6 use same button on 2 textbox - vb6

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

Related

Unable to allow row selection on click over mshflexgrid control in vb6

The form that has mshflexgrid control is not allowing to select the row on which the mouse click has done. Sometimes it allows selection of previous row and not the row which is been clicked
I have tried adding one to the property .RowSel of mshflexgrid to allow the row selection, it is not working for second row selection.
Private Sub MSHFlexGrid1_Click()
last_row_selected = MSHFlexGrid1.RowSel
If last_row_selected <> 1 Then last_row_selected = last_row_selected + 1
With MSHFlexGrid1
If (boolShift And vbShiftMask) = vbShiftMask Then
SelectionOneAfterTheOther
ElseIf (boolShift And vbCtrlMask) = vbCtrlMask Then
SelectUnSelectGridRow
Else
UnSelectAllGridRows
.Row = last_row_selected
.ColSel = .Cols - 1
.CellBackColor = vbHighlight
.CellForeColor = vbHighlightText
End If
End With
MSHFlexGrid1.Refresh
End Sub
It seems that the .RowSel property isn't taking correct value for selection of row
The result needs to be proper selection of row on which it is clicked.
In your grid Click event, try something like this:
With MSHFlexGrid1
If .Row = .RowSel Then
.Col = .Cols - 1
.ColSel = 0
End If
End With
You may need to set SelectionMode on the grid control also. The above code should work if set to flexSelectionFree
Have you set the SelectionMode property? If not, you get to it by clicking "Custom" in the properties window. This opens a dialog where SelectionMode is one of the available properties.

Event Handlers for Dynamic Table Layout Panel in Visual Basic

I am making a risk-type game for school that dynamically creates a 4x4 grid of buttons inside a table layout panel in visual basic. I have successfully created the panel and buttons with names that correspond to the row and column of the button. There are also two parallel arrays - one for button owner and the other for button number - that correspond to the owner of the button and the number of "armies" in the button. My issue is that when the user clicks a certain button, I need to reference the button name/value to know how many "armies" the button has to control the "attack" portion of the player's turn.
The following code creates the table layout panel and the buttons with names.
'Create table Dynamically
Dim ColCount As Integer = 4
Dim RowCount As Integer = 4
Dim f As New System.Drawing.Font("Arial", 15)
riskTable.AutoScroll = True
riskTable.Dock = DockStyle.Fill
riskTable.ColumnCount = ColCount
riskTable.RowCount = RowCount
For rowNo As Integer = 0 To riskTable.RowCount - 1
For columnNo As Integer = 0 To riskTable.ColumnCount - 1
Dim buttonname As String
buttonname = "B" & rowNo & columnNo
Dim button As Control = New Button
button.Size = New Size(179, 100)
button.Name = buttonname
button.Text = "1"
button.ForeColor = Color.White
button.Font = f
AddHandler button.Click, AddressOf buttonname_Click
riskTable.Controls.Add(button, columnNo, rowNo)
Next
Next
Me.Controls.Add(riskTable)
This is the dynamic event handler that I created. I tried using 'Me.Click' to get the name of the button, but it only returns the name of the form. I need to have code in here that references the name of the currently clicked button to then in turn reference the box owner and box number arrays.
Private Sub buttonname_Click(sender As Object, e As EventArgs) Handles Me.Click
MessageBox.Show(Me.Name)
End Sub
Any help would be greatly appreciated! I think that once I get this working, the rest of the game will be pretty simple to figure out.
Thanks!
Put the name in 'button.Tag' instead/also:
button.Tag = buttonname
Then it is easy to get the name with:
Private Sub buttonname_Click(sender As Object, e As EventArgs) Handles Me.Click
Dim result As String = CType(CType(sender, System.Windows.Forms.Button).Tag, String)
End Sub
(Check the System.Windows.Forms.Button though, might need some tweak to match your buttons inside the table. riskTable.Controls.button ?)

How to reset the box

Using masked box in the form
masked1.mask = ##:##
In form load, masked1 display as __:__
Once user enter the values like 08:00 then reset means it should display again like this __:__
How to do this?
To clear a MaskEditBox you set the Text property to an empty string, however when the PromptInclude property is True you'll get an error. I would suggest writing a Sub method that you can call when you want to clear it.
Private Sub ClearMaskedEditBox(ByVal vMaskEditBox As MaskEdBox)
Dim strMask As String
strMask = vMaskEditBox.Mask 'save the current mask
vMaskEditBox.Mask = "" 'clear the control's mask
vMaskEditBox.Text = "" 'clear the text
vMaskEditBox.Mask = strMask 'reset the mask
End Sub
To use you call the Sub with the MaskEditBox control you want to clear.
Call ClearMaskedEditBox(masked1)

List view Double click Event

Using VB6
Listview
ID Name
001 Raja
002 Ramu
003 Sajee
..
…
Code
Private Sub listview1_DblClick()
If Not (listview1.SelectedItem Is Nothing) Then
Textbox1.text = listview1.selectedItem(0)
Textbox2.text = listview1.SelectedItem(1)
End If
End Sub
Above code is not showing the values in the text box
How to show the list view row values in the text box.
Need VB6 Code Help
The ListView SelectedItem property does not return a collection of items selected on your ListView, so therefore you can't explicitly get the first selected item, the second selected item, etc. You will need to loop through all ListItems in your ListView and check if each is selected. If it is, do what you want to do.
One problem I see with your sample code is you're using the ListView DblClick event. I might be wrong, but it looks like whenever it fires only one ListView item can be selected (the one that fired the event). A solution for this is to put your code into a new procedure. Here's one that should work:
Private Sub GetSelectedItems()
' Make sure exactly two items are selected on our ListView.
If (CheckListViewSelectedItemCount(listview1, 2)) Then
Dim blnFoundFirstItem As Boolean
blnFoundFirstItem = False
Dim i As Integer
' Find out which items are selected.
For i = 1 To listview1.ListItems.Count
If (listview1.ListItems(i).Selected) Then
' Assign the Text of the 'first' selected item to Textbox1.Text.
If (Not blnFoundFirstItem) Then
Textbox1.Text = listview1.ListItems(i).Text
blnFoundFirstItem = True
' Assign the Text of the 'second' selected item to Textbox2.Text.
Else
Textbox2.Text = listview1.ListItems(i).Text
End If
End If
Next i
Else
MsgBox "You need to select two items."
End If
End Sub
I'm not sure in which order ListItems are iterated through in my For loop. It's possible that what would be assigned to Textbox1.Text in my code you might want to assign to Textbox2.Text.
Your code required at that at least two items are selected on the ListView. I don't know if VB6 has a way to return the number of selected items so I wrote a small function that does this:
' Return True if the passed ListView control has a number of selected items that's equal to the intExpectedItemCount parameter.
Private Function CheckListViewSelectedItemCount(listView As listView, intExpectedItemCount As Integer) As Boolean
Dim intSelectedItemCount As Integer
intSelectedItemCount = 0
Dim i As Integer
For i = 1 To listView.ListItems.Count
If (listView.ListItems(i).Selected) Then
intSelectedItemCount = intSelectedItemCount + 1
End If
Next i
CheckListViewSelectedItemCount = (intSelectedItemCount = intExpectedItemCount)
End Function
I dont have vb6 at hand and its been a while since I used it, but if memory serves me right:
ListView1.SelectedItem will return you a ListViewItem which gives you a Text property along with SubItems property that gives you access to associated columns as an array.

textbox validation in vb6 (disable paste option)

validation on textbox in vb 6.0
i tried
Private Sub Text1_KeyPress(KeyAscii As Integer)
If Not (KeyAscii = vbKeyBack Or KeyAscii = vbKeyDelete Or KeyAscii = vbKeySpace Or (KeyAscii >= Asc("1") And KeyAscii <= Asc("9"))) Then
KeyAscii = 0
End If
but now i want that paste option should disable when i right click on textbox
If you want to prevent right-click menu items from being used on the textbox you can create your own context menu.
Then, using API calls you need to unhook the default menu from the textbox and hook up the custom menu.
(I'm not aware of other APIs which only let you disable/hide items in the existing context menu)
The downside off course is that any menu item you want to keep such as copy or delete you need to write the code for yourself.
You can find a very good explenation on how to do this here Disable right click menu inside a textbox and here Weird reaction on a popup menu
What next,.. what is if the user uses CTRL+V to paste? Or what if the user has paste mapped to different key combinations, other than CTRL+V?
Validate the data instead?
You can end up writing a lot of code trying to prevent data entry. Why not save that work and instead let the user enter what they like and use the validate event to validate the data?
I wrote a sample on another site on using the validate event of a textbox here: Validate Value Is Numeric. That link also has a vb6 demo project I put together attached in the post.
The type of validation is irrelevant I suppose, it simply demonstrates using the validate event allows you to focus on validating the data, rather than trying to code every single possible way you can think of trying to prevent data to be entered in the first place.
The Validate event is triggered before lostfocus and before the next control's getfocus.
Only if the validate event is not told to cancel the action, will the lostfocus event and any subsequent event be executed.
The Validate event is intended to be used to ensure the control's value is validated before executing any other event.
Private Sub Text1_Change()
If Text1.Tag <> Text1.Text Then
Text1.Text = Text1.Tag
Text1.SelStart = Len(Text1.Text)
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
Text1.Tag = Text1.Text & Chr(KeyAscii)
End Sub
Old problem but still relevant with legacy projects.
The solution proposed by Sam Sylvester is interesting but doesn't allow you to delete characters (use backspace) and appends the 22 ASCII character (Ctrl + V) in the textbox (which is bad).
This solution I found worked for me (prevents paste using Ctrl + V and also using the context menu):
Dim preventPasteFlag As Boolean
Private Sub Text1_Change()
If preventPasteFlag Then
Text1.Text = Text1.Tag
preventPasteFlag = False
Else
Text1.Tag = Text1.Text
End If
End Sub
Private Sub Text1_KeyPress(KeyAscii As Integer)
If KeyAscii = 22 Then
preventPasteFlag = True
End If
End Sub
Private Sub Text1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If Button = 2 Then
preventPasteFlag = True
End If
End Sub
this is quite similar:
Private Sub Text1_Change()
Dim i As Long, sTemp As String
For i = 1 To Len(Text1)
If InStr(1, "0123456789", Mid$(Text1, i, 1)) > 0 Then sTemp = sTemp & Mid$(Text1, i, 1)
Next
Text1 = sTemp
End Sub

Resources