Call public function in User Control WPF - wpf

I am new to WPF
I have many master forms.which is having some buttons in all forms.so i wrote that button handing in usercontrols and using this same in all other master forms
now i want to handle each button.for that i wrote code on each form and trying to access it to user control class. but i couldn't. How can i do it ?
I have 3 forms
1)Aform
2)BForm
3)CForm
each form having 2 button.Say Save and Delete
Each form have to differently handle these buttons. So i wrote code on each form as function or Sub
Now I want to call that function to user control..'
I tried like this
in AForm.Vb :
Public Sub SaveA()
{}
In UserControl :
Dim ParentControl as Window=Window.GetWindow(me)
ParentControl.SaveA () // But i couldn't
What can i do for this ?

You need to type-cast the Window object to your specific Window implementation. For example given you defined the function in MainWindow class :
Dim ParentControl as MainWindow = CType(Window.GetWindow(me), MainWindow)
ParentControl.SaveA()

Related

How to make a custom control compatible with errorProvider?

I have a winform custom control that looks like :
Public Class MyCustomControl
Inherits Control
Implements INotifyPropertyChanged
'Some textboxes...
private Textbox1 as textbox
private Textbox2 as textbox
private Textbox3 as textbox
'Control value
Property Value as MyCustomClass
get
'Extract some values from textboxes and return MyCustomClass (a separate custom object)
end get
Set(value As MyCustomClass)
'Set the values of the textboxes from the Value object
end Set
end Property
end class
I would like to insert this object in a winform and I would like my control to be "compatible" with errorProvider. Such that if I insert an errorProvider in the form and I call : errorProvider.SetError(MyCustomControl, "Error message"), it will show the error message on a specific text box in my custom control according to a custom logic.
Does anyone know which interface should be implemented or how to that please ?
Thanks. Cheers,
What you're trying to do would be better achieved with a UserControl, instead of inheriting directly from Control. (Having three TextBox instances inside your control is a good indication here.) In your UserControl you have a couple of options:
Add an ErrorProvider component and hook it up to each TextBox; or
For each TextBox expose it by making it public or via a public property, and then in your form hook it up to the form's ErrorProvider.
Ideally, you would use a BindingSource and data bind each TextBox, with the ErrorProvider.DataSource being set to your BindingSource.

How to use a grid in the XAML Mainwindow to another class in WPF?

In the code behind of a main window of a WPF project, there is a grid with a specific name in XAML page as follow:
<Grid Grid.Row="2" x:Name="PnlGraphics" Margin="5"></Grid>
In another class (e.g. ClsChart) in a function, I need to set this grid value.
Here is the piece of code that uses that grid name in clsChart class:
PnlGraphics.Children.Add(host)
I tried many things as below, but my problem has not been resolved yet.
1- I made an object from the main class and tried to use that to get access to the grid name. But in practice, it gives an error when I run the program!
Dim mainClass1 as new mainClass = new mainClass()
mainclass1.pnlGraphics.Children.Add(host)
2- I made a panel control in the ClsChart class and tried to fill the grid in the main class.
In the clsChart class:
Private _panel1 As Panel
Public Property Panel1 As Panel
Get
Return _panel1
End Get
Set(value As Panel)
_panel1 = value
End Set
End Property
Panel1.Children.Add(host)
and in the main:
ChartObject.Panel1 = PnlGraphics
3- I changed the grid modifier to the public.
Any thought on this?
Instantiate an clsChart object in your main class and pass the grid as parameter to the constructor (e.g. clsChart c = new clsChart(grid)). In the constructor you assign grid to some member variable of type Grid and use it as needed.

Use the same WPF window for two different purposes

I have a WPF application written in VB with several windows. These windows have a several controls in them for the users to enter data. This data is then saved to a database. I want the users to be a able to edit a given set of data and it would be a lot more convenient to use the same window the data was entered in. Depending on whether the user clicks "Add" or "Edit", I want to run different code behind the window.
My issue is that I can't figure out how to differentiate between these two events. The MainWindow class has buttons "Add" and "Edit". When clicked, they create a new tab that contains a new instance of "Data.xaml". "Data.xaml" has "Data.vb" behind it. How can "Data.vb" tell whether it should execute "Edit" or "Add" code?
Simple solution is to add some property to the Data class which will tell what should be done:
Public Partial Class Data Inherits Window
// ...
Public Property Mode As Mode
// ...
End Class
where Mode is enum with two fields: Add and Edit.
In click handler of Add button set Mode to Mode.Add, in click handler of Edit button set Mode to Mode.Edit.
If you want to prevent changing of Mode after window constructed you can create new constructor that will take mode as an argument:
Public Partial Class Data Inherits Window
// ...
Public Sub New(mode As Mode)
Me.New()
Mode = mode
End Sub
Public ReadOnly Property Mode As Mode
// ...
End Class
Then in your logic in Data.vb look at the Mode and do appropriate actions.
Private Sub AddButton_Click(sender As Object, e As RoutedEventArgs)
Dim dataWindow = New Data(Mode.Add)
dataWindow.ShowDialog()
End Sub
Private Sub EditButton_Click(sender As Object, e As RoutedEventArgs)
Dim dataWindow = New Data(Mode.Edit)
dataWindow.ShowDialog()
End Sub

Double click listview item and pass the variable to another page to run a search

I have 2 forms; MainWindow and OwnerShares. There is a search on MainWindow that looks up what items are being shared. OwnerShares has a search function that looks up the owner details and what they are sharing. On OwnerShares the options are displayed in a ListView. What i want to do is allow the user to double click on a row in ListView and be directed to the MainWindow which runs the SEARCH based on the value that was selected from the ListView. The variable ACEName will be the one that i want to pass to the MainWindow form search.
Private Sub listSearchOwner_MouseDoubleClick(sender As Object, e As MouseButtonEventArgs) Handles listSearchOwner.MouseDoubleClick
Dim ACEName As String
ACEName = listSearchOwner.SelectedItems(0).xShareName
OwnerShares.Close()
End Sub
The name of the search in MainWindow is (cmdSearch_Click). I am currently getting the value that i click on with the above code. I just dont know how to open the MainWindow form and run the search command automatically.
If I read your question correctly you could just call show your form and run your other routine
Private Sub listSearchOwner_MouseDoubleClick(sender As Object, e As MouseButtonEventArgs) Handles listSearchOwner.MouseDoubleClick
Dim ACEName As String
ACEName = listSearchOwner.SelectedItems(0).xShareName
OwnerShares.Close()
MainWindow.show()
' YourMethod or Button here.
End Sub
Approach it this way:
Add a module to your project, if you don't have one.
Create a public variable in the module, e.g.
Public VariableName As DataType
Everytime that you double-click the listview control, assign a value to your variable.
After getting the value stored in the public variable, delete the value to create a room to pass another value again.
That should do the trick.

How to get all forms in a windows form application

I have a windows form application.
I want to get all of form that application has, but i just found about the function Application.OpenForms.
This function return all of forms are opening.
But I want to get all of forms in that application.
Is there any functions to get but not add to FormsColection for new Form created like this solution https://support.microsoft.com/en-us/kb/815707 ?
Thank you!
"All of the forms that an application has" typically would mean using reflection for all of the types in the assembly that inherit from the Windows.Forms.Form class.
The Application.OpenForms only tracks forms that are opened, not necessarily those that have been instantiated and not opened.
What you really need to do is track all of the forms when the form objects are instantiated. See code below:
Public Class Form1
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Globals.InstantiatedForms.Add(Me)
End Sub
End Class
Public Module Globals
Public InstantiatedForms As New List(Of Windows.Forms.Form)
End Module

Resources