I'm still new here so please consider. My question is that I have two forms, (form1 and form2). In form1 there's a listbox with customers names on it. Now what i want is when the user clicks a name on the listbox, a new form (form2) pops up and displays the rest of the information of the customer (age,address,phone number) on the textboxes. When I click a name on the listbox it displays the rest of the info but only on that form, I can't get it to display on another form. I'm coding it using Visual studio, C#. Any help would be appreciated. Thanks!
You have to pass information from form1 to form2 in one way or another.
If the data for customers is collected within a class, you can simply pass the object that corresponds to the selected index in the listbox.
For instance if your customer data in form1 is set up like this:
List<CustomerData> Customers { get; set; }
And each object in that list corresponds to its respective index in the listbox, then you would need a form2 constructor similar to this:
public form2(CustomerData customer){
// set all form data here based on the customer
}
Potentially, if you asign the passed object in form2, you can manipulate the object within form2 and it will automatically update in form1 as well (assuming it is a class).
Then create your listbox click event method and open the form, passing the selected customer:
if(listbox.SelectedIndex < 0) return;
form2 f = new form2(Customers[listbox.SelectedIndex]);
f.Show();
I hope this is what you were looking for. A bit difficult to understand from your original question.
Related
I have a C# windows app that has two forms Form1 which is the main form and Form2.
Form1 has a combobox on it and Form2 has a textbox.
I want to put the value selected in the Form1.ComboBox1 into Form2.TextBox1.
I am trying this:
Form1 Form1Object = new Form1();
string fff = Form1Object.ComboBox1.SelectedItem.ToString(); //not working
TextBox1.Text = fff;
Problem is that when I run this Form1 is reinitialized and i don't want that. (I have a splash screen that runt when the application starts so when i run my code the splashscreen starts all over again.
Is there a way to read ComboBox1 value without restarting the first form?
If I try it directly it does not work, it sees the Form1 as calss instead of object.
Form1.ComboBox1.SelectedItem.ToString(); //does not work
I am also trying to add the value to the textbox when opening the second form:
Form2 form2 = new Form2();
form2.TextBox1.Text = ComboBox1.SelectedValue.ToString();
form2.Show();
This gives me the following error: "Object reference not set to an instant of an object."
EDIT:
It works using this code:
Form2 form2 = new Form2();
form2.TextBox1.Text = ComboBox1.Text;
form2.Show();
Now my question still remains: If i am in Form2 can i still get the value from form1? If not, that is ok. I will post this as a solution.
While this is not the most proper answer, it is one way to solve the problem.
Form1
Add a method to get value
public string TransmitSelectedValue()
{
return ComboBox1.SelectedItem.ToString();
}
Form2
var myvalue = ((Form1)ParentForm.Controls.Find(Form1Name,true)).TransmitSelectedValue();
This type of question has been asked and answered many times, and in different versions.
I would suggest looking at a few of the following I have posted in the past...
This example shows two forms where second form is passed as a parameter the first form's instance. Then, from public methods exposed on the first, the second can call them to get the values. It is your discretion on if you want to allow setting from alternate source, or just allowing a get method... could be done as a property public get; protected set;
This stackoverflow search will show several links to posts I've done in the past with slightly altering versions between different forms.
FEEDBACK FROM COMMENT
There has to be something done in your FIRST form to call the second.. is it from a click button, or based on the actual combobox selection being changed. Whatever it is, the first Example I provided SHOULD be what you need. You are not having the second form call the first.
Without a full copy\paste of the first example, all you would need to really do is in the form 2 constructor is set your text as pulled from the first...
public Form2(Form1 viaParameters) : this()
{
this.textBox1.Text = viaParameters.Combobox1.SelectedItem;
}
however, I don't know how your items are defined.. dictionary, list, array, whatever.. so you may need to typecast to get the selected item via
if( viaParameters.Combobox1.SelectedIndex > -1 )
this.textBox1.Text = viaParameters.Combobox1.Items[ viaParameters.Combobox1.SelectedIndex ].WhateverStringValue;
This way, the start of form 2 from form 1 can grab the value directly.
If you expose the method from the first form via a property or method, your text value could be as simple as
this.textBox1.Text = viaParameters.YourForm1sMethodToGetStringFromCombobox();
i am not sure where the problem is
while starting/opening form2
like
Form2 f2 = new Form2();
f2.Show(this);
you have a reference to form1 as 'owner'
on form2 you can just put this on any event you want or on a button or whatever
Form1 f1 = Owner as Form1;
textBox1.Text = f1.comboBox1.SelectedItem.ToString();
converted to C# ...
Anytime I want to display a new form, I create a new form object and hide the current form.
For example:
this.Hide();
new Form2().Show();
In this way, a new object keeps getting created over and over, and the old form is still running but hidden somewhere.
I would like to know what would be the most appropriate way to do so, I've been doing this for long time and anytime I do this I feel like I am doing it wrong. I mean how can we access the previous object and set it back to show instead of creating a new one.
Use Application.OpenForms property to get already opened form object and show that.
The OpenForms property represents a read-only collection of forms
owned by the application. This collection can be searched by index
position or by the Name of the Form.
Form1 frm1 = Application.OpenForms["Form1"] as Form1 ;
if (frm1 != null)
{
frm1.Show();
}
I'm stuck, and couldn't find anything on the site to help me.
What I have:
I have a WinForms app, written in C# with Visual Studio 2010.
I have built a custom class with about 10 data fields and properties.
On my main form, I have declared a List<> for the housing of each object. It is declared on Form Level.
I have created a custom form (since MessageBox/Interaction.InputBox won't work) for data entry with mostly textboxes and DateTime elements.
Also has one ComboBox. Stuck at Customdialog_Load event handler - want to fill when form loads up.
Problem:
I need to feed the ComboBox with items when the form loads to enter data. It needs to feed from the List<> in the main form, from a specific properties, let's call it ClientName. If there are 50 clients listed in my List<>, then I want their Name Properties to populate the combobox Collection[].
Request:
Can anyone please advise on how to go about feeding a ComboBox which is not on the same form, preferably without duplicating data/List<>. I can do most of the other things, and the logic flow is all correct. I do know how to add items to a collection the normal way, on the same form.
Edit:
I can make a separate array for client names on the main form if I have to. But it HAS to be on the main form. So the combobox will still need to be populated from an array from a different form.
Edit 2:
I'm still 1st year at University. We haven't done DataBinding yet, but I do know that there are better ways to do what I want to accomplish, I just don't have the tools in my mind yet. I work way ahead of the class. Thank you for all the help, very quick! Will keep on trying!
When you create the instance of your second form, pass in the constructor the reference to your List and store it in a form level variable inside the second form. Then use it to initialize your combobox in that form load event
in your main form
List<Customer> myCustomerList;
.....
using(Customdialog f = new CustomDialog(myCustomerList))
{
if(DialogResult.OK == f.ShowDialog())
{
.....
}
}
in your CustomDialog class
public class CustomDialog
{
List<Customer> _customerList;
public CustomDialog(List<Customer> customers)
{
InitializeComponent();
_customerList = customers;
}
private void CustomDialog_Load(object sender, EventArgs e)
{
comboBox1.DataSource = _customerList;
comboBox1.ValueMember = "Id"; // Supposing the Customer object contains ID property
comboBox1.DisplayMember = "ClientName"; // The property shown on the combobox items
}
}
class MyDialog : Form
{
public IEnumerable<Whatever> Items
{
get { return _items; }
set
{
_items = value;
someComboBox.Items = value;
}
}
public MyDialog(IEnumerable<Whatever> items)
{
InitializeComponent();
Items = items;
}
}
Now you can pass them in via the constructor and/or separately at a later time.
If you are forced down the Winforms route then :( but it sounds like you need some advice on passing stuff around (and maybe databinding?)
Ideally you need to get the data across to the custom form (I assume you have a main form and a popup/another form that opens).
One way to do it is to create a property on the second form which is of the List<> type and assign it before you open the form... e.g.
in form2.cs
// an auto property will work well here
public List<YourClass> YourClassList { get; set;}
Then in your form1.cs
// When the user opens the second form
Form2 frm2 = new Form2();
frm2.YourClassList = yourClassListFromThisForm;
frm2.ShowDialog(); // etc
Are you also having a problem filling the box or using databinding?
To be honest, if you haven't done much work on this, I'd seriously consider moving to WPF - it's just so much better in every respect
I have two forms for submitting a skill update request.
Form 1: Is the request form, it contains a datagridview binded by a datatable (not binded to the database)
Form 2: The skills form, contains a datagridview control binded to my database, it lists all the skills.
What I want to happen is go be able to click add on form 1, open form 2 select a skill there and then when I click ok, skill details are transferred to form1. I can do it only once. I want to be able to send multiple skills in one request so the previous data must not be erased, however it does not happen because Form 2 opens another instance of the form every time.
Form.Activate() and Form.Show and Form.Update does not work. Help?
Open form2 link in form 1 to open form with list of skills.
private void AddSkills_Btn_Click(object sender, EventArgs e)
{
AddSkill SkillsForm = new AddSkill();
SkillsForm.Show();
From form2 (after selecting which skill the user wants to request)
SkillUpdateRequest SUpdateReq = new SkillUpdateRequest();
SUpdateReq.FName_txt.Text = fname;
SUpdateReq.EmpID_Txt.Text = EmpID;
SUpdateReq.MName_Txt.Text = mname;
SUpdateReq.LName_Txt.Text = lname;
SUpdateReq.EmpInitials_Txt.Text = LoggedUser;
SUpdateReq.Position_Txt.Text = PositionName;
SUpdateReq.SkillID_Txt.Text = SkillID;
SUpdateReq.SkillName_Txt.Text = SkillName;
SUpdateReq.SkillDescription_Txt.Text = SkillDesc;
SUpdateReq.DateSubmitted_Txt.Text = DateTime.Now.ToString();
SUpdateReq.GetEmpID(EmpID);
SUpdateReq.Update();
this.Close();
Without knowing the exact data types, lists, data table, whatever you are trying to pass between forms, these prior posts might help
One link, multiple form passing back and forth data / control
Another that even samples method calling to go back and forth with data / actions
HI,
Iam trying to get value's out of a datagridview.
this datagridview is on form one.
and where i want the value's is on form two;
but i dont want to do this :
[code]form1 frm = new from1();[/code]
because that form1 already exists so i dont want to create it again
can anytone plz help me get a solution for this
thank you very much
Please don't even try to do that. Store your data in a data container object that is shared between the two forms. Bind form1 to the data and access it from form2.
You can access other open forms using the OpenForms collection on Application:
Application.OpenForms
Then all you need to do is test for the type or name of the form and cast it to your second form to grab the reference, then you can access its properties etc.
However, grabbing pieces of information like this across forms is considered bad design. If the information can be aggregated out into something both forms can reference, this is better. Alternatively, if the forms need to interact based on the state of each of their data, consider creating events between the two forms.
in form1.designer.cs we have datagrid
public System.Windows.Forms.DataGridView GridOgrenci;
and form2 name yetkiler we can reach all form1 values
public partial class Yekiler : Form
{
Utils Utility = new Utils();
Form1 anaform = new Form1();
public Yekiler()
{
InitializeComponent();
}
public void Yekiler_Load(object sender, EventArgs e)
{
anaform = Application.OpenForms["Form1"] as Form1;
MessageBox.Show(anaform.GridOgrenci.ColumnCount.ToString());