So I have a ListView that is populated with information and edit button in form1.
Now when i select item in ListView and click edit, I want it to bring me to form2 and display selected items data.
For example, I select name from ListView hit edit it brings me to form2 where is TextBox, lets say textbox1 = Name and textbox2 = Age and in my List<data> have 2 items in it name and age.
What i need now is, when I select one of the ListView items and pres edit it opens form2 and loads name in textbox1 and age in textbox2 how can I do that ?
I was trying listview_SelectedIndexChanged event like this :
Form2 edit = new Form2();
edit.textBox1.Text = People[this.listView1.SelectedItems[0].Index].Name;
edit.textBox2.Text = People[this.listView1.SelectedItems[0].Index].Age;
Is there any way to reference this to button click edit? or is there another way?
and how do I save changes afterwards?
Pass in the value in the form of constructor .
In your main form where you have your listbox store the value of the listbox selected value in a variable and then in your button edit event call the constructor of the 2nd form
Form2 child= new Form2(Name,Age);
Form2.show()
In the 2nd form create global variables for name and age
private string _name = String.Empty;
private int _age;
Then create a constructor for your 2nd form
public void Form2(string name, int age)
{
InitializeComponent();
this._name = name;
this._age = age;
}
Use Properties to pass data to Form2 and Events to return the Saved Data:
Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 edit = new Form2();
edit.SaveEvent += new Form2.SaveEventHandler(edit_SaveEvent); //Add event handler
edit.name = People[this.listView1.SelectedItems[0].Index].Name;
edit.age = People[this.listView1.SelectedItems[0].Index].Age;
edit.ShowDialog(this);
}
void edit_SaveEvent(object sender, SaveEventArgs e)
{
//Do Your work here with e.newAge and e.newName
((Form2)sender).Close(); //Close Form2
}
Form2
public partial class Form2 : Form
{
public delegate void SaveEventHandler(object sender, SaveEventArgs e);
public event SaveEventHandler SaveEvent;
public string name { get; set; }
public string age { get; set; }
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
textBox1.Text = name;
textBox2.Text = age;
}
private void button1_Click(object sender, EventArgs e)
{
SaveEvent(this, new SaveEventArgs(textBox1.Text, textBox2.Text));
}
}
public class SaveEventArgs
{
public SaveEventArgs(string name, string age) {newName = name; newAge = age; }
public String newName {get; private set;} // readonly
public String newAge {get; private set;}
}
Use custom constructor for this
In Form 1
private void button1_Click(object sender, EventArgs e)
{
string name = People[this.listView1.SelectedItems[0].Index].Name;
int age = People[this.listView1.SelectedItems[0].Index].Age;
Form2 edit = new Form2(name,age);
edit.ShowDialog(this);
}
In Form 2
public form2(string name,int age)
{
textBox1.Text = name;
textBox2.Text = age.ToString();
}
Use oop concepts..It is nice.
Related
I am trying to do a big coding project however I hit a wall.
I need to show the name and score once the data has been entered in.
I tried using youtube tutorials, classes for the code. But no such luck.
Any help would be great!
form1:
private void bNew_Click(object sender, EventArgs e)
{
score link = new score();
link.Show();
SudentBox.Items.Clear();
}
form2:
public object StudentBox { get; private set; }
private void bCancel_Click(object sender, EventArgs e)
{
this.Close();
try
{
string name = txtName.Text;
int score = Convert.ToInt32(txtScore.Text);
txtStoreScores.Text += score.ToString() + " ";
}
catch (Exception x)
{
MessageBox.Show("Please enter a number");
}
}
private void bClearScores_Click(object sender, EventArgs e)
{
txtName.Text = "";
txtScore.Text = "";
txtStoreScores.Text = "";
}
Examples of what the forms should look like with the final result.
If I'm correct, you are trying to code a form of DialogBox.
Say, you want to get a name from the dialog (e.g. from a TextBox in Form2), you can have a model like this (in Form2 of course).
public string Name
{
//where myTextBox is the design name of your textbox
get => myTextBox.Text;
set => myTextBox.Text=value;
}
Simple Ok Button
public void OkBtnClick(object sender, EventArgs e)
{
this.Close();
}
Now, you need to actually get this info to display in your Form1. That is easy.
Just like you have started above:
private void bNew_Click(object sender, EventArgs e)
{
score link = new score();
link.ShowDialog();
//Note that you won't be able to access form1.
SudentBox.Items.Clear();
//You can now get the name
string _nameResult=link.Name;
NameTextbox.Text=_nameResult;
}
I hope this gets you started!
You do this by using the property.
Add public static property on Form 2 and set the values of the text to the property respectively and then access them on the Form 1.
On Form 2 in the Ok button click event do this
public static string Name { get; set; }
public static string Scores { get; set; }
private void bOk_Click(object sender, EventArgs e)
{
Name = txtName.Text;
Scores = txtStoreScores.TextBox;
}
Then in the Form 1 OnLoad event access those properties and display them in the TextBox
private Form1_Load (object sender, EventArgs e)
{
StudentBox.Items.Add(string.Format("{0} {1}", Form2.Name, Form2.Scores);
}
I have 2 forms. One have a textbox to show name of chosen Customer and one have datagridview to show list of customers.
When i click on textbox the second form will open. I want to know is it possible to double click on datagridview cell then the text of textbox change by datagridview selected cell on first form?
Here is my codes.
I wrote this code to open form2:
private void textBox1_Click(object sender, EventArgs e)
{
frmCustomer customer = new frmCustomer();
customer.ShowDialog();
}
and in form2 :
private void dataGridView1_CellDoubleClick(object sender,DataGridViewCellEventArgs e)
{
string name = dataGridView1.CurrentRow.Cells["clmName"].Value.ToString();
form1 f = new form1();
f.txtCustomer.Text = name;
this.close();
}
there is nothing in textbox when form2 closed.
any Help? Thanks a milion
You can try passing Form1 as a Parameter when creating the Form2.
private void textBox1_Click(object sender, EventArgs e)
{
frmCustomer customer = new frmCustomer(this); // this represents the Form1
customer.ShowDialog();
}
then on the form2
private Form1 frm_1;
public Form2(Form1 frm)
{
InitializeComponent();
frm_1 = frm;
}
private void dataGridView1_CellDoubleClick(object sender,DataGridViewCellEventArgs e)
{
string name = dataGridView1.CurrentRow.Cells["clmName"].Value.ToString();
frm_1.txtCustomer.Text = name;
this.close();
}
this will do the work.
Closing the second form does not dispose it off. First form will still have access to its public members.
The second form can have a public property which can be accessed by first form after it is closed.
Try the following:
first form should contain:
private void textBox1_Click(object sender, EventArgs e)
{
frmCustomer customer = new frmCustomer();
customer.ShowDialog();
textBox1.Text = customer.Name;
customer.Dispose();
}
second form should contain:
public string Name { get; private set; }
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
Name = dataGridView1.CurrentRow.Cells["clmName"].Value.ToString();
this.close();
}
I have two forms ,Form1 and Form2.
Form1 - Parent
Form2 - Child
Form1 Contains the following,
Textbox - it loads the file path,
Datagridview - it loads the file with its data,
ButtonNext -when button cliked it opens Form2,
Form2 Contains the following,
BrowseButton - it broswe for the file from the directory
Textbox - it then shows the path
ButtonFinish - it will tabes you back to Form1
*Now i want to access datagridview from Form1(Parent) from Form2(child). Now i can broswe the file on Form2 and when i click finish i can see my file path on Form1(parent) from the textbox but with no databeing loaded.
How can i load the data on Form1 into the datagridview ?
this is my code so far..
Form2.
public frmInputFile(frmMain_Page _frmMain)
{
InitializeComponent();
this._frmMain = _frmMain;
}
private void btnBrowse_Click(object sender, EventArgs e)
{
BrowseFile();
}
private void btnFinish_Click(object sender,EventArgs e)
{
_frmMain.SetFilepath(txtInputfile.Text);
_grid.Rows.Clear(); //cant get the grid from form1
string PathSelection = "";
if (txtInputfile.Text.Length > 0)
{
PathSelection = txtInputfile.Text;
}
oDataSet = new DataSet();
XmlReadMode omode = oDataSet.ReadXml(PathSelection);
for (int i = 0; i < oDataSet.Tables[2].Rows.Count; i++)
{
string comment = oDataSet.Tables["data"].Rows[i][2].ToString();
string font = Between(comment, "[Font]", "[/Font]");
string datestamp = Between(comment, "[DateStamp]", "[/DateStamp]");
string commentVal = Between(comment, "[Comment]", "[/Comment]");
string[] row = new string[] { oDataSet.Tables["data"].Rows[i][0].ToString(), oDataSet.Tables["data"].Rows[i][1].ToString(), font, datestamp, commentVal };
_grid.Rows.Add(row);
}
this.Hide();
Program._MainPage.Show();
Form1
private void btnLoadfile_Click(object sender, EventArgs e)
{
frmInputFile frmInput = new frmInputFile(this);
frmInput.Show();
}
public void SetFilepath(string Filepath)
{
txtInputfile.Text = Filepath;
}
//I dont know how i can handle the gridview here
public void Loadgrid(string LoadGrid)
{
Gridview_Input.ToString();
}
First things first. Please avoid duplicate posts.
What is the variable _grid doing here ?? Your way of passing data from one form to the other looks very strange. Nevertheless, I tried to simulate your problem and I am able to add the rows in Form1 from From2. The code listing is given below. Just a thing to note,I have added four columns in to my DataGridView in the designer. In your case you might want to add the columns as well programmatically.
public partial class Form2 : Form
{
public Form2(Form1 frm1)
{
InitializeComponent();
Form1Prop = frm1;
}
private void button1_Click(object sender, EventArgs e)
{
Form1Prop.SetFilepath("HIHI");
Form1Prop.DataGridPropGrid.Rows.Add("HIH", "KI", "LO", "PO");
}
public Form1 Form1Prop { get; set; }
}
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 frm2 = new Form2(this);
frm2.Show();
}
public void SetFilepath(string filepath)
{
textBox1.Text = filepath;
}
public DataGridView DataGridPropGrid
{
get
{
return dataGridView1;
}
}
}
Cheers
I have text boxes on my second form and a send button which is in the code shown below.
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.PassName = richTextBox1.Text;
f1.PassLastName = richTextBox2.Text;
f1.PassAge = comboBox1.Text;
f1.PassGender = richTextBox3.Text;
f1.ShowDialog();
}
and a DataGridView on form 1 with this code
public partial class Form1 : Form
{
private string name;
private string lastName;
private string age;
private string gender;
public string PassName
{
get { return name; }
set { name = value; }
}
public string PassLastName
{
get { return lastName; }
set { lastName = value; }
}
public string PassAge
{
get { return age; }
set { age = value; }
}
public string PassGender
{
get { return gender; }
set { gender = value; }
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = name;
dataGridView1.Rows[n].Cells[1].Value = lastName;
dataGridView1.Rows[n].Cells[2].Value = age;
dataGridView1.Rows[n].Cells[3].Value = gender;
}
private void mnuExit_Click(object sender, EventArgs e) //adding the quit on the top file with caution message
{
if (MessageBox.Show("Do you really want to Quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
Application.Exit();
}
}
private void addTask_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(); //show form2 so user can input data
f2.ShowDialog();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
}`
This is fine if I want to send one set of data to the DataGridView, but if I add new information again then this opens a new DataGridView and stores it to another seperate DataGridView then I have two DataGridView forms. I want to put all the data onto one DataGridView and keep adding rows. So when the user clicks the add button on the first form with the DataGridView, it opens up the TextBox form which is form 2, then the user fills out the information and clicks the send button which sends the information back to the DataGridView, however this then opens up a new window with a new DataGridView. I dont want this to happen I want it to keep adding rows on the first form.
Could someone show me how to do this?
You can use ShowDialog(this) and Owner to get the parent form's property.
Form1
private void Form1_Load(object sender, EventArgs e)
{
//Move to Form1_Activated
this.Activated += new System.EventHandler(this.Form1_Activated); //connect
}
private void Form1_Activated(object sender, EventArgs e)
{
int n = dataGridView1.Rows.Add();
dataGridView1.Rows[n].Cells[0].Value = name;
dataGridView1.Rows[n].Cells[1].Value = lastName;
dataGridView1.Rows[n].Cells[2].Value = age;
dataGridView1.Rows[n].Cells[3].Value = gender;
}
private void addTask_Click(object sender, EventArgs e)
{
Form2 f2 = new Form2(); //show form2 so user can input data
f2.ShowDialog(this);//set this form as Owner
}
Form2
private void button1_Click(object sender, EventArgs e)
{
Form1 f1 = (Form1)this.Owner;//Get the Owner form
f1.PassName = richTextBox1.Text;
f1.PassLastName = richTextBox2.Text;
f1.PassAge = comboBox1.Text;
f1.PassGender = richTextBox3.Text;
//f1.ShowDialog();
f1.Show();
this.Close();
}
I have a search form in my program. When the user double-clicks a cell (of the dgv) on the search form, I want the program to close that form and jump to the item on the main form.
I'm doing this by identifying each item with a unique id.
I'm trying to pass the value of the rows id to the other form. The problem is that, it says that I'm passing the value zero each time. But when I insert some message boxes on the search form, it says that the integer 'id' has successfully been assigned to the variable on the main form: public int CellDoubleClickValue { get; set; }
Here is my code:
Search Form:
private int id;
private void searchdgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
this.rowIndex1 = e.RowIndex;
this.id = Convert.ToInt32(this.searchdgv.Rows[this.rowIndex1].Cells["id"].Value);
invmain inv = new invmain();
inv.CellDoubleClickValue = this.id;
this.DialogResult = DialogResult.OK;
this.Close();
//MessageBox.Show(inv.CellDoubleClickValue.ToString());
//Above, it shows it got assigned successfully.
}
Main Form:
public int CellDoubleClickValue { get; set; }
private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
search form1 = new search();
form1.ShowDialog();
if (form1.DialogResult == DialogResult.OK)
{
MessageBox.Show(CellDoubleClickValue.ToString());
}//Here it shows that the value is: 0
I suggest you do as follows:
Search Form:
public int SelectedId { get; set; }
private void searchdgv_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
this.rowIndex1 = e.RowIndex;
this.SelectedId = Convert.ToInt32(this.searchdgv.Rows[this.rowIndex1].Cells["id"].Value);
this.DialogResult = DialogResult.OK;
this.Close();
}
Main form:
private void searchToolStripMenuItem_Click(object sender, EventArgs e)
{
search form1 = new search();
form1.ShowDialog();
if (form1.DialogResult == DialogResult.OK)
{
int selectedId = form1.SelectedId;
// Do whatever with selectedId...
}