I want to create a simple addition program in vb.net form application. Also i want to create all controls programmatically. I have three text boxes and one button. When the button is clicked , it take value from two text boxes and assign it to the third text box value.
I am unable to get text boxes string from button click handle.
My coding is as follows:
Public Class Form1
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Pushbutton
Dim PushButton As New Button()
PushButton.Text = "Add"
PushButton.Location = New Point(10, 100)
Me.Controls.Add(PushButton)
AddHandler PushButton.Click, AddressOf myButtonHandler_Click
'
'TextBox1
Dim TextBox1 As New TextBox
TextBox1.Location = New Point(10, 3)
Me.Controls.Add(TextBox1)
'TextBox2
Dim TextBox2 As New TextBox
TextBox2.Location = New Point(200, 3)
Me.Controls.Add(TextBox2)
Dim TextBox3 As New TextBox
TextBox3.Location = New Point(200, 100)
Me.Controls.Add(TextBox3)
End Sub
Private Sub myButtonHandler_Click(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Public Function ADD(x As Double, y As Double) As Double
ADD = x + y
End Function
End Class
Moving the declaration of the controls to Form level allows them to be seen in all the methods of the form.
Public Class Form3
Private TextBox1 As New TextBox
Private TextBox2 As New TextBox
Private TextBox3 As New TextBox
Private PushButton As New Button
Private Sub Form3_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Pushbutton
PushButton.Text = "Add"
PushButton.Location = New Point(10, 100)
Me.Controls.Add(PushButton)
AddHandler PushButton.Click, AddressOf myButtonHandler_Click
'TextBox1
TextBox1.Location = New Point(10, 3)
Controls.Add(TextBox1)
'TextBox2
TextBox2.Location = New Point(200, 3)
Controls.Add(TextBox2)
'TextBox3
TextBox3.Location = New Point(200, 100)
Controls.Add(TextBox3)
End Sub
Private Sub myButtonHandler_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim firstNumber = CDbl(TextBox1.Text)
Dim secondNumber = CDbl(TextBox2.Text)
TextBox3.Text = ADD(firstNumber, secondNumber).ToString
End Sub
Public Function ADD(x As Double, y As Double) As Double
ADD = x + y
End Function
End Class
You can technically do it the way you were, but only if you use an anonymous event handler for the button click event as shown below:
Public Class Form1
Public Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TextBox1
Dim TextBox1 As New TextBox
TextBox1.Location = New Point(10, 3)
Me.Controls.Add(TextBox1)
'TextBox2
Dim TextBox2 As New TextBox
TextBox2.Location = New Point(200, 3)
Me.Controls.Add(TextBox2)
Dim TextBox3 As New TextBox
TextBox3.Location = New Point(200, 100)
Me.Controls.Add(TextBox3)
'Pushbutton
Dim PushButton As New Button()
PushButton.Text = "Add"
PushButton.Location = New Point(10, 100)
Me.Controls.Add(PushButton)
AddHandler PushButton.Click, Sub()
Dim dblA, dblB As Double
If Double.TryParse(TextBox1.Text, dblA) AndAlso Double.TryParse(TextBox2.Text, dblB) Then
TextBox3.Text = ADD(dblA, dblB)
Else
MessageBox.Show("One or more Invalid Inputs")
End If
End Sub
End Sub
Public Function ADD(x As Double, y As Double) As Double
ADD = x + y
End Function
End Class
Though I do recommend moving those control declarations out to Form level as Mary suggested.
Related
I am trying to write (type) directly onto a bitmap. I need to be able to type at the mouse position, so where ever on the screen i click the mouse, I can start typing text with the keyboard.
Here is a working VS 2017 VB Win Form code that will print "Hello World" at the mousedown position. But it only works with predetermined text. I would like to be able to just type at that spot. I feel I am so close, just can't get it to work.
Imports System.IO
Imports System.Windows.Forms.DataVisualization.Charting
Public Class Form1
Dim WithEvents Chart1 As New Chart
Private Structure TextPoints
Dim MPos As Point
Dim Txt As String
End Structure
Private TextList As New List(Of TextPoints)
Private TempPoint As Point
Private FirstPoint As Point
Dim xcnt As Integer = -1
Dim ycnt As Integer = -1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.Size = New Size(1100, 700)
Me.Location = New Point(10, 10)
MakeBackImage()
With Chart1
.Name = "Chart1"
.Location = New System.Drawing.Point(40, 40)
.Size = New System.Drawing.Size(1010, 610)
.BackImage = "BackImg.jpg"
.Parent = Me
End With
End Sub
Private Sub Chart1_MouseDown(ByVal sender As Object,
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Chart1.MouseDown
FirstPoint = New Point(e.X, e.Y)
TempPoint = New Point(e.X, e.Y)
Me.Refresh()
End Sub
Private Sub Chart1_MouseUp(ByVal sender As Object,
ByVal e As System.Windows.Forms.MouseEventArgs) _
Handles Chart1.MouseUp
Dim T As New TextPoints With {
.MPos = TempPoint,
.Txt = "Hello World"}
TextList.Add(T)
Me.Refresh()
End Sub
Private Sub MakeBackImage()
Dim x, y As Integer
Dim img As Image = New Bitmap(1020, 620)
Dim graphics As Graphics = Graphics.FromImage(img)
graphics.Clear(Drawing.Color.White)
For x = 0 To 1000 Step 20
graphics.DrawLine(Pens.Black, x, 0, x, 600)
xcnt += 1
Next
For y = 0 To 600 Step 20
ycnt += 1
graphics.DrawLine(Pens.Black, 0, y, 1000, y)
Next
img.Save("BackImg.jpg", Imaging.ImageFormat.Jpeg)
End Sub
Private Sub Chart1_Paint(ByVal sender As Object,
ByVal e As System.Windows.Forms.PaintEventArgs) _
Handles Chart1.Paint
Dim drawString As String = "Hello World"
Dim drawFont As New Font("Arial", 14)
Dim drawBrush As New SolidBrush(Color.Black)
For Each t As TextPoints In TextList
e.Graphics.DrawString(t.Txt, drawFont,
drawBrush, t.MPos.X, t.MPos.Y)
Next
End Sub
End Class
This is a simplified code. Actually, the background image is only created once, but I added code to dynamically create it here to make the demo better.
First of all excuse me for my poor grammar and vocabulary :)
please see this source and run it:
Public Class Form1
Public pointX As Integer
Public pointY As Integer = 32
Public dynamicText As TextBox
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
pointX = 330
For i = 1 To 4
dynamicText = New Windows.Forms.TextBox
dynamicText.Name = "T" + Trim(Str(i))
dynamicText.Text = ""
dynamicText.Location = New Point(pointX, pointY)
dynamicText.Size = New Size(100, 20)
Me.Controls.Add(dynamicText)
pointX = pointX - 106
Next
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
pointX = 330
pointY = pointY + 26
For i = 1 To 4
dynamicText = New Windows.Forms.TextBox
dynamicText.Name = "T" + Trim(Str(i))
dynamicText.Text = ""
dynamicText.Location = New Point(pointX, pointY)
dynamicText.Size = New Size(100, 20)
Me.Controls.Add(dynamicText)
pointX = pointX - 106
AddHandler dynamicText.Click, AddressOf printHello1
Next
End Sub
Private Sub printHello1(ByVal sender As System.Object, ByVal e As System.EventArgs)
MsgBox(dynamicText.Name)
If dynamicText.Name = "T1" Then MsgBox("Oh! this is T1")
End Sub
End Class
why If never is not true?!
why MsgBox(dynamicText.Name) always return T4?!
i want all controlls to be access by name or array of names.
please help me thank you. :)
The global variable dynamicText takes the value of the last TextBox added in the loop inside the Button1_Click event. This happens to be the control named T4. You don't really need a global variable in this case. You can cast the sender parameter to a TextBox instance because the sender parameter is the control that has raised the event.
Private Sub printHello1(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim txt = CType(sender, "TextBox")
if txt IsNot Nothing then
MsgBox(txt.Name)
If txt.Name = "T1" Then MsgBox("Oh! this is T1")
End If
End Sub
You also don't need to recreate the controls again in the button click event. The action executed in the form load event is enough (You could add the AddHandler there). Global variables are dangerous, avoid them when possible.
See if this is acceptable. Place a panel at the bottom of your form, set Dock to Bottom, add a single button to the panel and a TextBox. Place a FlowLayoutPanel onto the form, Dock = Fill, AutoScroll = True.
The code below creates the amount of TextBox controls as inputted into TextBox. Each newly created TextBox a click event is added with simple logic.
Form code
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim count As Integer = 0
If Integer.TryParse(TextBox1.Text, count) Then
Dim demo = New TextBoxCreate(FlowLayoutPanel1, "Demos", count)
demo.CreateTextBoxes()
End If
End Sub
End Class
Class code (add a new class to the project, name it TextBoxCreate.vb)
Public Class TextBoxCreate
Public Property TextBoxes As TextBox()
Public Property TextBoxBaseName As String
Public Property TextBoxCount As Integer
Public Property ParentControl As Control
Public Sub New(
ByVal ParentControl As Control,
ByVal BaseName As String,
ByVal Count As Integer)
Me.ParentControl = ParentControl
Me.TextBoxBaseName = BaseName
Me.TextBoxCount = Count
End Sub
Public Sub CreateTextBoxes()
Dim Base As Integer = 10
TextBoxes = Enumerable.Range(0, TextBoxCount).Select(
Function(Indexer)
Dim b As New TextBox With
{
.Name = String.Concat(TextBoxBaseName, Indexer + 1),
.Text = (Indexer + 1).ToString,
.Width = 150,
.Location = New Point(25, Base),
.Parent = Me.ParentControl,
.Visible = True
}
AddHandler b.Click, Sub(sender As Object, e As EventArgs)
Dim tb As TextBox = CType(sender, TextBox)
If tb.Name = TextBoxBaseName & "1" Then
tb.Text = "Got it"
Else
MessageBox.Show(tb.Name)
End If
End Sub
Me.ParentControl.Controls.Add(b)
Base += 30
Return b
End Function).ToArray
End Sub
End Class
I'm trying to make a userinterface that generates itself on request (button click)
Private Sub Body_new_part_add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Body_new_part_add.Click
So when i add a Combobox as first it's no problem it generates the box & places it on the right position etc.
Dim oTypeBox As New ComboBox
oTypeBox.Name = "Body_type_" & oBodyPartsNumber
oTypeBox.Location = New System.Drawing.Point(7, 78)
Body_parts.Controls.Add(oTypeBox)
Now i want to add another control, a textbox next to the Combobox.
Dim oTypeBox As New ComboBox
oTypeBox.Name = "Body_type_" & oBodyPartsNumber
oTypeBox.Location = New System.Drawing.Point(7, 78)
Body_parts.Controls.Add(oTypeBox)
Dim oTextbox As New TextBox
oTextbox.name = "test"
oTextbox.Location = New System.Drawing.Point(50, 78)
Body_parts.Controls.Add(oTextbox)
This gives me this error.
'New' cannot be used on an interface.
What do i need to change in order to get this done? I need to add +- 10 controls on each button click event.
Try this one
Public Class Form1
Dim cLeft As Integer = 1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
AddNewTextBox()
End Sub
Public Function AddNewTextBox() As System.Windows.Forms.TextBox
Dim txt As New System.Windows.Forms.TextBox()
Me.Controls.Add(txt)
txt.Top = cLeft * 25
txt.Left = 100
txt.Text = "TextBox " & Me.cLeft.ToString
cLeft = cLeft + 1
Return txt
End Function
End Class
I have a programs path..like utorrent and it pid too. I have achieved these values programatically using vb.net. I just want to hide their icon form tray just to run them in background and if possible attach the process with a hotkey to call them back. Is there any way to achieve this.
Option Strict On
Option Explicit On
Option Infer Off
Imports TrayHelper
Public Class Form1
Dim x1, y1 As Single
Friend WithEvents lv As New ListView With {.Parent = Me, .Dock = DockStyle.Fill}
Private il As New ImageList
Dim nxt As Integer
Friend WithEvents mnuContextMenu As New ContextMenu() 'Moved this to be declared as global
Dim mnuItemHide As New MenuItem()
Dim mnuItemExit As New MenuItem()
Dim things As List(Of TrayButton) = TrayHelper.Tray.GetTrayButtons()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.Add(lv)
lv.View = View.Details
il.ColorDepth = ColorDepth.Depth32Bit
lv.SmallImageList = il
lv.Columns.Add("Button Text", 300, HorizontalAlignment.Left)
lv.Columns.Add("PID", 50, HorizontalAlignment.Left)
lv.Columns.Add("Process Path", 600, HorizontalAlignment.Left)
'Dim things As List(Of TrayButton) = TrayHelper.Tray.GetTrayButtons()
For Each b As TrayButton In things
If b.Icon IsNot Nothing Then
il.Images.Add(b.TrayIndex.ToString, b.Icon)
Else
' When we can't find an icon, the listview will display this form's one.
' You could try to grab the icon from the process path I suppose.
il.Images.Add(b.TrayIndex.ToString, Me.Icon)
End If
Dim lvi As New ListViewItem(b.Text)
lvi.SubItems.Add(b.ProcessIdentifier.ToString)
lvi.SubItems.Add(b.ProcessPath)
lvi.ImageKey = b.TrayIndex.ToString
lv.Items.Add(lvi)
Next
lv.MultiSelect = False
'lv.ContextMenu = mnuContextMenu 'Don`t need to add if done this way
lv.FullRowSelect = True 'Added this but, you don`t need it if you don`t want it
mnuItemHide.Text = "&Hide"
mnuItemExit.Text = "&Exit"
mnuContextMenu.MenuItems.Add(mnuItemHide)
mnuContextMenu.MenuItems.Add(mnuItemExit)
AddHandler mnuItemHide.Click, AddressOf Me.menuItem1_Click
End Sub
Private Sub lv_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles lv.MouseDown
If e.Button = Windows.Forms.MouseButtons.Right Then
If lv.GetItemAt(e.X, e.Y) IsNot Nothing Then
lv.GetItemAt(e.X, e.Y).Selected = True
mnuContextMenu.Show(lv, New Point(e.X, e.Y))
mnuItemExit.Visible = True
mnuItemHide.Visible = True
End If
End If
End Sub
Private Sub menuItem1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim txtValue as String
txtValue = lv.FocusedItem.SubItems(2).Text
Kill(txtValue)
Dim txtValue1 As String
txtValue1 = lv.FocusedItem.SubItems(0).Text
MessageBox.Show(txtValue1 + " has been hidden")
End Sub
End Class
this is my code
To hide your form -> form1.visible=false
To hide your form from taskbar -> form1.ShowinTaskbar=false
then go to the form1 keydown event and put this...
If e.Control And e.KeyCode = Keys.Q Then ' ---> activate with Ctrl-Q
form1.visible=true
End If
This is my code for dynamic textbox controls in button click event. The code is working well. If i click the button 3 times, it is generated 3 text boxes. But I have no idea to assign text box values to a variable. I dont know the names of dynamic generated controls. if i want to add value to 3rd text box, how to do it?
Dim txtBx As TextBox
Static x As Integer
Static i As Integer
txtBx = New TextBox
txtBx.Location = New Point(10, 10 + x)
txtBx.Size = New Size(100, 20)
i = i + 1
x = x + 20
Me.Controls.Add(txtBx)
if i create normal textbox i can do it with,
TextBox3.Text = "Some value"
But I dont know to do this for dynamic controls.
Here's an example, storing the references in a List(Of Textbox):
Public Class Form1
Private tbList As New List(Of TextBox)
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim tb As TextBox
Dim n As Integer
n = tbList.Count + 1
tb = New TextBox
With tb
.Location = New Point(10, 10 + (n * 20))
.Name = "dynTB" & n.ToString
.Size = New Size(100, 20)
End With
Me.tbList.Add(tb)
Me.Controls.Add(tb)
End Sub
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
' Testing:
If Me.tbList.Count >= 3 Then Me.tbList(2).Text = "This is textbox 3"
End Sub
End Class