Related forms
-
Tuesday, August 02, 2011 11:59 AMI created the new form1 the Equipment - includes fields - type, vendor, model, serial number, the status etc. I create other form2 in which also is type, model of the equipment etc. Prompt, please, an operations procedure to relate this fields. The user chooses Type, the vendor who corresponds to the chosen type, model. Serial number already it is populated automatically from the equipment form1. How such binding to realize?
All Replies
-
Tuesday, August 02, 2011 12:09 PM
Hi,
I hope I am understanding your issue properly. When the user searches on form1 could you not save the search criteria in a struct etc and then when you are opening the second form you pass in this struct in the constructor (of form2). Then you have all the details the usser entetred in form1 to correctly configure form2.
hope this helps?
-
Tuesday, August 02, 2011 12:19 PMI want to field in the form2 have been linked with other fields of Equipment CI (form1). When filling out the properties (on form2) of the type, vendor, model, other properties-serial number,inventory number etc are populated automatically.
-
Tuesday, August 02, 2011 12:26 PM
Hi,
Okay I get you. you won't be able to do this with binding in xaml because they are 2 seperate forms. Instead what you can do is if a combo box selected item changes in form1 you can pass the data to the the second window by using the following code -
//This line will give you a handle to your form2 screen
form2 formTwo = App.current.MainWindow as Form2.
//pass in selectedvalue from combo box (in form1) to public method on form2
formTwo.PublicMethod(string comboval);
Then in this public method (of form2) you can repopulate all the controls according to the criteria.
does that make sense?
-
Tuesday, August 02, 2011 1:13 PM
Can make a method that will be choose the condition data (serial number, inventory number etc) about equipment when changing field model,type,vendor ?
I created a form Equipment for data entry and new class for Equipment, which has the properties: type, vendor, etc. This form is stored. The user must open another form and it when choosing the type, model, other fields are filled in automatically.
-
Tuesday, August 02, 2011 1:26 PM
Hi PufikYS,
The way Pritesh has mentioned above should be a way to obtain what you are trying to achieve.
You can expose a method on the second form to pass in the parameters that you require from the first form and call it whenever necessary.
Kindly let know if you are facing any issues using that approach.
Providing a sample code of what you are currently doing would make it more easier to help you out.
Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem -
Tuesday, August 02, 2011 1:30 PMThanks!!! I'll try!
-
Tuesday, August 02, 2011 1:30 PM
Yeah Rahul is correct. I'm a little confused by what you are trying to achieve so I'm not sure if I'm providng you with the right solution.
Here what I think you want to do -
If you have the employee classs with the values set from form1 then you can pass that employee class to form2 as a parameter in a method and then using its values form2 can determine how to configure itself.
Is this what you want to happen?
-
Wednesday, August 03, 2011 7:33 AM
My form1 - Equipment:
<Window x:Class="Equipment.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="282" Width="304"> <Grid> <Label Content="Type" Height="28" HorizontalAlignment="Left" Margin="26,34,0,0" Name="label1" VerticalAlignment="Top" /> <Label Content="Vendor" Height="28" HorizontalAlignment="Left" Margin="26,0,0,147" Name="label2" VerticalAlignment="Bottom" /> <Label Content="Serial number" Height="28" HorizontalAlignment="Left" Margin="26,0,0,113" Name="label3" VerticalAlignment="Bottom" /> <TextBox Height="23" HorizontalAlignment="Left" Margin="113,34,0,0" Name="txtType" VerticalAlignment="Top" Width="120" Text="{Binding Path=Type, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> <TextBox Height="23" HorizontalAlignment="Left" Margin="113,68,0,0" Name="txtVendor" VerticalAlignment="Top" Width="120" Text="{Binding Path=Vendor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> <TextBox Height="23" HorizontalAlignment="Left" Margin="113,102,0,0" Name="txtSerialNum" VerticalAlignment="Top" Width="120" Text="{Binding Path=SerialNum, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> </Grid> </Window>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.ComponentModel; namespace Equipment { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } MainWindow formTwo = App.Current.MainWindow as MainWindow; } public class ConfItem: INotifyPropertyChanged { // PropertyChanged event definition. public event PropertyChangedEventHandler PropertyChanged; // Private fields. string strType; string strVendor; string strSerialNum; // Public properties. public string Type { set { strType = value; OnPropertyChanged("Type"); } get { return strType; } } public string Vendor { set { strVendor = value; OnPropertyChanged("Vendor"); } get { return strVendor; } } public string SerialNum { set { strSerialNum = value; OnPropertyChanged("SerialNum"); } get { return strSerialNum; } } // Fire the PropertyChanged event. protected virtual void OnPropertyChanged(string strPropertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName)); } } }My form2 - user must open this form and it when choosing the type, vendor, other serial number must filled in automatically.
<Window x:Class="Equipment.Window1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Window1" Height="300" Width="300"> <Grid> <Label Content="Type" Height="28" HorizontalAlignment="Left" Margin="25,12,0,0" Name="label1" VerticalAlignment="Top" /> <Label Content="Vendor" Height="28" HorizontalAlignment="Left" Margin="25,46,0,0" Name="label2" VerticalAlignment="Top" /> <Label Content="Serial Number" Height="28" HorizontalAlignment="Left" Margin="25,80,0,0" Name="label3" VerticalAlignment="Top" /> <ComboBox Height="23" HorizontalAlignment="Left" Margin="118,12,0,0" Name="comboType" VerticalAlignment="Top" Width="120" /> <ComboBox Height="23" HorizontalAlignment="Left" Margin="118,46,0,0" Name="comboVendor" VerticalAlignment="Top" Width="120" /> <TextBox Height="23" HorizontalAlignment="Left" Margin="118,80,0,0" Name="txtSerialNum" VerticalAlignment="Top" Width="120" /> </Grid> </Window>
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Shapes; namespace Equipment { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { public Window1() { InitializeComponent(); } } }How to add ?
formTwo.PublicMethod(string comboval);
maybe something I'm doing wrong?)
-
Wednesday, August 03, 2011 7:45 AM
Hi,
How is the user supposed to open form2? From the code you supplied. There is no button or control on form1 that the user selects to open form2.
Thanks.
-
Wednesday, August 03, 2011 7:55 AM
Sorry:)
<Window x:Class="Equipment.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="282" Width="304"> <Grid> <Label Content="Type" Height="28" HorizontalAlignment="Left" Margin="26,34,0,0" Name="label1" VerticalAlignment="Top" /> <Label Content="Vendor" Height="28" HorizontalAlignment="Left" Margin="26,0,0,147" Name="label2" VerticalAlignment="Bottom" /> <Label Content="Serial number" Height="28" HorizontalAlignment="Left" Margin="26,0,0,113" Name="label3" VerticalAlignment="Bottom" /> <TextBox Height="23" HorizontalAlignment="Left" Margin="113,34,0,0" Name="txtType" VerticalAlignment="Top" Width="120" Text="{Binding Path=Type, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> <TextBox Height="23" HorizontalAlignment="Left" Margin="113,68,0,0" Name="txtVendor" VerticalAlignment="Top" Width="120" Text="{Binding Path=Vendor, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> <TextBox Height="23" HorizontalAlignment="Left" Margin="113,102,0,0" Name="txtSerialNum" VerticalAlignment="Top" Width="120" Text="{Binding Path=SerialNum, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> <Button Content="Open" Height="23" HorizontalAlignment="Left" Margin="29,175,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" /> </Grid> </Window>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.ComponentModel; namespace Equipment { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } MainWindow formTwo = App.Current.MainWindow as MainWindow; private void button1_Click(object sender, RoutedEventArgs e) { Window window = new Window1(); window.WindowStartupLocation = WindowStartupLocation.CenterScreen; window.Show(); } } public class ConfItem: INotifyPropertyChanged { // PropertyChanged event definition. public event PropertyChangedEventHandler PropertyChanged; // Private fields. string strType; string strVendor; string strSerialNum; // Public properties. public string Type { set { strType = value; OnPropertyChanged("Type"); } get { return strType; } } public string Vendor { set { strVendor = value; OnPropertyChanged("Vendor"); } get { return strVendor; } } public string SerialNum { set { strSerialNum = value; OnPropertyChanged("SerialNum"); } get { return strSerialNum; } } // Fire the PropertyChanged event. protected virtual void OnPropertyChanged(string strPropertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName)); } } } -
Wednesday, August 03, 2011 8:37 AM
Okay thanks for the update.
So how I would choose to do it is to pass in the information via the constructor to Window1 -
private void button1_Click(object sender, RoutedEventArgs e) { Window window = new Window1(string selectedComboVal); window.WindowStartupLocation = WindowStartupLocation.CenterScreen; window.Show();}You could pass in something other than a string e.g. a struct or class containing all the information the user filled in the MainWindowThen in window1 you could have a method that does the populating of its fields
something like this -
public Window1(string comboVal) { InitializeComponent();PopulateFeilds(comboVal);
}
private void PopulateFeilds(comboVal){
//Populate fields in here
}
-
Wednesday, August 03, 2011 9:10 AM
Hi,
The above way of doing this through the constructor is one way of doing.
Since in WPF all the controls are with parameterless constructors , I would suggest you to define a method in your Window1 class and do the same by invoking the method.
Modifying the above code:
public Window1() { InitializeComponent(); } public void PopulateFeilds(comboVal) { //Populate fields in here }private void button1_Click(object sender, RoutedEventArgs e) { Window window = new Window1(); window.WindowStartupLocation = WindowStartupLocation.CenterScreen; window.PopulateFields("Your Data"); window.Show(); }Feel free to ask if you need more help on this
Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem -
Wednesday, August 03, 2011 10:08 AMWhat is the comboVal? and how Populate feilds? Thanks!
-
Wednesday, August 03, 2011 10:20 AM
Hi,
My example doesn't fit your problem exactly.
But heres a slighlty different example -
Type
private void button1_Click(object sender, RoutedEventArgs e) { Window window = new Window1(string Typr); window.WindowStartupLocation = WindowStartupLocation.CenterScreen; window.Show();}public Window1(string comboVal) { InitializeComponent();PopulateFeilds(type);
}
private void PopulateFeilds(type){
//Populate fields in here
label1.Content = type;
}
I hope that makes more sense?
-
Wednesday, August 03, 2011 10:59 AM
This code in the MainWindow.xaml.cs?
private void button1_Click(object sender, RoutedEventArgs e) { Window window = new Window1(string Type); window.WindowStartupLocation = WindowStartupLocation.CenterScreen; window.Show();}
This in Window1.cs?public Window1(string comboVal) { InitializeComponent(); PopulateFeilds(type);} private void PopulateFeilds(type) { //Populate fields in here label1.Content = type;}
-
Wednesday, August 03, 2011 11:02 AM
-
Wednesday, August 03, 2011 11:42 AM
Error then I change line to Window window = new Window1(string Type);
Invalid expression term 'string'
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.ComponentModel; namespace Equipment { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } MainWindow formTwo = App.Current.MainWindow as MainWindow; private void button1_Click(object sender, RoutedEventArgs e) { Window window = new Window1(); window.WindowStartupLocation = WindowStartupLocation.CenterScreen; window.Show(); } } public class ConfItem : INotifyPropertyChanged { // PropertyChanged event definition. public event PropertyChangedEventHandler PropertyChanged; // Private fields. string strType; string strVendor; string strSerialNum; // Public properties. public string Type { set { strType = value; OnPropertyChanged("Type"); } get { return strType; } } public string Vendor { set { strVendor = value; OnPropertyChanged("Vendor"); } get { return strVendor; } } public string SerialNum { set { strSerialNum = value; OnPropertyChanged("SerialNum"); } get { return strSerialNum; } } // Fire the PropertyChanged event. protected virtual void OnPropertyChanged(string strPropertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName)); } } } -
Wednesday, August 03, 2011 11:44 AM
Change this line -
Window window = new Window1(string Type);
to
Window window = new Window1(Type);
-
Wednesday, August 03, 2011 12:02 PMerror 'System.Type' is a 'type' but is used like a 'variable'
-
Wednesday, August 03, 2011 12:06 PM
The reason your getting this error is becuase you have a public property named Type but that is a reserved word in C#.
change the name of the public property Type to something else and then it should work.
something like this -
// Public properties. public string Type { set { strType = value; OnPropertyChanged("Type"); } get { return strType; }to// Public properties. public string StrType { set { strType = value; OnPropertyChanged("StrType"); } get { return strType; }Then change this line -
Window window = new Window1(Type);to
Window window = new Window1(strType);
-
Wednesday, August 03, 2011 12:47 PM
Yes, thanks! I changed the name of the public property Type. And this code changing too?
public Window1(string type)
{
InitializeComponent(); PopulateFeilds(type);}
private void
PopulateFeilds(type)
{
//Populate fields in here
label1.Content = type;}
-
Wednesday, August 03, 2011 12:49 PM
for now you can leave that as is. You can rename the type to strType but it should work even if you leave it "as is".
does it do what you require?
-
Wednesday, August 03, 2011 12:59 PM
private void PopulateFeilds(type)
Identifier expected -
Wednesday, August 03, 2011 1:03 PM
so what do you think the problem is here now?
I could give you the answer but I think its better for you (if you want to learn) that you try to resolve this particular issue yourself?
-
Wednesday, August 03, 2011 1:16 PMYes, but I don't understand what is the PopulateFeilds(type) ?
-
Wednesday, August 03, 2011 1:18 PMIts a method. Look into how you pass in parameters to a method
-
Wednesday, August 03, 2011 1:37 PM
to declare a method with parameters: static void MethodWithParameters(int n, string y)
public Window1(string type)
{
InitializeComponent(); PopulateFeilds(type);}
private void PopulateFeilds(type) - it's a method
{
//Populate fields in here
label1.Content = type;}
-
Wednesday, August 03, 2011 1:47 PM
Okay basically you just need to add string to the parameter -
private void PopulateFeilds(string type)
I hope you didnt think I was messing you around earlier. I think that if you look up issues yourself you learn something rather than someone force feeding you the answers.
Pritesh
-
Wednesday, August 03, 2011 2:13 PM
'System.Type' is a 'type' but is used like a 'variable'
Argument 1: cannot convert from 'System.Type' to 'string'
The best overloaded method match for 'Equipment.Window1.Window1(string)' has some invalid arguments
namespace Equipment { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } MainWindow formTwo = App.Current.MainWindow as MainWindow; private void button1_Click(object sender, RoutedEventArgs e) { Window window = new Window1(Type); window.WindowStartupLocation = WindowStartupLocation.CenterScreen; window.Show(); } } public class ConfItem : INotifyPropertyChanged { // PropertyChanged event definition. public event PropertyChangedEventHandler PropertyChanged; // Private fields. string strName; string strVendor; string strSerialNum; // Public properties. public string Name { set { strName = value; OnPropertyChanged("Name"); } get { return strName; } } public string Vendor { set { strVendor = value; OnPropertyChanged("Vendor"); } get { return strVendor; } } public string SerialNum { set { strSerialNum = value; OnPropertyChanged("SerialNum"); } get { return strSerialNum; } } // Fire the PropertyChanged event. protected virtual void OnPropertyChanged(string strPropertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName)); } } }namespace Equipment { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { public Window1(string type) { InitializeComponent(); PopulateFeilds(type); } private void PopulateFeilds(string type) { //Populate fields in here label1.Content = type; } } } -
Wednesday, August 03, 2011 2:15 PM
We have alrweady dealt with this problem previously.
in mainwindow you cannot use Type as a public property it is a a reserved word which is why you are getting this error. I thought you changed this so that the public property would be called Types or StrType?
-
Wednesday, August 03, 2011 2:31 PMWhy the label1.Content = type ?
-
Wednesday, August 03, 2011 2:33 PM
Okay,
I thuoght you wanted the user to complete some text fields (in the Mainform). Then when they cluick the button the second form (Window1) is displayed with the values they entered prepopulated.
'
so Label "Type" has the value the user entered in the textfield (on mainform). This is what I thought you meant by prepopulated?
-
Wednesday, August 03, 2011 2:53 PMYes, but my LabelName and combobox is empty
-
Wednesday, August 03, 2011 2:59 PM
You should be able to modify the code so that the fields gets prepopulated. Using what you already have, pass in extra parameters in the constructor of Mainform.
As for the combo box, is it already propluated with items? Or is it empty?
-
Wednesday, August 03, 2011 3:10 PMIn the Mainform I have 3 textBoxes - name, vendor, serial number. User must fill in these fields. In Window1 I have 2 combobox and 1 textbox. When user click button - open Window1, user click to combobox name, vendor and choose the item. Serial number populate automatically.
-
Wednesday, August 03, 2011 3:16 PM
so then this is quite simple, I told you to pass in Type (which is now StrType or Types) but instead pass in the value from serial number and then
private void button1_Click(object sender, RoutedEventArgs e) { Window window = new Window1(txtSerialNum); window.WindowStartupLocation = WindowStartupLocation.CenterScreen; window.Show();}Then you window1 code should look like -public Window1(string serialNum)
{
InitializeComponent(); PopulateFeilds(serialNum);}private void PopulateFeilds(string serialNum)
txtSerialNum.Text = serialNum;
{
//Populate fields in here
}
and then your done
-
Wednesday, August 03, 2011 3:33 PMWindow window = new Window1(txtSerialNum);
Argument 1: cannot convert from 'System.Windows.Controls.TextBox' to 'string' -
Wednesday, August 03, 2011 3:34 PM
change to
Window window = new Window1(txtSerialNum.Text);
-
Wednesday, August 03, 2011 3:43 PMYes! it's work, but user must choose a name and a vendor from the list (in combobox) that wrote in the Mainwindow, after that serial number populate auto.
-
Wednesday, August 03, 2011 3:45 PM
I give up. Maybe someone else can help you.
I don't know what you want and you are not that good (unfortunatly) at explaining yourself.
you keep talking about prepopulating but now you are saying this is incorrect and you need something different.
Maybe someone else on here can help you achieve your goal
-
Wednesday, August 03, 2011 3:52 PM
Now, serial number automatically filled in immediately. User must choose items from combobox (this items he input on form Mainwindow) after that serial number populate auto. Ok?
-
Wednesday, August 03, 2011 8:34 PM
Okay i think I'm getting you but you are going to have to answer some questions about this -
If I select an item in the combobox, where are you getting the serial number from? e.g. are you retrieving the serial number from a database etc.
Secondly can you post your code behind for MainWindow?
-
Wednesday, August 03, 2011 8:43 PM
Is there a whole project being done here :)
Lets make all the questions crisp and clear so that everyone benifits.
PufikYS could you list down what exactly you are trying to achieve and what problems that you are facing.
Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem -
Thursday, August 04, 2011 7:12 AMIn MainWindow have 3 text boxes-Name,vendor, serial number. The user enters the data in these fields.
The data from the fields Name, Vendor must fall within the drop-down list on the form of Window1.
After selecting the data (name and vendor) by user, Serial Number field is automatically filled. Ok? -
Thursday, August 04, 2011 7:20 AM
I understand that this is your requirement.
Kindly detail out the problem that you are facing now in implementing this
Please mark posts as answers/helpful if it answers your query. This would be helpful for others facing the same kind of problem -
Thursday, August 04, 2011 7:29 AM
I do not know how to make such data binding.
MineWindow.xaml.cs
namespace Equipment { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } MainWindow formTwo = App.Current.MainWindow as MainWindow; /* private void button1_Click(object sender, RoutedEventArgs e) { Window window = new Window1(Name); window.WindowStartupLocation = WindowStartupLocation.CenterScreen; window.Show(); }*/ private void button1_Click(object sender, RoutedEventArgs e) { Window window = new Window1(txtSerialNum.Text); window.WindowStartupLocation = WindowStartupLocation.CenterScreen; window.Show(); } } public class ConfItem : INotifyPropertyChanged { // PropertyChanged event definition. public event PropertyChangedEventHandler PropertyChanged; // Private fields. string strName; string strVendor; string strSerialNum; // Public properties. public string Name { set { strName = value; OnPropertyChanged("Name"); } get { return strName; } } public string Vendor { set { strVendor = value; OnPropertyChanged("Vendor"); } get { return strVendor; } } public string SerialNum { set { strSerialNum = value; OnPropertyChanged("SerialNum"); } get { return strSerialNum; } } // Fire the PropertyChanged event. protected virtual void OnPropertyChanged(string strPropertyName) { if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(strPropertyName)); } } }Window1.cs
namespace Equipment { /// <summary> /// Interaction logic for Window1.xaml /// </summary> public partial class Window1 : Window { public Window1(string serialNum) { InitializeComponent(); PopulateFeilds(serialNum); } private void PopulateFeilds(string serialNum) { //Populate fields in here txtSerialNum.Text = serialNum; } } }

