|
my first approach said:
add a public Frm property, basically doing what GetInstance() did
which means it (creates if necessary) and returns a form; the Frm you concocted is putting a string, not getting a form.
That is quite different from what I meant.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
is doesn't work because frm is not initialised. You could call GetInstance() to make it work. Either...
frmChildForm.GetInstance().Enabled = true;
frmChildForm.GetInstance().Visible = true;
or...
frmChildForm form = frmChildForm.GetInstance();
form.Enabled = true;
form.Visible = true;
...Actually where is frmChildForm initialised?
Illogical thoughts make me ill
|
|
|
|
|
Thanks Musefan - your second suggestion did it for me.
|
|
|
|
|
As an addition to the first alternative given by Luc's reply, add a static constructor to initialize the private static frm field, so you will be able to get rid of the if (frm == null) statement every time the Frm property is called.
|
|
|
|
|
Ok, thanks for the replies - very helpful all.
Just to wrap this up, here is the working example of taking text from one form and appending it onto a second form.
frmParentForm has a textBox, plus two buttons now - one to send the text and one to hide/show the second form.
frmChildForm is the second form that has to have text appended. It has a textBox plus one button to close this form - if you close it, a new instance will be created next time you send to it.
frmParentForm code:-
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
<br />
namespace PassingDataExample<br />
{<br />
public partial class frmParentForm : Form<br />
{<br />
public frmParentForm()<br />
{<br />
InitializeComponent();<br />
}<br />
<br />
public void btnSendToNextForm_Click(object sender, EventArgs e)<br />
{<br />
frmChildForm form = frmChildForm.GetInstance();<br />
form.Enabled = true;<br />
form.Visible = false;<br />
form.Hide();<br />
form.textBox1.AppendText(textBox1.Text + "\r\n");<br />
}<br />
<br />
private void btnShowChild_Click(object sender, EventArgs e)<br />
{<br />
frmChildForm form = frmChildForm.GetInstance();<br />
if (form.Visible == false)<br />
{<br />
form.Visible = true;<br />
form.Show();<br />
}<br />
else<br />
{<br />
form.Visible = false;<br />
form.Hide();<br />
}<br />
}<br />
}<br />
}
frmChildForm code:-
using System;<br />
using System.Collections.Generic;<br />
using System.ComponentModel;<br />
using System.Data;<br />
using System.Drawing;<br />
using System.Linq;<br />
using System.Text;<br />
using System.Windows.Forms;<br />
<br />
namespace PassingDataExample<br />
{<br />
public partial class frmChildForm : Form<br />
{<br />
<br />
private static frmChildForm frm;<br />
<br />
public static frmChildForm GetInstance()<br />
{<br />
if (frm == null || frm.IsDisposed)<br />
{<br />
frm = new frmChildForm();<br />
}<br />
return frm;<br />
}<br />
<br />
private frmChildForm()<br />
{<br />
InitializeComponent();<br />
}<br />
<br />
private void btnClose_Click(object sender, EventArgs e)<br />
{<br />
this.Close();<br />
}<br />
}<br />
}
Hope this helps someone else - I do like practical examples.
modified on Tuesday, February 15, 2011 6:47 PM
|
|
|
|
|
Hi,
Assuming I have four items in my collection, i.e.
list.add("Method");
list.add("Method");
list.add("Method2");
list.add("Method");
I would like the list now to have method and Method2 and the rest should be removed.
How is this done please?
Thanks
|
|
|
|
|
Try this code :
public static void removeDuplicate(ArrayList arlList)
{
HashSet h = new HashSet(arlList);
arlList.clear();
arlList.addAll(h);
}
Hope this helps.
All the best.
|
|
|
|
|
I really would like to have list colection instead of hashtable because my data is inside a list collection at present.
Thanks
|
|
|
|
|
Where are you getting HashSet from? as System.Collections.Generic.HashSet does not compute
Illogical thoughts make me ill
|
|
|
|
|
|
I tested in .Net 4 originally. But I have just set target to 3.5 and it still complains...
1. HashSet requires a type argument
2. (when 1 is addressed) best overload method has some invalid arguments (i.e. cannot pass ArrayList to constructor
Illogical thoughts make me ill
|
|
|
|
|
That was left as an exercise.
|
|
|
|
|
PIEBALDconsult wrote: That was left as an exercise
but I chose a programming career to avoid such things as exercise
Illogical thoughts make me ill
|
|
|
|
|
Well then write a program to do it for you.
|
|
|
|
|
HashSet is present in System.Collections.Generic
and it is a part of .NET 3.5 framework.
|
|
|
|
|
Sorry you should try reading what I put before you answer. I already said about the System.Collections.Generic namespace and that it does not compile with your code
Illogical thoughts make me ill
|
|
|
|
|
the type is missing; you may have forgotten to escape the < and > signs inside the PRE block.
Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum
Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.
|
|
|
|
|
Perhaps your example is not quite what you have coded but have you considered not adding them in the first place?..
if(!list.contains("Method"))
list.add("Method");
Illogical thoughts make me ill
|
|
|
|
|
This does not make it generic because I do not know the name of the item. Method was just an example.
Thanks
|
|
|
|
|
I think you are missing the point.
How are the elements being added to your ArrayList in the first place?
Illogical thoughts make me ill
|
|
|
|
|
They are added to the list collection i.e.
foreach (...)
{
myList.Add(myClass)
}
|
|
|
|
|
so you should check to see if the list already contains myClass before you add it in...
foreach(...)
{
if(!myList.Contains(myClass))
myList.Add(myClass);
}
...not 100% sure the contains will work with a class but worth a try
EDIT: Tested and does NOT work
Illogical thoughts make me ill
|
|
|
|
|
Object reference not set to an instance of an object.
|
|
|
|
|
What are you talking about? Show me what code you have
As I have edited the compare does not work. But it will do if you override the Equals() function is your custom class. For example...
public override bool Equals(object obj)
{
if (obj == null)
return false;
MyClass m = obj as MyClass;
if (m == null)
return false;
return Name == m.Name && Value == m.Value;
}
EDIT: Microsoft recommend the GetHashCode function is also overridden along with the Equals function (see here[^]). Example...
public override int GetHashCode()
{
return Name.GetHashCode() ^ Value;
}
Illogical thoughts make me ill
modified on Tuesday, February 15, 2011 7:37 AM
|
|
|
|
|
Loop over list get items which are duplicate and remove them.
See This as example
|
|
|
|