Programmatically adding event handler to array of radio buttons - visual-studio

Trying to add event handlers to radio buttons that are created on user input, looking at this and this as examples.
However I get an error that answerOptions is not an event.
Create the radio buttons
Private answerOptions(n) As RadioButton
...
Private Sub showQuestion(n As Integer)
For i = 0 To answerOptions.Length - 1
answerOptions(i) = New RadioButton
AddHandler answerOptions, AddressOf Me.Radios_Click
With answerOptions(i)
' --------- SET TEXT, LOCATION ETC.
End With
Me.Controls.Add(answerOptions(i))
Next
End Sub
Planning on then handling events with
Private Sub Radios_Click(sender As Object, e As EventArgs) Handles answerOptions.checked
End Sub
I want things to happen when the radios are checked. Don't want to use checkboxes as I want to limit one selection at a time.

Try this in your form or page
Private Sub ClickButton(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim Rbtn As RadioButton
Rbtn = CType(sender, RadioButton)
MsgBox(Rbtn.Text)
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
Dim Rbtn As New RadioButton()
For i = 0 To 19
Rbtn = New RadioButton()
With Rbtn
.Name = "RBtn" & i
.Text = .Name
.Checked = False
.Left = 20
.Top = (i * 20)
.Visible = True
Me.Controls.Add(Rbtn)
AddHandler Rbtn.Click, AddressOf ClickButton
End With
Next
End Sub

Related

Drag and drop rows between two datatable using msflexgrid

I have two forms with msflexgrid to display data with datasource from datatable.
I want to drag and drop rows between two form each other. I saw this topic and edited but it doesn't work.
Drag data from DG and other controls to another DG in vb.net
This error:
Please help me!
This is my form1
form1
Code form 1:
Imports C1.Win.C1FlexGrid
Public Class frm1
Private mdt As New DataTable("Test")
Private downHitInfo As C1.Win.C1FlexGrid.HitTestInfo = Nothing
Private Sub frm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
mdt.Columns.Add("EmplCode")
mdt.Columns.Add("EmplName")
mdt.Rows.Add("012345", "A")
mdt.Rows.Add("012346", "B")
mdt.Rows.Add("012347", "C")
flg1.DataSource = mdt
With flg1
.DragMode = DragModeEnum.Manual
.DropMode = DropModeEnum.Manual
End With
End Sub
Private Sub flg1_MouseDown(sender As Object, e As MouseEventArgs) Handles flg1.MouseDown
Dim view As C1FlexGrid = CType(sender, C1FlexGrid)
Dim hitInfo As C1.Win.C1FlexGrid.HitTestInfo = view.HitTest(e.X, e.Y)
If Not Control.ModifierKeys = Keys.None Then
Exit Sub
End If
If e.Button = MouseButtons.Left Then
downHitInfo = hitInfo
End If
End Sub
Private Sub flg1_MouseMove(sender As Object, e As MouseEventArgs) Handles flg1.MouseMove
Dim view As C1FlexGrid = CType(sender, C1FlexGrid)
If e.Button = MouseButtons.Left And Not downHitInfo Is Nothing Then
Dim dragSize As Size = SystemInformation.DragSize
Dim DragRect As Rectangle = New Rectangle(New Point(Convert.ToInt32(downHitInfo.X - dragSize.Width / 2), _
Convert.ToInt32(downHitInfo.Y - dragSize.Height / 2)), dragSize)
If Not DragRect.Contains(New Point(e.X, e.Y)) Then
'Extract the DataRow
Dim gridRowView As C1.Win.C1FlexGrid.Row = DirectCast(view.Rows(downHitInfo.Row), C1.Win.C1FlexGrid.Row)
'Dim rowView As DataRowView = DirectCast(gridRowView.DataBoundItem, DataRowView)
Dim rowView As DataRowView = DirectCast(gridRowView.DataMap, DataRowView)
'Raise the DragDrop with the extracted DataRow
view.DoDragDrop(rowView.Row, DragDropEffects.Move)
downHitInfo = Nothing
End If
End If
End Sub
Private Sub btn1_Click(sender As Object, e As EventArgs) Handles btn1.Click
Dim lfrm As New frm2()
lfrm.Show()
End Sub
End Class
This is form 2:
Form 2
Code of form 2:
Imports C1.Win.C1FlexGrid
Public Class frm2
Private Sub frm2_Load(sender As Object, e As EventArgs) Handles MyBase.Load
With flg2
.DragMode = DragModeEnum.Manual
.DropMode = DropModeEnum.Manual
End With
End Sub
Private Sub flg2_DragOver(sender As Object, e As DragEventArgs) Handles flg2.DragOver
e.Effect = DragDropEffects.Move
End Sub
Private Sub flg2_DragDrop(sender As Object, e As DragEventArgs) Handles flg2.DragDrop
Dim draggedRow As DataRow = CType(e.Data.GetData(GetType(DataRow)), DataRow)
End Sub
End Class

VBa : multiple pictures on multiple picturebox

hi its a catalogue application
I have 50 pictures on my folder but i want in the app load show 12 pictures in 12 picturebox
i use this code but it gives me nothing
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim pic As PictureBox
For i = 0 To 12
pic = Me.Controls("picturebox" & i)
pic.Image = Image.FromFile("C:\Dev\Images\TEST400.jpg")
Next i
End Sub
Help plz
Your image files should be saved as TEST1.jpg,TEST2.jpg, ......TEST12.jpg etc.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim pic As PictureBox
For i = 1 To 12
pic = Me.Controls("picturebox" & i)
pic.Image = Image.FromFile(#"C:\Dev\Images\TEST" + i.ToString +".jpg")
Next i
End Sub
Note: I just wrote this code here only. Its not tested.

Add handler to runtime-created control

I found in StackOverflow some vb.net code written by "PaRiMaL RaJ". i want the same think but i must convert this code to work on VB6.
can you help me please???
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
' creating control
Dim btn1 As Button = New Button()
Dim btn2 As Button = New Button()
btn1.Parent = Me
btn1.Name = "btn1"
btn1.Top = 10
btn1.Text = "Btn1"
btn2.Parent = Me
btn2.Name = "btn2"
btn2.Top = 50
btn2.Text = "Btn2"
'adding handler for click event
AddHandler btn1.Click, AddressOf HandleDynamicButtonClick
AddHandler btn2.Click, AddressOf HandleDynamicButtonClick
End Sub
Private Sub HandleDynamicButtonClick(ByVal sender As Object, ByVal e As EventArgs)
Dim btn As Button = DirectCast(sender, Button)
If btn.Name = "btn1" Then
MessageBox.Show("Btn1 clicked")
ElseIf btn.Name = "btn2" Then
MessageBox.Show("Btn2 Clicked")
End If
End Sub
The easiest way is to use a control array. Add a button to your form in the designer and set its Index property to 0. You can hide the button if you don't want it to be visible.
Then, when you want to dynamically add more buttons at run-time, just use the Load statement.
For example, if your button was named Command1:
Load Command1(1) ' Create a new button
Command1(1).Move 50, 50, 1500, 500 ' Set its size and position
Command1(1).Caption = "New Button" ' Give it a caption
Command1(1).Visible = True ' Make it visible
The button will use the same event handlers as your original button:
Private Sub Command1_Click(Index As Integer)
' Print the caption of the clicked button...
Debug.Print Command1(Index).Caption
End Sub

Custom MsgBox without form activate being fired

I have developed a custom MsgBox that works fine in almost every way. The only problem is that when the MsgBox closes the parent form runs the Form_Activate code. A normal MsgBox does not run that code (again).
I know i could add a boolean variable to Form_Activate to check if it has fired already, but that's not the best solution when you have a dozen forms.
So, is there a way to not run Form_Activate after closing my custom MsgBox? Does the MsgBox form need to be of some special type or something? I tried all BorderStyles but that doesn't make any difference.
Are you using another form to make custom MsgBox?
You shouldn't use directly other form to show a custom messagebox.
You should create an Activex control, and Activate event won't fire again when the MsgBox is closed.
Within the control you can use a form if you want it. (Probably just have to place your code inside an ActiveX control project and use it in your forms)
I use it that way.
This is a custom MsgBox example using Activex Control, with a test form too.
http://www.codeguru.com/code/legacy/vb_othctrl/2166_CustomMsgBox.zip
I created a Class for a Custom MsgBox.
Public Class CustomMsgBox
'Creates the Main form
Dim Main As New Form
'Creates the buttons
Dim Btn1, Btn2, Btn3 As New Button
'Creates the label
Dim Lbl As New Label
'Creates the Output variable
Dim Output As Integer = 0
Private Sub Load()
'Btn1 properties
Btn1.Location = New Point(168, 69)
Btn1.AutoSize = True
Btn1.AutoSizeMode = AutoSizeMode.GrowOnly
'Btn2 properties
Btn2.Location = New Point(87, 69)
Btn1.AutoSize = True
Btn1.AutoSizeMode = AutoSizeMode.GrowOnly
'Btn3 properties
Btn3.Location = New Point(6, 69)
Btn1.AutoSize = True
Btn1.AutoSizeMode = AutoSizeMode.GrowOnly
'Lbl properties
Lbl.Location = New Point(12, 19)
Lbl.AutoSize = True
Lbl.AutoEllipsis = True
'Main form properties
Main.Size = New Size(211, 129)
Main.AutoSize = True
Main.AutoSizeMode = AutoSizeMode.GrowOnly
Main.ShowIcon = False
Main.Controls.Add(Btn1)
Main.Controls.Add(Btn2)
Main.Controls.Add(Btn3)
Main.Controls.Add(Lbl)
'Adds Handlers to the buttons
AddHandler Btn1.Click, AddressOf btn1_Click
AddHandler Btn2.Click, AddressOf btn2_Click
AddHandler Btn3.Click, AddressOf btn3_Click
End Sub
Function CstMsgBox(ByRef Msg As String, ByRef Title As String, ByRef B1 As String, Optional ByRef B2 As String = Nothing, Optional ByRef B3 As String = Nothing) As Integer
'Runs the Load() Sub
Load()
'Sets the values
Lbl.Text = Msg
Btn1.Text = B1
Btn2.Text = B2
Btn3.Text = B3
Main.Text = Title
'Checks if there is a value set to Btn2 and Btn3
If Btn2.Text = Nothing Then
Btn2.Hide()
End If
If Btn3.Text = Nothing Then
Btn3.Hide()
End If
'Shows the MsgBox
Main.Show()
'Waits until a button is pressed
Do Until Output <> 0
Application.DoEvents()
Loop
'Closes the MsgBox
Main.Close()
Return Output
End Function
Private Sub btn1_Click(ByVal sender As Object, ByVal e As EventArgs)
'Sets the Output value to 1
Output = 1
End Sub
Private Sub btn2_Click(ByVal sender As Object, ByVal e As EventArgs)
'Sets the Output value to 2
Output = 2
End Sub
Private Sub btn3_Click(ByVal sender As Object, ByVal e As EventArgs)
'Sets the Output value to 3
Output = 3
End Sub
End Class
You can use it by typing this:
Dim CMB As New CustomMsgBox
CCMB.CstMsgBox('MSG, 'TITLE, 'BUTTON1, 'Optional: BUTTON2, 'Optional: BUTTON3)
OR
Dim CMB As New CustomMsgBox
Select Case CMB.CstMsgBox('MSG, 'TITLE, 'BUTTON1, 'Optional: BUTTON2, 'Optional: BUTTON3)
Case 1
'Code to execute when button1 is pressed
Case 2
'Code to execute when button2 is pressed
Case 3
'Code to execute when button3 is pressed
End Select

How to change the location of a picture box in VB 2010 using the arrow or wasd keys?

I only have access to the internet at school, so the filters get in the way of any real research. I'm currently coding an rpg for a school project but it's difficult to get the avatar to move on a map. Here's my pathetic code so far:
Public Class Map1
Private Sub USER_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
User.Top = User.Top - 1
End Sub
Private Sub USER_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyUp
User.Top = User.Bottom + 1
'User.Location.X = 200
End Sub
End Class
I have the following problems with it:
User.location.x = 200 had syntax errors when I deleted the x and when I didn't.
The player also had to continually press the keys to move.
both I do not know how to correct.
Any help at all is greatly appreciated as it's for my final grade.
You can put it in a timer_tick to loop over it, that is what I do.
And the correct version of User.Location.X = 200 is:
User.location = new point(200, User.location.y)
nvm found the solution.
Here it is for anyone else how might need the code in the future.
Private Sub Map_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles MyBase.KeyDown
If e.KeyCode = Keys.Up Then
User.Location = New Point(User.Location.X, User.Location.Y - 5)
ElseIf e.KeyCode = Keys.Down Then
User.Location = New Point(User.Location.X, User.Location.Y + 5)
ElseIf e.KeyCode = Keys.Left Then
User.Location = New Point(User.Location.X - 5, User.Location.Y)
ElseIf e.KeyCode = Keys.Right Then
User.Location = New Point(User.Location.X + 5, User.Location.Y)
End If
Module
Public Sub MovePictureBox(ByRef PictureBox As PictureBox)
Tiempo.Tag = PictureBox
AddHandler PictureBox.Parent.KeyDown, AddressOf Parent_KeyDown
AddHandler PictureBox.Parent.KeyUp, AddressOf Parent_KeyUp
AddHandler CType(PictureBox.Parent, Form).Load, AddressOf Parent_Load
End Sub
Private Up, Down, Left, Right As Boolean
WithEvents Tiempo As New Timer() With {.Interval = 1}
Private Sub Tiempo_Tick() Handles Tiempo.Tick
If Up Then Tiempo.Tag.Top -= 2
If Down Then Tiempo.Tag.Top += 2
If Left Then Tiempo.Tag.Left -= 2
If Right Then Tiempo.Tag.Left += 2
End Sub
Private Sub Parent_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
If e.KeyCode = Keys.Up Then Up = True
If e.KeyCode = Keys.Down Then Down = True
If e.KeyCode = Keys.Left Then Left = True
If e.KeyCode = Keys.Right Then Right = True
Tiempo.Enabled = True
End Sub
Private Sub Parent_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs)
Tiempo.Enabled = False
Up = False : Down = False : Right = False : Left = False
End Sub
Private Sub Parent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
sender.KeyPreview = True
End Sub
End Module

Resources