I have a form called SelectCatagoryId which contains a DataGridView. I want to pass the selected cellvalue from here to another form called Add_product.
This is the code inside the button event in SelectcatagoryId.
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
String dgv;
dgv = dataGridView1.CurrentCell.Value.ToString();
Add_product ap = new Add_product(dgv);
this.Close();
}
This is the code inside Add_product form
namespace Supermarket
{
public partial class Add_product : Form
{
public Add_product()
{
InitializeComponent();
}
SqlConnection con;
string file;
public Add_product(String datagridvalue)
{
// MessageBox.Show(datagridvalue);
textBox10.Text = Convert.ToString(datagridvalue);
}
MessageBox is working correctly,value of dgv in form selectCatagoryId is passed correctly to form Add_product, but I can't assign the value of variable datagridvalue to textBox10.Text.
Before assign text to textBox you need to show form Add_product.
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
String dgv;
dgv = dataGridView1.CurrentCell.Value.ToString();
Add_product ap = new Add_product();
ap.Show();
ap.SetProduct(dgv);
this.Close();
}
...
Also add this method to Add_product class
public SetProduct(String datagridvalue)
{
// MessageBox.Show(datagridvalue);
textBox10.Text = datagridvalue;
}
Even with you are creating a overload of constructor, you need to InitializeComponent() so all the instance of the controls are created.
public Add_product(String datagridvalue)
{
InitializeComponent();
// MessageBox.Show(datagridvalue);
textBox10.Text = Convert.ToString(datagridvalue);
}
By default all the UI Component are initiated within the constructor of the form.
And Visual Studio generates all the Initialization in the InitializeComponent() method.
Related
I am using Windows Form Application. I have created on form for listing and on Add button I have create new window. When new record is adding datasource is updating but gridview not displaying last added record. Why this is happening?
public MainForm()
{
InitializeComponent();
BindCompanyData();
}
public void BindCompanyData()
{
List<CompanyListModel> companyListModel = new List<CompanyListModel>();
companyListModel = _obiClient.GetCompanies();
companyDataGrid.DataSource = null;
companyDataGrid.DataSource = companyListModel;
companyDataGrid.Refresh();
companyDataGrid.CellClick += new DataGridViewCellEventHandler(DatGridCell_Click);
}
private void btn_addCompany_Click(object sender, EventArgs e)
{
CompanyAddEdit companyAddEdit = new CompanyAddEdit();
companyAddEdit.ShowForm();
}
On button add It open new form. and on close that form I have called BindCompanyData() method.
private void btn_save_Click(object sender, EventArgs e)
{
string selectedItem = cmbbx_companyType.SelectedItem.ToString();
WriteXML(selectedItem);
this.Close();
MainForm mainForm = new MainForm();
mainForm.BindCompanyData();
}
What is missing?
Your problem is you are running BindCompanyData(); on newly created form with your code:
MainForm mainForm = new MainForm();
mainForm.BindCompanyData();
What you should do is inside CompanyAddEdit constructor request for MainForm form parameter and pass your current form which you use in button. So your code look like this:
//Inside CompanyAddEdit form
class CompanyAddEdit : Form
{
MainForm passedForm;
public CompanyAddEdit(MainForm form)
{
this.passedForm = form;
}
//other code
private void btn_save_Click(object sender, EventArgs e)
{
string selectedItem = cmbbx_companyType.SelectedItem.ToString();
WriteXML(selectedItem);
this.Close();
passedForm.BindCompanyData();
}
}
//Inside main form
private void btn_addCompany_Click(object sender, EventArgs e)
{
CompanyAddEdit companyAddEdit = new CompanyAddEdit(this);
companyAddEdit.ShowForm();
}
I have set up my program so that the user can enter a new line into the combo box via a text box on a separate form (Popup form). So far the program allows the new entry and closes the popup form when the user presses the "Accept" button however the entry does not appear in the combobox and the entry is not saved.
Currently the only way to view the new entry is by the .ShowDialog(); function which opens a second instance of the first form.
Form 2
namespace RRAS
{
public partial class NewRFRPopup : Form
{
public NewRFRPopup()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnAccept_Click(object sender, EventArgs e)
{
formRRAS main = new formRRAS();
string newRFR = txtNewRFR.Text;
main.AddRFR(newRFR);
this.Close();
main.ShowDialog();
}
private void NewRFRPopup_Load(object sender, EventArgs e)
{
}
}
}
AddRFR in Form 1
public void AddRFR(object item)
{
cmbRFR.Items.Add(item);
}
you are creating a new instance of your form1 in the accept handler:
formRRAS main = new formRRAS();
(which is why when you call showdialog you get another formRRAS appearing).
You need to pass the original formRRAS to the popup and call AddRFR on the instance passed through. I'd pass it on the constructor of the popup - i.e.
public partial class NewRFRPopup : Form
{
formRRAS _main;
public NewRFRPopup(formRRAS main)
{
InitializeComponent();
_main = main;
}
and then in your Accept handler:
string newRFR = txtNewRFR.Text;
_main.AddRFR(newRFR);
this.Close();
and of course to show the popup from formRRAS
NewRFRPopup popup = new NewRFRPopup (this);
popup.ShowDialog();
Ok, am going to try and ask this without sounding dumb. I have a user form that I create dynamic textboxes inside a panel. I want to reference these textboxes or the panel from another form to send the texbox data to excel. I need to know how I can reference these controls. Thank you in advance!!
Create public properties on the user control, one for each element you want to access.
public class MyControl : UserControl
{
public string Name
{
get { return textBoxName.Text; }
}
public string Address
{
get { return textBoxAddress.Text; }
}
...
Then use those from the parent control that hosts the user control.
string name = myControl1.Name;
string address = myControl.Address;
Assume you have Form1 that dynamically adds a TextBox to panel panel1 during the load event like this:
private void Form1_Load(object sender, EventArgs e)
{
panel1.Controls.Add(new TextBox());
}
And you have Form2 from which you want to access the data on panel1. On Form2 you can add a public field to store the reference to panel1, but you can call it whatever you want. In this case, I use sourcePanel:
public partial class Form2 : Form
{
public Panel sourcePanel;
public Form2()
{
InitializeComponent();
}
}
Then you can pass the panel1 reference to any new Form2 instance when the new instance is created, for example from a button click event on Form1:
private void button1_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2();
f2.sourcePanel = panel1;
f2.Show();
}
Then on Form2, you can access all the values in the TextBoxes with code like this:
private void Form2_Load(object sender, EventArgs e)
{
foreach (TextBox txt in sourcePanel.Controls.OfType<TextBox>())
{
System.Diagnostics.Debug.WriteLine(txt.Text);
}
}
I have 2 forms. The second form is opened from a method in the first form and I wish to be able to update the textbox that exists within that second form.
Basically I have the following code:
private void sendAllButton_Click(object sender, EventArgs e)
{
SendConsoleGUI sendOutGUI = new SendConsoleGUI();
sendOutGUI.Show();
sendOutGUI.sendConsoleTextBox.Text = "Test";
}
When I press the button the second form (SendConsoleGUI form) opens but "Test" is never added to its textbox.
Am I doing something wrong here?
You need to use invoke method.
sendOutGUI.Invoke((MethodInvoker) delegate { sendOutGUI.sendConsoleTextBox.Text = "Test"; });
public partial class ParentForm : Form
{
public ParentForm()
{
InitializeComponent();
}
private void sendAllButton_Click(object sender, EventArgs e)
{
SendConsoleGUI sendOutGUI = new SendConsoleGUI("Test");
sendOutGUI.Show();
}
}
public partial class ChildForm : Form
{
public ChildForm(string str)
{
InitializeComponent();
sendConsoleTextBox.Text = str;
}
}
This will work for you if you only wish to update it when the ChildForm is initially created.
I have a MainForm and AnotherForm. AnotherForm is accessed via MainForm's menuItem.
AnotherForm has listView. When user clicks on an item it I want to get the string element and pass it to MainForm's textbox, so the element shows there and AnotherForm is closed. So far AnotherForm closes but nothing shows in the textbox in MainForm. Any suggestions?
private void listView1_ItemActivate(object sender, EventArgs e)
{
string input = listView1.SelectedItem[0].ToString();
MainForm mainF = new MainForm(input);// called the constructor
this.Close(); //close this form and pass the input to MainForm
mainF.inputTextBox.Text = input;
mainF.loadThis(input);
}
I assume you have an instance of MainForm already, and that's what creates an instance of AnotherForm.
Inside the event you posted, you're actually creating an entirely new instance of MainForm, never showing it, and then it's destroyed anyway when AnotherForm closes.
The reason you see nothing in the text box is because you're looking at the original instance of MainForm, which you haven't actually changed.
One quickie way of fixing this would be passing a reference to the original MainForm into AnotherForm:
public class AnotherForm
{
private MainForm mainF;
public AnotherForm(MainForm mainF)
{
this.mainF = mainF;
}
...
...
private void listView1_ItemActivate(object sender, EventArgs e)
{
...
mainF.inputTextBox.Text = input;
...
}
}
Note: Instead of having AnotherForm aware of MainForm, you might want to switch it around and create a public property in AnotherForm like this:
public class AnotherForm
{
public InputValue { get; private set; }
private void listView1_ItemActivate(object sender, EventArgs e)
{
...
InputValue = input;
...
}
}
Which you can then access from MainForm when the other form is closed:
private void SomeMethodInMainForm()
{
var newAnotherForm = new AnotherForm();
newAnotherForm.ShowDialog();
var inputValueFromAnotherForm = newAnotherForm.InputValue;
// do something with the input value from "AnotherForm"
}
If your MainForm has already been created you cannot just create another one in order to access it and set properties. You've created two separate MainForms (though the 2nd one is hidden because you never showed it).
It sounds like what you want to do is a modal dialog pattern. Your MainForm is the main window in your application. You want to have a 2nd form pop up when you click on a menu link. This is called a dialog. Then when you close that dialog you want your MainForm to retrieve a value as a returned result of the dialog.
In your MainForm the event handler which handles the menu item click should look something like this:
private void pickSomethingMenuItem_Click(object sender, EventArgs e)
{
using (var picker = new PickerDialog())
{
if (picker.ShowDialog(this) == DialogResult.OK)
{
LoadSomething(picker.SomethingPicked);
}
}
}
Then the following code would be inside your dialog form:
public string SomethingPicked { get; private set; }
private void somethingListView_ItemActivate(object sender, EventArgs e)
{
SomethingPicked = somethingListView.SelectedItem[0].ToString();
DialogResult = DialogResult.OK;
}
Notice how I named all of the objects with meaningful names. Well, except for "Something". It was impossible to tell from your code what you were actually using the dialog to pick. You should always use meaningful names for your objects and variables. Your code is almost completely nonsensical.
And you should almost never make a control on a Form public like you have with your inputTextBox. You should always expose values you want to share as public properties.
On this presented solution, you could do five main things in order to achieve what you want to do, namely:
1) Declare a global object for AnotherForm in MainForm
2) Initiate a FromClosing event handler for AnotherForm in MainForm
3) Make a public property or field in AnotherForm
4) Before closing in AnotherForm you save it the public property mentioned above
5) In the MainForm get the public property from AnotherForm
Here is the code:
MainForm
public partial class MainForm : Form
{
AnotherForm anotherForm; // Declare a global object for AnotherForm
public MainForm()
{
InitializeComponent();
}
private void showToolStripMenuItem_Click(object sender, EventArgs e)
{
anotherForm = new AnotherForm(); // when Menu Item is clicked instantiate the Form
anotherForm.FormClosing += new FormClosingEventHandler(anotherForm_FormClosing); // Add a FormClosing event Handler
anotherForm.ShowDialog();
}
void anotherForm_FormClosing(object sender, FormClosingEventArgs e)
{
inputTextBox.Text = anotherForm.listViewValue; // get the Value from public property in AnotherForm
}
}
AnotherForm
void listView1_ItemActivate(object sender, EventArgs e)
{
listViewValue = listView1.SelectedItems[0].Text; // Get the listViewItem value and save to public property
this.Close(); // Close
}
public String listViewValue { get; set; } // public property to store the ListView value
One thing to note here in comparison to your code I didn't use ToString() in ListView.SelectedItems:
listView1.SelectedItems[0].ToString();
But instead use the Text Property:
listView1.SelectedItems[0].Text;