Use the same WPF window for two different purposes - wpf

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

Related

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 do I improve performance of WPF UserControl rendered in Windows Forms

We are using wpf user controls in windows form like data entry screen with more than 100 Controls. The form rendering time increased after continue usage. Any suggestions?
In Xaml part:
Grid Panel to arrange the controls
Windows form calling method:
Public form As New WPFUserControl
form.Tag = Me
ElementHost1.Child = form
form.Formload()
-- more code from comments --
Private Sub frmSwitch_New_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Public form As New WPFUserControl
form.Tag = Me ElementHost1.Child = form
form.Formload()
End Sub
You are creating instance of you 100s control everytime you call
frmSwitch_New_Load
but you never unload new controls created. Just when your frmSwich unloads you need to unload your wpf controls as well
Where you give a call to frmSwitch_New_Load if that's a Load event handler to your form, have an event handler for unload event to that form and underthat event handler unload your current wpf control

Call public function in User Control 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()

Only one windows should be open at once in WPF?

I am developing a WPF project in vb.net and have multiple windows in it. When user selects a menu item a new windows opens and the problem is when the user clicks on other menu item the current window should close by itself.
How do i achieve it?
Thanks!
I think you mean this based upon your comment:
Class MainWindow
Public Win3 As Window3 = New Window3()
Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
Win3.Show()
Me.Close()
End Sub
End Class
There are two ways to do this:
If the menu items share the same form then in the form create a subroutine, that you call instead of new, that checks if it is already shown/created and if not opens (as you already have). If it is open reload with the new information.
Otherwise before opening the new form go though the open forms (shown below from this website in C#):
FormCollection fc = Application.OpenForms;
foreach (Form frm in fc) {
//iterate through
}
For each form check if it's name is equal to one of the menu items and if it is close it (after saving if required). Then after you exit the for loop you open the new menu item.

Handling events from user control containing a WPF textbox

In order to take advantage of the spell checking ability of WPF textboxes, I have added one to a user control (with the use of elementhost). This user control is used in various window forms. My current problem is trying to handle keyup events from this textbox but the windows form is unable to "get" any event from the control. I can access the properties of the textbox just fine (i.e. text, length, etc.) but keyboard events don't seem to work.
I have found, however, that the following will bring back events from the WPF textbox:
Public Class MyUserControl
Private _elementHost As New ElementHost
Private _wpfTextbox As New System.Windows.Controls.Textbox
Private Sub MyUserControl_Load(...) Handles Me.Load
Me.Controls.Add(_elementHost)
_elementHost.Dock = DockStyle.Fill
_elementHost.Child = _wpfTextbox
Dim MyEventInfo As EventInfo
Dim MyMethodInfo As MethodInfo
MyMethodInfo = Me.GetType().GetMethod("WPFTextbox_KeyUp")
MyEventInfo = _wpfTextBox.GetType().GetEvent("PreviewKeyUp")
Dim dlg As [Delegate] = [Delegate].CreateDelegate(MyEventInfo.EventHandlerType, Me, MyMethodInfo)
MyEventInfo.AddEventHandler(_wpfTextBox, dlg)
End Sub
Public Sub WPFTextbox_KeyUp(ByVal sender As Object, ByVal e As RoutedEventArgs)
' something goes here
End Sub
End Class
The user control is now able to do something after the PreviewKeyUp event is fired in the WPF textbox. Now, I'm not completely sure how to have the window form containing this user control to work with this.
Im a C# person not VB so please bear with me.. Basically you could assign the event from your Window rather than within your UserControl.. So in the constructor of your Window assign the PreviewKeyUp:
this.myUserContorl.PreviewKeyUp += new System.Windows.Input.KeyEventHandler(WPFTextbox_KeyUp);
then place the event handler in your Window:
private void WPFTextbox_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
}
Incidentally you needn't go through the hassle of capturing the event in your UserControl as you can still access your TextBox within you UserControl directly from your Window (if you make it public), again from your constructor in your Window:
this.myUserContorl.wpfTextbox.PreviewKeyUp += new System.Windows.Input.KeyEventHandler(WPFTextbox_KeyUp);
I imagine it would look like this in VB (at a guess):
AddHandler myUserContorl.wpfTextbox.PreviewKeyUp, AddressOf WPFTextbox_KeyUp
ElementHost has a static method called EnableModelessKeyboardInterop(). Try calling it?
ElementHost.EnableModelessKeyboardInterop();
Read more here
Seems basic, but did you set the KeyPreview to TRUE on your form?

Resources