I'm currently trying to create a tower building tool, this is based on drag and drop functionality. I'm currently ready for the user to build his tower, but now I would also like to add the option that he can adjust his tower. This by dragging the added blocks so they swap position.
I'm currently using the flowlayoutpanel to arrange my added block's (groupboxes). These are newly created after each dragevent based on the input.
Public Class Form1
Private Sub BODY_ADD_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
BODY_ADD_CILINDER.MouseMove,
BODY_ADD_CONE_DOWN.MouseMove,
BODY_ADD_CONE_UP.MouseMove,
BODY_ADD_HEAD_DOWN.MouseMove,
BODY_ADD_HEAD_UP.MouseMove
' Initiate dragging.
Me.DoDragDrop(sender, DragDropEffects.Copy)
End Sub
Private Sub BODY_Arrange_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles _
BODY_LAYOUT_PANEL.MouseMove
' Initiate dragging.
Me.DoDragDrop(sender, DragDropEffects.Copy)
End Sub
Private Sub FlowLayoutPanel1_DragEnter(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles BODY_LAYOUT_PANEL.DragEnter
' Check the format of the data being dropped.
If (e.Data.GetDataPresent(GetType(PictureBox))) 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 BODY_LAYOUT_PANEL_DragDrop(ByVal sender As Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles BODY_LAYOUT_PANEL.DragDrop
Dim oPB = e.Data.GetData(GetType(PictureBox))
Dim oGB As New GroupBox
oGB.Name = "Test" & GET_BODY_ITEM_NO()
oGB.Visible = True
oGB.Width = 520
oGB.Height = 119
If oPB.name = BODY_ADD_CILINDER.Name Then
oGB.Text = "Cilinder" & GET_CILINDER_ITEM_NO()
BODY_LAYOUT_PANEL.Controls.Add(oGB)
FILL_CILINDER_GROUP_BOX(oGB)
ElseIf oPB.name = BODY_ADD_CONE_DOWN.Name Then
oGB.Text = "Cone down" & GET_BODY_ITEM_NO()
BODY_LAYOUT_PANEL.Controls.Add(oGB)
ElseIf oPB.name = BODY_ADD_CONE_UP.Name Then
oGB.Text = "Cone up" & GET_BODY_ITEM_NO()
BODY_LAYOUT_PANEL.Controls.Add(oGB)
ElseIf oPB.name = BODY_ADD_HEAD_DOWN.Name Then
oGB.Text = "Head down" & GET_HEAD_DOWN_ITEM_NO()
BODY_LAYOUT_PANEL.Controls.Add(oGB)
ElseIf oPB.name = BODY_ADD_HEAD_UP.Name Then
oGB.Text = "Head up" & GET_BODY_ITEM_NO()
BODY_LAYOUT_PANEL.Controls.Add(oGB)
End If
End Sub
Private Sub FILL_CILINDER_GROUP_BOX(ByVal oGB As GroupBox)
Dim oPB As New PictureBox
oPB.Dock = DockStyle.Left
oPB.SizeMode = PictureBoxSizeMode.StretchImage
oPB.Image = My.Resources.ResourceManager.GetObject("Cilinder")
oPB.Name = oGB.Name & "_PictureBox"
oGB.Controls.Add(oPB)
Dim oLBL1 As New Label
oLBL1.Name = oGB.Name & "_LABEL_HOOGTE"
oLBL1.Text = "Hoogte"
oLBL1.Location = New System.Drawing.Point(128, 25)
oLBL1.Width = 42
oGB.Controls.Add(oLBL1)
Dim oLBL2 As New Label
oLBL2.Name = oGB.Name & "_LABEL_DIKTE"
oLBL2.Text = "Dikte"
oLBL2.Location = New System.Drawing.Point(138, 52)
oLBL2.Width = 32
oGB.Controls.Add(oLBL2)
Dim oLBL3 As New Label
oLBL3.Name = oGB.Name & "_LABEL_ORIENTATIE"
oLBL3.Text = "Orientatie LW"
oLBL3.Location = New System.Drawing.Point(311, 25)
oLBL3.Width = 72
oGB.Controls.Add(oLBL3)
Dim oLBL4 As New Label
oLBL4.Name = oGB.Name & "_LABEL_SEGMENTEN"
oLBL4.Text = "Segmenten"
oLBL4.Location = New System.Drawing.Point(322, 52)
oLBL4.Width = 61
oGB.Controls.Add(oLBL4)
Dim oTB1 As New TextBox
oTB1.Name = oGB.Name & "_TB_HOOGTE"
oTB1.Location = New System.Drawing.Point(176, 22)
oGB.Controls.Add(oTB1)
Dim oTB2 As New TextBox
oTB2.Name = oGB.Name & "_TB_DIKTE"
oTB2.Location = New System.Drawing.Point(176, 49)
oGB.Controls.Add(oTB2)
Dim oTB3 As New TextBox
oTB3.Name = oGB.Name & "_TB_ORIENTATIE"
oTB3.Location = New System.Drawing.Point(389, 22)
oGB.Controls.Add(oTB3)
Dim oTB4 As New TextBox
oTB4.Name = oGB.Name & "_TB_SEGMENTEN"
oTB4.Location = New System.Drawing.Point(389, 45)
oGB.Controls.Add(oTB4)
End Sub
Private Function GET_BODY_ITEM_NO()
Return (BODY_LAYOUT_PANEL.Controls.Count)
End Function
Private Function GET_CILINDER_ITEM_NO()
Dim s As String
Dim y As Integer = 1
For i = 0 To BODY_LAYOUT_PANEL.Controls.Count - 1
s = BODY_LAYOUT_PANEL.Controls.Item(i).Text
If s.Contains("Cilinder") Then
y = y + 1
End If
Next
Return (y)
End Function
Private Function GET_HEAD_DOWN_ITEM_NO()
Dim s As String
Dim y As Integer = 1
For i = 0 To BODY_LAYOUT_PANEL.Controls.Count - 1
s = BODY_LAYOUT_PANEL.Controls.Item(i).Text
If s.Contains("Head down") Then
y = y + 1
End If
Next
Return (y)
End Function
End Class
For the rearranging part I would have to create a drag-event for the dynamically added groupboxes. How do you write this? How should I specify what to handle?
So the Question is: How do I hook the mouse-event to the dynamically added group-box control?
Private Sub BODY_REARRANGE_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles
Also if there are any comments on my sample code I could use some pointers!
Related
Imports System.Windows.Threading
Imports System.Threading
Imports System.Net
Imports System.Drawing
Imports System.IO
Imports Microsoft.Win32
Class Window1
#Region "Fields"
Private geoDoc As XDocument
Private location As String
Private zoom As Integer
Private saveDialog As New SaveFileDialog
Private mapType As String
Private lat As Double
Private lng As Double
#End Region
Private Sub GetGeocodeData()
Dim geocodeURL As String = "http://maps.googleapis.com/maps/api/" & _
"geocode/xml?address=" & location & "&sensor=false"
Try
geoDoc = XDocument.Load(geocodeURL)
Catch ex As WebException
Me.Dispatcher.BeginInvoke(New ThreadStart(AddressOf HideProgressBar), _
DispatcherPriority.Normal, Nothing)
MessageBox.Show("Ensure that internet connection is available.", _
"Map App", MessageBoxButton.OK, MessageBoxImage.Error)
Exit Sub
End Try
Me.Dispatcher.BeginInvoke(New ThreadStart(AddressOf ShowGeocodeData), _
DispatcherPriority.Normal, Nothing)
End Sub
Private Sub ShowGeocodeData()
Dim responseStatus = geoDoc...<status>.Single.Value()
If (responseStatus = "OK") Then
Dim formattedAddress = geoDoc...<formatted_address>(0).Value()
Dim latitude = geoDoc...<location>(0).Element("lat").Value()
Dim longitude = geoDoc...<location>(0).Element("lng").Value()
Dim locationType = geoDoc...<location_type>(0).Value()
AddressTxtBlck.Text = formattedAddress
LatitudeTxtBlck.Text = latitude
LongitudeTxtBlck.Text = longitude
Select Case locationType
Case "APPROXIMATE"
AccuracyTxtBlck.Text = "Approximate"
Case "ROOFTOP"
AccuracyTxtBlck.Text = "Precise"
Case Else
AccuracyTxtBlck.Text = "Approximate"
End Select
lat = Double.Parse(latitude)
lng = Double.Parse(longitude)
If (SaveButton.IsEnabled = False) Then
SaveButton.IsEnabled = True
RoadmapToggleButton.IsEnabled = True
TerrainToggleButton.IsEnabled = True
End If
ElseIf (responseStatus = "ZERO_RESULTS") Then
MessageBox.Show("Unable to show results for: " & vbCrLf & _
location, "Unknown Location", MessageBoxButton.OK, _
MessageBoxImage.Information)
DisplayXXXXXXs()
AddressTxtBox.SelectAll()
End If
ShowMapButton.IsEnabled = True
ZoomInButton.IsEnabled = True
ZoomOutButton.IsEnabled = True
MapProgressBar.Visibility = Windows.Visibility.Hidden
End Sub
' Get and display map image in Image ctrl.
Private Sub ShowMapImage()
Dim bmpImage As New BitmapImage()
Dim mapURL As String = "http://maps.googleapis.com/maps/api/staticmap?" & _
"size=500x400&markers=size:mid%7Ccolor:red%7C" & _
location & "&zoom=" & zoom & "&maptype=" & mapType & "&sensor=false"
bmpImage.BeginInit()
bmpImage.UriSource = New Uri(mapURL)
bmpImage.EndInit()
MapImage.Source = bmpImage
End Sub
Private Sub ShowMapUsingLatLng()
Dim bmpImage As New BitmapImage()
Dim mapURL As String = "http://maps.googleapis.com/maps/api/staticmap?" & _
"center=" & lat & "," & lng & "&" & _
"size=500x400&markers=size:mid%7Ccolor:red%7C" & _
location & "&zoom=" & zoom & "&maptype=" & mapType & "&sensor=false"
bmpImage.BeginInit()
bmpImage.UriSource = New Uri(mapURL)
bmpImage.EndInit()
MapImage.Source = bmpImage
End Sub
' Zoom-in on map.
Private Sub ZoomIn()
If (zoom < 21) Then
zoom += 1
ShowMapUsingLatLng()
If (ZoomOutButton.IsEnabled = False) Then
ZoomOutButton.IsEnabled = True
End If
Else
ZoomInButton.IsEnabled = False
End If
End Sub
' Zoom-out on map.
Private Sub ZoomOut()
If (zoom > 0) Then
zoom -= 1
ShowMapUsingLatLng()
If (ZoomInButton.IsEnabled = False) Then
ZoomInButton.IsEnabled = True
End If
Else
ZoomOutButton.IsEnabled = False
End If
End Sub
Private Sub SaveMap()
Dim mapURL As String = "http://maps.googleapis.com/maps/api/staticmap?" & _
"center=" & lat & "," & lng & "&" & _
"size=500x400&markers=size:mid%7Ccolor:red%7C" & _
location & "&zoom=" & zoom & "&maptype=" & mapType & "&sensor=false"
Dim webClient As New WebClient()
Try
Dim imageBytes() As Byte = webClient.DownloadData(mapURL)
Using ms As New MemoryStream(imageBytes)
Image.FromStream(ms).Save(saveDialog.FileName, Imaging.ImageFormat.Png)
End Using
Catch ex As WebException
MessageBox.Show("Unable to save map. Ensure that you are" & _
" connected to the internet.", "Error!", _
MessageBoxButton.OK, MessageBoxImage.Stop)
Exit Sub
End Try
End Sub
Private Sub MoveUp()
' Default zoom is 15 and at this level changing
' the center point is done by 0.003 degrees.
' Shifting the center point is done by higher values
' at zoom levels less than 15.
Dim diff As Double
Dim shift As Double
' Use 88 to avoid values beyond 90 degrees of lat.
If (lat < 88) Then
If (zoom = 15) Then
lat += 0.003
ElseIf (zoom > 15) Then
diff = zoom - 15
shift = ((15 - diff) * 0.003) / 15
lat += shift
Else
diff = 15 - zoom
shift = ((15 + diff) * 0.003) / 15
lat += shift
End If
ShowMapUsingLatLng()
Else
lat = 90
End If
End Sub
Private Sub MoveDown()
Dim diff As Double
Dim shift As Double
If (lat > -88) Then
If (zoom = 15) Then
lat -= 0.003
ElseIf (zoom > 15) Then
diff = zoom - 15
shift = ((15 - diff) * 0.003) / 15
lat -= shift
Else
diff = 15 - zoom
shift = ((15 + diff) * 0.003) / 15
lat -= shift
End If
ShowMapUsingLatLng()
Else
lat = -90
End If
End Sub
Private Sub MoveLeft()
Dim diff As Double
Dim shift As Double
' Use -178 to avoid negative values below -180.
If (lng > -178) Then
If (zoom = 15) Then
lng -= 0.003
ElseIf (zoom > 15) Then
diff = zoom - 15
shift = ((15 - diff) * 0.003) / 15
lng -= shift
Else
diff = 15 - zoom
shift = ((15 + diff) * 0.003) / 15
lng -= shift
End If
ShowMapUsingLatLng()
Else
lng = 180
End If
End Sub
Private Sub MoveRight()
Dim diff As Double
Dim shift As Double
If (lng < 178) Then
If (zoom = 15) Then
lng += 0.003
ElseIf (zoom > 15) Then
diff = zoom - 15
shift = ((15 - diff) * 0.003) / 15
lng += shift
Else
diff = 15 - zoom
shift = ((15 + diff) * 0.003) / 15
lng += shift
End If
ShowMapUsingLatLng()
Else
lng = -180
End If
End Sub
Private Sub DisplayXXXXXXs()
AddressTxtBlck.Text = "XXXXXXXXX, XXXXX, XXXXXX"
LatitudeTxtBlck.Text = "XXXXXXXXXX"
LongitudeTxtBlck.Text = "XXXXXXXXXX"
AccuracyTxtBlck.Text = "XXXXXXXXX"
End Sub
Private Sub HideProgressBar()
MapProgressBar.Visibility = Windows.Visibility.Hidden
ShowMapButton.IsEnabled = True
End Sub
' ShowMapButton click event handler.
Private Sub ShowMapButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ShowMapButton.Click
If (AddressTxtBox.Text <> String.Empty) Then
location = AddressTxtBox.Text.Replace(" ", "+")
zoom = 15
mapType = "roadmap"
Dim geoThread As New Thread(AddressOf GetGeocodeData)
geoThread.Start()
ShowMapImage()
AddressTxtBox.SelectAll()
ShowMapButton.IsEnabled = False
MapProgressBar.Visibility = Windows.Visibility.Visible
If (RoadmapToggleButton.IsChecked = False) Then
RoadmapToggleButton.IsChecked = True
TerrainToggleButton.IsChecked = False
End If
Else
MessageBox.Show("Enter location address.", _
"Map App", MessageBoxButton.OK, MessageBoxImage.Exclamation)
AddressTxtBox.Focus()
End If
End Sub
' SaveFileDialog FileOk event handler.
Private Sub saveDialog_FileOk(ByVal sender As Object, ByVal e As EventArgs)
Dim td As New Thread(AddressOf SaveMap)
td.Start()
End Sub
' ZoomInButton click event handler.
Private Sub ZoomInButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ZoomInButton.Click
ZoomIn()
End Sub
' ZoomOutButton click event handler.
Private Sub ZoomOutButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles ZoomOutButton.Click
ZoomOut()
End Sub
' SaveButton click event handler.
Private Sub SaveButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles SaveButton.Click
saveDialog.ShowDialog()
End Sub
' RoadmapToggleButton Checked event handler.
Private Sub RoadmapToggleButton_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles RoadmapToggleButton.Checked
If (mapType <> "roadmap") Then
mapType = "roadmap"
ShowMapUsingLatLng()
TerrainToggleButton.IsChecked = False
End If
End Sub
' TerrainToggleButton Checked event handler.
Private Sub TerrainToggleButton_Checked(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles TerrainToggleButton.Checked
If (mapType <> "terrain") Then
mapType = "terrain"
ShowMapUsingLatLng()
RoadmapToggleButton.IsChecked = False
End If
End Sub
Private Sub MapImage_MouseLeftButtonUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles MapImage.MouseLeftButtonUp
If (location IsNot Nothing) Then
Dim gMapURL As String = "http://maps.google.com/maps?q=" & location
Process.Start("IExplore.exe", gMapURL)
End If
End Sub
Private Sub Window1_Loaded(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
AddressTxtBox.Focus()
With saveDialog
.DefaultExt = "png"
.Title = "Save Map Image"
.OverwritePrompt = True
.Filter = "(*.png)|*.png"
End With
AddHandler saveDialog.FileOk, AddressOf saveDialog_FileOk
End Sub
Private Sub MinimizeButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles MinimizeButton.Click
Me.WindowState = Windows.WindowState.Minimized
End Sub
Private Sub CloseButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles CloseButton.Click
Me.Close()
End Sub
//ds
Private Sub BgndRectangle_MouseLeftButtonDown(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles BgndRectangle.MouseLeftButtonDown
Me.DragMove()
End Sub
//df
Private Sub MoveUpButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles MoveUpButton.Click
MoveUp()
End Sub
//sdf
Private Sub MoveDownButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles MoveDownButton.Click
MoveDown()
End Sub
Private Sub MoveLeftButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles MoveLeftButton.Click
MoveLeft()
End Sub
Private Sub MoveRightButton_Click(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles MoveRightButton.Click
MoveRight()
End Sub
End Class
i have this class for searching through google maps.
how can i pass it a location so that on startup it can show the given location on the map?
The WPF form has a loaded event, you can add some code there to load your first map:
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
ShowMapButton_Click(sender, e)
End Sub
In this case I hardcoded an address into the AddressTxtBox and called Show Map. You can add whatever code you need to set up the conditions for the first map.
If the question is "How do I pass information to a form to be used when it first load" that is a much shorter post!
I don't work a lot with WPF so I'm not up on naming conventions and best practices so keep that in mind... For example the called form would not be called MainWindow!
In Winforms a method like this could be used:
Public in_StartAddress As String = ""
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
If in_StartAddress.Length > 0 Then AddressTxtBox.Text = in_StartAddress
ShowMapButton_Click(sender, e)
End Sub
' calling routine on another form
Private Sub Button_Click(sender As Object, e As RoutedEventArgs)
Dim frm As New MainWindow
frm.in_StartAddress = "1600 Pensylvania Ave, Washington DC"
frm.Show()
frm = Nothing
End Sub
Look for posts on passing parameters to new forms/windows.
I have some textboxes (9) and I want to copy the data from those textboxes so when I press save on my access database I can press past and have my previous data appear on the new grid line.
Is this possible?
Here is what I have so far
COPY BUTTON -
Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
If SİPARİŞ_MUMARASITextBox.TextLength = 0 Then
MsgBox("yok")
Else
Clipboard.SetText(İSİMTextBox.Text & TARİHTextBox.Text & SİPARİŞ_MUMARASITextBox.Text _
& SİPARİŞİN_ADI__BASKIDAKİ_BİLGİ_TextBox.Text & SİPARİŞİN_ADI__BASKIDAKİ_BİLGİ_TextBox.Text _
& SAYACIN_BULUNDUĞU_KAZANTextBox.Text & KUMAŞ_RENGİTextBox.Text)
End If
End Sub
PASTE BUTTON -
Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
If Clipboard.ContainsText = True Then
İSİMTextBox.Text = Clipboard.GetText
TARİHTextBox.Text = Clipboard.GetText
SİPARİŞ_MUMARASITextBox.Text = Clipboard.GetText
SİPARİŞİN_ADI__BASKIDAKİ_BİLGİ_TextBox.Text = Clipboard.GetText
BOBİN_GRAMJI__gr_m2_TextBox.Text = Clipboard.GetText
BOBİN_ÜZERİNDE_YAZAN_METRAJ__cmXmt_TextBox.Text = Clipboard.GetText
BASKIDAKİ_TUR_SAYISITextBox.Text = Clipboard.GetText
SAYACIN_BULUNDUĞU_KAZANTextBox.Text = Clipboard.GetText
KUMAŞ_RENGİTextBox.Text = Clipboard.GetText
Else
İSİMTextBox.Clear()
TARİHTextBox.Clear()
SİPARİŞ_MUMARASITextBox.Clear()
SİPARİŞİN_ADI__BASKIDAKİ_BİLGİ_TextBox.Clear()
BOBİN_GRAMJI__gr_m2_TextBox.Clear()
BOBİN_ÜZERİNDE_YAZAN_METRAJ__cmXmt_TextBox.Clear()
BASKIDAKİ_TUR_SAYISITextBox.Clear()
BASKIDAKİ_TUR_SAYISITextBox.Clear()
SAYACIN_BULUNDUĞU_KAZANTextBox.Clear()
KUMAŞ_RENGİTextBox.Clear()
End If
End Sub
Using the clipboard is very likely to be the wrong approach. Instead, you could have a Class with properties for each item that you want remembered:
Option Infer On
' ...
Dim thingsToCopy As CopyBuffer
Public Class CopyBuffer
Property İSİM As String = ""
Property TARİH As String = ""
Property SİPARİŞ_MUMARASI As String = ""
Property SİPARİŞİN_ADI__BASKIDAKİ_BİLGİ As String = ""
Property BOBİN_GRAMJI__gr_m2 As String = ""
Property BOBİN_ÜZERİNDE_YAZAN_METRAJ__cmXmt As String = ""
Property BASKIDAKİ_TUR_SAYISI As String = ""
Property SAYACIN_BULUNDUĞU_KAZAN As String = ""
Property KUMAŞ_RENGİ As String = ""
End Class
Private Sub bnCopy_Click(sender As Object, e As EventArgs) Handles bnCopy.Click
If SİPARİŞ_MUMARASITextBox.TextLength = 0 Then
MsgBox("yok")
Else
thingsToCopy = New CopyBuffer With
{.İSİM = İSİMTextBox.Text,
.TARİH = TARİHTextBox.Text,
.SİPARİŞ_MUMARASI = SİPARİŞ_MUMARASITextBox.Text,
.SAYACIN_BULUNDUĞU_KAZAN = SAYACIN_BULUNDUĞU_KAZANTextBox.Text,
.KUMAŞ_RENGİ = KUMAŞ_RENGİTextBox.Text}
End If
End Sub
Private Sub bnPaste_Click(sender As Object, e As EventArgs) Handles bnPaste.Click
If thingsToCopy Is Nothing Then
' create a new one, which will have empty text
thingsToCopy = New CopyBuffer
End If
With thingsToCopy
İSİMTextBox.Text = .İSİM
TARİHTextBox.Text = .TARİH
SİPARİŞ_MUMARASITextBox.Text = .SİPARİŞ_MUMARASI
SAYACIN_BULUNDUĞU_KAZANTextBox.Text = .SAYACIN_BULUNDUĞU_KAZAN
KUMAŞ_RENGİTextBox.Text = .KUMAŞ_RENGİ
End With
End Sub
You appear to be pasting more things than you have copied; you will have to adjust the code to fit what is required.
i want to display my datagridview1 data in relevant textboxes. when i select any cell in the datagridview1 relevant data should be displayed in textboxes.
here is the code i did
Private Sub DataGridView1_CellClick(ByVal sender As Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView1.CellClick
Dim i As Integer
i = DataGridView1.CurrentRow.Index
Me.Label8.Text = DataGridView1.Item(0, i).Value
Me.TextBox1.Text = DataGridView1.Item(1, i).Value
Me.TextBox2.Text = DataGridView1.Item(2, i).Value
Me.ComboBox1.Text = DataGridView1.Item(3, i).Value
Me.ComboBox2.Text = DataGridView1.Item(4, i).Value
Me.TextBox5.Text = DataGridView1.Item(5, i).Value
Me.TextBox3.Text = DataGridView1.Item(6, i).Value
Me.TextBox4.Text = DataGridView1.Item(7, i).Value
Me.RichTextBox1.Text = DataGridView1.Item(8, i).Value
Me.RichTextBox2.Text = DataGridView1.Item(9, i).Value
Me.Label14.Text = DataGridView1.Item(10, i).Value
End Sub
i did another code here it is
Private Sub DataGridView1_CellContentClick(ByVal sender _
As System.Object, ByVal e As _
System.Windows.Forms.DataGridViewCellEventArgs) _
Handles DataGridView1.CellContentClick
Try
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.DataGridView1.Rows(e.RowIndex)
Label8.Text = row.Cells("id").Value.ToString
TextBox1.Text = row.Cells("firstname").Value.ToString
TextBox2.Text = row.Cells("lastname").Value.ToString
ComboBox1.Text = row.Cells("year").Value.ToString
ComboBox2.Text = row.Cells("month").Value.ToString
TextBox5.Text = row.Cells("gender").Value.ToString
TextBox3.Text = row.Cells("address").Value.ToString
TextBox4.Text = row.Cells("telephone").Value.ToString
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
however both codes are not working they give errors. inside the "" marks represent the column name
can anyone give me the solution in vb.net
Private Sub DataGridView1_CellClick (ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim i As Integer
With DataGridView1
if e.RowIndex >= 0 Then
i = .CurrentRow.Index
tbID.text = .Rows(i).Cells("id").Value.ToString
tbFirstNametext = .Rows(i).Cells("firstname").Value.ToString
tbLastNametext = .Rows(i).Cells("lastname").Value.ToString
tbAddresstext = .Rows(i).Cells("address").Value.ToString
End If
End With
End Sub
Please use this,
Private Sub DataGridView1_CellContentClick_1(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellDoubleClick
namatxt.Text = Me.DataGridView1.SelectedCells(1).Value.ToString
End Sub
How about this?
Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick
Dim row As DataGridViewRow = DataGridView1.CurrentRow
Me.Label8.Text = row.Cells(0).Value.ToString()
Me.TextBox1.Text = row.Cells(1).Value.ToString()
Me.TextBox2.Text = row.Cells(2).Value.ToString()
Me.ComboBox1.Text = row.Cells(3).Value.ToString()
Me.ComboBox2.Text = row.Cells(4).Value.ToString()
Me.TextBox5.Text = row.Cells(5).Value.ToString()
Me.TextBox3.Text = row.Cells(6).Value.ToString()
Me.TextBox4.Text = row.Cells(7).Value.ToString()
Me.RichTextBox1.Text = row.Cells(8).Value.ToString()
Me.RichTextBox2.Text = row.Cells(9).Value.ToString()
Me.Label14.Text = row.Cells(10).Value.ToString()
End Sub
Use this code, it works fine for me:
Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.DataGridView1.Rows(e.RowIndex)
tbID.Text = row.Cells("id").Value.ToString
tbFirstName.Text = row.Cells("firstname").Value.ToString
tbLastName.Text = row.Cells("lastname").Value.ToString
tbAddress.Text = row.Cells("address").Value.ToString
End If
End Sub
replace the names of the columns i have (i.e "id" "firstname" "lastname") etc with the name of your columns.
To navigate through DataGridView rows with up and down keys and show the selected record in textboxes you can use this:
'declare variable
Private DB As New databasenameDataSetTableAdapters.tablenameTableAdapter
'add constructor to your form
Public Sub New()
' This call is required by the designer.
InitializeComponent()
'Setting KeyPreview To True makes sure that the Form1_KeyDown will always
'be called If a key Is pressed,
Me.KeyPreview = True
End Sub
Private Sub form1_KeyDown(sender As Object, e As KeyEventArgs) Handles Me.KeyDown
Select Case e.KeyCode
'if else statement is there otherwise when going through the rows in grid
'it would show the previous row in textboxes instead of current one
'GetData is a select query you can generate by double clicking the dataset.xsd file in solution explorer
Case Keys.Up
Dim rowUp As Integer = dataGridView.CurrentRow.Index
If (rowUp <= 0) Then
rowUp = 0
Else
rowUp = rowUp - 1
End If
Try
txtBox1.Text = DB.GetData.Rows(rowUp).Item(0).ToString
txtBox2.Text = DB.GetData.Rows(rowUp).Item(1).ToString
txtBox3.Text = DB.GetData.Rows(rowUp).Item(2).ToString
Catch ex As Exception
MsgBox(ex.Message)
End Try
Exit Select
Case Keys.Down
Dim rowDown As Integer = dataGridView.CurrentRow.Index
If (rowDown >= DB.GetData.Rows.Count - 1) Then
rowDown = DB.GetData.Rows.Count - 1
Else
rowDown = rowDown + 1
End If
Try
txtBox1.Text = DB.GetData.Rows(rowDown).Item(0).ToString
txtBox2.Text = DB.GetData.Rows(rowDown).Item(1).ToString
txtBox3.Text = DB.GetData.Rows(rowDown).Item(2).ToString
Catch ex As Exception
MsgBox(ex.Message)
End Try
Exit Select
Another way of doing it use the datagridview CellEnter event
Private Sub DataGridView1_CellEnter(sender As Object, e As EventArgs)
Handles DataGridView1.CellEnter
Dim row As DataGridViewRow = DataGridView1.CurrentRow
txtBox1.Text = row.Cells(0).Value.ToString()
txtBox2.Text = row.Cells(1).Value.ToString(),
End Sub
Private Sub DataGridView1_CellEnter(sender As Object, e As EventArgs)
Handles DataGridView1.CellEnter
Dim row As DataGridViewRow = DataGridView1.CurrentRow
txtBox1.Text = row.Cells(0).Value.ToString()
txtBox2.Text = row.Cells(1).Value.ToString(),
End Sub
The TabLoad() function doesn't seem to be working on clicking of this dynamic button. The aim of this button click event is that it deletes text from a text file and loads the form again.
Below is the complete code. The TabLoad() function is in the NextDelbtn_Click sub at the end.
Also any suggestion regarding change of code is appreciated.
Imports System.IO
Public Class Form1
Dim str As String
Dim FILE_NAME As String = "D:\1.txt"
Dim file_exists As Boolean = File.Exists(FILE_NAME)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If TextBox1.Text = "" Then
MsgBox("Please enter text that you want to save", MsgBoxStyle.Information, "TOCC Error")
Else
str = TextBox1.Text
Dim fs As FileStream = Nothing
If (Not File.Exists(FILE_NAME)) Then
fs = File.Create(FILE_NAME)
Using fs
End Using
End If
If File.Exists(FILE_NAME) Then
Dim sw As StreamWriter
sw = File.AppendText(FILE_NAME)
sw.WriteLine(str)
sw.Flush()
sw.Close()
TabLoad()
TextBox1.Text = ""
End If
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
TabControl1.SelectedIndex = TabControl1.TabIndex + 1
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
TabLoad()
End Sub
Private Sub Nextbtn_Click(sender As Object, e As EventArgs)
Dim s As String = DirectCast(DirectCast(sender, Button).Tag, Label).Text
Clipboard.SetText(s)
End Sub
Private Sub TabLoad()
Dim i As Integer = 1
For Each line As String In System.IO.File.ReadAllLines(FILE_NAME)
Dim NextLabel As New Label
Dim Nextbtn As New Button
Dim NextDelbtn As New Button
NextLabel.Text = line
Nextbtn.Text = "Copy"
NextDelbtn.Text = "Delete"
NextLabel.Height = 22
Nextbtn.Width = 55
Nextbtn.Height = 22
NextDelbtn.Width = 55
NextDelbtn.Height = 22
Nextbtn.BackColor = Color.WhiteSmoke
NextLabel.Tag = Nextbtn
Nextbtn.Tag = NextLabel
NextDelbtn.Tag = NextLabel
NextDelbtn.BackColor = Color.WhiteSmoke
NextLabel.BackColor = Color.Yellow
TabPage2.Controls.Add(NextLabel)
TabPage2.Controls.Add(Nextbtn)
TabPage2.Controls.Add(NextDelbtn)
NextLabel.Location = New Point(10, 10 * i + ((i - 1) * NextLabel.Height))
Nextbtn.Location = New Point(120, 10 * i + ((i - 1) * Nextbtn.Height))
NextDelbtn.Location = New Point(180, 10 * i + ((i - 1) * NextDelbtn.Height))
AddHandler Nextbtn.Click, AddressOf Me.Nextbtn_Click
AddHandler NextDelbtn.Click, AddressOf Me.NextDelbtn_Click
i += 1
Next
File.WriteAllLines(FILE_NAME,
File.ReadAllLines(FILE_NAME).Where(Function(y) y <> String.Empty))
End Sub
Private Sub NextDelbtn_Click(sender As Object, e As EventArgs)
Dim btn As Button = CType(sender, Button)
Dim s As String = (btn.Tag).Text
Dim lines() As String = IO.File.ReadAllLines(FILE_NAME)
Using sw As New IO.StreamWriter(FILE_NAME)
For Each line As String In lines
If line = s Then
line = ""
End If
sw.WriteLine(line)
Next
sw.Flush()
sw.Close()
TabLoad()
End Using
End Sub
End Class
Your problem appears to be that you're initializing new controls but you're not changing the controls on the form or even creating a new form.
Hey all i am creating some run-time label for my form:
Dim tmpLbl As New Label
Dim intX As Integer = 0
Do Until intX = 4
With tmpLbl
.Size() = New System.Drawing.Size(58, 15)
.Text = "Run-time Controls " & intX
.Location = New System.Drawing.Point(2, 20)
.Name = "test" & intX
End With
With Me.Controls
.Add(tmpLbl)
End With
intX += 1
Loop
This works just fine... but when i want to, say place a button on the form that calls one of these run-time controls like so:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Debug.Print(tmpLbl.Name)
End Sub
Naturally this above does not work. how can i get a labels name (ex: test2) and read its text (ex: Run-time Controls 2) when i click on that button?
Got it!
Private genTmpLbl As New List(Of Label)
Public Sub makeRunTimeControls()
Dim tmpLbl As New Label
Dim intX As Integer = 0
Do Until intX = 4
With tmpLbl
.Size() = New System.Drawing.Size(58, 15)
.Text = "Run-time Controls " & intX
.Location = New System.Drawing.Point(2, 20)
.Name = "test" & intX
End With
With Me.Controls
.Add(tmpLbl)
End With
genTmpLbl.Add(tmpLbl)
intX += 1
Loop
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
For Each label In genTmpLbl
For Each control In Me.Controls
If TypeOf (control) Is Label Then
Debug.Print(control.Name)
End If
Next
Next
End Sub