|
Thank you very much I fixed it, but what do you think I should do for the 2nd error?
Form1.CheckboxPublicName.Checked = false;
Error
Error 1 An object reference is required for the non-static field, method, or property 'Gadgets.Form1.CheckboxPublicName.get' C:\Users\Kyle\documents\visual studio 2010\Projects\Gadgets\Gadgets\Time.cs 130 17 Gadgets
|
|
|
|
|
systemerror121 wrote: Error 2 An object reference is required for the non-static field, method, or property 'Gadgets.Form1.CheckboxPublicName.get' C:\Users\Kyle\documents\visual studio 2010\Projects\Gadgets\Gadgets\Time.cs 130 17 Gadgets
Its because (I think) that "CheckboxPublicName" is not static, so trying to access it from the class "Form1" (unless its an object then you need to change your naming convention) wont work.
Try Form1 form = new Form1(); somewhere and then you can get the reference for the property form.CheckboxPublicName :P
Also instead of :
if (e.KeyData == Keys.Escape)
{
this.Visible = false;
Form1.CheckboxPublicName.Checked = false;
}
try this for a better code structure:
bool result = (e.KeyData == Keys.Escape);
Visible = form.CheckboxPublicName = result;
|
|
|
|
|
I wanna do something like
<br />
bool result = (e.KeyData == Keys.Escape);<br />
Visible = form.CheckboxPublicName = result;
But I wanna Keep
if (e.KeyData == Keys.Escape)<br />
{<br />
<br />
}
that border. and I don't really understand the
Visible = form.CheckboxPublicName = result;
Part, I want the toolstipitem to be unchecked not visible
|
|
|
|
|
You could make the property in form 1 "public static" so that you can use "Form1.CheckboxPublicName"
Use your original code instead of mine if its easier to understand
|
|
|
|
|
For the second part of your question: This is the wrong approach. Form2 should NOT be touching controls in Form1 at all.
Instead, assuming Form1 instanciates Form2 , Form1 should subscribe to an event raised by Form2 .
In Form1
form2.RequestUncheckPublicName += form2_RequestUncheckPublicName;
private void form2_RequestUncheckPublicName(object sender, EventArgs e)
{
}
In Form2
public event EventHandler RequestUncheckPublicName;
private void Time_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyData == Keys.Escape)
{
Visible = false;
OnRequestUncheckPublicName(EventArgs.Empty);
}
}
protected virtual void OnRequestUncheckPublicName(EventArgs e)
{
EventHandler eh = RequestUncheckPublicName;
if(eh != null)
eh(this,e);
}
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)
|
|
|
|
|
DaveyM69 wrote: EventHandler eh = RequestUncheckPublicName;
if(eh != null)
eh(this,e);
+5 for showing the correct way to do this, ensuring that the handler has not been dereferenced between the null check and the actual raising of the event.
"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
|
|
|
|
|
Ok this is the last error and if you fix it I will finally be done with this
code
public static ToolStripMenuItem CheckboxPublicName<br />
{<br />
get<br />
{<br />
return this.timeToolStripMenuItem;<br />
}<br />
}
Location Of Error
return this.timeToolStripMenuItem;
The Error
Error 1 Keyword 'this' is not valid in a static property, static method, or static field initializer C:\Users\Kyle\documents\visual studio 2010\Projects\Gadgets\Gadgets\Form1.cs 41 24 Gadgets<br />
|
|
|
|
|
That's because this can only be used in an instance, it cannot be used in a static property.
"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
|
|
|
|
|
Yeh but when I take the static off I get this error at this line
Form1.CheckboxPublicName.Checked = false;
Error 1 An object reference is required for the non-static field, method, or property 'Gadgets.Form1.CheckboxPublicName.get' C:\Users\Kyle\documents\visual studio 2010\Projects\Gadgets\Gadgets\Time.cs 130 17 Gadgets
|
|
|
|
|
That's because Form1 is the name of the form, and not the instance you are referring to. As has been pointed out to you many times now, the approach you are choosing here is very poor form, and not to be chosen. You have been told that the best method is to use a delegate/event - follow that pattern instead as it is a much better OOP design.
"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
|
|
|
|
|
Seriously, quit trying to access controls on Form1 from Form2 . It's not any easier than doing things the correct way and ends up messy. Trust me, drop that idea now... really!
Consider the following - it may not apply right now, but if you continue with programming you can be sure it will one day very soon.
Imagine you are developing a new class/control that you want to reuse across applications, and maybe even sell. In your use of this class, depending on a result you want to disable certain controls.
You could pass the controls to your class in a constructor or via a property/method - sounds good? No! If you want to reuse your class elsewhere, or others (customers) use it - it just isn't going to work without those controls which may not be relevant to their useage. So your class is broken. It will end up having to be re-written, or in the case of customers - refunds given.
Worse still, you could attempt the method you are trying, and access public static fields/properties on the object that owns the controls. What if they don;t exist? Again, your class is just plain broken.
However, you could adopt the same principles used by MS and all the others. When they developed for example the Button control they had no idea what you would do with it and certainly had no knowledge of the classes/objects that you would use it with, but still it works perfectly without issues. They provided a few properties to control it's appearance etc, and the most important thing - a Click event. Now you handle that event easily and everything is good in the world, yet Button has no knowledge of your classes/objects that are using it and it never should have.
Do the same, Form2 should let the world and all their relatives/friends know that something has happened (an event) that they may want to react to. If they are interested they can opt to listen for this event by adding a handler and respond accordingly. In your case this is Form1 that should handle an event from Form2 and only Form1 should do the thing with the menu item.
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)
|
|
|
|
|
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
|
|
|
|
|