how to save an image from a picturebox vb - vb.net

I am interested in vb and am confused how to save the contents of a Picturebox as a png file when button1 is clicked. When I try to do it now, it produces the error I show below. I can't figure out how to fix this because I'm new to using visual basic. The debugger seemed to catch something but I can't make sense of it. I would be extremely pleased if someone would help me to fix this. Here is the code:
'*** Acknowlegements ***
'Ideas for this code came from the MicroSoft "Scribble" sample code,
'Christian Graus's excellent arcticle on a C++ code called "Doodle"
'and the MicroSoft website.
'By John Buettner
'26 July 2003
Imports System
Imports System.Drawing
Imports System.Windows.Forms
Public Class Form
'Namespace myPaint
Inherits System.Windows.Forms.Form ' Of course ;)
Dim mousePath As New System.Drawing.Drawing2D.GraphicsPath() 'declare a new Graphic path to follow the mouse movement
'*** below I declare some values for an Alpha and other user selected variables
'these will be used as I expand this program for a higher level use.
Dim myAlpha As Integer = 100 ' declare a Alpha variable
Dim myUserColor As New Color() 'this is a color the user selects
Friend WithEvents Button1 As Button
Friend WithEvents SaveFileDialog1 As SaveFileDialog
Dim myPenWidth As Single = 5 'set pen width variable
'**************************************************************
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
Friend WithEvents PictureBox1 As System.Windows.Forms.PictureBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Dim resources As System.ComponentModel.ComponentResourceManager = New System.ComponentModel.ComponentResourceManager(GetType(Form))
Me.PictureBox1 = New System.Windows.Forms.PictureBox()
Me.Button1 = New System.Windows.Forms.Button()
Me.SaveFileDialog1 = New System.Windows.Forms.SaveFileDialog()
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'PictureBox1
'
Me.PictureBox1.BackColor = System.Drawing.Color.GhostWhite
Me.PictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D
Me.PictureBox1.Location = New System.Drawing.Point(0, 33)
Me.PictureBox1.Name = "PictureBox1"
Me.PictureBox1.Size = New System.Drawing.Size(696, 423)
Me.PictureBox1.TabIndex = 2
Me.PictureBox1.TabStop = False
'
'Button1
'
Me.Button1.Image = Global.Ubernote.My.Resources.Resources.fl
Me.Button1.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft
Me.Button1.Location = New System.Drawing.Point(12, 4)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(75, 23)
Me.Button1.TabIndex = 3
Me.Button1.Text = "Save"
Me.Button1.UseVisualStyleBackColor = True
'
'SaveFileDialog1
'
'
'Form
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.White
Me.ClientSize = New System.Drawing.Size(696, 456)
Me.Controls.Add(Me.Button1)
Me.Controls.Add(Me.PictureBox1)
Me.Icon = CType(resources.GetObject("$this.Icon"), System.Drawing.Icon)
Me.Name = "Form"
Me.Text = "Draw"
CType(Me.PictureBox1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As MouseEventArgs) Handles PictureBox1.MouseDown
If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down
mousePath.StartFigure() ' The L mouse is down so we need to start a new line in mousePath
End If
End Sub
Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
If e.Button = MouseButtons.Left Then ' draw a filled circle if left mouse is down
Try
mousePath.AddLine(e.X, e.Y, e.X, e.Y) 'Add mouse coordiantes to mousePath
Catch
MsgBox("No way, Hose!")
End Try
End If
PictureBox1.Invalidate() 'Repaint the PictureBox using the PictureBox1 Paint event
End Sub
Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
' Here is where we do the actual painting
Try ' error trapping
myUserColor = (System.Drawing.Color.Black) 'You can remove this line and add a user selected color to
'change the value of myUserColor
myAlpha = 100 ' This will give the color a Alpha effect, you can set this to 255 if you want a full color
Dim CurrentPen = New Pen(Color.FromArgb(myAlpha, myUserColor), myPenWidth) 'Set up the pen
e.Graphics.DrawPath(CurrentPen, mousePath) 'draw the path! :)
Catch
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
SaveFileDialog1.ShowDialog()
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
Private Sub SaveFileDialog1_FileOk(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles SaveFileDialog1.FileOk
Try
' Dim FileToSaveAs As String = System.IO.Path.Combine(My.Computer.FileSystem.SpecialDirectories.Temp, SaveFileDialog1.FileName)
' PictureBox1.Image.Save(FileToSaveAs, System.Drawing.Imaging.ImageFormat.Jpeg)
'PictureBox1.Image.Save(FileToSaveAs)
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
End Class

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem3.Click
Dim path As String
Dim pic As Image
pic = PictureBox1.Image
SaveFileDialog1.ShowDialog()
pic.Save(SaveFileDialog1.FileName)
End Sub
try this

Related

Vb-net Hide layout on prepare the form

Visual Studio 2019, Visual Basic.
I have a Form. On load i add a lot of textbox. I need to hide layout during this process. I try:
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.SuspendLayout()
MakePanel() ' Sub adding textbox
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
But it showing all process. I Try
Private Sub Main_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Panel1.visible = false
MakePanel() ' Sub adding textbox
Me.Panel1.visible = true
End Sub
But nothing ...
I don't have the rep to comment but your second attempt is on the right track. The problem likely lies in your MakePanel() function not actually adding all the text boxes within Panel1's control but rather as part of your form itself. So when you go to hide the panel, it doesn't hide the text boxes within it.
They need to be added via Panel1.Controls.Add to actually 'hide' with Panel1
ie (starting with a new blank form):
Public Panel1 = New Panel()
Public TextBox1 = New TextBox()
Public TextBox2 = New TextBox()
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Controls.Add(Panel1)
Panel1.Visible = False
MakePanel()
Panel1.Visible = True
End Sub
Private Sub MakePanel()
Panel1.Location = New Point(3, 3)
Panel1.Name = "Panel1"
Panel1.Size = New Size(100, 100)
Panel1.TabIndex = 0
TextBox1.Location = New Point(3, 3)
TextBox1.Name = "TextBox1"
TextBox1.Size = New Size(49, 20)
TextBox1.TabIndex = 0
Panel1.Controls.Add(TextBox1)
TextBox2.Location = New Point(3, 29)
TextBox2.Name = "TextBox2"
TextBox2.Size = New Size(49, 20)
TextBox2.TabIndex = 1
Panel1.Controls.Add(TextBox2)
End Sub
Hope this helps!

VB.NET Drag text, set focus and enter key issue

I have an app that allows for text to be dragged-dropped into a text box. I also have a check box that allows the app to always be on top of all windows. My issues is when I drag text into the text box and I hit the enter key, it does not run the function unless I have the window in actual focus (by clicking on it).
My question is, how can I make sure when I drag text into the text box, it will make the window be the focus so when I hit the enter key, it will run my function?
Here is what I am trying with no luck:
Private Sub Form1_DragEnter(sender As Object, e As DragEventArgs) Handles Me.DragEnter
Me.Focus()
End Sub
Private Sub Form1_DragOver(sender As Object, e As DragEventArgs) Handles Me.DragOver
Me.Focus()
End Sub
I am posting the code as it works fine for me, try it on new project:
Public Class Form1
Private Sub TextBox1_DragEnter(sender As Object, e As System.Windows.Forms.DragEventArgs) Handles TextBox1.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(DataFormats.Text)) Then
' Display the copy cursor.
e.Effect = DragDropEffects.Copy
Else
' Display the no-drop cursor.
e.Effect = DragDropEffects.None
End If
End Sub
Private Sub TextBox1_DragDrop(sender As Object, e As DragEventArgs) Handles TextBox1.DragDrop
' Drop text and move cursor to end of drag-dropped text
TextBox1.Text = e.Data.GetData(DataFormats.Text)
TextBox1.SelectionStart = TextBox1.Text.Length + 1
TextBox1.Focus()
Me.Activate()
End Sub
End Class
and designer:
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class Form1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.AllowDrop = True
Me.TextBox1.Location = New System.Drawing.Point(30, 54)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(216, 125)
Me.TextBox1.TabIndex = 0
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(284, 262)
Me.Controls.Add(Me.TextBox1)
Me.Name = "Form1"
Me.Text = "Form1"
Me.TopMost = True
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
End Class
Try to use Me.Activate() instead in DragEnter it should be enough

image drag and drop - how to get image

I am trying to write a program that allows the user to drag and drop an image from any folder to the form's picture box. I got the drag and drop part, but I don't know to do is how to store the image on a variable to be able to be dropped on the picturebox. Here is what I have so far:
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
AddHandler picCategoryImage.DragDrop, AddressOf picCategoryImage_DragDrop
AddHandler picCategoryImage.DragEnter, AddressOf picCategoryImage_DragEnter
End Sub
Private Sub picCategoryImage_DragDrop(sender As Object, e As DragEventArgs) Handles picCategoryImage.DragDrop
Dim picbox As PictureBox = CType(sender, PictureBox)
Dim g As Graphics = picbox.CreateGraphics()
g.DrawImage(CType(e.Data.GetData(DataFormats.Bitmap), Image), New Point(0, 0))
End Sub
Private Sub picCategoryImage_DragEnter(sender As Object, e As DragEventArgs) Handles picCategoryImage.DragEnter
If e.Data.GetDataPresent(DataFormats.Bitmap) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
where am I going wrong here?
You need DataFormats.FileDrop instead of DataFormats.Bitmap in DragEnter event. And in DragDrop you open the file name you get from e.Data.GetData():
Private Sub picCategoryImage_DragDrop(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles picCategoryImage.DragDrop
Dim picbox As PictureBox = CType(sender, PictureBox)
Dim files() As String = CType(e.Data.GetData(DataFormats.FileDrop), String())
If files.Length <> 0 Then
Try
picbox.Image = Image.FromFile(files(0))
Catch ex As Exception
MessageBox.Show("Problem opening file ")
End Try
End If
End Sub
Private Sub picCategoryImage_DragEnter(sender As System.Object, e As System.Windows.Forms.DragEventArgs) Handles picCategoryImage.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop) Then
e.Effect = DragDropEffects.Copy
Else
e.Effect = DragDropEffects.None
End If
End Sub
You need also to set(in form load event):
picCategoryImage.AllowDrop = True

vb.net listen for parent form event on each child form

I am trying to implement a function on my parent form, that when the event fires, I want to perform actions on all of the child forms that are open. Because any given child form may or may not be open at a given time, I can't handle it directly from the event on the parent form: i.e., cant do the following as Child1 may not be initiated at the time:
--Parent Form--
Public Sub ParentEvent()
DoParentAction()
DoChild1Action()
DoChild2Action()
End Sub
Is there a way on each child page to listen for ParentEvent() to be fired? essentially, what I want to do is handle the ParentEvent() being fired, on the child page the same as if a button was clicked on the child page, something like this:
--Child1--
Public Sub ChildEvent() Handles ParentForm.DoParentAction()
DoChild1Action()
End Sub
This is easy to do, you just have to step around VB's WithEvents and Handles syntax to get at it.
Public Class ParentForm
Event OnDoSomething()
Private Sub DoSomething()
RaiseEvent OnDoSomething()
End Sub
End Class
and then
Public Class ChildForm
Public Sub New()
InitializeComponent()
AddHandler ParentForm.OnDoSomething, AddressOf DoSomething
End Sub
Private Sub DoSomething()
' do something
End Sub
Private Sub ChildForm_FormClosing(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.FormClosingEventArgs) _
Handles MyBase.FormClosing
RemoveHandler ParentForm.OnDoSomething, AddressOf DoSomething
End Sub
End Class
It's important to always make sure the event handler is removed before disposing the child form (else you end up with a memory leak).
The above assumes you are using the VB default instance of ParentForm - if you're not, obviously you have to reference things accordingly. A better approach might be to make the parent an argument in the constructor like:
Public Sub New(ByVal parent as ParentForm)
InitializeComponent()
AddHandler parent.OnDoSomething, AddressOf DoSomething
End Sub
also, of course, modifying the RemoveHandler section as well (you'd need to keep a reference to the parent). Another option is to hook/unhook in the ParentChanged event if this is an MDI application.
The only other caveat is that you can't create any of the child forms in the constructor of the parent form since you end up with self-reference during construction.
Sure.
Add a public event to the parent form:
Public Event EventFired(ByVal timestamp As DateTime)
In each child form, add a handler:
Public Sub ParentEventFired(ByVal timestamp As DateTime)
Label1.Text = "Child 1: Parent Event Fired (" & timestamp.ToLongTimeString() & ")"
End Sub
When you create the child form, add a handler:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim l_child1 = New ChildForm1()
AddHandler Me.EventFired, AddressOf l_child1.ParentEventFired
l_child1.Show(Me)
End Sub
You can use this approach whether you are using an MDI or simply free-floating windows.
FULL CODE
ParentForm Designer
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class ParentForm
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Button1 = New System.Windows.Forms.Button
Me.Button2 = New System.Windows.Forms.Button
Me.Button3 = New System.Windows.Forms.Button
Me.SuspendLayout()
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(12, 12)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(166, 23)
Me.Button1.TabIndex = 0
Me.Button1.Text = "Open Child 1"
Me.Button1.UseVisualStyleBackColor = True
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(12, 41)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(166, 23)
Me.Button2.TabIndex = 0
Me.Button2.Text = "Open Child 2"
Me.Button2.UseVisualStyleBackColor = True
'
'Button3
'
Me.Button3.Location = New System.Drawing.Point(12, 231)
Me.Button3.Name = "Button3"
Me.Button3.Size = New System.Drawing.Size(166, 23)
Me.Button3.TabIndex = 0
Me.Button3.Text = "Fire Event"
Me.Button3.UseVisualStyleBackColor = True
'
'ParentForm
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Button3)
Me.Controls.Add(Me.Button2)
Me.Controls.Add(Me.Button1)
Me.Name = "ParentForm"
Me.Text = "ParentForm"
Me.ResumeLayout(False)
End Sub
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Friend WithEvents Button3 As System.Windows.Forms.Button
End Class
ParentForm Code Behind
Public Class ParentForm
Public Event EventFired(ByVal timestamp As DateTime)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim l_child1 = New ChildForm1()
AddHandler Me.EventFired, AddressOf l_child1.ParentEventFired
l_child1.Show(Me)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim l_child2 = New ChildForm2()
AddHandler Me.EventFired, AddressOf l_child2.ParentEventFired
l_child2.Show(Me)
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
RaiseEvent EventFired(DateTime.Now)
End Sub
End Class
ChildForm1 Designer
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class ChildForm1
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(12, 9)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(39, 13)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Label1"
'
'ChildForm1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Label1)
Me.Name = "ChildForm1"
Me.Text = "ChildForm1"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
End Class
ChildForm1 Code Behind
Public Class ChildForm1
Public Sub ParentEventFired(ByVal timestamp As DateTime)
Label1.Text = "Child 1: Parent Event Fired (" & timestamp.ToLongTimeString() & ")"
End Sub
End Class
ChildForm2 Designer
<Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class ChildForm2
Inherits System.Windows.Forms.Form
'Form overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(12, 9)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(39, 13)
Me.Label1.TabIndex = 1
Me.Label1.Text = "Label1"
'
'ChildForm2
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(292, 266)
Me.Controls.Add(Me.Label1)
Me.Name = "ChildForm2"
Me.Text = "ChildForm2"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
End Class
ChildForm2 Code Behind
Public Class ChildForm2
Public Sub ParentEventFired(ByVal timestamp As DateTime)
Label1.Text = "Child 2: Parent Event Fired (" & timestamp.ToLongTimeString() & ")"
End Sub
End Class

Telerik version of system.windows.text code

I want to have a partial class to manage my focus between radtextboxes when a user presses the enter key and I am looking for the telerik interpretation of this line of code tbs(i).KeyDown += New KeyEventHandler(AddressOf textBoxes_KeyDown) Any ideas. Please help.
Partial Public Class MstFileTruck Inherits Form
Private tbs() As Telerik.WinControls.UI.RadTextBox
Public Sub New()
InitializeComponent()
tbs = New Telerik.WinControls.UI.RadTextBox() {txtTruckNumber, txtRegistrationNumber, txtOwnerName}
For i As Integer = 0 To tbs.Length - 1
tbs(i).Tag = i
tbs(i).KeyDown += New KeyEventHandler(AddressOf textBoxes_KeyDown)
' 'tbs(i).IsHandleCreated += New KeyEventArgs(Keys.Enter) '(AddressOf textBoxes_KeyDown)
' tbs(i).RootElement.KeyDownEvent.EventName(textBoxes_KeyDown) '= New KeyEventArgs(AddressOf Telerik.WinControls.UI.RadTextBoxElement.KeyDownEvent.EventName(textBoxes_KeyDown(tbs, RootRadElement.KeyDownEvent))) '(AddressOf textBoxes_KeyDown)
Next
tbs(0).Focus()
End Sub
Private Sub textBoxes_KeyDown(sender As Object, e As KeyEventArgs)
Dim tb As Telerik.WinControls.UI.RadTextBox = TryCast(sender, Telerik.WinControls.UI.RadTextBox)
If tb IsNot Nothing Then
If e.KeyCode = Keys.Enter Then
Dim tag As Integer = CInt(tb.Tag)
If tag = 2 Then
tbs(0).Focus()
Else
tbs(tag + 1).Focus()
End If
End If
End If
End Sub
End Class
I am not quite sure what your code should do. I am not even sure that your event subscription will work, however, here is how to switch the focus between two instances of RadTextBox:
Private Sub RadTextBox_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles RadTextBox1.KeyDown, RadTextBox2.KeyDown
If e.KeyCode = Keys.Enter Then
Dim tb As RadTextBox = DirectCast(sender, RadTextBox)
If tb.Name = "RadTextBox1" Then
RadTextBox2.TextBoxElement.Focus()
Else
RadTextBox1.TextBoxElement.Focus()
End If
End If
End Sub
Another way to subscribe to an event in VB is:
AddHandler RadTextBox1.KeyDown, AddressOf RadTextBox_KeyDown
Private Sub RadTextBox_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs)
'your code here
End Sub
here is the whole code as it should look like in your .vb file
Imports Telerik.WinControls.UI
Public Class Form1
Public Sub New()
' This call is required by the designer.
InitializeComponent()
AddHandler RadTextBox1.KeyDown, AddressOf RadTextBox_KeyDown
End Sub
Private Sub RadTextBox_KeyDown(sender As System.Object, e As System.Windows.Forms.KeyEventArgs) Handles RadTextBox1.KeyDown, RadTextBox2.KeyDown
If e.KeyCode = Keys.Enter Then
Dim tb As RadTextBox = DirectCast(sender, RadTextBox)
If tb.Name = "RadTextBox1" Then
RadTextBox2.TextBoxElement.Focus()
Else
RadTextBox1.TextBoxElement.Focus()
End If
End If
End Sub
End Class