|
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.
|
|
|
|
|
I understand. Supposing i wanted to write a file to a drive that is somewhat protected by the UAC, say Windows. This would throw an exception with an access denied message. I have a feeling i should request for permission, then the UAC would pop up, then if would say allow. Is that possible in code. That is really what i want to do. My English is not that good
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.
|
|
|
|
|
yes, since Vista, some folders (not drives) are protected. The idea is your app should not write there at all since these folders have special functionality for all users, not just for you.
You can invoke the UAC dialog and get admin access rights if you must; see the relevant CodeProject articles[^].
|
|
|
|
|
LPXYZ
word cont*aining an asterisk
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.
|
|
|
|
|
LPXYZ
word cont3aining a digit
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.
|
|
|
|
|
LPXYZ
words containing a period
for.each
object.field
object.method(parameter)
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.
modified on Thursday, January 13, 2011 3:46 PM
|
|
|
|
|
LPXYZ double
words containing a period
for.each
object.field
object.method(parameter)
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.
|
|
|
|
|
Hi developers
I want make a quiz program.
It use image recognition (recon the person from the given image) also.Every questions have a four options as answers.And the entire quiz is controlled with timer.Means it must end within the given time.Am really doubtful in so many factors.I searched many in CodeProject,but couldn't find an appropriate application to study.If you have any idea please help me.
I need help to build it in c#.net
varun
|
|
|
|
|
Hi,
the trickiest part will be the face detection. Here are two articles that describe some basics:
Face Detection in C#[^]
Face and Eyes Detection Using OpenCV[^]
You would need to capture an image with a webcam and try to recognize a face that is stored somewhere (image file or just in database). The whole quiz-timer thing should be quite simple. Try to break down your problem in simple tasks and solve them step for step.
|
|
|
|
|
Thanks first
Your help is so much valuable for me.but still i am confused in making that to work with in the given time.Can you point me to any c# source codes available
|
|
|
|