Call and load a bound data set in second form - c#

I bound a dataset in my main form to DataGridView source and using this code to call and loading last added record.
this.phoneTableAdapter1.Fill(this.sDs1.phone);
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1;
I have second form that include a save button. I cant use this code for call and show last record of DataGridView in main form. How to fix it? need a properties for define main form data set and use it in form2 or something else??

If second form opened from Main form, then pass main form reference to the second form through contructor
private MainForm _main;
public SecondForm(MainForm main)
{
_main = main;
}
Then use this constructor when open second form
private void OpenSecondForm()
{
SecondForm second = New SecondForm(this);
second.Show();
}
This is simple example. Give some more information how you used your forms(How second form created and showed).

Related

MenuStrip not updating when editing it from another form (C#)

I've been having an issue that's been bugging me for the past 3-4 hours.
Basically, I have a main form, that is an MDI parent. I have another form, that is an MDI child of that parent. I use the second form for logging in and after I've been logged in, I want to add new items to the menuStrip of that main form, but for some reason I can't. I tried debugging and I saw that the menuStrip's indexes are expanding correctly for the new items, but they are not being updated in the form. I tried displaying the main form again with this.Show. This worked! I got a new form displaying the new items in the menuStrip. However, I just want to update them and not create another form. I tried refreshing/updating, but nothing worked. :/ Any suggestions?
Thank you!
EDIT:
Here is some more info:
this is the code from the 2nd form
if (successLogin==DialogResult.OK || successLogin==DialogResult.Cancel)
{
Form1 main = new Form1(); //define main form
FullUser = textBox1.Text; //getting the username (will use it for the menuStrip items "Logged in as: "+ FullUser)
main.LogedIn(FullUser);//calls method
this.Close(); //closing the 2nd form
}
that calls for the public method in the main form
public void LogedIn(string user)
{
menuStrip1.Items.Add("Item here"); //adding the item to the menuStrip
}
Edit 2: I put a timer which starts when opening the 2nd form and checks for a public boolean. That got it working, but still not the way I wanted it to.
You are creating a new form of type Form1 in the line:
Form1 main = new Form1();
And adding the items to that new form, instead of your existing one.
If the form is the MDIParent of the second form as you state in your question, you could change that line to:
Form1 main = (Form1)this.MDIParent;
If it's not, then you need to keep a reference to the existing form. If you create a new one (like you are doing) then the items are added to that new one, but not to the one that is already created.

Get ComboBox selected value form Form1 to another Form2

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# ...

Can't close a form from other form

Maybe this is a really dumb problem, but I can't close a form.
This is what I'm trying to do:
Start Main form -> Open second form -> Open third form and close the second form..
I use this code to open the second form:
this.Hide();
System.Threading.Thread.Sleep(200);
pauzescreen p = new pauzescreen();
p.Show();
And I use this code to open the third form:
this.WindowState = FormWindowState.Maximized;
Form1 form1 = new Form1();
form1.TopMost = true;
form1.Show();
form1.Activate();
And then I close the second form with this code: (Here is the problem, this doesn't work..)
pauzescreen pauze = new pauzescreen();
pauze.Hide();
Can't explain it very well, but what it does it creates an fullscreen screen capture at the second form (Pauzeform) and at the third form you can select an region.
The second form and third form have no border and are maximized.
You are creating two separate references to two separate forms: p and pauze. To close the original form you would need to retain the reference and call Close() or Hide() on that:
pauzescreen p = new pauzescreen();
p.Show();
// other stuff
p.Hide();
I have a multiform application, and I'm quite fond of using the Program class.
static class Program
{
// declare the forms in the program member space
static Form1 firstForm;
static Form2 secondForm;
static Form3 thirdForm;
}
What I do is in the Main method, initialize the forms.
static void Main()
{
firstForm = new Form1()
// ... so on and so forth
}
Then, whenever you want to show or hide those forms, use
// To hide a form, use its Hide method
Program.firstForm.Hide()
// To show a form, use its Show method
Program.secondForm.Show()
in your code. It's worked in my applications just fine like that. :)
As Sid pointed out, the main problem is that you're creating a new instance of your second form when writing
pauzescreen p = new pauzescreen();
As you're trying to close it in a part of the code different from where your form's instantiation is, one fittable solution, instead of handling events, is to keep reference of your object by passing it as a parameter to the class or method attempting the closure.
Here is an example of how you could work with the same object in two different classes.

Getting listbox items to display to another form

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.

How to pass a value selected on an initial form to the main form?

I want to do something like the answer here:
How can I close a login form and show the main form without my application closing?
...but I want to pass a value selected on the initial form to the next (main) form. If I call an overridden constructor on the main form, where do I store the value in the meantime (between the initial form being dismissed and the main form being called)?
OTOH, if, instead of using the program.cs file to do this, I create the "initial form" inside the main form's Load() event (is there a better place), I could do something like this, but admittedly it seems rather kludgy:
0) Set the main form's size to 0,0 to hide it
1) Show the "initial" form/modal dialog, and store the value the user selects (in a button click event) in a form-global variable
2) Once the initial form/modal dialog closes, set the main form's size back to what it should be (unless modal result <> OK, in which case I close the main form and the app)
I know there's a better way to do this...
You don't have to pass a value to the main form. Just like your link explains, open your main form first. Then your main form can open the other form. This other form can place the information in a public property that the main form can access. Since the main form controls the lifetime of this other form, the main form gets the information held in the other form's public property, then closes the other form.
string myVariable;
using (OtherForm otherForm = new OtherForm())
{
otherForm.ShowDialog();
myVariable = otherForm.OtherVariable;
}
Try using ApplicationContext (System.Windows.Forms.ApplicationContext). You can show multiple forms as shown in the example in the following MSDN thread. And regarding data,you can have a common data object which is created once and the forms are instantiated with the data object passed to them before showing them.
http://msdn.microsoft.com/en-us/library/system.windows.forms.applicationcontext%28v=vs.100%29.aspx

Categories

Resources