Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, March 24, 2012 5:07 AM
I have a C# project that utilizes a form to enter data and build a structure array. I would like to add a seond form to the project that is used to display the contents of the array. How do I pass the array from form 1 to form 2?
Steve
All replies (15)
Saturday, March 24, 2012 6:10 AM ✅Answered | 4 votes
You can create a public method on form2, and access to if from form1. This method will display content of array where ever you want to.
Example:
//on form1:
string[] myArray;
private void CreateArray()
{
myArray = new string[2];
myArray[0] = "item 1";
myArray[1] = "item 2";
}
private void PassArrayToForm2()
{
if(myArray.Length > 0)
{
Form2 f2 = new Form2();
f2.ShowingArray(myArray);
}
else
MessageBox.Show("there is nothing to pass to form2!");
}
//on form2:
//create a method to show content of array:
public void ShowingArray(string[] array)
{
//1. or to pass to some control:
foreach(string item in array)
{
listBox1.Items.Add(item);
}
//2. or to show in meesageBox (or similar):
StringBulder sb = new StringBuilder();
foreach(string item in array)
{
sb.AppendLine(item);
}
MessageBox.Show(sb.ToString());
}
Hope it helps.
bye
Mitja
Monday, March 26, 2012 7:33 AM ✅Answered | 1 vote
Hi swcbyte,
I have found three ways to implement it after researching how to transfer data from child form to parent form. You can refer to the following link which has posted at the past time as seen below:
http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvcs/thread/94d578f2-3d44-4ee6-9084-6081f04f93b9/#098c71fe-3975-451c-9e96-7f5b0b35bf28
If you want to guarantee its safety, you’d better using the delegate to achieve the data communication.
I hope it helps you.
Sincerely,
Jason Wang
Jason Wang [MSFT]
MSDN Community Support | Feedback to us
Monday, March 26, 2012 5:25 PM ✅Answered | 1 vote
Try it this way:
//in 1st class or form:
private void CreateArray()
{
CoffeeSales[] CoffeeTransactions = new CoffeeSales[20];
CoffeeTransactions[0] = new CoffeeSales { Type = "type 1", Quanitity = "1", Price = 12.3M };
//and so on... fill all 20 indexers
//then pass to new class:
frmTranactionRPT Transactions = new frmTranactionRPT();
Transactions.RptValues(CoffeeTransactions);
}
}
//other...
class frmTranactionRPT
{
public void RptValues(CoffeeSales[] Transaction)
{
if (Transaction.Length >= 0)
{
//iam not sure what you wanted with these lines of code (since I dont know what lstTransactions are)
}
}
}
public struct CoffeeSales
{
public string Type;
public string Quanitity;
public decimal Price;
}
Mitja
Monday, March 26, 2012 6:26 PM ✅Answered
I want to thank every one for their assistance with your help and suggestions I was able to find a solution to my problem
Steve
Monday, March 26, 2012 3:54 PM
Thank You this seems to work ok when I use a single array but I am having trouble using a structure. I keep receiving a overload error. There is a problem with the arguments matching between the two forms and I am not sure how to get around the problem.
Steve
Monday, March 26, 2012 4:21 PM
Thank You this seems to work ok when I use a single array but I am having trouble using a structure. I keep receiving a overload error. There is a problem with the arguments matching between the two forms and I am not sure how to get around the problem.
Steve
You'll need to list the exact error message along with the code that you're using.
Monday, March 26, 2012 5:09 PM
Form 1 defines the structurepublic struct CoffeeSales { public string Type; public string Quanitity; public decimal Price; }CoffeeSales[] CoffeeTransactions = new CoffeeSales[20];And then Uses this routine to display form 2frmTranactionRPT Transactions = new frmTranactionRPT();Transactions.RptValues(CoffeeTransactions);Form 2 then uses this routine to populate a list box and display the information public void RptValues(CoffeeSales[] Transaction) If Transactions.Length > 0 for (int i = 0; i < N; i++) lstTransactions.Items.Add(CoffeeTransactions[i]); }Error 1 The best overloaded method match for 'daCoffeeShop.frmTranactionRPT.RptValues(daCoffeeShop.frmTranactionRPT.CoffeeSales[])' has some invalid arguments C:\Documents and Settings\clauses\My Documents\Visual Studio 2010\Projects\daCoffeeShop\daCoffeeShop\frmTransactions.cs 123 17 daCoffeeShopError 2 Argument 1: cannot convert from 'daCoffeeShop.frmTransactions.CoffeeSales[]' to 'daCoffeeShop.frmTranactionRPT.CoffeeSales[]' C:\Documents and Settings\clauses\My Documents\Visual Studio 2010\Projects\daCoffeeShop\daCoffeeShop\frmTransactions.cs 123 40 daCoffeeShop
Steve
Monday, March 26, 2012 5:13 PM
You define a structure of the same name in two different namespaces. Just look at the error message. The class name is the same, but the namespaces are different.
Even if the struct definition is the same, if they're in two different namespaces they're two different Types and will be treated as such. You need to just define the struct once and reference that single struct in both locations.
Monday, March 26, 2012 5:48 PM
My structure array is being filled fine where I get the error is when I try to display the second form --
Transactions.RptValues(CoffeeTransactions);
The structure is only defined in form one but i still get the errors
Error 1 The best overloaded method match for 'daCoffeeShop.frmTranactionRPT.RptValues(daCoffeeShop.CoffeeSales[])' has some invalid arguments
Error 2 Argument 1: cannot convert from 'daCoffeeShop.frmTransactions.CoffeeSales[]' to 'daCoffeeShop.CoffeeSales[]'
I have tried to define my structure in a class but have had trouble defining and building an array that way.
Steve
Monday, March 26, 2012 6:29 PM | 1 vote
So it will be good to paste the code in here, since you decided to mark your post as answered. So every one can find a solution, in case of same issue.
I hope you understand.
bye
Mitja
Monday, March 26, 2012 7:13 PM
The code is already display I used the code you suggested but I used a class file to contain my structured and then I referenced it to define the structure in both forms and that took care of my compatability issues. So i am now successfully passing the array to my second form.
Steve
Monday, March 26, 2012 8:07 PM
So you can mark my post as well.
bye
Mitja
Monday, March 26, 2012 10:15 PM
So you can mark my post as well.
bye
Mitja
Marking your own posts as answers does not score any Answer points. I'll try to keep an eye on it.
Rudy =8^D
Mark the best replies as answers. "Fooling computers since 1971."
http://thesharpercoder.blogspot.com/
Tuesday, March 27, 2012 1:58 AM
Marking your own posts as answers does not score any Answer points. I'll try to keep an eye on it.
Very true, but I notice that when an OP marks their own post as the answer the mods rarely mark any other posts. If the OP marks none then the mods will mark posts as answers, and they tend to be more liberal with the marking than posters.
I think he's just upset that he's not getting his 20 points (granted, he does deserve them here) more so than thinking the OP is getting points for his own non-answer.
For the record, I'm not judging, just explaining.
Tuesday, March 27, 2012 7:55 AM
I think he's just upset that he's not getting his 20 points (granted, he does deserve them here)..
Hehe, I bet he is.
But honestly, why is he here (at the moment of speaking)? Isnt he asking for help?
If he would know how to salve the problem, he wouldnt be asking questions.
Eh, same theme all over again every day.
Mitja