|
I also recommend looking at the performance of your program too. I think you're not storing huge amounts of continuous data. I use a profiler http://www.red-gate.com/products/dotnet-development/[^] to help me spot nastiness. When collecting data from devices, it pays to make sure that your program runs smoothly and doesn't accumulate extra memory.
I know it's a little off topic, but doing performance analysis has helped me quite a bit.
"Simplicity carried to the extreme becomes elegance."
-Jon Franklin
|
|
|
|
|
hi guys, i have a form that needs to call a function on his opener form. How can i do this?
ex: form1 opens form2, and form2 needs to call a method in form1.
thanks a lot!
|
|
|
|
|
Make the method public, and pass a reference to itself to the second form (in the second form's constructor).
*** In Form1 ***
Form2 form2 = new Form2(this);
*** In Form2 ***
public Form2 (Form1 form1) : this()
{
_form1 = form1;
}
private Form1 _form1;
|
|
|
|
|
i've used From2 f = new Form2(ref Form1 this); but didnt work.
|
|
|
|
|
This works for me. Two forms, one with a button and textbox (Form1), the other just a button (Form2).
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2(this);
form2.Show();
}
public void Change(string text)
{
textBox1.Text = text;
}
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
public Form2(Form1 form1) : this()
{
_form1 = form1;
}
private Form1 _form1;
private void button1_Click(object sender, EventArgs e)
{
_form1.Change("Put some text in the TextBox");
}
}
|
|
|
|
|
ok, almost there... i got only one more problem... i need to pass 2 param to my form2.
ex: From2 form = new Form2(id, this);
|
|
|
|
|
Just change the constructor... Simple.
public Form2(int id, Form1 form1) : this()
{
_id = id;
_form1 = form1;
}
private int _id;
private Form1 _form1;
|
|
|
|
|
solved... just removed the : this().
anyway... thank you very much
|
|
|
|
|
The this() call makes the constuctor call the lower-level constructor overload. In this example, Form2(Form1 form1) : this() causes it to call public Form2() during its execution, which thereby causes it to call InitializeComponent(); (and anything else inside Form2() ).
If you don't have a parameter-less constructor overload of course, it will fail.
public Form2()
{
InitializeComponent();
}
public Form2(Form1 form1) : this()
{
_form1 = form1;
}
|
|
|
|
|
you can do it something like:
using(Form2 frm2 = new Form2())<br />
{<br />
frm2.myVoid();<br />
}
I Love T-SQL
"Don't torture yourself,let the life to do it for you."
If my post helps you kindly save my time by voting my post.
www.aktualiteti.com
|
|
|
|
|
your answer is not clear at all.
If this is a third form you are creating, it is wrong as it would not share anything with the original Form it is trying to interact with.
|
|
|
|
|
Either do the way others have suggested or follow on of the following approach:
1. Create a class called Common or something similar and place the methods used by more than one forms there. Then all the forms can call methods in this class. Or
2. Have a base class that contains common methods (possibly marked virtual) and all forms in your application inherit from that class.
|
|
|
|
|
I never like solutions that include a child knowing about its parent. I question whether the UI forms should be communicating directly in the first place and if some re-architecturing needs to take place on that design.
But suffice to say, if the forms do need to communicate, I would much rather see it done where Form2 raises an event for which Form1 creates a handler when creating Form2. That way, at least the child still knows nothing about the parent.
if( eventToCall != null )
eventToCall();
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
With events instead, it could look something like this. (Probably better to pass the message in an event argument, though.)
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
form2.Clicked += new EventHandler(form2_Clicked);
}
private void form2_Clicked(object sender, EventArgs e)
{
textBox1.Text = form2.Message;
}
private void button1_Click(object sender, EventArgs e)
{
form2.Show();
}
private Form2 form2 = new Form2();
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Message = "Here is some text";
if (Clicked != null) Clicked(sender, e);
}
public string Message = string.Empty;
public event EventHandler Clicked;
}
[Edit] Public to private on Form2.
modified on Wednesday, January 5, 2011 1:31 PM
|
|
|
|
|
In theory correct, but there may be a few issues.
public Form2 form2 = new Form2(); should probably be made a private member unless it it required by something outside this form.(That would likely not be a good idea)
Other than that, I think it is pretty close.
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
Did I say public? I meant private...
|
|
|
|
|
Hide your members whenever possible... Showing them almost always leads to embarrassment and ridicule...
I wasn't, now I am, then I won't be anymore.
|
|
|
|
|
Might as well give the "event" version that passes the string as an event arg...
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
form2.Clicked += new Form2.StringEventHandler(form2_Clicked);
}
private void form2_Clicked(string message)
{
textBox1.Text = message;
}
private void button1_Click(object sender, EventArgs e)
{
form2.Show();
}
private Form2 form2 = new Form2();
}
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
if (Clicked != null) Clicked("Here is some text");
}
public event StringEventHandler Clicked;
public delegate void StringEventHandler(string message);
}
|
|
|
|
|
Ok, the question is: how to load image from resource (and then do some manipulations on it)?
In a C++ app, i am simply doing:
specifying image in file myapp.rc like:
IDR_MYPIC RCDATA "Res\\bkg.jpg"
then in file resource.h:
#define IDR_MYPIC 111
then in application i can simply access it by:
HRSRC hResInfo = FindResourceW((HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),
MAKEINTRESOURCEW(IDR_MYPIC), RT_RCDATA);
I am trying to do actually the same thing in C#, but after hours of searching, experimenting - i got nothing.
This code returns resources path:
System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
string[] resources = thisExe.GetManifestResourceNames();
string list = "";
foreach (string resource in resources)list += resource + "\r\n";
MessageBox.Show(list);
which are:
ComboTest.Form1.resources
ComboTest.Properties.Resources.resources
Then i am trying do apply each path like this:
System.Reflection.Assembly thisExe;
thisExe = System.Reflection.Assembly.GetExecutingAssembly();
System.IO.Stream file = thisExe.GetManifestResourceStream("ComboTest.Form1.resources.zzz.jpg");
this.pictureBox1.Image = Image.FromStream(file);
file always = null
Same thing with that:
IntPtr hModule = ResImage.LoadLibraryEx(Application.ExecutablePath,
IntPtr.Zero, 0x2 );
if (hModule == IntPtr.Zero) throw new Exception();
IntPtr hResource = ResImage.FindResource(hModule, "zzz.jpg", "JPEG");
hResource is always null.
My image file is "zzz.jpg" and i have imported it into a project by clicking on a main form (in a designer), clicking on BackgroundImage in Properties tab, then selecting "Project resource file" radio button and clicking "Import" button.
Image now appears in "Resources" folder in Solution Explorer tab.
So, how can i access it from code?
thanks
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
To access Image from Resource you can use
this.pictureBox1.Image = WindowsApplication1.Properties.Resources.zzz;
modified on Wednesday, January 5, 2011 7:57 AM
|
|
|
|
|
Hi,
I've just tried the following code that works for me:
ResourceManager resourceManager = new ResourceManager("WindowsFormsApplication1.Properties.Resources", this.GetType().Assembly);
Bitmap image = (Bitmap)resourceManager.GetObject("snapshot1");
image.Save("C:\\Temp\\Test123.bmp");
This code works if you add a bitmap called "snapshot.bmp" to your resources. Please make sure that the assembly string does match you project. You will find both strings in the "Resources.Designer.cs" file.
Have a look here for a more detailed explanation:
http://www.mysuperiorgames.com/resources.html[^]
|
|
|
|
|
Thanks guys, that clarified a few things.
Most important thing here for me is to make this work:
IntPtr hResource = ResImage.FindResource(hModule, "zzz.jpg", "JPEG");
I am trying to implement custom background image drawing from memory (cached image) in C# so the app will not flicker when its 2000 X 1000 px bkg image plus 100 of controls on a form. The only method i know is the only by using win32 API (already implemented in c++ project) but here i need a valid "hResource" parameter to pass to my native dll to do the job.
IS it possible to achieve? Maybe you know some ways? thanks in advance
011011010110000101100011011010000110100101101110
0110010101110011
|
|
|
|
|
Hi all. When i try accessing drive C: where the O.S.(windows 7), i keep on getting the error:
Access to the path 'C:\Documents and Settings\' is denied. and throws the Exception:
I have a good idea i have the following piece of code
private static void CopyFiles(string directoryPath, string destination)
{
try
{
int count = 0;
DirectoryInfo dir = new DirectoryInfo(directoryPath);
FileInfo[] wordFiles = dir.GetFiles("*.doc*", SearchOption.AllDirectories);
foreach (FileInfo f in wordFiles)
{
count++;
f.CopyTo(destination + count.ToString() + f.Name);
Console.WriteLine("***************************");
Console.WriteLine("File name: {0} Copied", f.Name);
Console.WriteLine("***************************\n");
}
}
catch (UnauthorizedAccessException ex)
{
}
}
I tried to request permisions in the two commented lines but i was totally unable. Please assist. .
Wamuti: Any man can be an island, but islands to need water around them!
Edmund Burke: No one could make a greater mistake than he who did nothing because he could do only a little.
|
|
|
|
|
Wamuti wrote: Access to the path 'C:\Documents and Settings\' is denied. and throws the Exception:
yes it will throws Exception because Microsoft replaced "Documents and Settings" folder with "Users" folder from Vista. hence same is the case in Windows 7 then if you want to access the "Documents and Settings" you should looking for Users folder
use given Path instead of C:\Document and Setting in Windows 7 or even in Vista
C:\Users
to get more clarification please read the thread of given link
C:\Document and Setting[Win 7][^]
modified on Wednesday, January 5, 2011 6:00 AM
|
|
|
|
|
Hi,
You should never have a hard coded folder name in your app.
C:\Documents and Settings\ might not exist for one of many reasons:
- your system drive may not be called C:
- your Windows system might be an international one using a different language
- your Windows system might be more recent than XP and use a completely different folder name.
The correct way is by using Environment.GetFolderPath() with the right parameter. That is bound to work on a range of Windows versions starting before XP. See the MSDN doc.
|
|
|
|