VB.net result for x base - vb.net-2010

This is my first question and I am new to vb.net and programming I am trying to build the simple xbase calculation like if I had 2 eggs value 2 dollars so 3 eags value calculated by the program.
I did this code but the answer never show up in the Textbox4
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim a As Integer
a = TextBox1.Text
Dim b As Integer
b = TextBox2.Text
Dim c As Integer
c = TextBox3.Text
Dim d As Integer
d = TextBox4.Text
TextBox4.Text = b * c / a
End Sub
End Class
I want Textbox4 automatically show result when i put the values.

Add a button to the form and move the code the click event handler for the button. When the form loads, those textboxes are empty.

You can't do this in the Form load event. There is no way to enter stuff into the textboxes before the code executes. You can either add a button and use its click event, or textchanged events when you enter stuff into the textboxes.

Related

Visual Basic - Create an application to pick a number

Hye,
I'm new with Visual Basic and i had a few problems.
I've create a new Windows Forms Application in Visual Basic. Using one TextBox and two Button. The TextBox for displaying the number. One button for Generate and another one for Help.
I want to create a simple application that will pick one of the listed number instead of generating it.
Example :
Each time I click on the Generate button, it will pick either 14412GG or TE921W or 13123SA only. The number will appear in the TextBox.
Evertime i click on Help button, a windows will pop-up with help messages. Multiple line messages.
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
**This is for Generate button**
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
**This is for Help button**
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
**This is to display the number**
End Sub
End Class
Save those 3 values in an arrayList, create a Random class.
private static ArrayList values1 = new ArrayList{"14412GG","TE921W","13123SA"};
Random rnd = new Random();
int r = rnd.Next(values1.Count);
(string)values1[r]
Display the string when Generate button clicks.
TextBoxVariable.Text = (string)values1[r];
Use Messagebox.Show to display the help message.
MessageBox.Show(""Help message line1" + Environment.NewLine + "Help message line2";");
Hope this helps!
Here is what you need :
'Button 1
Dim pool As String = "ABCDEFGHIJKLMNOPQURSTUVWXYZ1234567890"
Dim cc As New Random
Dim count = 0
While count <= 6
textbox1.text = cc.Next(0, pool.Length)
'button2
msgbox("message line1" + vbnewline + "message line2.")
i hope it be useful to you .

Visual Basic how to disable all checkbox?

I'm working in Visual Basic 2010 Express
I want to disable all checkbox of a Form
Not only that, as you can see in the code, there are 49 checkbox. And everyone has an event, I want to save code and optimize it.
Public Class Form1
Dim intNumber As Integer
Dim arrNumber(0 To 5) As Integer
Dim i, x, y As Integer
Dim maxCheck = 6
Dim count As Integer = 1
Private Sub setCheckbox(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckBox9.Click, CheckBox8.Click, CheckBox7.Click, CheckBox6.Click, CheckBox5.Click, CheckBox49.Click, CheckBox48.Click, CheckBox47.Click, CheckBox46.Click, CheckBox45.Click, CheckBox44.Click, CheckBox43.Click, CheckBox42.Click, CheckBox41.Click, CheckBox40.Click, CheckBox4.Click, CheckBox39.Click, CheckBox38.Click, CheckBox37.Click, CheckBox36.Click, CheckBox35.Click, CheckBox34.Click, CheckBox33.Click, CheckBox32.Click, CheckBox31.Click, CheckBox30.Click, CheckBox3.Click, CheckBox29.Click, CheckBox28.Click, CheckBox27.Click, CheckBox26.Click, CheckBox25.Click, CheckBox24.Click, CheckBox23.Click, CheckBox22.Click, CheckBox21.Click, CheckBox20.Click, CheckBox2.Click, CheckBox19.Click, CheckBox18.Click, CheckBox17.Click, CheckBox16.Click, CheckBox15.Click, CheckBox14.Click, CheckBox13.Click, CheckBox12.Click, CheckBox11.Click, CheckBox10.Click, CheckBox1.Click
If count < 6 Then
count = count + 1
Else
End If
End Sub
End Class
Loop through all children of the form and if the child is of type CheckBox, disable it. This should take only a few lines of code: you can use the Controls collection of the form to access children. Something like:
For Each ctrl As Control In container.Controls
If TypeOf ctrl Is CheckBox Then
ctrl.Enabled = False
End If
Next
You should do the same with event handlers: instead of having that crazy Handles control list, just add the event handler to the checkboxes in a loop during runtime. MSDN has an example on how to use AddHandler for that.

Data doesn't display when working with multiple forms

I'm new to VB.NET and have been struggling all afternoon with something. I've found similar questions on the forum but none of them seemed to describe my problem exactly. I'm fairly sure that I'm missing something very basic.
I have made a main form which currently holds only one button which purpose is to open up a second form and close the main form. Based on the settings the user will select on the 2nd form the first form might have to be adapted to match with the new settings. But the problem occurs even before that.
The 'settings' form has 15 textboxes which I drew onto the form in development mode. They are called ID1, ID2,..,ID15. The values which I want to display in there are saved in an array:
Dim ids(15) as integer
Next, I created a module to simulate what you could call a control array as I used to use them in VB6.
Public sources() As TextBox = [frmSettings.ID1, frmSettings.ID2, //and so on
I did this to be able to iterate through all the 15 textboxes:
For i = 0 To 14
Sources(i).Text = ids(i + 1)
Next
Then I added on the main form this code to the Button1_Click() event:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
frmSettings.Show()
Me.Close()
End Sub
I did the same thing for the 'exit ' button on the frmSettings form.
This seems to work, but only once. I launch the application, push the button and frmSettings pops up and shows all the values from the array in the textboxes. When I push the 'close' button, I return to the main page.
So far so good, but if I try to return to frmSettings a second time, all the textboxes remain blank as if the code I added to the form never gets executed.
Any help would be greatly appreciated!
First, make sure the array that holds your data is accessible to both forms:
Module Module1
Public ids(15) As Integer
End Module
There should not be a declaration for "ids" in either form.
Next, make frmSettings itself responsible for loading and saving the data:
Public Class frmSettings
Private Sub frmSettings_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim matches() As Control
For i As Integer = 0 To 14
matches = Me.Controls.Find("ID" & (i + 1), True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is TextBox Then
Dim TB As TextBox = DirectCast(matches(0), TextBox)
TB.Text = ids(i)
End If
Next
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim valid As Boolean = True
Dim matches() As Control
For i As Integer = 0 To 14
matches = Me.Controls.Find("ID" & (i + 1), True)
If matches.Length > 0 AndAlso TypeOf matches(0) Is TextBox Then
Dim TB As TextBox = DirectCast(matches(0), TextBox)
Dim value As Integer
If Integer.TryParse(TB.Text, value) Then
ids(i) = value
Else
MessageBox.Show(TB.Name & ": " & TB.Text, "Invalid Value", MessageBoxButtons.OK, MessageBoxIcon.Warning)
valid = False
End If
End If
Next
If valid Then
Me.Close()
End If
End Sub
End Class

Form_Load doesn't execute in application

I am new to Visual Basic. I have installed Microsoft Visual Studio 2010. Created a new Windows Form Application. As an example, I made a simple program which will ask the end user to input 2 numbers and allow them to either add them or subtract the second number from the first one and display the output in a Textbox.
Now, I added another Subroutine which would be executed automatically when the Windows Form loads. This would calculate the width of the output Textbox and the Form Width and display at the bottom.
This is how the code looks like right now:
Public Class Form1
' Run this Subroutine initially to display the Form and Text box width
Private Sub Form_Load()
Label5.Text = TextBox3.Width
Label7.Text = Me.Width
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim a As Integer
Dim b As Integer
a = TextBox1.Text
b = TextBox2.Text
TextBox3.Text = a + b
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim a As Integer
Dim b As Integer
a = TextBox1.Text
b = TextBox2.Text
TextBox3.Text = a - b
End Sub
End Class
While everything works correctly for the addition and subtraction, it does not display the Form and output Textbox width in the Windows Form.
I think, Form_Load() is not executing properly.
I also tried, Form_Activate() but that did not work either.
Once I am able to do this, I would like to extend this concept to resize the output Textbox along with the Form resize. However, for the purpose of understanding I wanted to see if I can execute Form_Load() successfully.
Thanks.
Form_Load doesn’t execute. For now, it’s just any other method. In order to tell VB to make this method handle the Load event, you need to tell it so:
Private Sub Form_Load(sender As Object, e As EventArgs) Handles MyBase.Loasd
Label5.Text = TextBox3.Width
Label7.Text = Me.Width
End Sub
(And add the required parameters for the event.)
A few other remarks:
Ensure that Option Strict On is enabled in your project options at all times. This will make the compiler much stricter with your code and flag more errors. This is a good thing since these errors are potential bugs. In particular, your code is very lax with conversions between different data types, these should be made explicit.
Initialise variables when you declare them, don’t assign a value in a separate statement. That is, write this:
Dim a As Integer = Integer.Parse(TextBox1.Text)
(Explicit conversion added as well.)
If you want to make a control fill the form, you can just set its Dock property appropriately in the forms editor, instead of having to program this manually.
You need to add the Handle so the app executes it automatically:
Private Sub Form_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
'...
End Sub

[VB.NET]How to add a string item, returned from another form, to a list box at a specific spot in place of another?

The idea is that there's a list-box with items, and say you want to modify an item in the middle of the list. You select that item and click "Modify" button and a new form appears with the previously selected item data from first form ready to be modified in a text-box. After modifying and clicking Ok the second form suppose to return that modified string to the first form and insert the modified string into the same spot instead of the originally selected item, so it looks like it was edited to the user.
Edit:
Translated the pseudo code to actual VB.NET code to refresh my own memory :D
string = InputBox("Enter text")
// Do whatever you want with the string
x = listBox.SelectedIndex
listBox.Items(x) = string
You can try Content in place of Text too.
Make sure that the form that pops up is Modal. Here is a simple example of what you can do. (This assumes your listbox items are strings and is an example for editing up to three listbox items only. If the list is going to be much larger you will want to pursue a different architecture.)
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim intTextboxCounter As Integer = 0
For Each i As Integer In Form1.ListBox1.SelectedIndices
Select Case intTextboxCounter
Case 0
TextBox1.Text = Form1.ListBox1.Items(i)
Case 1
TextBox2.Text = Form1.ListBox1.Items(i)
Case 3
TextBox3.Text = Form1.ListBox1.Items(i)
End Select
intTextboxCounter += 1
Next
End Sub
When this loads it will spin through the selected list items and put its value in a textbox. To update the values...
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim intTextboxCounter As Integer = 0
For Each i As Integer In Form1.ListBox1.SelectedIndices
Select Case intTextboxCounter
Case 0
Form1.ListBox1.Items(i) = TextBox1.Text
Case 1
Form1.ListBox1.Items(i) = TextBox2.Text
Case 2
Form1.ListBox1.Items(i) = TextBox3.Text
End Select
Next
End Sub