How to Get the X/Y Point of a Textbox Using For Each Loop

In this tutorial I will teach you how to get X/Y Point of a Textbox Using For Each Loop in Visual Basic 2008. With this you can easily get the location of a specific Textbox. I used the Foreach Loop so that you don’t have to specify what is the name of your Textbox. All you have to do, is to get the type of it. Let’s begin. Open Visual Basic 2008, create a new Windows Application, drag the Textbox and Button you needed. It will look like this. first form After that, double click the button to fire the click event handler. Then, add the following code to the method created.
  1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  2.  
  3.         'FOR EACH TXT AS WINDOWS FORMS CONTROL IN THE FORM CONTROLS
  4.         'ME IS AN OBJECT THAT STANDS AS A FORM
  5.         'CONTROLS ARE BASE CLASS WHICH ARE COMPONENT WITH VISUAL REPRESENTATION
  6.         For Each txt As System.Windows.Forms.Control In Me.Controls
  7.             'GETTING THE TYPE OF THE TXT CONTROL.
  8.             If txt.GetType Is GetType(TextBox) Then
  9.                 'IF THE TYPE OF TXT IS TEXBOX,
  10.                 'THE LOCATION OF A SPECIFIC TEXTBOX WILL EXIST INTO IT.
  11.                 txt.Text = txt.Location.ToString
  12.             End If
  13.         Next
  14.  
  15.     End Sub
Run your project and click the Button to start the code in the method.

Add new comment