-1

I want to trigger a BUTTON_CLICK event IF the condition("file.length()<0") is true. I tried as follows but I am getting error as "WindowsFormsApplication1.Form1.info is a 'field' but used as a 'type' ". Can you give your inputs please. Thank you.

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        private  int flag;
        public Form1()
        {
            InitializeComponent();

        }


         FileInfo info = new FileInfo("c:\\test.txt");
            if (info.Length > 0)  // throws an error "invalid token if in class"
                flag= 1;             //throws an error "WindowsFormsApplication1.Form1.info               
                                       is a 'field' but used as a 'type' "  
                                     //similar error for flag as for info

    if(flag==1) //similar error for flag as for info
    {

    private void button1_Click(object sender, EventArgs e)
        {
            //Code for Redirection to New Form


        }
    }
    }
}
Vignesh Kumar A
  • 27,863
  • 13
  • 63
  • 115
Kandarp Joshi
  • 219
  • 2
  • 4
  • 12
  • why you can't check flag in the `button1_Click`? – Guru Stron Dec 17 '13 at 05:33
  • 1
    I'm not trying to sound harsh, but it seems as though you need to refer to some introduction tutorials on classical programming in C#. I *think* what you are trying to do is something that simply isn't possible. That is to say, there are other ways to do said thing, but your understanding of the language may be fundamentally flawed. – cwharris Dec 17 '13 at 05:52
  • Thank you Christopher. Yes, I am a newbie. I will consider your inputs for sure. – Kandarp Joshi Dec 17 '13 at 05:59

5 Answers5

1

You cannot put an eventhandler inside an if statement but you can trigger button click. Modify your if statement to.

if(flag==1)
{
    button1.PerformClick();
}

Also, all the codes throwing you an error are in the class directly, they should be in a method instead. Modify your code to.

private void CheckFile()
{
    FileInfo info = new FileInfo("c:\\test.txt");
        if (info.Length > 0) 
             flag=1;

    if(flag==1)
    {
        button1.PerformClick();
    }
}

Then you can call CheckFile(); wherever you need to check if the size of data in the file is > 0.

  • I did the same. I called the method in my main function as "formObject.CheckFile(); " But it's not working. The button click event is not triggered. I also tried the other options of button_click calls. – Kandarp Joshi Dec 17 '13 at 07:35
1

Your code isn't compilable. Here is the reasons:

  1. The conditions must be placed into some method
  2. The declaration of event handler must be moved out from the condition scope

Here is the workable code:

namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        private  int flag;
        FileSystemWatcher watcher = new FileSystemWatcher();

        public Form1()
        {
            InitializeComponent();
            watcher.Path = @"C:\";
            watcher.Filter = "test.txt";
            watcher.Changed += watcher_Changed;
            watcher.EnableRaisingEvents = true;
        }

        private void SetFlag()
        {
            FileInfo info = new FileInfo("c:\\test.txt");
                if (info.Length > 0)  
                    flag= 1;
        }

        private void CheckFlag()
        {
            if(flag==1)
            {
                button1.PerformClick();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Code for Redirection to New Form
        }            

        void watcher_Changed(object sender, FileSystemEventArgs e)
        {
            SetFlag();
            CheckFlag();
        }
    }
}

And now you can call method SetFlag() to set flag. And use method CheckFlag() to check flag. In this example I use FileSystemWatcher to catch all file changes and call SetFlag() and CheckFlag() inside the handler of event Changed

Instead of button1.PerformClick() you can use button1_Click(this, EventArgs.Empty)

Max
  • 133
  • 7
  • I did the same. I created a main method and tried calling every method as follows:: private void button1_Click(object sender, EventArgs e) { MessageBox.Show("File length > 0"); } public static void main(String[] args) { Form1 f=new Form1(); f.SetFlag(); f.CheckFlag(); } But it is not working somehow. I'm not getting the Message Box. I checked with the file and it's working fine. – Kandarp Joshi Dec 17 '13 at 07:19
  • Are you sure that file "test.txt" is not empty? Are you sure that you added handler for event button1.Click? There is two way to add event handler: 1. using form designer 2. manually add this code into Form1 constructor: `button1.Click += button1_Click;` now you can put your code with message box into the method `button1_Click` – Max Dec 17 '13 at 07:43
  • Yes, I m sure that the file is not empty. I added the button1.Click handler in constructor. The thing I am trying to do is that the button click should be triggered automatically if the length of the file is greater than 0 i.e "file.Length()>0". But the action is not performed on execution. – Kandarp Joshi Dec 17 '13 at 08:54
  • If you want to automate file monitoring, then watch [this link](http://stackoverflow.com/questions/721714/notification-when-a-file-changes). You should put calls of the `SetFlag()` and `CheckFlag()` into the handler of event `Changed` of the `FileSystemWatcher` – Max Dec 17 '13 at 09:08
  • 1
    I've made some changes in code sample. You can check it. It should work as you want now. – Max Dec 17 '13 at 09:33
  • Thanks alot for your efforts mate. But still my form is the same as it was before. the Automatic trigerring of button_click event is still not executed. There are no errors at all. But somehow there is issue in invocation of methods "SetFlag()" and "CheckFlag()" as I m throwing message boxes on their calls but I m not getting those msg boxes at all. And niether the "button1.PerformClick()" seems to work. @Max – Kandarp Joshi Dec 17 '13 at 11:45
  • Hello @Max The issue I am facing now is that the moment I run the application, the automatic button click is triggered before form load. I tried calling button_click in form load but still the button is triggered before the form loads. Any inputs on that? – Kandarp Joshi Dec 18 '13 at 04:44
  • 1
    Hi, @KandarpJoshi, the `Load` event appears while the form is loading. But I suppose you need to do your action when the loading of your form is finished. You can try to attach action to `Activate` event, but this is not good, because this event will happend each time when form will be activete. Another desision is to try to use `Timer`. This will allow you to make your action periodically – Max Dec 18 '13 at 05:04
  • Yes, I want to trigger it everytime myForm1 loads. It is kind of an entry point of my application. Talking of timer, I used it but still the form is not loaded prior to the function calls. I also used Form_Load method but still the issue prevails. The Activate event should work I guess. Can you help me with the Activate event please? – Kandarp Joshi Dec 18 '13 at 05:42
  • you can use form designer to create handler for event `Activated`, and also you can do it manually. Add this code `Activated += Form1_Activated;` into your form's constructor. And add the handler for this event, just like a handler for the button click event. `private void Form1_Activated(object sender, EventArgs e) { /*your action*/ }` – Max Dec 18 '13 at 06:04
  • I got the solution with Activate event. Thank you. – Kandarp Joshi Dec 18 '13 at 08:57
0

Method1

button1.PerformClick();

Method2

this.button1.Click += new EventHandler(button1_Click);
zey
  • 5,939
  • 14
  • 56
  • 110
0

this

if (info.Length > 0)  // throws an error "invalid token if in class"
flag= 1;             //throws an error "WindowsFormsApplication1.Form1.info               
                     //is a 'field' but used as a 'type' "  

and this code

if(flag==1) //similar error for flag as for info
{

}

will throw you compile time error cause such this is code for class methods not class itself

A class is like a blueprint. It defines the data and behavior of a type. Basically c# class can contain only methods, properties, fields, events, delegates and nested classes.

so your class should look like:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        button1.Click += button1_Click;
    }     

    private void button1_Click(object sender, EventArgs e)
    {
        FileInfo info = new FileInfo("c:\\test.txt");
        if (info.Length > 0)  
        {           
            //Code for Redirection to New Form
        }
    }
}
Guru Stron
  • 102,774
  • 10
  • 95
  • 132
0

You can't put if-else statment in class, you may put it into a method.Try this:

  private void button1_Click(object sender, EventArgs e)
  {
     FileInfo info = new FileInfo("c:\\test.txt");
     if (info.Length > 0)  flag = 1;                         
     if(flag==1)
     {
            //Code for Redirection to New Form
     }
  }
Chris
  • 339
  • 4
  • 17