How to add direct numbers in a text box in visual studio 2012? - visual-studio-2012

I'm trying to make something like a quantity box for my finals windows form project, and when I input as simple code like:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
TextBox2.Text = HigB
HigB += 1
End Sub
Basically, what I want to happen is that when I click the button, it adds '1' to the text box, like a quantity box.
It does work but when I click the button it doesn't start with '1' directly, instead, it starts with '0'. I think the problem is with the declaration of 'HigB' which is declared as an Integer but when I tried declaring it to different a type of numerical data such as Double, etc it still doesn't work.

Related

VB6 Automation Error when calling Add on previously created MultiPage

I want to generate a bunch of MultiPages and create new Pages dynamically in my app, but i'm getting Run-time error '-2147417848 (80010108)': Automation error The object invoked has disconnected from its clients.
Steps to reproduce
In a Class Module named TestClass:
Public WithEvents TestMultiPage As MsForms.MultiPage
Sub createPage()
TestMultiPage.Add
End Sub
In a UserForm named TestForm:
Dim TestInstances as New Collection
Private Sub UserForm_MouseDown(ByVal Button As Integer, ByVal Shift As Integer, ByVal X as Single, ByVal Y as Single)
If Button = fmButtonRight Then
Dim TestInstance as New TestClass
Set TestInstance.TestMultiPage = Me.Controls.Add("Forms.MultiPage.1")
TestInstances.Add TestInstance
End If
End Sub
Private Sub UserForm_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
Dim TestInstance As TestClass: Set TestInstance = TestInstances(1)
TestInstance.createPage
End Sub
When i right-click the UserForm twice, i get two MultiPages. Then i double-click the UserForm, expecting the first MultiPage to have a new Page. But i hit the automation error at TestInstance.createPage -> TestMultiPage.Add, even though all the variables seem present from the Locals window.
What am i missing?
Conclusion
Following #GSerg's answer, i suppose there's no way to do this with MultiPage.
Instead i have to use TabStrip instead and emulate the other behaviour of MultiPage.
Just to add some context, i was trying to create a browser-like UI with windows and tabs (a TabStrip at the bottom representing different windows, each window corresponding to a MultiPage with multiple tabs). I hit the obscure error when switching back to a previous MultiPage and creating a new tab.
There appears to be a problem in MSForms, where it cripples the existing MultiPage controls when a new one is added. To reproduce the problem, you don't need collections, arrays, classes, or even variables:
Sub Reproduce()
Me.Controls.Add "Forms.MultiPage.1", "TestInstance1"
Me.Controls("TestInstance1").Add ' That works
Me.Controls.Add "Forms.MultiPage.1", "TestInstance2"
Me.Controls("TestInstance1").Add ' Now it does not
Me.Controls("TestInstance2").Add ' But the new shiny one does
Me.Controls.Add "Forms.MultiPage.1", "TestInstance3"
Me.Controls("TestInstance2").Add ' Now the instance 2 is also defunct
Me.Controls("TestInstance3").Add ' Only the latest one works
End Sub
I do not know why that is so. It looks like a bug in MSForms.
The controls work fine otherwise, and their properties are accessible, you just can't call Add anymore.

VBA Excel : Populate TextBox with specific string of text when Option Button is selected

I developed a Form in Excel (2016) and I am trying (with VBA) to configure its behavior so that if the user selects a particular option button, two additional things happen:
A checkbox further down on the form is automatically checked.
A text box further down on the form automatically displays a set string of text.
More specifically, if the user selects OptionButtonABC, then ...
CheckBoxNone should become checked
TextBoxCompanyName (which does not display any text by default) should now display the string: 'ABC'.
I initially created a subroutine that just targeted condition 1, and everything worked fine. However, when I try to integrate the code required to handle condition 2, things start to unravel.
Below, you will see the code in its most current state. I have a Private Sub that initiates upon the Click event and then immediately defines a variable as a string. I then set up an if/then statement that specifies what should happen IF OptionButtonABC is true. Namely, CheckBoxNone should be selected (this works fine) AND TextBoxCompanyName should now display the string 'ABC'.
Private Sub OptionButtonABC_Click()
Dim Variable_ABC As String
Variable_ABC = ABC
If Me.OptionButtonABC.Value = True Then
Me.CheckBoxNone = True And Me.TextBoxCompanyName.Text = Variable_ABC
End If
End Sub
The desired behavior should (theoretically) be pretty easy to achieve, but my experience with VBA is still pretty limited, so I am reaching out to the community for a bit of advice. As I mentioned above, the code above works for the first condition. However, I am still unable to get the string of text ('ABC') to show up in the text box.
Thanks in advance for any advice you may offer.
Private Sub OptionButtonABC_Click()
Dim Variable_ABC As String
Variable_ABC = "ABC" 'String Values uses double quotes
If Me.OptionButtonABC.Value = True Then
Me.CheckBoxNone = True
Me.TextBoxCompanyName.Text = Variable_ABC
End If
End Sub
The operator AND must be used only in the IF statement comparison, not in what you want to do.

Can I use a string as a name in Visual Basic to change an objects properties?

My Idea is not too hard. I have a button, a string called "progname", a TextBox and three Progressbars.
When I enter a number into the TextBox and press the button, the following code run's through.
Dim progname As String
progname = "Progressbar" & TextBox1.Text
Now I have a string called "progname" with relevant value.For an example "Progressbar2".
What I want to achieve is write something like:
progname.Value += 1
Which can't be done, as "Value" is not a Member of "String". How can I do this?
Overall what I want, is to be able to select one of the three progressbars by typing one of the numbers 1-3 into the TextBox and then change that ones porperties.
Yes you can.
A basic example is this, which searches your form for controls with the name matching your string. It then changes the type to a ProgressBar so you can access all the methods ..
Dim progbar As ProgressBar = CType(Me.Controls.Find(progName, False)(0), ProgressBar)
progbar.Value += 1

New to Visual Basic

I'm completely new to visual basic and somewhat new to programming in general. I'm trying to teach myself how to use visual basic because I was told it was easier to learn. My question is how do I connect an execute button to a text box to calculate a simple math problem? Do I double click the button I want to make the execution and then put in the code? Can someone give me a simple sample code that when you enter a number in a text box and press the execute button, the number entered would be multiplied by another number like 2 or whatever and then be displayed in another box? I just need a way to understand how the coding works and connects the gui buttons and text boxes so I can understand it better. I'm good and learning on my own when I see how things work and then I can put things together and figure them out. Thanks
That's pretty much it...
...of course the devil is in the details. The main problem you'll run into in this scenario is that your TextBox holds a String, but you want to manipulate it as a Number. First you need to convert the string to a number, then do the math. This also allows you to determine if the String in the TextBox is not a number, such as if the user entered "Unicorns are real!" in it:
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim x As Integer
Dim strInput As String = TextBox1.Text
If Integer.TryParse(strInput, x) Then
Dim result As Integer = x * 2
Label1.Text = x.ToString & " * 2 = " & result.ToString
Else
Label1.Text = "Invalid Integer: " & strInput
End If
End Sub
End Class

String declaration error in if else

I have Camp as string. When I write this code, I get an error:
*Me.BoatDesc =< the expression you entered refer to an object that is close*
Here is my code
private Sub Save_Click()
Dim Camp As String
If Me.BoatDesc = "Camp" Then
Me.Amount = Me.Amount * 12
End If
Correct me if I am wrong.
You are using VBA, not VB.Net. Here are some notes
Here is a simple form, it will be open when the code is run. The code will be run by clicking Save. Note that the default for an MS Access bound form is to save, so you might like to use a different name.
This is the form in design view, note that there is a control named BoatDesc and another named Amount, as can only be seen from the property sheet.
The save button have an [Event Procedure], which is the code.
Note that the code belongs to Form2, the form I am working with, and the words Option Explicit appear at the top. This means I cannot have unnamed variables, so it is much harder to get the names wrong.
This is the code to be run by the save button.
Option Compare Database
Option Explicit
Private Sub Save_Click()
''Do not mix up strings, variables, controls and fields
''If you want to know if a control, BoatDesc, equals
''the string "camp", you do not need this
''Dim Camp As String
If Me.BoatDesc = "Camp" Then
Me.Amount = Me.Amount * 12
End If
End Sub

Resources