Click here to Skip to main content
14,268,557 members
Rate this:
Please Sign up or sign in to vote.
See more:
I'm using one button for "on and "off" in c#. When i click that button it is "on" and when i click that same button second time it is "off". Now i want to do this within one click. This means that once i click and hold that button it should be "on" till i release the button, when released it should be "off". Please let me know how can i do that !

What I have tried:

protected void Page_Load(object sender, EventArgs e)
        ardo = new SerialPort();
        ardo.PortName = "COM5";
        ardo.BaudRate = 9600;
    protected void BlueOn(object sender, EventArgs e)
        if(Session["currentState"] == null)
           Session["currentState"] = "2";

       if(Session["currentState"].ToString() == "1")

            Session["currentState"]= "2";

            Session["currentState"]= "1"; 

Updated 8-Aug-19 1:32am
Richard MacCutchan 7-Aug-19 2:20am
It is more usual to use 0 for off and 1 for on.
Richard MacCutchan 7-Aug-19 7:52am
Which serial port are you trying to use? If it is not the one on the web server then your code will never work.
Member 14165241 7-Aug-19 8:55am
Please check my code up there."ardo = new SerialPort();
ardo.PortName = "COM5";
ardo.BaudRate = 9600;"
I have to pass the value "1" or "2" to this "ardo" port by "ardo.Write(Session["currentState"].ToString());" .
How can i do this when using script ?
Richard MacCutchan 7-Aug-19 9:12am
The code is irrelevant if you think this is going to connect to the client's serial port. What exactly are you trying to do?
Member 14165241 7-Aug-19 9:19am
As you can see from my code up there, when i press the button "BlueOn" it send the value "1" or "2" to the arduino port "ardo.PortName = "COM5";". For these 2 value i need to press the button 2 times. Now i'm trying to do something like when i press and hold the button it will pass the value "1" and when i release the button it will pass "2".
Richard MacCutchan 7-Aug-19 10:12am
According to your tags this is a web application (ASP.NET). So, I will ask once again: where do you expect the serial port to be connected?
Member 14165241 7-Aug-19 22:27pm
Serial port is connected with arduino. When i press the button from web application it pass the value to arduino.
Richard MacCutchan 8-Aug-19 3:58am
The serial port is a physical connection, so how is it connected physically. And what is it connected to?
Member 14165241 8-Aug-19 4:13am
There is usb port in server and i connect arduino with usb port of server. That port could be "COM3" or "COM4".
Richard MacCutchan 8-Aug-19 4:32am
Then you need server side code to send data to the device. But that cannot be controlled from a web browser, which runs on the client.
Member 14165241 8-Aug-19 5:09am
I'm not sure but maybe with jquery ajax it is possible.
Richard MacCutchan 8-Aug-19 5:46am
Yes, possibly. But what exactly are you trying to do with the arduino device that will help the users of your web page?
Member 14165241 8-Aug-19 5:54am
From web page i would like to on off a light using arduino. Can you help me ?
Richard MacCutchan 8-Aug-19 6:00am
Since the user of the web page cannot see the device there is really no point.
Rate this:
Please Sign up or sign in to vote.

Solution 1

Don't do it in C# - all C# code runs in the server, so it means a round trip via the internet for everything - or your UI will be as fluid and responsive as treacle! Instead, do it in the client via Javascript, via the up and down events there: onmousedown Event[^]
Member 14165241 7-Aug-19 2:16am
Can you please give me example code, so i can follow that ...
George Swan 7-Aug-19 2:27am
A neat way to change the value of a ‘bool’ on each occurrence of a specific event is to use the NOT logical operator e.g. IsClicked=!IsClicked
OriginalGriff 7-Aug-19 2:50am
Yes, but still - do it in the Client!
Member 14165241 7-Aug-19 3:00am
Kindly help me on this matter
OriginalGriff 7-Aug-19 3:11am
Help you how? Have you followed the link and tried to do it yourself?
If so, how far did you get?
Member 14165241 7-Aug-19 5:35am
How can i pass string value by script as like code behind button ?
Ralf Meier 7-Aug-19 6:16am
you should carefully read what was suggested in this solution.
Take (for example) the MouseDown-Event for Set to "2" and the MouseUp-Event for set to "1".
Both Events are also connected to the Button like the MouseClick-Event what is alrready used by you ...
Member 14165241 7-Aug-19 7:10am
I understand that. When i am pressing and releasing button color is changing. But i'm confused how i can pass this value to serial port. Please help me to fix this issue.
Ralf Meier 7-Aug-19 13:40pm
You should give more Information.
I suppose you are allready connected to a COM-Port. So now which action from the COM-Port should change the Color and which action should change it back.
If you want help YOU have also to help and YOU have to give the required information. You should not ignore questions from us ...
Member 14165241 7-Aug-19 22:26pm
Yes , thanks for your appreciation. Color is different part, that one i just tried to test how script works. In my code up there you may find that there is a port specified for arduino. When button "BlueOn" is pressed it pass value "2" and when again press it send value "1" to the port, so light will on and off following command. Now i want to implement this within single click. This means that i'm trying to do something like when i press the button it will pass the value "1" to arduino so light will be on and when i release the button it will pass "2" to arduino so light will off.
Ralf Meier 9-Aug-19 5:36am
I think your understanding is wrong ...
If you are creating a "Smart Home Control" it should (my opinion) like this :
You press the Button the first time it sends (for example) a "2" to the Comport (to switch the light on) and the Button-Color changes to green.
After some time you press the Button again - now you should send a "1" to the Comport (to switch the light off) and the Button-Color goes back to grey.
Next Button-Press will again change to "2" and so on.
Is it that what you are trying to achieve ?
If Yes :
All the action should come from the ButtonClick-Event and it's method. The last value you send to the Comport must be memorisized in your application this value makes the decision inside your method - basicly as posted by you in your question.
So ... please help me to understand ... where is your actual problem ...? (Because I don't understand why you want to hold the Button for the time the light is needed - the light is only on during the time the Button is pressed).
Member 14165241 7-Aug-19 7:10am
I understand that. When i am pressing and releasing button color is changing. But i'm confused how i can pass this value to serial port. Please help me to fix this issue.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Instead of using a single event, try with two separate

for example:

private void BlueOn_Click(object sender, EventArgs e)
            //This will be fired as soon as the button is clicked
            //Code for on condition

private void BlueOn_MouseUp(object sender, MouseEventArgs e)
            //This will be fired as soon as you release the click
            //Code for off condition
Member 14165241 8-Aug-19 8:57am
In that case what will be code from client site i mean in aspx ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100