I am trying to update a label from one form to another. the code is compiling fine but not updating?
Class MainWindow
Private Sub Window_Loaded(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles MyBase.Loaded
Dim frm As New Window1
frm.Show()
End Sub
End Class
second form:
Public Class Window1
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
My.Windows.MainWindow.Label1.Content = "dsfdsfsdf"
My.Windows.MainWindow.Label1.UpdateLayout()
End Sub
End Class
it doesn't update the main form's label... hope that makes it clearer
So here's the code that you actually need:
Public Class Window1
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
DirectCast(Application.Current.MainWindow, MainWindow).Label1.Content = "test"
End Sub
End Class
I'm not sure what the My.Windows collection is, but the Application.MainWindow gives you a reference to the window that is set as the start up object in the project properties (or the one you set in your app.xaml.cs file). Previously you were probably getting a reference to a different instance of the Window1 class, hence it was running and not throwing an exceptions, but since it wasn't the actual visible instance of the window then you didn't see any changes.
Related
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
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.
I have a WPF Project to which i've added a Windows Form.
The windows form has a method runme(). But I'd like to call it from the WPF form.
Currently I've used
Dim wpfForm as new wpfForm
wpfForm.Show()
to display the the WPF form from a Windows form.
I'm not sure how to send a command back though.
I've tried:
Me.parent.runme()
But this gives me errors
The only way that I can think of to do what you are wanting is to create a Custom Constructor for your Winform and pass the reference to the Wpf Window to it. There appears to be no easy way to assign a WPF Window as an Owner/Parent of a Winforms Window.
Winforms Form
Public Class Form1
Dim myParent As System.Windows.Window
Public Sub New()
InitializeComponent()
End Sub
Public Sub New(parent As System.Windows.Window)
myParent = parent
InitializeComponent()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
CType(myParent, MainWindow).runMe()
End Sub
End Class
Wpf Form
Class MainWindow
Dim winForm As Form1
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
winForm = New Form1(Me)
winForm.Show()
End Sub
Public Sub runMe()
Me.Background = Brushes.Red
End Sub
End Class
Eventually what worked was to create an interface as suggested by Cyborgx37
Interface ILogOut
Sub DoIt()
End Interface
Class WinFormWindow : Implements ILogOut
Public Sub DoIt() Implements ILogOut.DoIt
MsgBox("Message Received!")
End Sub
End Class
You can then use the ILogOut interface to send a message to the Windows forms window from a WPF window
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
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