I made a form for deleting/removing products.
Now my question is how do I get the value from one form to the other so I can use this to update the amount of products or delete the products from a database?
I'm trying to get the value of tbAantal.Text, so I can use this in my other form to update the amount of products.
Or should I do it in a other way?
public partial class Bevestiging : Form
{
public Bevestiging()
{
InitializeComponent();
Aantal = 0;
}
public int Aantal { get; set; }
private void btnOk_Click(object sender, EventArgs e)
{
int aantal;
if (!int.TryParse(tbAantal.Text, out aantal))
{
MessageBox.Show("U kunt alleen numerieke waardes invullen.", "Fout");
return;
}
}
private void btnCancel_Click(object sender, EventArgs e)
{
DialogResult = DialogResult.Cancel;
Close();
}
private void BtUp_Click(object sender, EventArgs e)
{
Aantal++;
tbAantal.Text = Aantal.ToString();
}
private void BtDown_Click(object sender, EventArgs e)
{
Aantal--;
tbAantal.Text = Aantal.ToString();
}
So I can use this to update it here:
private void gridGeregistreerd_ColumnButtonClick(object sender, ColumnActionEventArgs e)
{
var dialog = new Bevestiging();
if (DialogResult.OK != dialog.ShowDialog()) ;
}
You've already made the public property "Aantal" on your first form with the right get/set so to retrieve the value on your second form use this:
using (Bevestiging myForm = new Bevestiging())
{
DialogResult result = myForm.ShowDialog();
if (result != DialogResult.OK)
{
int returnedValue = myForm.Aantal;
}
}
In you form, you have already define a public property :
public partial class Bevestiging : Form
{
public int Aantal {
get; set;
}
}
Then in your callee form, you can access it :
private void gridGeregistreerd_ColumnButtonClick(object sender, ColumnActionEventArgs e)
{
var dialog = new Bevestiging();
if (DialogResult.OK != dialog.ShowDialog())
{
int aantal = dialog.Aantal;
/* Save it to database or whatever you want */
}
}
Related
I have a "MainForm" and a "GraphicsForm". Clicking "New" on the main form, a "GraphicsForm" will be created.
The problem is that when I create multiple "GraphicsForm", and when I want to save the content of one of the "GraphicsForm", I need to clicking "Save" on the "MainForm" and the program will save the content of the active "GraphicsForm" to a file, I don't know how to pass the content of this "GraphicsForm" to "MainForm" for storage.
MainForm.cs
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private ToolStripMenuItem _winMenuItem = new ToolStripMenuItem();
private GraphicsForm _graphicsForm;
private int _counter = 1;
private void New_Click(objec sender, EventArgs e)
{
_winMenuItem.Name = "Win";
_winMenuItem.Text = "Windows";
int item = MainMenuStrip.Items.IndexOf(_winMenuItem);
if (item == -1)
{
MainMenuStrip.Items.Add(_winMenuItem);
MainMenuStrip.MdiWindowListItem = _winMenuItem;
}
_graphicsForm = new GraphicsForm();
_graphicsForm.Name = string.Concat("Win_", _counter.ToString());
_graphicsForm.Text = _graphicsForm.Name;
_graphicsForm.MdiParent = this;
_graphicsForm.Show();
_graphicsForm.WindowState = FormWindowState.Maximized;
_counter++;
}
private void Save_Click(object sender, EventArgs e)
{
... // Problem here
}
private void Open_Click(object sender, EventArgs e)
{
... // Problem here
}
}
GraphicsForm.cs
public partial class GraphicsForm : Form
{
//StorageDoc is a class to manage all the graphics drawn by the user in the form.
private StorageDoc _storageDoc = new StotageDoc();
public GraphicsForm()
{
InitializeComponent();
}
private Canvas_MouseDown()
{
}
private Canvas_Paint()
{
}
...
Because MainForm is a MDI form, it is easy to use ActiveMdiChild to get the active child form.
class MainForm : Form
{
public void OnSaveButtonClick(object sender, EventArgs e)
{
if(ActiveMdiChild is GraphicsForm g)
Save(g);
}
}
I'm sure this has been answered before but basically, you pass in an instance of the 'data storage' to the new form.
interface ISaveForm
{
void Save();
}
class MainForm
{
private DataStorage _dataStorage;
private ICollection<ISaveForm> _forms = new List<ISaveForm>();
public void OnNew()
{
var subForm = new GraphicsForm(_dataStorage);
subForm.Show();
_forms.Add(subForm);
}
public void OnSave()
{
foreach(var form in _forms)
{
form.Save();
}
}
}
class GraphicsForm : Form,ISaveForm
{
private DataStorage _dataStorage;
public GraphicsForm(DataStorage dataStorage)
{
_dataStorage = dataStorage;
}
public void Save()
{
}
}
my problem is that i can't transfer values in the list between two classes in WFA
public partial class Example : Form
{
public List<string> myList = new List<string>();
private void Btn1_Click(object sender, EventArgs e)
{
new Example2().Show();
}
private void Example_Activated(object sender, EventArgs e)
{
if (myList.Count == 0)
{
//...
}
else
{
//...
}
}
public partial class Example2 : Form
{
static Example ex = new Example();
private void Btn2_Click(object sender, EventArgs e)
{
ex.myList.Add("something");
Close();
}
Form "Example" is shown first. Then i click "Btn1" on the screen, and form "Example2" appears. When i click "Btn2" on "Example2" form, myList should get new value of "something" and "Example2" closes. But this part of script
private void Example_Activated(object sender, EventArgs e)
{
if (myList.Count == 0)
{
//...
}
else
{
//...
}
}
shows that myList has no values (myList.Count equals 0).
What can i do?
you need to reference existing Example form with data from Example2 form. static Example ex is another (static) instance, which doesn't have data.
public partial class Example : Form
{
private List<string> myList = new List<string>();
private void Btn1_Click(object sender, EventArgs e)
{
var e2 = new Example2();
e2.SetMainForm(this);
e2.Show();
}
public void AddItem(string item)
{
myList.Add(item);
}
private void Example_Activated(object sender, EventArgs e)
{
if (myList.Count == 0)
{
//...
}
else
{
//...
}
}
}
public partial class Example2 : Form
{
private Example ex;
private void Btn2_Click(object sender, EventArgs e)
{
ex.AddItem("something");
Close();
}
public void SetMainForm(Example e1)
{
ex = e1;
}
}
I would like to send my Datagridview values to my Combobox in my mainform.
This is my code in my Datagridview form :
private void EquipmentList_Load(object sender, EventArgs e)
{
DataTable.RowCount = 30;
//Beam Description default value
DataTable.Rows[0].Cells[1].Value = "8.000m x 0.400m x 0.550m";
}
Below code is what i've used but nothing happened :
private void MainForm_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
EquipmentList ELform = new EquipmentList();
//Beam Combo Box default values
var BCBi1 = ELform.DataTable.Rows[0].Cells[1].Value.ToString();
this.BeamCB.Items.Add(BCBi1);
}
When i run it i'm getting this error:
error when running
An advance thank you for all those who will help me.
DataTable.Rows[0].Cells[1].Value is assigned in EquipmentList form when it is loaded.
EquipmentList ELform = new EquipmentList(); - here form is created but not loaded (you need to invoke Show or ShowDialog for that).
I suggest to declare publicly accessible default value (and not rely on existance of DataTable object with 1 row, which is a low-level implementation detail)
public const string DefaultSize = "8.000m x 0.400m x 0.550m";
private void EquipmentList_Load(object sender, EventArgs e)
{
DataTable.RowCount = 30;
DataTable.Rows[0].Cells[1].Value = DefaultSize;
}
and then
private void MainForm_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
this.BeamCB.Items.Add(EquipmentList.DefaultSize);
}
To the question "So is there any possible to load that values wihtout clicking/showing my equipmentlist form." Absolutely. Start by building a class for your data:
public class gridviewdata
{
public string content { get; set; }
public int row { get; set; }
public int cell { get; set; }
public gridviewdata(string content, int row, int cell)
{
this.content = content;
this.row = row;
this.cell = cell;
}
}
add a mediator class:
public static class mediator
{
public static List<gridviewdata> gridviewdatalist;
}
then add this code to your ELform load event:
private void EquipmentList_Load(object sender, EventArgs e)
{
DataTable.RowCount = 30;
//Beam Description default value
DataTable.Rows[0].Cells[1].Value = "8.000m x 0.400m x 0.550m";
mediator.gridviewdatalist.Add(new gridviewdata("8.000m x 0.400m x 0.550m", 0, 1);
}
then add a new method identifying your specific row to your MainForm:
private gridviewdata selectdata(int row, int cell)
{
foreach(gridviewdata data in mediator.gridviewdatalist)
{
if(data.row == row && data.cell == cell)
{
return data;
}
}
return null;
}
And finally call that code in your MainForm:
private void MainForm_Load(object sender, EventArgs e)
{
this.KeyPreview = true;
//and call it there
var BCBi1 = selectdata(0, 1);
this.BeamCB.Items.Add(new Item(BCDi1, 1)); //Add value and index
}
Hope this helps.
I write code which get my data source data from a gridview in this form. But I can't find a way to change my data and save my context. Because my savebtn is on another form.
Here is my code:
namespace FactorEntity
{
public partial class CustomerResearchForm : MetroFramework.Forms.MetroForm
{
FactorEntities contex;
public CustomerResearchForm()
{
InitializeComponent();
}
private void CustomerResearchForm_Load(object sender, EventArgs e)
{
}
private void CResearchGrid_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
CustomerUpdateAndDelete CustomerUpdateAndDelete = new CustomerUpdateAndDelete();
contex = new FactorEntities();
{ var sendergrid=(DataGridView)sender;
int customercode = Convert.ToInt32(sendergrid.Rows[e.RowIndex].Cells[1].Value);
var customer = from _customer in contex.tblCustomers where
_customer.CustomerCode==customercode select _customer;
CustomerUpdateAndDelete.tblCustomerBindingSource.DataSource = customer.ToList();
CustomerUpdateAndDelete.Show();
if (CustomerUpdateAndDelete.tblCustomerBindingNavigatorSaveItem.CheckOnClick == true)
{
contex.SaveChanges();
}
}
}
}
}
And this is my CustomerUpdateAndDelete form which tblCustomerBindingNavigatorSaveItem is here
namespace FactorEntity
{
public partial class CustomerUpdateAndDelete : MetroFramework.Forms.MetroForm
{
public CustomerUpdateAndDelete()
{
InitializeComponent();
}
private void CustomerUpdateAndDelete_Load(object sender, EventArgs e)
{
}
private void tblCustomerBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
}
}
}
I have to send the value of 2 TextBoxes from FormB when I clic on the "validate buton" to the datagridView in FormA;this is what I try to code:
FormB:
namespace RibbonDemo.Fichier
{
public partial class NvFamillImmo : Form
{
public event BetweenFormEventHandler BetweenForm;
SqlDataAdapter dr;
DataSet ds = new DataSet();
string req;
public NvFamillImmo()
{
InitializeComponent();
affich();
}
private void button2_Click(object sender, EventArgs e) //the validate buton
{
if (BetweenForm != null)
BetweenForm(textBox1.Text, textBox2.Text);
}
private void fillByToolStripButton_Click(object sender, EventArgs e)
{
try
{
this.amortissementFiscalTableAdapter.FillBy(this.mainDataSet.amortissementFiscal);
}
catch (System.Exception ex)
{
System.Windows.Forms.MessageBox.Show(ex.Message);
}
}
}
}
and this is FormA:
namespace RibbonDemo.Fichier
{
public delegate void BetweenFormEventHandler(string txtbox1value, string txtbox2value);
public partial class FammileImm : Form
{
private NvFamillImmo nvFamillImmo;
public FammileImm()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
NvFamillImmo frm2 = new NvFamillImmo();
frm2.BetweenForm += frm2_BetweenForm;
frm2.ShowDialog();
}
void frm2_BetweenForm(string txtbox1value, string txtbox2value)
{
//dataGridView1.Refresh();
String str1 = nvFamillImmo.textBox1.Text.ToString();
this.dataGridView1.Rows[0].Cells[0].Value = str1;
}
}
}
EDIT:I filled the method frm2_BetwwenForm but now I get a problem in reference
thanks for Help
No Need to create event for that. you can create properties in second form where you want to send value from existing form. for example if you have two forms FormA and FormB then FormB should contains properties like Value1 and Value2.
//FormB
public class FormB :Form
{
public string Value1{get; set;}
public string Value2{get; set;}
}
Now you can assign value into both properties from FormA.
//FormA
public void button1_click(object sender, EventArgs e)
{
FormB myForm = new FormB();
myForm.Value1 = textBox1.Text;
myForm.Value2 = textBox1.Text;
myForm.Show();
}
Then you can get both textboxes value into FormB. You can handle value into Form Load event
//FormB
public void FormB_Load(object sender, EventArgs e)
{
string fromTextBox1 = this.Value1;
string formTextBox2 = this.Value2;
}
If the FormB is already loaded and want to send value from FormA then create a method UpdateValues() and modify the properties to call that method.
//FormB
string _value1 = string.Empty;
public string Value1
{
get { return _value1; }
set {
_value1 = value;
UpdateValues();
}
}
string _value2 = string.Empty;
public string Value1
{
get { return _value2; }
set {
_value2 = value;
UpdateValues();
}
}
private void UpdateValues()
{
string fromTextBox1 = this.Value1;
string fromTextBox2 = this.Value2;
}
and Assign the values in FormB.Value1 and FormB.Value2 properties from FormA.
//FormA
FormB myForm = new FormB();
public void button1_click(object sender, EventArgs e)
{
if (myForm != null && myForm.IsDisposed == false)
{
myForm.Value1 = textBox1.Text;
myForm.Value2 = textBox1.Text;
}
}
When the value is updated from FormA then UpdateValues() method will be called.