Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Friday, July 9, 2010 12:26 PM
I am trying to write a windows forms application where I need to declare a "void" that hadles the "me.click" function.
I have found out that c# uses :this" instead of "me" but I don't know how to make a void that will do this :(
This is the vb.net version of what I want to do:
Sub Collapse() Handels Me.Click
If Me.Height
But I want to do this in c# so I tried this.
void Collapse() Handles this.click
If this.height
but that won't work.
Does anybody have any Idea what I need to do?
Any help would be greatly appreciated.
Thanks in advance.
Jai Brown
All replies (10)
Friday, July 16, 2010 9:21 AM âś…Answered
Hello,
The keyword 'this ' can only be used in class members, it refers to the current instance of the class.
public partial class Form1 : Form
{
private int readonly StartHeight;
private int readonly MinimumHeight = 50;
public Form1()
{
InitializeComponent();
this.StartHeight = this.Height;
}
}
Eyal, Regards.
Friday, July 9, 2010 12:34 PM
C# doesnt handle event the same way as vb.net,
private void Collapse(object sender, EventArgs e )
{
}
In c#, either you add eventhandler through the control properties window or manually code it
button1.Click += new System.EventHandler(Collapse);
also, you can read more about eventhandling in c# http://www.c-sharpcorner.com/uploadfile/ddutta/eventhandlinginnetusingcs11092005052726am/eventhandlinginnetusingcs.aspx
kaymaf
CODE CONVERTER SITE
http://www.carlosag.net/Tools/CodeTranslator/ .
http://www.developerfusion.com/tools/convert/csharp-to-vb/ .
Friday, July 9, 2010 12:38 PM
Don't forget to do the following in your clean up code
button1.Click -= new EventHandler(Collapse);
Kev
Friday, July 9, 2010 12:40 PM
You can just do this (to save typing mostly, but also gives you cleaner looking code):
button1.Click += Collapse;
. . .
button1.Click -= Collapse;
Friday, July 16, 2010 3:46 AM
Ok maybe you guys didn't understand me. I need to know how to set a void as the event handler for when the form has been clicked.
This is my source and maybe you will understand.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Collapsing_form
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void Collapsing(object sender, EventArgs e)
{
// Declaring Variables
int StartHeight = this.Height;
int MinimumHeight = 50;
if (this.Height > MinimumHeight)
{
while (this.Height > MinimumHeight)
{
this.Height -= 1;
}
}
else
{
if (this.Height < StartHeight)
{
while (this.Height < StartHeight)
{
this.Height += 1;
}
}
}
}
}
}
Now I need all the code in public void Collapsing() to execute when I click the form.
Can anybody help me with this????
Friday, July 16, 2010 4:45 AM
Hello,
There you go.
private void Form1_Load(object sender, EventArgs e)
{
this.Click += Collapsing // or new EventHandler(Collapsing);
}
Eyal, Regards.
Friday, July 16, 2010 9:08 AM
Thank you that helps but I still have one problem.
The IDE won't allow me to declare a variable outside of a void like I need to.
This is what I need it to look like.
public partial class Form1 : Form
{
// Declaring Variables
int StartHeight = this.Height;
int MinimumHeight = 50;
But when I do that I get an error below "this" and it says
"Error 1 Keyword 'this' is not available in the current context C:\Users\Administrator\AppData\Local\Temporary Projects\WindowsFormsApplication1\Form1.cs 15 27 WindowsFormsApplication1"
because if I declare that in Form1_Load the value of StartHeight will change every time I click on the form.
Which incidentally causes a problem as the form won't resize the way I want it to :(
Any help there guys?
Thanks in advance
Jai Brown
Friday, July 16, 2010 9:56 AM
Hi Jai Brown,
Please mark the answers to your question in this thread, and create another thread for your new question.
Thanks,
Harry
Please remember to mark the replies as answers if they help and unmark them if they provide no help. Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
Friday, July 16, 2010 10:02 AM
Thank you Eyal-Shilony for your help.
That has solved that problem now there is only one left :(
No matter what height I set my form it will only return to one.
Does anybody have any idea why?
Try this code out on a new form and you will see that there should be no errors :(
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace Collapsing_form
{
public partial class Form1 : Form
{
// Declaring the integer's that are needed
private readonly int StartHeight;
private readonly int MinimumHeight = 100;
public Form1()
{
// Giving a value to StartHeight
this.StartHeight = this.Height;
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
/* Calling the Collapsing function when the user clicks
* on the form. */
this.Click += new EventHandler(Collapsing);
}
public void Collapsing(object sender, EventArgs e)
{
if (this.Height > MinimumHeight)
{
while (this.Height > MinimumHeight)
{
this.Height -= 1;
}
}
else if (this.Height < StartHeight)
{
while (this.Height < StartHeight)
{
this.Height += 1;
}
}
}
}
}
Can anybody see what is wrong because I can't.
I have the vb.net version of this code and it works great but for some reason it is playing up now.
Saturday, July 17, 2010 2:05 AM
Since the properties (including Height) are set in InitializeComponent, set StartHeight after calling it, not before. By the way, you can set the Click event handler at that time too.
public Form1()
{
InitializeComponent();
// Giving a value to StartHeight
this.StartHeight = this.Height;
// Calling the Collapsing function when the user clicks
// on the form.
this.Click += new EventHandler(Collapsing);
}