Add to listview from another form - c#

I am wondering how I can update my listview in form1 by entering data via textboxes in form2. My code works fine if i put all the text boxes on the same form etc.
I figured I needed some reference to the first form on 2nd but can't get it working.
Any tips for putting me in the right direction would be nice, also any tips for any better way of doing this.
This is the code I have so far:
Form1:
public partial class form1 : Form
{
public form1()
{
InitializeComponent();
}
public ListView MyListView
{
get
{
return taskList;
}
}
Form2:
public partial class form2 : Form
{
public form2()
{
InitializeComponent();
}
form1 f;
public add(form1 f)
{
this.f = f;
}
public void AddToList()
{
ListViewItem item1 = new ListViewItem(txtName.Text);
item1.SubItems.Add(txtEmail.Text);
item1.SubItems.Add(txtPhone.Text);
f.MyListView.Items.AddRange(new ListViewItem[] { item1 });
}

The most straight forward way of doing things would be to use events. You could add an event on form2 that would fire each time an item is added, and includes the text to be inserted (you have multiple pieces of information, so a custom data type would be appropriate). You can then add a handler method to form2 which adds the item to its ListView. You then tie the two together in the code that is creating the two forms, and life should be good.
So, to provide some code, First up is the data structure for the event:
public delegate void HandleItemAdded(object sender, ItemAddedEventArgs e);
public struct ItemAddedEventArgs : EventArgs
{
public string Name;
public string Email;
public string Phone;
public ItemAddedEventArgs(string name, string email, string phone)
{
Name = name;
Email = email;
Phone = phone;
}
}
Then we have the event code on form2
public event HandleItemAdded ItemAdded;
// .. some other stuff
public void RaiseItemAdded(ItemAddedEventArgs e)
{
if(ItemAdded != null)
ItemAdded(this,e);
}
// ... now for your AddToList
public void AddToList()
{
RaiseItemAdded(new ItemAddedEventArgs(txtName.Text,txtEmail.Text, txtPhone.Text);
}
And now we can add a handler in form1
public void HandleItemAdded(object sender, ItemAddedEventArgs e)
{
ListViewItem item1 = new ListViewItem(txtName.Text);
item1.SubItems.Add(txtEmail.Text);
item1.SubItems.Add(txtPhone.Text);
MyListView.Add(item1);
}
And last but not least we need to tie them together
//...not sure what your code looks like, but we'll assume we have instances of the two forms named form1Form and form2Form
form2Form.ItemAdded += form1Form.HandleItemAdded

the listview control should be private, instead add a public method to your form that contains the listview control, which receives the data you want to insert and inserts it into the listview.

If form2 is not created by and displayed by form1, you're not going to have a reference to call. In that case, things are going to get a bit more interesting from a communication standpoint. When that happens, you'll need to use an eventing model to get the information from one place to another.

Related

Singleton: Form instance returning null

I have a windows Forms application with one form and a few classes.
I want to get the values of some textBoxes from the Form1 instance and extract the values.
My first way of achieving this was by using Application.OpenForms[] array to get the form but I realised that using a singleton on the class Form1 would be more efficient as I can have direct access and it would be impossible to make other instances.
Here is how I have set it up:
1. Controls class to gets controls from Form1
class Controls
{
//Request Form1 instance
private static Form1 form = Form1.GetInstance();
//Sets global values for easy access with getters and null setters
//--Variable 'form' is still null hence I get the NullReferenceException
private TextBox employer = form.Controls["textBoxEmployerName"] as TextBox;
private TextBox role = form.Controls["textBoxRole"] as TextBox;
private TextBox company = form.Controls["textBoxCompanyName"] as TextBox;
private TextBox website = form.Controls["textBoxWebsite"] as TextBox;
private TextBox refNumber = form.Controls["textBoxRefNumber"] as TextBox;
private TextBox reason = form.Controls["textBoxReason"] as TextBox;
private TextBox dateListed = form.Controls["textBoxDateListed"] as TextBox;
private Label charLimit = form.Controls["labelCharsRemaining"] as Label;
public TextBox Employer { get { return employer; } }
public TextBox Role { get { return role; } }
public TextBox Company { get { return company; } }
public TextBox Website { get { return website; } }
public TextBox RefNumber { get { return refNumber; } }
public TextBox Reason { get { return reason; } }
public TextBox DateListed { get { return dateListed; } }
public Label CharLimit { get { return charLimit; } }
}
}
2. Singleton set up inside class Form1
public partial class Form1 : Form
{
private static Form1 theInstance;
public Form1()
{
InitializeComponent();
}
//Return instance of Form1
//--This is obviously returning null for some reason
public static Form1 GetInstance()
{
if (theInstance == null)
theInstance = new Form1();
return theInstance;
}
As you can probably see I am getting the "NullReferenceException" when I attempt to get the Singleton from class Form1.
The following methods I have used are as follows:
Using Windows.OpenForms["Form1"].Controls["--somecontrol--"]
Using Windows.ActiveForm
Using a Singleton Design Pattern on class Form1
All of these ways are returning null and I cant think of a reason why it is returning null.
Any help would be appreaciated.
Thankyou
I want to get the values of some textBoxes from the Form1 instance and extract the values.
This is where you need to stop and re-think your approach. Forms represent views of your data; however, your data itself needs to be in the model, a separate place independent of the views.
Text boxes need to reflect the state of some model object, such as a Person object that has string properties for employer, company, role, web site, and so on. The form would read from that object's properties, display them in a text box, and then react to text box changes, and save values back to the model Person object.
If you make Person a singleton, or provide some other universal way of accessing it, you would be able to access person's properties from all forms, without accessing the forms themselves.

TextBox B in Form2 to display text from TextBox A in Form 1 using C# and WinForms

After entering in the appropriate text and pressing enter, I want txtbA text in Form1 to display in txtbB in Form 2.
I already have the key events code written, but I can't seem to figure out the rest.
Visual basic seems to be more straightforward with this, and I am new to C#.
This is using WinForms.
I am really only familiar with visual basic's way of handling this:
txtbA.text = My.Forms.Form2.txbB.text
Thank you for any help you can give!
You can use a static variable
In Form1
public static string txtbAtext
{
get { return txtbAtext ; }
set {txtbAtext = value; }
}
OnTextChanged Event
txtAtext = txtA.Text;
In Form2
Form1 f1 = new Form1();
txtbB.Text = Form1.txtAText;
The simpliest and not very elegant way is to do ist like this:
Make a property in the second form, that can hold a Textbox and set the Text property in the TextChange event of the property in Form2
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public TextBox TextBoxWithSameText { get; set; }
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (TextBoxWithSameText != null)
TextBoxWithSameText.Text = textBox2.Text;
}
}
All you have to do now is to set the property to the Form1.textbox1 when the second form (Form2) is created:
Form2 form = new Form2();
form.TextBoxWithSameText = textbox1;
In order to exchange strings between two Forms classes, you can use a third class. Once added to your project, you need to instantiate this new Class1 in your Form1 and Form2:
Class1 class = new Class1;
Make sure Class1 has a constructor. Now, you can create properties in your Class1, with get and set properties:
public string TextA
{
get
{
return textA;
}
set
{
textA = value;
}
}
You can then call these properties from any other class that has instantiated your Class1, like so:
class.TextA = txtbA.Text;
In order to show the text in the other textbox, you need an event to trigger changing the txtbB.Text value. You could use txtbA.ValueChanged event, or a button with a Click event. You should figure out what event is most appropriate for your project.
I hope this helped! Good luck.
Based on your comment:
Right, Form1 will open an instance of Form2 upon pressing the Enter
key. Thanks!
You can setup Form2 to receive the initial value for the TextBox via its Constructor:
public partial class Form2 : Form
{
public Form2(string InitialValue)
{
InitializeComponent();
this.txtbB.Text = InitialValue;
}
}
Then in Form1 you'd do something like:
Form2 f2 = new Form2(this.txtbA.Text);
f2.Show();

Passing Data back to First Form from Second Form in C#

I have two Forms. Say FormA, FormB.
From FormA i called FormB using
frmB.Show();
In FormB, i've two textboxes and a combobox controls.
Assume User enters some data in those two textboxes and selected an item from combobox in Form2 and clicked an OK button.
After the click i want those textboxes user entered values, combobox selected item value back to FormA.
How can i achieve this using C#.
I approached the simple way .. little embellishments..
create public variables in your form class i.e.
public string passVariable1 = "";
if you have text boxes go to properties, then click on the lightning bolt and double click on the empty TextChanged eventhandler. This will create a code snippet in the code begind that gets executed when the content of the textbox changed .. in this code block assign the content of the text box to your corresponding public variable.
i.e. my public variable was
public string issue = "";
private void txtIssue_TextChanged(object sender, EventArgs e)
{
issue = txtIssue.Text;
}
Add a button and create a click event for this button (simply double click on the button in the design pane). In the click event code block set the dilog result to ok and hide the
this.DialogResult = DialogResult.OK;
this.Hide();
In the parent window code check on the dialog result and grab the form data from the child form's public variables
if (f.ShowDialog() == DialogResult.OK)
{
string b = f.issue;
string e = f.year;
string f = f.month;
};
In the scenario that you are describing I would probably call frmB.ShowDialog() rather than frmB.Show().
// Inside FormA open frmB as a modal dialog waiting for
// OK or Cancel result using the following statement
if (frmB.ShowDialog() == DialogResult.OK)
{
// Retrieve selected values from frmB here (while frmB is still not disposed)
}
The benefits of ShowDialog() are that you:
Get the return value from the form easily allowing you to determine that OK (rather than cancel) was clicked to close it.
The form is not immediately disposed when closed thus allowing you to retrieve the values that you want.
By opening frmB as a modal dialog you avoid having to check for the complexities that may occur if your user starts interacting with formA while frmB is open.
NOTE: When designing frmB you have to set the DialogResult property of the OK button-control to DialogResult.OK in order for the form to return the correct DialogResult when this button is pressed (alternatively can also set this.DialogResult in the OK button's Click event handler)
Or you could pass an object from FormA to FormB and bind its properties to the controls in FormB. If you want FormA to be notified when you click OK button you could declare an event in your data container class, subscribe to it in FormA and fire it from FormB.
Be DataContainer some class you define
public class DataContainer
{
public event EventHandler AcceptedChanges;
protected virtual void OnAcceptedChanges()
{
if ((this.AcceptedChanges != null))
{
this.AcceptedChanges(this, EventArgs.Empty);
}
}
public void AcceptChanges()
{
this.OnAcceptedChanges();
}
public string Text1 { get; set; }
public string Text2 { get; set; }
}
in FormA:
private void button4_Click(object sender, EventArgs e)
{
DataContainer data = new DataContainer();
data.Text1 = "text1";
data.Text1 = "text2";
Form2 frm = new Form2();
frm.Data = new DataContainer();
data.AcceptedChanges += new EventHandler(data_AcceptedChanges);
frm.Show();
}
void data_AcceptedChanges(object sender, EventArgs e)
{
// your code here
}
and in FormB:
public DataContainer Data { get; set; }
private void Form2_Load(object sender, EventArgs e)
{
textBox1.DataBindings.Add(new Binding("Text", Data, "Text1"));
textBox2.DataBindings.Add(new Binding("Text", Data, "Text2"));
}
private void button1_Click(object sender, EventArgs e)
{
Data.AcceptChanges();
}
You should also implement INotifyPropertyChanging and INotifyPropertyChanged on DataContainer class to play nice with bindings.
You can create an EventHandler on FormB which FormA will subscribe to. Also, add a couple of public properties to FormB that represent that data that you want FormA to be able to use. Then, when FormB fires off the event, FormA will know to refresh his data.
Note: The key principle in this example is implementing an EventHandler (you can create your own event handler type) which notifies FormA when data is ready to be refreshed/viewed/etc. Hopefully, this example will allow you to see how you might implement an event handler for your particular situation.
Example:
FormA -
public partial class FormA : Form
{
//FormA has a private instance of FormB
private FormB formB = null;
public FormA()
{
InitializeComponent();
}
void formB_OnDataAvailable(object sender, EventArgs e)
{
//Event handler for when FormB fires off the event
this.label1.Text = string.Format("Text1: {0}\r\nText2: {1}",
formB.Text1, formB.Text2);
}
private void InitializeFormB()
{
this.formB = new FormB();
//FormA subscribes to FormB's event
formB.OnDataAvailable += new EventHandler(formB_OnDataAvailable);
}
private void button1_Click(object sender, EventArgs e)
{
this.InitializeFormB();
formB.Show();
}
}
FormB -
public partial class FormB : Form
{
//Event that fires when data is available
public event EventHandler OnDataAvailable;
//Properties that expose FormB's data
public string Text1 { get; private set; }
public string Text2 { get; private set; }
public FormB()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
//Set the exposed properties, then fire off the event.
this.Text1 = this.textBox1.Text;
this.Text2 = this.textBox2.Text;
if (OnDataAvailable != null)
OnDataAvailable(this, EventArgs.Empty);
}
}
A dirty, but also fastest solution is to make those controls public. This you can do by adding the word public in the Form2.Decisgner.cs file. If FormA has a member variable or local variable of FormB, you can access the control (say TextBox1) with:
frmB.TextBox1.Text
which is now accessible outside FormB too.
How about using events and delegates? See this http://colinmackay.scot/2005/04/22/passing-values-between-forms-in-net/
I was having this same issue and came up with an idea which is a bit different. In my scenario, I am making a flashcard program for my youngest two children and I wanted to be able to carry back the answer provided to the parent form (new child form for each new flashcard question so that the parent form can update how many are left, how many correct, how many incorrect, etc.) without having to add values to a database. Seems to be overkill for something that should be simple. What I did was to create a class with 3 of each variable type. I figured three of each type would be sufficient for most jobs.
This is an example of my new class:
namespace ClassNamespace
{
public class ValueHolder
{
public int intValue1 { get; set; }
public int intValue2 { get; set; }
public int intValue3 { get; set; }
public long longValue1 { get; set; }
.
.
.
}
}
I create a new ValueHolder (ValueHolder vh;) from parent form and pass it to the child form. In the child form I create a new ValueHolder and then set it equal to the ValueHolder object sent in the child form's class constructor. Now, when the enter key is pressed (answer given), I can set vh.intValue1 equal to this.answerBox.text;... well, I have to use int.tryparse(); but you get the idea. I then only need to reference vh.intValue1 from the parent form to get the value entered.
Parent form:
for (int i = 0; i < limit; i++)
{
ValueHolder vh = new ValueHolder();
ChildClass cc = new ChildClass(vh);
MessageBox.Show(vh.intValue1.ToString()); //to test that it works
}
and child form:
ValueHolder vh;
public ChildClass (ValueHolder vhIncoming)
{
vh = vhIncoming;
}
private void answerBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
vh.intValue1 = 1234;
}
}
This seems to be the simplest solution for my scenario. I know that this question is old, but wanted to add this option for anyone in a similar position. Just add the class to your project, add more of a type or more types to the class as needed, rinse repeat for future projects.
If its on the same page you should be able to read it directly from your controls as Textbox1.Text, Textbox2.Text, Combobox.SelectedValue (i guess)
But if its on different pages use Session variables like:
Session["date1"] = TextBox1.Text;
Session["date2"] = TextBox2.Text;
Session["comboValue"] = Combobox.SelectedValue;
and use them to populate your form
This would depend on how you normally design your applications.
You could work by using a event
driven system where you would create
events and delegates. Mentioned by #Dave81
Or you could create properties that
return the given/selected values so
that the parent can retrieve them
from the object (Wanted to say Dialog
but not sure about what your using).
Or you can follow #zmilojko and just
set them public, which is basically
the same as creating properties but
more to the dark side of coding
practices :D
All these would work but it all depends on how you like your applications to be structured.

Accessing Form's Control from Custom Control

I want to access the list box and add the item into it for my Custom control which is dynamically created on run time. I want to add the Item when I press the button place in the custom control, but it does not work. I have use the following code to work:
private void button1_Click(object sender, EventArgs e)
{
Form1 frm = new Form1();
frm.ABC = "HI";
}
the 'ABC' is the Public string on the form ie:
public string ABC
{
set { listBox1.Items.Add (value); }
}
the above string works fine when I use it form the Button on the form and it adds the value in the lsitbox but whent I use it form the custom control's button the text of the 'value' changes but it does not add the item in list box.I have also try it on tabel but does not help. I change the Modifires of the ListBox1 from Private to Public but it does not works. The above function works well in the form but cannot work from the custom control.
Thanks.
Expose an event ("ItemAdded" or whatever) in the child form that your main form can handle. Pass the data to any event subscribers through an EventArgs derived object. Now your mainform can update the UI as it please with no tight coupling between the two classes. One class should not know about the UI layout of another, it's a bad habit to get into (one that everyone seems to suggest when this question crops up).
What I think you should use is
this.ParentForm
So in your case it should be:
public string ABC
{
set { this.ParentForm.listBox1.Items.Add (value); }
}
The easiest way would be to pass the form down into your custom control as a parameter in the constructor that way you could access it from the custom control.
EX:
public class CustomControl
{
private Form1 _form;
public CustomControl(Form1 form)
{
_form = form;
}
private void button1_Click(object sender, EventArgs e)
{
_form.ABC = "HI";
}
}

How to change text in a textbox on another form in Visual C#?

In Visual C# when I click a button, I want to load another form. But before that form loads, I want to fill the textboxes with some text. I tried to put some commands to do this before showing the form, but I get an error saying the textbox is inaccessible due to its protection level.
How can I set the textbox in a form before I show it?
private void button2_Click(object sender, EventArgs e)
{
fixgame changeCards = new fixgame();
changeCards.p1c1val.text = "3";
changeCards.Show();
}
When you create the new form in the button click event handler, you instantiate a new form object and then call its show method.
Once you have the form object you can also call any other methods or properties that are present on that class, including a property that sets the value of the textbox.
So, the code below adds a property to the Form2 class that sets your textbox (where textbox1 is the name of your textbox). I prefer this method method over making the TextBox itself visible by modifying its access modifier because it gives you better encapsulation, ensuring you have control over how the textbox is used.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public string TextBoxValue
{
get { return textBox1.Text;}
set { textBox1.Text = value;}
}
}
And in the button click event of the first form you can just have code like:
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.TextBoxValue = "SomeValue";
form2.Show();
}
You can set "Modifiers" property of TextBox control to "Public"
Try this.. :)
Form1 f1 = (Form1)Application.OpenForms["Form1"];
TextBox tb = (TextBox)f1.Controls["TextBox1"];
tb.Text = "Value";
I know this was long time ago, but seems to be a pretty popular subject with many duplicate questions. Now I had a similar situation where I had a class that was called from other classes with many separate threads and I had to update one specific form from all these other threads. So creating a delegate event handler was the answer.
The solution that worked for me:
I created an event in the class I wanted to do the update on another form. (First of course I instantiated the form (called SubAsstToolTipWindow) in the class.)
Then I used this event (ToolTipShow) to create an event handler on the form I wanted to update the label on. Worked like a charm.
I used this description to devise my own code below in the class that does the update:
public static class SubAsstToolTip
{
private static SubAsstToolTipWindow ttip = new SubAsstToolTipWindow();
public delegate void ToolTipShowEventHandler();
public static event ToolTipShowEventHandler ToolTipShow;
public static void Show()
{
// This is a static boolean that I set here but is accessible from the form.
Vars.MyToolTipIsOn = true;
if (ToolTipShow != null)
{
ToolTipShow();
}
}
public static void Hide()
{
// This is a static boolean that I set here but is accessible from the form.
Vars.MyToolTipIsOn = false;
if (ToolTipShow != null)
{
ToolTipShow();
}
}
}
Then the code in my form that was updated:
public partial class SubAsstToolTipWindow : Form
{
public SubAsstToolTipWindow()
{
InitializeComponent();
// Right after initializing create the event handler that
// traps the event in the class
SubAsstToolTip.ToolTipShow += SubAsstToolTip_ToolTipShow;
}
private void SubAsstToolTip_ToolTipShow()
{
if (Vars.MyToolTipIsOn) // This boolean is a static one that I set in the other class.
{
// Call other private method on the form or do whatever
ShowToolTip(Vars.MyToolTipText, Vars.MyToolTipX, Vars.MyToolTipY);
}
else
{
HideToolTip();
}
}
I hope this helps many of you still running into the same situation.
In the designer code-behind file simply change the declaration of the text box from the default:
private System.Windows.Forms.TextBox textBox1;
to:
protected System.Windows.Forms.TextBox textBox1;
The protected keyword is a member access modifier. A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member (for more info, see this link).
I also had the same doubt, So I searched on internet and found a good way to pass variable values in between forms in C#, It is simple that I expected. It is nothing, but to assign a variable in the first Form and you can access that variable from any form. I have created a video tutorial on 'How to pass values to a form'
Go to the below link to see the Video Tutorial.
Passing Textbox Text to another form in Visual C#
private void button1_Click(object sender, EventArgs e)
{
Form2 frm = new Form2();
TextBox txt = (TextBox)frm.Controls.Find("p1c1val", true)[0];
txt.Text = "foo";
}

Categories

Resources