|
OK, here are three demo projects that show you ways this can be done.
First is the correct way.
The second should NOT be used but is better than what you are attempting!
The third shows you how do do it your way - this is terrible so DO NOT use it!
First - Correct
using System;
using System.Drawing;
using System.Windows.Forms;
namespace FormsDemo
{
public partial class Form1 : Form
{
private Label label;
public Form1()
{
InitializeComponent();
CreateLabel();
Form2 form2 = new Form2();
form2.RequestDisable += new EventHandler(form2_RequestDisable);
form2.Show();
}
private void CreateLabel()
{
label = new Label();
label.Location = new Point(12, 12);
label.Text = "Disable Me";
Controls.Add(label);
}
private void form2_RequestDisable(object sender, EventArgs e)
{
label.Enabled = false;
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace FormsDemo
{
public partial class Form2 : Form
{
public event EventHandler RequestDisable;
private Button buttonDisable;
public Form2()
{
InitializeComponent();
CreateButtonDisable();
}
private void buttonDisable_Click(object sender, EventArgs e)
{
OnRequestDisable(EventArgs.Empty);
}
private void CreateButtonDisable()
{
buttonDisable = new Button();
buttonDisable.Location = new Point(12, 12);
buttonDisable.Text = "&Disable";
buttonDisable.Click += new EventHandler(buttonDisable_Click);
Controls.Add(buttonDisable);
AcceptButton = buttonDisable;
}
protected virtual void OnRequestDisable(EventArgs e)
{
EventHandler eh = RequestDisable;
if (eh != null)
eh(this, e);
}
}
}
Second - bad!
using System;
using System.Drawing;
using System.Windows.Forms;
namespace FormsDemo
{
public partial class Form1 : Form
{
private Label label;
public Form1()
{
InitializeComponent();
CreateLabel();
Form2 form2 = new Form2(label);
form2.Show();
}
private void CreateLabel()
{
label = new Label();
label.Location = new Point(12, 12);
label.Text = "Disable Me";
Controls.Add(label);
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace FormsDemo
{
public partial class Form2 : Form
{
private Button buttonDisable;
private Control controlToDisable;
public Form2(Control controlToDisable)
{
InitializeComponent();
CreateButtonDisable();
this.controlToDisable = controlToDisable;
}
private void buttonDisable_Click(object sender, EventArgs e)
{
controlToDisable.Enabled = false;
}
private void CreateButtonDisable()
{
buttonDisable = new Button();
buttonDisable.Location = new Point(12, 12);
buttonDisable.Text = "&Disable";
buttonDisable.Click += new EventHandler(buttonDisable_Click);
Controls.Add(buttonDisable);
AcceptButton = buttonDisable;
}
}
}
Third - Worst!
using System;
using System.Drawing;
using System.Windows.Forms;
namespace FormsDemo
{
public partial class Form1 : Form
{
private static Label label;
public Form1()
{
InitializeComponent();
CreateLabel();
Form2 form2 = new Form2();
form2.Show();
}
public static Label Label
{
get { return label; }
}
private void CreateLabel()
{
label = new Label();
label.Location = new Point(12, 12);
label.Text = "Disable Me";
Controls.Add(label);
}
}
}
using System;
using System.Drawing;
using System.Windows.Forms;
namespace FormsDemo
{
public partial class Form2 : Form
{
private Button buttonDisable;
public Form2()
{
InitializeComponent();
CreateButtonDisable();
}
private void buttonDisable_Click(object sender, EventArgs e)
{
Form1.Label.Enabled = false;
}
private void CreateButtonDisable()
{
buttonDisable = new Button();
buttonDisable.Location = new Point(12, 12);
buttonDisable.Text = "&Disable";
buttonDisable.Click += new EventHandler(buttonDisable_Click);
Controls.Add(buttonDisable);
AcceptButton = buttonDisable;
}
}
}
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
And yet, why do I think he'll use version 3?
"WPF has many lovers. It's a veritable porn star!" - Josh Smith As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
My blog | My articles | MoXAML PowerToys | Onyx
|
|
|
|
|
Yeah, that was my worry too.
I figured that this guy wasn't going to give up until he got the answer he wanted. So long as he's been adequately warned that it's the wrong way and given the correct way then it's still a responsible answer.
If he decides to use 2 or 3 then it's his fault when he fails/gets sacked
DaveIf this helped, please vote & accept answer!
Binging is like googling, it just feels dirtier.
Please take your VB.NET out of our nice case sensitive forum.(Pete O'Hanlon)
BTW, in software, hope and pray is not a viable strategy. (Luc Pattyn)
|
|
|
|
|
Hi,
I created windows service applcation in vs 2005. And registed the application sucessfully. The problem is i cannot able to start up the serice, while doing this am facing problem , which is as follows,
Note:
"service on local computer started and then stopped. some services stop automatically if they have no work to da, for example , the performance log and alerts logs"
So how to resolve this error from my end. Please can u clear me this problem.
regards,
kannak.........
kannak
|
|
|
|
|
No chance without better information. I know it is difficult to post relevant code fragments for a problem like this, but without something more it could be anything! Have you tried logging activity in the service so you can see what it thinks it is doing?
Real men don't use instructions. They are only the manufacturers opinion on how to put the thing together.
|
|
|
|
|
We'd need to see the code.
|
|
|
|
|
Hey,
I've a comboBox with set of items (num of seconds) but the item is also can be written manually bu the user, how it can be prevented ?
is there any combobox property for this demand ? or it should be implemented by me ?
thanks
|
|
|
|
|
Set the 'DropDownStyle' to 'DropDownList' - that'll mean that the user can't type in the box
|
|
|
|
|
thanks a lot
|
|
|
|
|
I was tinkering with one of my desktop applications today and discovered that on Vista (and presumably Weven) that MS took away the functionality of the Printer button on the PageSetup dialog. PageSetup.AllowPrinter = true; used to allow the printer to be selected on XP, but now the printer selection button doesn't show up at all.
WTF?
In my application I need to print to a variety of page sizes and I need to show the user how the layout will look on their printer. Under XP when the user selects a different printer, my app changes the margins and readjusts the formatting. That makes a major difference when changing from, say a normal page printer to a continuous feed plotter!
As far as I can tell, the actual printing still works fine, but what you see on the screen preview may be totally different than what gets printed and the actual number of pages is totally bogus in many cases.
Has anyone else run into this problem? What is the recommended method on Vista for setting up a print preview and allowing the user to change the printer for a given application? Do I need separate code for Vista/W7 and XP?
Any ideas?
CQ de W5ALT
Walt Fair, Jr., P. E.
Comport Computing
Specializing in Technical Engineering Software
|
|
|
|
|
Hi,
This question is a little generic, but is there a difference in the way Windows Services handle events vs. Windows Forms?
I want to create my own hotkey program to test out a new class I found in one of the articles here(UserActivityHook.cs). When I press 1 key, a combination of keys are sent
In a windows form, I could do something like this (shortened examples):
private void btnStart_Click(object sender, EventArgs e)
{
hook = new UserActivityHook();
hook.KeyPress += new KeyPressEventHandler(KeyPressHandler);
}
public void KeyPressHandler(object sender, KeyPressEventArgs e)
{
string strKeyPressed = e.KeyChar.ToString();
if(strKeyPressed == 'a')
{
SendKeys.Send("B");
System.Threading.Thread.Sleep(50);
SendKeys.Send("N");
}
}
So for a Windows Service, I have a service up and running, I have confirmed that it starts properly with a few tests.
So if the windows service starts, and I do the same thing:
protected override void OnStart(string[] args)
{
hook = new UserActivityHook();
hook.KeyPress += new KeyPressEventHandler(KeyPressHandler);
}
Assuming I have the same event handler, shouldn't this accomplish the same thing? (i.e. the Button click does the same thing as a service starts)
What's happening is that the Windows Forms app sends the hotkeys, but the Windows Service does not, so the event handler didn't run.
I don't know how straight forward this is, but I'm new to windows services
Thanks!
modified on Friday, August 20, 2010 1:33 PM
|
|
|
|
|
A service runs under a different account than a user, and with less rights. It would be running before a user logs in - and it would be a huge security hole if you could start a keyboard hook there.
Does your application "have" to run before the user logs in? If not, then stick to a WinForm and simply hide the mainform.
I are Troll
|
|
|
|
|
The user is currently logged in. I install the service, run it, but the event handlers are still unable to run. Surely this should be somehow possible?
But anyway, it does not "have to" be a service. I was experimenting with services, but I guess I basically have the equivalent behaviour with a hidden windows form.
this.Hide();
this.ShowInTaskbar = false;
I'm still open to possible fixes using the Windows Service, but this workaround is just as good for my use. Thanks
|
|
|
|
|
See_Sharp wrote: The user is currently logged in. I install the service, run it
Not all processes on your computer run under the users' account. Doesn't matter if anyone is logged in, the service is already logged in under a local system account. That brings a multitude of limitations, like not being able to interact with the desktop.
Open the control panel, go to "Administrative Tools", open the "Services" applet, double-click any service in the list, and check out the second tab-page called "Log on". You can specify the credentials and the password there.
I are Troll
|
|
|
|
|
I do have one i frame like on my .aspx page
<iframe height="500px" width="500px" src="http://www.codeproject.com" id="Iframe1" runat="server"></iframe>
but when i run this page.
the webpage inside page taking control of whole the form and just redirects to given src of Iframe.
Can anybody say reason behind this ?
modified on Friday, August 20, 2010 9:42 AM
|
|
|
|
|
Why are you asking this to C# developers?
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
|
|
|
|
|
Don't worry about it, CodeProject has code explicitly designed to do that. Use Google.com as your sample. Now if you are trying to adframe around CodeProject, then I think you see why they have done what they have done.
|
|
|
|
|
Wrong forum: try web development or asp.net.
Probably has some JavaScript on the page that makes it the topmost or parent page to stop it being opened inside another web site - view the source of the page in question and you may be able to track it down.
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair.
nils illegitimus carborundum
me, me, me
|
|
|
|
|
Thanks you all. for your kind words.
There is not any solution for that .... right ?? sharp devlopers .
|
|
|
|
|
There's no way in C# to do this. Maybe there's a solution in some CLIENT-SIDE language like JavaScript.
Don't forget to rate answer, that helped you. It will allow other people find their answers faster.
|
|
|
|
|
Actually there is a way in C# to do this. It involves using the WebRequest object and parsing out the offending js method and then rendering the newly stolen web page to the client. Of course you have to rewrite the URL's too but it isn't so terribly difficult as to be hard.
|
|
|
|
|
Actually I suppose you could read in the source of the page, strip out anything you don't want and then write it back out to a window.
"If you think it's expensive to hire a professional to do the job, wait until you hire an amateur." Red Adair.
nils illegitimus carborundum
me, me, me
|
|
|
|
|
The reason is that you are probably violating the terms of use of the site. If a site doesn't want its content used outside of the site itself, that is their choice. You should always ask permission if you intend to integrate someone else's site into your own.
|
|
|
|
|
i am not taking content of anysite. it is just my test and given test link for that.
if everysite is coming to iframe and not popping then why codeproject is popping out.
Just a Questiong.
|
|
|
|
|
Look at the source code of CodeProject's web pages.
I think
if(top!=self)top.location.href=location.href;
does the trick.
|
|
|
|