|
An easy is with custom events. In a simple implementation, you add an event to each form (e.g., "FormDataChanged") that gets triggered when the data changes that you want to transfer. You can store that data in a static variable in program. Then pass that data to the other form whenever you like.
public static Program
{
private static object _myTransferData = new object();
static int Main(arg, args)
{
MyForm1.DataChanged += new FormDataChanged;
}
private static void FormDataChanged(object sender, EventArgs e)
{
_myTransferData = MyForm1.DataStorage;
}
}
|
|
|
|
|
Oh, dear, oh dear.
You were doing so well, right up to the "You can store that data in a static variable in program".
What happens if there are two instances of the form, with data changing?
What happens if the data changes again?
Static is generally a bad idea, particularly when you are passing the information to another class. Either keep it local to your class instance, and let the handler pick it up from the sender parameter if it is just the latest info they need, or also create a custom EventArgs based class that you hand the changed data though with when you signal the event.
Ideological Purity is no substitute for being able to stick your thumb down a pipe to stop the water
|
|
|
|
|
I agree and my original example was setting form1.foo = form2.foo. However, I decided on a field to show how you can store data between instantiations if you want. In this case, any member of Program has to be static since it's a static class. Regardless, your point is generally a good one.
|
|
|
|
|
imho a question like this would be better posted in the Q/A secion, with taqs included: is this Windows Forms C# ?
Second, by "transfer data" you could mean several things: automatic update (binding) one-way, or two-way, or: in response to a user action on one or both forms (like the user clicks a button).
You need to specify if the data being passed from Form1 to Form2 is a "horse of different color" than the data being passed from Form2 and Form1: or, if they are the same exact structure, then you need to tell us you are synchronizing data between two forms.
Another plausible scenario is that both Forms are being streamed data from some real-time source, or that a control on one Form is bound to a data-provider of some type, and you need to take action everytime that Form is updated on the other Form.
The more specific the questions, the more detailed the scenario you take the time to describe: behold: the more focused answers you will get
best, Bill
"Humans are amphibians ... half spirit and half animal ... as spirits they belong to the eternal world, but as animals they inhabit time. This means that while their spirit can be directed to an eternal object, their bodies, passions, and imaginations are in continual change, for to be in time, means to change. Their nearest approach to constancy, therefore, is undulation: the repeated return to a level from which they repeatedly fall back, a series of troughs and peaks.” C.S. Lewis
|
|
|
|
|
i want to transfer data between two form automatic two way, and response by click a button too. Sory because my English is not goog, i'm come from Viet Nam, i'm learning C# my self. This my first time post a Question. 
|
|
|
|
|
Promance wrote: want to transfer data between two form automatic two way, and response by click a button too. Hi Promance,
Then it sounds to me like you want ... at all times ... for the exact same content to appear on both forms ... yes ? That's "synchronization."
And, if you have synchronization working, then why would you also need to have a button to press to make sure the content was synchronized ?
Please describe more clearly the nature of the data you are synchronizing here:
1. is it only text characters: example: you want a textbox on Form1 to always display the same text as a textbox on Form2, and the reverse.
2. or, is the data "more than that" ? does the data include content which you interpret and then construct controls based on your interpretation ?
And, finally, it would be helpful to clearly describe the source of the data:
1. by direct action of a user while your application is running: like typing in a text box
2. by making calls to some external database and getting new data ... or does some external source notify your application there is new data needed to be used ?
Give us a brief description of what's on Form1 and Form2?: what kind of controls ? Custom controls from 3rd. party companies, Excel used via VSTO, a UserControl you created yourself, standard Visual Studio toolbox controls ?
best, Bill
good luck, Bill
"Humans are amphibians ... half spirit and half animal ... as spirits they belong to the eternal world, but as animals they inhabit time. This means that while their spirit can be directed to an eternal object, their bodies, passions, and imaginations are in continual change, for to be in time, means to change. Their nearest approach to constancy, therefore, is undulation: the repeated return to a level from which they repeatedly fall back, a series of troughs and peaks.” C.S. Lewis
|
|
|
|
|
It don't need complicated as you say. First, i want a textbox on Form1 to always display the same text as a textbox on Form2 when i press a letter key. Second, i have a project about File Explore in Windows, when i want to rename a folder or a file(i have used ListView to show file and folder), i must be add a form named Rename. And i'm stuck here, i can rename file, but can't update file's name in ListView or TreeView.
|
|
|
|
|
there is three solution:
1、pipe.
2、shared memory.
3、Message.
|
|
|
|
|
You use anti-tank missles to kill mosquitoes don't you?
|
|
|
|
|
|
I am converting from PHP to C# and I might be doing this completely wrong but here goes. I am reading rows from a database with a reader. Then using a foreach command I am walking through the records creating an object and then adding the object to a List<t>. I then return the list to be used for other purposes. But since the object that I am adding to the List is sent by reference it ends up filling the list with the values of the last object added. I am looking for advice on alternate was to fill the List or alternate ways to use the objects. Please try to refrain from just telling me I am and idiot. Be assured I am already aware of this fact. Thanks to anyone that can offer advice. Below is some of the code. I hope that I posted it OK.
Mark
public object find_by_sql(string sql_statement, Type object_type, database working_database)
{
working_database.reader = (SqlDataReader)working_database.query(sql_statement);
object defined_object = Activator.CreateInstance(object_type);
List<object> object_array = new List<object>();
while (working_database.reader.Read())
{
Console.WriteLine("Calling Instantiation");
object_array.Add(instantiate(working_database.reader, defined_object));
Console.WriteLine("object test = " + defined_object.GetType().GetField("descr").GetValue(defined_object).ToString());
Console.WriteLine("Returned from Instantiation");
}
return (object_array);
}
private static object instantiate(SqlDataReader records,object defined_object)
{
for (int i = 0; i < (records.FieldCount); i++)
{
int one = (int)records.GetInt32(0);
string two = (string)records.GetName(i);
var three = records[i];
Console.WriteLine("{0} : {1} - {2}", one, two, three);
defined_object.GetType().GetField(two).SetValue(defined_object, three);
}
return(defined_object);
}
|
|
|
|
|
When you read N rows from a database and want all of it stored in some collection such as a List, you obviously need N objects to store all the data; so it takes a new or a CreateInstance or some such inside the loop, you just can't reuse a single object over and over.
|
|
|
|
|
please tell me any job for Asp.net in indore.
shubham vijay
|
|
|
|
|
This is [a] forum for programming questions on C# and .NET. Try this[^] site if you're looking for a job in Indore.
/ravi
modified 11-May-12 15:26pm.
|
|
|
|
|
I have an XML document that contains in one element XHTML fragments, specifically one or more p tags, which can contain further XHTML tags such as em, span etc. These are all in an "xhtml" namespace, and the XSD has imported the XHTML strict schema, so all is fine in that regard. My problem is this:
My application needs to display these XHTML paragraphs as a read-only document. My thought was to wrap these in an XHTML document with a namespace specified on the body tag, like this:
<!--
<html>
<head>
<meta http-equiv="content-type"
content="application/xhtml+xml;charset=utf-16" />
</head>
<body xmlns:xhtml="http://www.w3.org/1999/xhtml">
<!--
<xhtml:p>Namespaced paragraph from XML</xhtml:p>
<xhtml:p>A partly <xhtml:em>italicised</xhtml:em> paragraph from XML</xhtml:p>
<!--
</body>
</html>
This way, I wouldn't have to remove all namespaces from the XHTML elements when putting the document together.
When I give this generated document to the System.Windows.Forms.WebBrowser control (.NET 3.0), the two paragraphs flow together and the em is ignored (the same thing occurs in IE8). In other words, the WebBrowser doesn't unserstand the namespace, and treats xhtml:p as an unknown element and ignores it.
In Firefox, however, the two paragraphs FF they are on separate lines with normal paragraph distance, which is what I expected.
I'm curious from a purely academic point of view whether Firefox or IE8 is in the right here, but am more interested in a pragmatic solution to this problem, if one exists.
|
|
|
|
|
Hi,
I am studying the following control :
C# Custom Control Featuring a Collapsible Panel[^]
I would like to rewrite that control for the 4.0 framework.
This control consists of a headerpanel and in that headerpanel are 2 pictureboxes. When I study the CollapsiblePanel.Designer.cs file I see that the component designer added the parent child relations through Controls.Add statements. How can I achieve this relation through use of the designer. When I drag the panel and picturebox controls on the designer surface the controls get just created an no relation gets added.
|
|
|
|
|
All you do is drop the child control inside the bounds of the parent control. That's it!
|
|
|
|
|
If you are using a custom control (i.e., a UserControl), and you've placed an instance of it on the design-surface at design-time, it will not exhibit the behavior of other native C# container controls: where drag-dropping another control ... from the ToolBox ... into the container automatically makes the dropped control a child of the container.
Look for an article here on CP by Henry Minute explaining how to enable this "swallow dropped controls and make them children of the container" for custom controls used at design-time, and look for comments by me here on CP, suggesting the possibility of a simpler solution.
best, Bill
"Humans are amphibians ... half spirit and half animal ... as spirits they belong to the eternal world, but as animals they inhabit time. This means that while their spirit can be directed to an eternal object, their bodies, passions, and imaginations are in continual change, for to be in time, means to change. Their nearest approach to constancy, therefore, is undulation: the repeated return to a level from which they repeatedly fall back, a series of troughs and peaks.” C.S. Lewis
|
|
|
|
|
I have a win form that you can click a browse button - navigate to your image file - and view the image in the win form. I've been trying to associate my progress bar to the load time of the image selected with no success. Can anyone take a look at my code and see what I'm missing?
private void startButton_Click(object sender, EventArgs e)
{
OpenFileDialog open = new OpenFileDialog();
open.FileName = "";
open.Filter = "Image Files(*.tif; *.jpg; *.jpeg; *.gif; *.bmp)|*.tif; *.jpg; *.jpeg; *.gif; *.bmp";
open.InitialDirectory = @"g:\All Scanned Jobs\";
if (open.ShowDialog() != DialogResult.Cancel)
{
pictureBox1.Image = new Bitmap(open.FileName);
pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
pictureBox1.WaitOnLoad = false;
pictureBox1.LoadAsync(open.FileName);
}
}
void pictureBox1_LoadProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
|
|
|
|
|
make sure the pictureBox1_LoadProgressChanged method is associated with the pictureBox event, i tested your code here and it works without modifications, have you tried to put a breakpoint into the pictureBox1_LoadProgressChanged method?
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)
|
|
|
|
|
I have this in my Form1.Designer.cs code.
this.pictureBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.pictureBox1.BackColor = System.Drawing.SystemColors.ButtonFace;
this.pictureBox1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
this.pictureBox1.ImageLocation = "";
this.pictureBox1.Location = new System.Drawing.Point(13, 71);
this.pictureBox1.Name = "pictureBox1";
this.pictureBox1.Size = new System.Drawing.Size(1303, 701);
this.pictureBox1.TabIndex = 3;
this.pictureBox1.TabStop = false;
this.pictureBox1.LoadProgressChanged +=new System.ComponentModel.ProgressChangedEventHandler(pictureBox1_LoadProgressChanged);
Is that where it needs to be?
|
|
|
|
|
your code are exactly the same as mine, still, works fine, the image loads pretty fast, so the progressbar moves fast, but stops at 100% you said that a breakpoint don't helped, what was the behavior? the method isn't being called? the image isn't loading?
I'm brazilian and english (well, human languages in general) aren't my best skill, so, sorry by my english. (if you want we can speak in C# or VB.Net =p)
|
|
|
|
|
The break gives me this:
ImageViewer.exe!ImageViewer.Form1.pictureBox1_LoadProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) Line 71 + 0x5 bytes C#
ImageViewer.exe!ImageViewer.Program.Main() Line 18 + 0x1d bytes C#
I'm using 2010 Express of visual studio would that have anything to do with it?
|
|
|
|
|
And everything works just fine except I do not see the progress bar fill up at all and once the image is done loading completely I still dont see any green.
|
|
|
|
|
And a breakpoint didn't come up with much help.
|
|
|
|
|