how to insert record with wpf databinding? - wpf

im new to wpf, so please bear with me
i made a window for the purpose of managing "profile" records
while the navigation and updating is working
i cannot seem to get insertion working, as well
heres the very simple code
Imports System.Collections.ObjectModel
Public Class Window1
Dim WindowEntities As New DataEntities
Dim WindowList As ObservableCollection(Of Profile)
Function ProfilesViewSource() As CollectionViewSource
Return CType(FindResource("ProfilesViewSource"), CollectionViewSource)
End Function
Function DefaultView() As CollectionView
Return CollectionViewSource.GetDefaultView(ProfilesViewSource.View)
End Function
Sub Window_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles MyBase.Loaded
WindowList = New ObservableCollection(Of Profile)(From x In WindowEntities.Profiles Order By x.ProfileName)
ProfilesViewSource.Source = WindowList
End Sub
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles btnSave.Click
WindowEntities.SaveChanges()
End Sub
Private Sub btnAdd_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles btnAdd.Click
WindowList.Add(New Profile)
DefaultView.MoveCurrentToLast()
End Sub
Private Sub btnNext_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles btnNext.Click
DefaultView.MoveCurrentToNext()
End Sub
End Class
when i press add, i get a new empty record, when i fill it in, i see that the collection sees it, as there is an onform listbox showing the profiles, and it gets listed there, so its actually attached to the list, but the savechanges command does not insert it into the db
there may be some validation errors upon insert, but then i would get some indication, right? now its just silently failing. as if i never tied to commit changes
thank you very much for your help fellows

(by IVerzin)
1. create new Profile.
2. Add it to WindowEntities
3. Add it to WindowList

Related

Visual Basic Saving Form info on hide

Im using Visual Basic 2008
I have 2 forms
Main, EditCustomerInfo
Main form contains the following
Public Class Main
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
EditCustomerInfo.ShowDialog()
End Sub
EditCustomerInfo contains a text box and the following
Public Class EditCustomerInfo
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If Not CustomerIDTextBox.Text = "" Then
Me.Close()
Else : Me.Hide()
End If
End Sub
WHAT IT DOES:
So with this code alone when i debug the program it takes me to the main form and allows me to click a button to open the editcustomerinfo form
When im on the editcustomerinfo form i have a textbox and a button. If something is typed in the textbox and the button is clicked then the form hides, if nothing is typed in the textbox when the button is clicked then the form closes.
WHAT I WOULD LIKE IT TO DO:
If something is typed in the textbox i would like the button on the editcustomerinfoform to hide the editcustomerinfoform and also create a button on main form that allows the user to bring the editcustomerinfo form back up with what was typed in the text box.
Suggestions?
Automatic behavior, such as this, always worries me. How do you know when a user has completed their input without a lost focus event. If there are no other controls on the screen, then users won't readily know how to trigger the event. That being said, you can use a timer to delay the screen closing from a KeyPress event.
Public Class EditCustomerInfo
Private WithEvents userInputDelay As Timer = New Timer() With {.Interval = 1000} REM 1 second delay for next user input
Public ReadOnly Property CustomerId As String
Get
Return CustomerIDTextBox.Text
End Get
End Property
Private Sub CustomerIDTextBox_KeyPress(sender As Object, e As KeyPressEventArgs) Handles CustomerIDTextBox.KeyPress
userInputDelay.Enabled = False
REM Reset the timer
userInputDelay.Enabled = True
End Sub
Private Sub userInputDelay_Tick(sender As Object, e As KeyPressEventArgs) Handles userInputDelay.Tick
If Not CustomerIDTextBox.Text = "" Then
Me.Close()
Else : Me.Hide()
End If
End Sub
End Class
Add a button (Button2) to your Main. The code below will hide Button1 when the EditCustomerInfo.Textbox1.Text value is null/blank/white space. Button2's visibility is always the inverse of Button1.
Private EditCustomerInfoInstance As New EditCustomerInfo
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
EditCustomerInfoInstance.ShowDialog()
Button1.Visible = String.IsNullOrWhiteSpace(EditCustomerInfoInstance.CustomerId)
End Sub
Private Sub Button1_VisibleChanged(sender As Object, e As EventArgs) Handles Button1.VisibleChanged
Button2.Visible = Not Button1.Visible
End Sub

How to check which Gridview is selected, if form having more than one Gridview?

In form i created three dynamic Gridview , I want to perform some specific operations on selection of particular Gridview, can any one help me how to know which gridview got seletec?
There is a .GotFocus() event on the DataGridView you can use to store something in a form scope (or global) variable. (sorry for the VB...)
Dim LastGirdWithFocus As String = ""
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
MsgBox(LastGirdWithFocus)
End Sub
Private Sub dgv1_GotFocus(sender As Object, e As System.EventArgs) _
Handles dgv1.GotFocus, dgv2.GotFocus
LastGirdWithFocus = sender.name
End Sub

WPF forward/back button

I would like to make back and forward button in my app in WPF (Visual Basic 2010 Express). I have six tabitems and this two buttons will be used to navigate between tabitems.
I have used following code, but it doesn't work. what am I doing wrong?
Public ReadOnly Property CanGoForward As Boolean
Get
CanGoForward = True
End Get
End Property
Private Sub forward_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles forward.Click
TabIdentiPodatki.goforward()
End Sub
Private Sub TabIdentiPodatki_CanGoForwardChanged(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles forward.CanGoForwardChanged
forward.IsEnabled = TabIdentiPodatki.cangoforward
End Sub

trying to learn animation in WPF with CompositionTarget.Rendering

I am trying to figure out how to animate/move elements in WPF with VB.NET. It was suggested to me to try using CompositionTarget.Rendering but I am having difficulty getting started. Below is my code and below that is the error message I am getting. Could someone please tell me where I am going going wrong.
Code:
Imports System.ComponentModel
Class MainWindow
Dim flake0 As New flake
Private Sub Window_Loaded(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
canvas1.DataContext = flake0
AddHandler CompositionTarget.Rendering, AddressOf compo
End Sub
Private Sub compo(ByVal sender As Object, ByVal e As EventArgs)
flake0.move()
End Sub
End Class
Error:
Handles clause requires a WithEvents variable defined in the containing type or one of its base types
I had another method that accidentally got "handles CompositionTarget.Rendering" tagged to the end of it.

How can I make a globally accessible datatable in a Winforms application?

Disclaimer: I am new to Winforms.
I need to declare a datatable that I can load with data when the main form loads. I then want to be able to reference the datatable from within events like when a button is clicked etc.
Where/how should I declare this?
I'd suggest a private member at the top of the form class meaning it will be accessible throughout the entire form. No need for a public property, unless you have to access it outside of the form but its best to default to private if you are unsure.
Update: If it is a simple one form app, please check the suggestion by Quarrelsome..
Just Declare as a public property of your Data Access class.
Public
Class Form3
Private myTable as New DataTable
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox(t.Rows.Count)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
MsgBox(t.Rows.Count)
End Sub
End Class

Resources