How to create a full access from: Form 2 to Form1
So i can use all Textboxes, Datagridviews and the given information from my From1 in my second Form2
My Plan : User choose a Item in my DataGridView and then automatically my Second Form open, where all informations are given in Textboxes and so on... the user can modify them and save them into my SQL Database, Form2 closed and Form1 opens again
I look at Stackoverflow and google but i dont find a soulution, working for me ...
Assume you have person class:
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
And list of persons bound to grid
List<Person> people = GetPeople();
peopleGridView.DataSource = people;
When you double-click on some row, get data bound person and pass it to second form:
private void peopleGridView_DoubleClick(object sender, EventArgs e)
{
if (peopleGridView.CurrentRow == null)
return;
Person person = (Person)peopleGridView.CurrentRow.DataBoundItem;
using (var editForm = new PersonDetailsForm(person))
{
if (editForm.ShowDialog() != DialogResult.OK)
return;
// get updated person data and save them to database
UpdatePerson(editForm.Person);
}
}
In edit form display person data in controls (you can use data-binding also):
public partial class PersonDetailsForm : Form
{
public PersonEditForm(Person person)
{
InitializeComponent();
idLabel.Text = person.Id.ToString();
nameTextBox.Text = person.Name;
// etc
}
public Person Person
{
return new Person {
Id = Int32.Parse(idLabel.Text),
Name = nameTextBox.Text
};
}
}
Benefits - you can change PersonEditForm independently - add/remove controls, change their types, adding data binding etc without changing your main form.
you can create a constructor in your Form2 that takes the parameters that will fill your controls for example:
public Form2(string property1, List<object1> objects)
{
textbox1.text = property1;
gridview1.DataSource = objects;
//and so on
}
and then call them from form1
Form2 form = new Form2(string1,list1);
form.Open();
or you can pass a single object to the constuctor and extend its properties in Form2
Transfer all necessary data to a third class and pass the instance as a parameter to form2.
Related
I'm having trouble transporting information from one form to another, is to make a single save, but the information is distributed in 2 forms and I have to do it using dto. I know that for this I have to send the data that I want by the form builder method, as you can see in the code below:
public FrmModalFornecedor(int providerId, int providerDoc)
{
InitializeComponent();
CbxListarFornecedor();
providerDoc = Convert.ToInt32(txtDoc.Text);
providerId = Convert.ToInt32(((Provider)cbxFornecedor.SelectedItem).ProviderId);
}
But now my questions are:
How to make these variables take their respective text box and combo box values?
How to make the next form have access to this data?
You can create additional properties in the second form and you can pass the values from first form to second form.
Hope this gives some ideas for you.
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
frm.MyName = "Pass my name here";
frm.Show();
}
public partial class Form2 : Form
{
public string MyName { get; set }
public Form2()
{
InitializeComponent();
}
}
I currently have two forms, one to display information when a user is selected from the listbox(the listbox lists names, when selected it will fill a few textboxes I have, one for city and another for address), the second form allows me to input the information for the user, which when I click submit will display them in my listbox on form1. Currently I am able to add the user from my second form to my first form into the listbox, but I am having issues filling their information in the textbox whenever I click on their names in my listbox.
As of now I have tried implementing different code snippets, but being a beginner I'm not sure how to do this.
My first form is as follows
public Form1()
{
InitializeComponent();
}
private void ButtonAddUser_Click(object sender, EventArgs e)
{
Form2 form = new Form2(textBoxFirstName.Text, listBoxUsers);
form.Owner = this;
form.ShowDialog();
form.Show();
}
private void listBoxUser_SelectedIndexChanged(object sender, EventArgs e)
{
if (listBoxUser.SelectedIndex != -1)
{
User selected = (User)listBoxUser.SelectedItem;
textBoxStreet.Text = selected.Street;
textBoxCity.Text = selected.City;
}
}
My second form where I add the users information is as follows
public partial class Form2 : Form
{
private ListBox _listBoxUsers;
public Form(string value, ListBox listBoxUser)
{
InitializeComponent();
value=($"{textBoxFirstName.Text} {textBoxLastName.Text}");
_listBoxUsers = listBoxUsers;
}
private void ButtonSubmit_Click(object sender, EventArgs e)
{
_listBoxUsers.Items.Add($"{textBoxFirstName.Text}
{textBoxLastName.Text}");
this.Close();
}
}
And my Class where I am trying to store the textbox information
public class User : EventArgs
{
public string Street {get; set;}
public string City {get;set;}
public User(string street, string city)
{
Street = street;
City = city;
}
}
In Short: I'm trying to save information from my second form into my class, and when I select a user from my listbox it will display his street and city into textboxes (my listbox and textboxes are both on my first form.).
Thanks for any help
In the second form you should be creating User object and fill the details like street and city .
private void ButtonSubmit_Click(object sender, EventArgs e)
{
User user = new User(textBoxFirstName.Text, textBoxLastName.Text);
_listBoxUsers.Items.Add(user);
this.Close();
}
Since Listbox.Items expecting object type, you can add anything which is derived from System.Object. But in the form1 you have created list with User Objects and during selected index changed you are type casting as User Object. But in the form2 you have not actually inserted User object during the submit button click .
Because of this, i think you are facing this problem . Try with above code and check
I would suggest decoupling state management from the presentation. For example, try to create a separate class for User that is not derived from EventArgs. And manage its state inside a separate class - for start int will be in-memory storage. But as you flesh out your implementation you can latter move your data to Database with ease as it will not rely on UI and its elements for storage and management.
using c# - WinForms, .net Framework 4.5, VS 2012
Try to create small app with some entity.
I create separate class for my entity and put some simple code inside:
public class Car
{
public string Color {get; set;}
public string Make { get; set; }
public string CarModel { get; set; }
}
Then from main form i create some specimen of class Car (creating can be geted by clicking button from main form, after clicking new form with 3 text boxes will be opened, if information entered and button Ok clicked - new Car sample must be created and returned to main form).
For this i try to use next code:
public Car myCar = new Car();
private void buttonAdd_Click(object sender, EventArgs e)
{
myCar.Color = textBoxColor.Text;
myCar.Make = textBoxMake.Text;
myCar.CarModel = textBoxModel.Text;
this.DialogResult = DialogResult.OK;
this.Close();
MessageBox.Show("Added");
this.Close();
}
For moving data from new form to main form I use public field public Car myCar = new Car();, but this is not the best way to do this, due to using of public field.
Another way I found - in main form create next method
static List<Car> carInStock = null;
public static void myCar(string color, string make, string model)
{
Car myCar = new Car
{
Color = color,
CarModel = model,
Make = make
};
MainForm.carInStock.Add(myNewCar);
}
and for button can use method like:
private void buttonAdd_Click(object sender, EventArgs e)
{
MainForm.myCar(textBoxColor.Text,
textBoxMake.Text,
textBoxModel.Text);
MessageBox.Show("Added");
this.Close();
}
But think varian also not hte best and prefered.
Question: What is the best way to move created entity (in this case entity of Car, represented as myCar) from one form to another?
For this kind of GUI Application, I suggest you follow MVC or MVP pattern. The class car is the model, the Windows Forms are the views, the view doesn't hold an instance of the model, and the views are updated through controller or presenter.
You can find more details about MVC/MVP here
I am trying to the value of properties during run time. First I am trying to do it with the name property for a person but I am not sure how to complete the if statement down below. I would like it to reopen my Form2 which takes in the student as a parameter and it will display all the details for them. Any help or advice would be much appreciated. If I have done this completely wrong, please let me know give me some guidance or advice on how to edit a property data in runtime.
public class Student :IPerson
{
//Method that changes the original name
public string ChangeName(string newForename)
{
_forename = newForename;
return _forename;
}
}
public partial class Edit_Details : Form
{
public Edit_Details(Student student)
{
InitializeComponent();
nameLabel.Text = student.Forename;
student.ChangeName(editNameTextBox.Text);
//if(savebutton.Click) //how can I get this to replace the old name with the new one when the button is pressed??
}
}
in
public Edit_Details(Student student)
{
InitializeComponent();
nameLabel.Text = student.Forename;
savebutton.Click+=(sender,e)=>
{
savebutton.Text=student.ChangeName(editNameTextBox.Text);
};
}
What I'm trying to do it load some information from a database.
To do this I open a form that lists everything that can be loaded.
When you click load I want to pass the ID back to the original form.
However I can't seem to be able to call a method in that form.
Any help would be appreciated.
I would flip this around:
Make the selection form into a modal dialog that is created and displayed by the form where you want to load something
Expose the selection made in the dialog through a property or method in the dialog form
This way the selection form will be decoupled from the caller, and can be reused wherever it makes sense without the need to modify it.
In the selection dialog form class:
public string GetSelectedId()
{
return whateverIdThatWasSelected;
}
In the calling form:
using(var dlg = new SelectionDialogForm())
{
if (dlg.ShowDialog() == DialogResult.OK)
{
DoSomethingWithSelectedId(dlg.GetSelectedId());
}
}
You could add a property to your form class and reference it from your other form.
eg.
public class FormA : Form
{
private string _YourProperty = string.empty;
public string YourProperty
{
get
{
return _YourProperty;
}
set
{
_YourProperty = value;
}
}
}
public class FormB : Form
{
public void ButtonClick(object sender, EventArgs args)
{
using (FormA oForm = new FormA)
{
if (oForm.ShowDialog() == DialogResult.OK)
{
string Variable = oForm.YourProperty;
}
}
}
You just need to set your property on a button click on form A then you can access it from form B
}
Why not create a public property for the selected item in the dialog form, something like this.
public int SelectedItemId {get;private set;}
//In your item selected code, like button click handler..
this.SelectedItemId = someValue;
Then just open the form as a Dialog
//Open the child form
using (var form = new ChildForm())
{
if (form.ShowDialog(this) == DialogResult.OK)
{
var result = form.SelectedItemId;//Process here..
}
}
The proper way to do this is to introduce a Controller class which is used by both forms. You can then use a property on the Controller, when that is set will trigger the NotifiyPropertyChanged event.
see INotifyPropertyChanged for more info