Create a Checkbox

51

4

Write a program that creates a two-state checkbox that people can interact with using a mouse.

Specifically, your program should achieve all of the following:

  1. Have a 8×8 pixel (or larger if desired) region of the screen that is the clickable area of the checkbox. Henceforth this region is simply called the checkbox.

  2. When the mouse cursor is moved within the checkbox and the default* mouse button is pressed, the checkbox should toggle states.
    Checked becomes unchecked. Unchecked becomes checked.

  3. The checkbox should not move when it is toggled.

  4. In the checked state, the checkbox may be any colors.

  5. In the unchecked state, the checkbox may be any colors as long as at least 16 pixels are visually distinct from the checked state.

  6. In a single program instance, all checked states should be visually identical to one another and all unchecked states should be visually identical to one another.

  7. Don't end the program until it's explicitly terminated (e.g. via exit button or Alt+F4), so a user can click the checkbox as much as they want.

*You may assume the default mouse button is always left click, but it's also fine to use the default as defined by mouse software or the OS, which may not actually be left click depending on the user.

Notes

  • It does not matter what is outside of your checkbox region. It could just be the desktop. It could be a portion of a console that changes on every toggle.

  • It does not matter what state your checkbox starts in.

  • The checkbox region may have any dimensions at or above 8×8 pixels. It need not be square.

  • You may make minor allowances for settings that are out of your control such as console font size, browser zoom, monitor resolution, etc. As long your program works in a reasonable test setting it should be valid.

  • If your program opens a window, you may assume it has been dragged to an appropriate location on the screen if necessary (e.g. top left corner).

  • You may use markup languages such as HTML or other languages we usually don't consider as full-fledged programming languages.

  • Your checkbox must be be toggleable using the default mouse button. It's alright if it also toggles for other forms of input (e.g. right mouse button), with the exception of mouse motion. i.e. the state should not change if the mouse is merely moved within the checkbox.

  • Screenshots of your checkbox in action are highly encouraged!

Scoring

The shortest code in bytes wins.

Example

A canonical HTML example in 23 bytes.

<input type="checkbox">

For me in Google Chrome, this makes a 12×12 pixel checkbox and about 30 pixels clearly change between checked and unchecked states.

I make the allowance that browser zoom is at 100%.

Calvin's Hobbies

Posted 2017-02-05T11:04:12.307

Reputation: 84 000

2Can the checkbox cover the entire screen? – user41805 – 2017-02-05T11:19:52.400

@KritixiLithos Yes, that would be valid.Though note that you may, if desired, count a particular 8x8 region as the official checkbox, even if a larger region works. – Calvin's Hobbies – 2017-02-05T11:22:22.023

@Helka Homba - is it allowed for an app to display other controls, aside the checkbox itself ? – zeppelin – 2017-02-05T12:54:21.833

Is it OK for the checkbox to toggle on other sorts of input? (For example, is a solution correct if the checkbox toggles on any sort of user input, regardless of whether it's a mouse click, a mouse movement, or a keypress? It'd toggle on a mouse click, but it'd toggle on other stimuli too.) – None – 2017-02-05T16:27:11.977

Would touching a touchscreen instead of using a mouse be acceptable? – 12Me21 – 2017-02-05T17:00:16.787

When the mouse cursor is moved within the checkbox and the default mouse button is pressed Can we also toggle if the user clicks outside the checkbox or with another mouse button apart from the left one? – Dennis – 2017-02-05T18:25:27.787

1@zeppelin "It does not matter what is outside of your checkbox region. It could just be the desktop. It could be a portion of a console that changes on every toggle." So, as long as the checkbox is there, it doesn't matter what else is. – Calvin's Hobbies – 2017-02-05T20:42:28.777

@ais523 / 12Me21 / Dennis it's ok if other forms of input, except for mouse-motion, (like keypress/touch/right click) also toggle the checkbox, whether inside the region or outside, as long as the default mouse button works to toggle the checkbox as well. – Calvin's Hobbies – 2017-02-05T20:49:23.753

Finally a task where KV lang, or any lang that creates an instance from checkbox string just wins. But I fear there's some character/byte in some gibberish that does the same thing and if not, someone will write it just to solve this. :D – KeyWeeUsr – 2017-02-05T22:05:50.203

Can the checkbox display after another region is clicked? For example, the program runs, part of what displays is a button, upon clicking the button, a checkbox which satisfies all other conditions is visible. – briantist – 2017-02-06T02:59:14.013

@briantist No, I think the checkbox should show up right away – Calvin's Hobbies – 2017-02-06T06:19:01.733

May we abuse external programs which has checkboxes? – Matthew Roh – 2017-02-09T05:22:55.400

"It does not matter what state your checkbox starts in." Does it matter if the initial state is distinct from the checked and the unchecked state? – thykka – 2017-02-09T08:23:59.493

Wait, does the checkbox have to be on the screen? 'cause we already have one on the keyboard! – Matthew Roh – 2017-03-28T09:28:41.840

Answers

33

Mathematica, 10 bytes

Checkbox[]

Assumes Mathematica's notebook environment.

The two states look like this:

enter image description here

Martin Ender

Posted 2017-02-05T11:04:12.307

Reputation: 184 808

73Sure. Mathematica cheats again. – Matthew Roh – 2017-02-05T12:45:33.343

1It's funny how the comment has more upvotes than the answer – RudolfJelin – 2017-02-09T15:56:24.800

4@RudolfL.Jelínek it's funny how the answer has any upvotes at all... – Martin Ender – 2017-02-09T18:28:14.910

31

HTML, 20 bytes

Don't know how valid this will be as an answer. HTML requires the tag to be closed to legally validate, but modern browsers will automatically close them so it's executable.

<input type=checkbox

Jan

Posted 2017-02-05T11:04:12.307

Reputation: 610

3I was looking at this as an option earlier (without quotes) but completely missed the closing >. +1 and valid IMHO. – ElPedro – 2017-02-05T21:08:21.703

1On this site, languages are defined by their implementation, so this seems fine to me. – John Gowers – 2017-02-06T15:59:07.093

1Thanks @JohnGowers , I've been wondering about the definition of how much abuse is acceptable. – Jan – 2017-02-06T16:05:09.483

27

Bash, 67 55 50 35 32 31 bytes

:()(read -p$[i^=1]
›?9h -sn6
:)

The code contains a CR byte (first newline) and a CSI byte (small right angle bracket with the Windows-1252 encoding). It requires xterm (or equivalent) and the ISO-8859-1 encoding (or similar).

The code defines a function named :, which alternately displays a white 1 or 0 in a black rectangle. There are exactly 24 differing pixels with xterm's default settings. (proof)

For an older version that toggles the color of the entire terminal, check revision 10 of this answer.

Thanks to @ais523 for suggesting xterm, which saves 4 bytes!

How it works

:(...) creates a function named : that executes ....

The read command does the actual magic.

  • -p specifies an input prompt, which is printed to STDOUT.

    • $[i^=1] is an arithmetic expansion which XORs the variable i with 1. i may initially be unset; when this occurs, it will be treated as 0.

    • The carriage return places the cursor back at the beginning of the line.

    • ›?9h captures the mouse in supported terminals. Each click will send six characters to the terminal, namely ←[Mbxy, where represents the ESC byte (0x1b). ←[ indicates an escape sequence, M the mouse, b the button (0x20 + the button number), x the x coordinate of the click (0x20 + coordinate), and y the y coordinate.

    • -sn6 makes read silent (the mouse input won't be echoed to STDERR) and stops after reading exactly 6 bytes. It saves the input in the REPLY variable, but we're not interested in the output it produces.

Finally, once read finishes (after exactly one mouse click), : recursively calls itself, entering an infinite loop.

Creation and invocation

$ echo $':()(read -p$[i^=1]\r\x9b?9h -sn6\n:);:' > checkbox.sh
$ xxd -c 17 -g 1 checkbox.sh
0000000: 3a 28 29 28 72 65 61 64 20 2d 70 24 5b 69 5e 3d 31  :()(read -p$[i^=1
0000011: 5d 0d 9b 3f 39 68 20 2d 73 6e 36 0a 3a 29 3b 3a 0a  ]..?9h -sn6.:);:.
$ LANG=en_US xterm -e bash checkbox.sh

Output

screencast

Dennis

Posted 2017-02-05T11:04:12.307

Reputation: 196 637

I was working on a similar solution to this myself (although in a different language, forcing me to give up because I couldn't find a way to take input without a newline). A likely improvement: change 1000h to 9h (the difference is that 1000h handles both mouse press and release, and 9h handles only mouse release). This might or might not need changes elsewhere in the code. – None – 2017-02-05T21:26:46.507

@ais523 I tried that, but at least my terminal emulator (Konsole) doesn't seem to support it. Do you know one that does? – Dennis – 2017-02-05T21:28:00.837

gnome-terminal seems to work, that's what I was using for testing (edit: but I don't think it supports a 1-byte CSI). xterm should also work and might be easier to install if you have a KDE system. (I just tested this myself; it works with \x1b[ but not with \x9b for some reason, possibly related to my stty settings rather than to the terminal itself.) – None – 2017-02-05T21:29:44.377

@ais523 I do have xterm. I'll check when I'm back at my PC. – Dennis – 2017-02-05T21:30:22.710

@ais523 xterm works like a charm! I'll update asap, with a few more byte savings. To use the CSI byte, you have to set the terminal to Latin-1. LANG=en_US should do the trick. Gnome terminal might require additional settings. – Dennis – 2017-02-05T21:53:09.830

@ais523 xterm saved 4 bytes. Thanks! – Dennis – 2017-02-05T23:07:06.080

8I love how it becomes :) from :( by the end – Andras Deak – 2017-02-06T23:08:41.667

22

Processing, 78 66 65 63 61 bytes

2 bytes saved thanks to @TuukkaX by using -a%2 instead of a%2*-1

2 bytes saved thanks to @TuukkaX by using a=-a

int a=1;void draw(){background(a);}void mousePressed(){a=-a;}

The checkbox alternates between being black and white.

Explanation

int a=1;                   //the variable that contains the state of the checkbox
void mousePressed(){a=-a;} //if the mouse is pressed, change the sign of a

Now there are many other alternatives like using an if-statement to process this, but then it gets weird and constantly changes the background while the mouse is being pressed. I first tried mouseButton, but then I would need more conditions and more code that will just end up much more verbose.

Now for mousePressed() to be called forever (otherwise it would just stop after the program has just started), we need a draw() function.

void draw(){               //loop infinitely
  background(a);           //set the sketch's background to -a%2
  //if a ==  1, the background colour is set to almost black
  //if a == -1, the background colour is set to white
}

enter image description here


A smaller checkbox would be 90 bytes (the checkbox is in the top left corner, so we can remove some mouse conditions):

int a=1;void draw(){fill(a);rect(0,0,8,8);}void mousePressed(){if(mouseX<9&mouseY<9)a=-1;}

user41805

Posted 2017-02-05T11:04:12.307

Reputation: 16 320

2Clever use of how Processing accept single integers as grayscale colours! – FlipTack – 2017-02-05T11:58:26.447

@FlipTack It seems that white can also be expressed as fill(-1) – user41805 – 2017-02-05T12:25:46.233

1I have an idea. Initialize a to 1 instead of 0. Change a++ to a=-a, which makes -a%2 just a. This will toggle a between 1 and -1. I don't know what color 1 produces though. – Yytsi – 2017-02-05T13:40:24.040

@TuukkaX Yup it works. color(1) produces #010101 which is very close to black – user41805 – 2017-02-05T13:54:24.517

22

HTML + JavaScript, 32 30 25 chars

<p onclick=innerHTML^=1>0

Qwertiy

Posted 2017-02-05T11:04:12.307

Reputation: 2 697

3this is implicit and can be removed. Also, innerHTML is shorter. – Dennis – 2017-02-06T17:41:37.350

@Dennis, strange with this... Thank you. – Qwertiy – 2017-02-06T17:59:32.827

+1 for not using the <input> element – RedClover – 2017-11-03T15:38:35.860

20

Unix Shell (+x11-apps), 3 bytes

Disclaimer

The answer below is boring, borderline and "feels cheating" (at least to me).

Yet, it does not seem to violate any of the challenge rules "as written", or "default loopholes", so I'm posting it.

(If you can point a specific rule it breaks, please comment on !)

Golfed

xgc

The xgc program demonstrates various features of the X graphics primitives.

The xgc screen is actually filled with various checkboxes:

enter image description here

and "sticky buttons" (which qualify as checkboxes, under this challenge rules):

enter image description here

zeppelin

Posted 2017-02-05T11:04:12.307

Reputation: 7 884

20

GitHub Flavored Markdown - 6 bytes

- [ ] 

(There is a trailing space)

In a gist : check/not check

Axel

Posted 2017-02-05T11:04:12.307

Reputation: 301

Sure this matches the 2nd half of the 1st sentence, “Write a program that creates a two-state checkbox that people can interact with using a mouse.”? – manatwork – 2017-02-07T19:58:55.210

1Yes, if you paste it in a gist and create the gist (for "compilation") you will be able to interact with it. – Axel – 2017-02-07T20:18:55.767

Interesting. Thanks @Ax31. I never used gists. – manatwork – 2017-02-07T20:36:06.583

1I would not have expected "Github Flavoured Markdown". – Jonas Schäfer – 2017-02-08T20:41:35.250

18

AHK, 25 bytes

Gui,Add,Checkbox
Gui,Show

Output -

Mouse pointer is not captured in this gif

Gurupad Mamadapur

Posted 2017-02-05T11:04:12.307

Reputation: 1 791

16

Scratch, 2 blocks

enter image description here

Put this script in the default sprite (which comes with 2 costumes)

SuperJedi224

Posted 2017-02-05T11:04:12.307

Reputation: 11 342

1

Online demo: https://phosphorus.github.io/app.html?id=143917094&full-screen=false

– primo – 2017-02-07T09:38:09.240

14

Python REPL, 45 bytes

from tkinter import*
Checkbutton(Tk()).pack()

Blue

Posted 2017-02-05T11:04:12.307

Reputation: 26 661

I don't participate in CG nor do I often read these posts. However it really surprises me that you are allowed to use external modules. Isn't that cheating? I don't know how many bytes the Tk source code is, but really shouldn't these bytes be added to your byte count? – FMaz – 2017-02-11T09:09:08.010

1@Krazor tkinter is a standard library for Python (but even if it wasn't I'd only have to declare Python REPL + tkinter). This means that I can use it's features by simply importing it. The penalty you suggest is similar to asking users to include in the byte count the number of bytes in the source of the language you're writing in. Here we allow any language or module to compete as long as it was written and published publicly before the challenge was posted. – Blue – 2017-02-11T09:27:49.000

Okay, thank you for the clarification. – FMaz – 2017-02-11T09:41:05.290

14

GeoGebra, 10 bytes

Checkbox[]

Entered into the input bar.

Here is a gif of the execution:

Program execution

TheBikingViking

Posted 2017-02-05T11:04:12.307

Reputation: 3 674

7Whoah, GeoGebra. I remember that. It's been a while. – Fund Monica's Lawsuit – 2017-02-05T21:18:38.967

I know this is off-topic but do you go to Waterloo Collegiate Institute? Just out of curiosity, based on your name. I'm not a stalker don't worry :) – HyperNeutrino – 2017-05-23T23:34:54.977

13

Tcl/Tk, 21 byte

Golfed

grid [checkbutton .c]

enter image description here

zeppelin

Posted 2017-02-05T11:04:12.307

Reputation: 7 884

11

HTML with JavaScript - 136 124 120 115 113 111 109 79 bytes

<p style=height:20;border:solid onClick=t=this;t.innerHTML=(t.e=!t.e)?'x':''>

-2 with thanks to @KritixiLithos

Reduced to 79 after some fantastic golfing from @Jan. Although the new version fits the requirements I have kept the old version for professional pride reasons.

While it is perfectly valid to use built-ins, I thought it was more fun to create my own from scratch. Tested with Chrome and Firefox. border:solid is there for IE compatibility but I don't know if it is actually needed in newer IE versions - I don't have IE available to test.

The px in the height is there for the benefit of "Run code snippet" so is not included in the "real" byte count. It works fine in the browser without it.

<p style=height:20px;border:solid onClick=t=this;t.innerHTML=(t.e=!t.e)?'x':''>

Previous version:

<p id=d style="width:20px;height:20px;border:solid 1px"onClick="d.innerHTML=d.innerHTML=='x'?'':'x'"align=center>

ElPedro

Posted 2017-02-05T11:04:12.307

Reputation: 5 301

1Can you do id=d to remove d=this? – user41805 – 2017-02-05T14:15:31.007

1@KritixiLithos - Indeed I can. Thanks and well spotted :) – ElPedro – 2017-02-05T14:23:21.097

1You don't need to set the width, the rules say it can be any dimensions. If you store the state on a property on this you'll save setting the id. If you store this in a property you'll save a few bytes. The align is unnecessary. Since there are no spaces in the attribute strings, you don't need the quotes.

<p style=height:20px;border:solid onClick=t=this;t.innerHTML=(t.e=!t.e)?'x':''> 79 bytes – Jan – 2017-02-05T19:19:52.217

@Jan - Thanks! The width bit is my own professional pride. If I don't set it it looks ugly and since this answer is never going to win and was just posted as an alternative I'll probably leave it there. Other than that some great hints. Many thanks. Will update tomorrow. – ElPedro – 2017-02-05T20:26:11.873

Thanks @Easterly. Updated the code but forgot the other bits. It's getting late. – ElPedro – 2017-02-05T20:44:44.453

The task is to make the code as short as possible. If that means abusing the language or the requirements, that's what you should do. I can relate to the feeling though – Jan – 2017-02-05T20:44:47.100

@Jan - Understood. I should stop thinking so much like a programmer and more like a golfer ;-) Thanks again. – ElPedro – 2017-02-05T20:46:50.600

Actually, for the challenge you could just skip the styling entirely in favour of just using a body element, making the entire application clickable. It won't work inside a code snippet, but you could post a jsbin instead like https://jsbin.com/pivumowuyi/edit?html,output

– Jan – 2017-02-05T20:51:51.200

Now that's getting really smart :-) Feel free to post as your own answer. You will get +1 from me. I'm going to leave this as it is now. – ElPedro – 2017-02-05T20:56:10.223

No, it's really just an extension of your answer. The logic is still the same, it's just been shifted. – Jan – 2017-02-05T21:18:19.943

Still ok by me if you mention my answer ;-) "Improvement of the answer from ElPedro with permission" would work for me. – ElPedro – 2017-02-05T21:19:38.143

Why use 'x' which is 3 bytes. Why not use 8 which is one byte. Rules don't say the check has to be anything specific. @Jan – Albert Renshaw – 2017-02-07T09:17:32.573

@Jan Also instead of saying t.e=!t.e you can just say t.e^=1 and knock off 2 more bytes – Albert Renshaw – 2017-02-07T09:19:53.930

@Jan Also you don't need the closing tag > you can simply leave it off for another saved byte :D (As referenced in your main answer +1'd btw) – Albert Renshaw – 2017-02-07T09:21:22.860

@AlbertRenshaw at OP's suggestion I posted my own version of this at http://codegolf.stackexchange.com/questions/109155/create-a-checkbox/109289#109289

– Jan – 2017-02-07T09:23:11.967

@AlbertRenshaw - Thanks for the hints. I think Jan's solution is way ahead of any improvements I can make on this now :) – ElPedro – 2017-02-07T09:29:25.720

You can use a <fieldset> and remove all styling – Scopey – 2017-02-08T04:05:12.027

You can remove ;border:solid and still meet the challenge requirements: <p style=height:20px onClick=t=this;t.innerHTML=(t.e=!t.e)?'x':''> (66 bytes). The works in IE 10 & 11, just like the original code does. – Julian Lachniet – 2017-02-08T11:53:23.760

9

Minecraft 1.0.0, 1 byte

A lever in Minecraft meets all the criteria for a check box. This answer is only valid for older versions of the game where punching a lever still toggles it.

enter image description here

Scored using @Connor O'Brien's suggested byte count.

Zavada

Posted 2017-02-05T11:04:12.307

Reputation: 189

A difference of 3 between two answers hardly makes or breaks a consensus, but considering that Conor's answer is quite controversial (currently at +24/-10) and that the accepted answer is overwhelmingly well received (+17/-0), I think the latter should be used to score Minecraft answers.

– Dennis – 2017-02-07T03:02:25.970

However there are no structure blocks in Minecraft 1.0.0 – ev3commander – 2017-02-08T12:37:10.790

1It's one n in Conor, btw :) – Conor O'Brien – 2017-03-28T17:38:57.073

Or you could rebind the left and right mouse buttons in any newer version to make it valid ;D – seadoggie01 – 2017-06-30T12:27:13.037

@ConorO'Brien Isn't that 1 block? Why 1 byte? I don't think mc stores every block as one byte... – RedClover – 2017-11-03T15:45:48.880

8

TI-Basic, 15 bytes

While 1:Shade(0,9:Input :ClrDraw:Input :End

Fairly straightforward. The checkbox covers approximately 40% of the screen and alternates between black and white. When checked, the screen will look something like this:

screen

Timtech

Posted 2017-02-05T11:04:12.307

Reputation: 12 038

8

Any version of Windows command line, 3 bytes

(cmd.exe, batch, powershell, start->run, whatever)

osk

Opens the on-screen-keyboard which has several "checkboxes" such as shift, ctrl, alt, capslock keys Similar to the xgc answer, and inspired by the SmileBASIC answer screenshot.

user65280

Posted 2017-02-05T11:04:12.307

Reputation: 81

7

Octave, 36 20 bytes

uicontrol('sty','c')

Thanks to @LuisMendo saved 16 bytes!

rahnema1

Posted 2017-02-05T11:04:12.307

Reputation: 5 435

1uicontrol('sty','c') also works – Luis Mendo – 2017-02-05T14:03:24.403

@LuisMendo Thanks! 1/2 byte counts saved! – rahnema1 – 2017-02-05T15:07:48.353

7

SmileBASIC, 49 bytes

@L
O=T
TOUCH OUT T,,
X=T<O!=X
KEY 1,""[X]GOTO@L

and are an empty box and a checked box in SB's font.
Toggle the checkbox by touching the screen (the closest thing to clicking with a mouse)

enter image description here enter image description here

Cheating answers:

0 bytes:

(the on-screen keyboard has buttons that act like checkboxes, such as the caps lock and insert keys)

14 bytes:

EXEC"SYS/SBGED

Opens up the built in graphics editor, which has multiple checkbox-like buttons

12Me21

Posted 2017-02-05T11:04:12.307

Reputation: 6 110

This is just brilliant, though I don't really understand what the 4th line is doing. Could you explain? – snail_ – 2017-02-08T01:06:36.930

T<O is true when you stop touching the screen, and != is used as a logical XOR. X=X XOR (O && !T) – 12Me21 – 2017-02-08T01:31:19.647

7

KV Lang, 8 bytes

CheckBox

enter image description here


assuming a really basic Python environment for kv lang to even run:

from kivy.app import runTouchApp
from kivy.lang import Builder
runTouchApp(Builder.load_string('CheckBox'))

This works basically because CheckBox is a Kivy widget included in a kivy.Factory as each widget from the standard library.

That simple string creates an instance of CheckBox and because of runTouchApp expecting a widget to run I can use Builder.load_string.

The background of load_string handles basically every kv string and if there is a single instance of a widget defined (e.g. CheckBox), then it is elevated to a root widget position, for example:

Application
    openGL Window
        root widget(mostly layout)
            main tree
            and some widgets
            ...

KeyWeeUsr

Posted 2017-02-05T11:04:12.307

Reputation: 181

7

C#, 124 120 bytes

saved 4 bytes thanks to grabthefish.

checkbox checked and not checked note: because of edits, the picture doesn't exactly represent what the code makes, the "checkBox1" isn't displayed with the current code.

using System.Windows.Forms;class P{static void Main(){var a=new Form();a.Controls.Add(new CheckBox());a.ShowDialog();}}

ungolfed: using System.Windows.Forms; class P{ static void Main(){ var a=new Form(); a.Controls.Add(new CheckBox()); Application.Run(a); a.ShowDialog(); } }

satibel

Posted 2017-02-05T11:04:12.307

Reputation: 221

If you didn't find anything about this on meta, ask about this on meta. – Matthew Roh – 2017-02-06T08:58:06.590

You can get rid of the spaces in var a = new Form();. – clismique – 2017-02-06T09:39:38.807

@Qwerp-Derp I removed them for counting, I just pasted the wrong thing, thanks. – satibel – 2017-02-06T09:57:53.307

you could use a.ShowDialog(); to save a few bytes – grabthefish – 2017-02-09T12:44:06.333

thanks @grabthefish, though c# still loses to java :( – satibel – 2017-02-09T13:09:13.343

a.Controls.Add(new CheckBox()) => new CheckBox(){Parent=a} – Qwertiy – 2017-02-26T21:54:32.100

Let's discuss byte count: http://meta.codegolf.stackexchange.com/q/11650/32091.

– Qwertiy – 2017-02-26T22:21:30.713

6

Excel VBA, 38 32 31 29 Bytes

Immediates Window function that inserts a checkbox of height=9 and width=9, at the cell A1 on the first sheet of the active Excel VBA project

Sheet1.CheckBoxes.Add 0,0,9,9

-6 Thanks to pajonk for replacing [A1],[A1],9,9 with 0,0,9,9

-1 for replacing Sheets(1) with [Sheet1]

-2 for realizing the above was stupid and using Sheet1 instead

Taylor Scott

Posted 2017-02-05T11:04:12.307

Reputation: 6 709

Seems that 1,1,9,9 as arguments works as well. – pajonk – 2017-02-05T18:14:54.777

1You dont need the Sheet(1)., you can run this quickly enough in Excel but CheckBoxes is inherint to the active sheet – Mr.Burns – 2017-02-06T10:56:26.210

@Mr.Burns I have checked into this, but without directly referencing any Worksheet object, the CheckBoxes.Add method throws an error stating "Object Required" – Taylor Scott – 2017-02-06T22:27:04.183

@TaylorScott You are correct, I put the code in a sheet module rather than a normal module .. My mistake xD – Mr.Burns – 2017-02-07T09:36:41.787

6

Ruby with Shoes, 16 characters

Shoes.app{check}

Sample output: checkbox created with Ruby

manatwork

Posted 2017-02-05T11:04:12.307

Reputation: 17 865

6

Powershell v5+, 94 Bytes

($a=[Windows.Forms.Form]::new()).Controls.Add([Windows.Forms.Checkbox]::new());$a.ShowDialog()

enter image description here enter image description here

This is pretty terrible, but I can't get the statements any shorter with either Add-Type or using namespace - and the variable is required, since I need to reuse the object to show the form, and Controls.Add() returns nothing usable.

ShowDialog is required on my system to have the box be interactive after running this command, otherwise the form is frozen.

also System.Windows.Forms.Form can only be shortened to Windows.Forms.Form before (at least my own) system stops auto completing it in a fresh session.

colsw

Posted 2017-02-05T11:04:12.307

Reputation: 3 195

Nice! Might want to add that this requires version 5+. – briantist – 2017-02-07T00:56:21.253

@briantist updated, thanks. – colsw – 2017-02-07T02:25:02.153

6

HTML with JavaScript, 46 33 28 bytes

<body onclick=innerHTML^=1>0

v2

<body onclick=this.innerHTML^=1>0

v1

<body onclick=t=this;t.innerHTML=t.e=!t.e||''>

This is a variant on @ElPedro's answer: Create a Checkbox . Got down to 28 bytes with the help of @Albert Renshaw It could be further reduced, but that would make it identical to Create a Checkbox

The whole application area is clickable. It will display 0 in the off state and 1 in the on state. Try it out at https://jsbin.com/nuhetiyocu/1/edit

Jan

Posted 2017-02-05T11:04:12.307

Reputation: 610

1Here's a neat variation at 39 bytes. <body onclick=t=this;t.innerHTML^=t?1:0 The 16 pixel rule between state change is up for debate though. – Albert Renshaw – 2017-02-07T09:28:45.040

38 <body onclick=this.innerHTML^=this?1:0 – Albert Renshaw – 2017-02-07T09:30:05.683

36 <body onclick=this.innerHTML^=1+this – Albert Renshaw – 2017-02-07T09:32:13.147

Nice on 33! That's a great solution hhahah – Albert Renshaw – 2017-02-07T09:32:25.087

1Oh no with your help I'm down on 33 already :) – Jan – 2017-02-07T09:32:30.730

I dont think I can beat that lol – Albert Renshaw – 2017-02-07T09:32:42.170

128 <body onclick=innerHTML^=1>0 – Albert Renshaw – 2017-02-07T09:36:20.893

1

Now that there's always a value inside the container, it could be changed from the body to a different element. But I just noticed that would make it identical to another answer http://codegolf.stackexchange.com/questions/109155/create-a-checkbox/109295#109295 so I'm just leaving it as is

– Jan – 2017-02-07T09:45:12.443

I always feel so original here until I see another answer in the same thread identical to my code hahaha – Albert Renshaw – 2017-02-07T09:46:47.700

Got a javascript solution down to 24 bytes now, different approach completely than what we were doing. http://codegolf.stackexchange.com/a/111422/16513

– Albert Renshaw – 2017-02-26T21:58:31.077

4

Pug, 20 bytes

input(type=checkbox)

theonlygusti

Posted 2017-02-05T11:04:12.307

Reputation: 1 221

Besides which I count is as 23. If you remove the quotes it still works on Firefox and Chrome (can't test on IE) and would come in at 21 in which case I would consider it an improvement on the example and therefore valid. – ElPedro – 2017-02-05T12:37:46.273

@heonlygusti - indeed but without the quotes I would have upvoted :-) btw, the downvote wasn't me. – ElPedro – 2017-02-05T12:40:37.143

I would have up-voted if it wasn't for those quotes, which are not needed. – NightSkyCode – 2017-02-06T19:34:42.830

@theonlygusti not at all. Give it a try. – NightSkyCode – 2017-02-06T20:27:01.540

4

VBA (Word), 74 57 bytes

-17 thanks to Taylor Scott

Sub a()
f = a.FormFields.Add(Selection.Range, 71)
End Sub

Filename of the document should be a.

Daniel

Posted 2017-02-05T11:04:12.307

Reputation: 1 808

1You can replace the wdFieldFormCheckBox with 71 (the integer value of the wdFieldFormCheckBox constant) for a savings of 17 bytes – Taylor Scott – 2017-02-05T17:27:43.107

I think you can remove the newlines for -2 bytes. – devRicher – 2017-02-05T23:08:41.723

4

Clojure, 201 195 145 127 bytes

(ns c(:require[quil.core :as q]))(def a(atom nil))(q/defsketch b :draw #(q/background(if@a 0 99)):mouse-clicked #(swap! a not))

-6 bytes by getting rid of the need for m/fun-mode.

-50 bytes by removing the :setup and :size options, since they are defaultable and unnecessary in non-fun-mode. Also removed the unnecessary import for middleware.

-18 bytes thanks to @ASCII-only. Changed to "smaller" colors, and removed an unnecessary space before the dereference operator (@).

Creates a Quil sketch (a Clojure library that provides a layer over Processing), therefore, this requires the Quil library to run.

(ns bits.golf.checkbox
  (:require [quil.core :as q]))

; Mutable global variable to hold the toggle state
(def a (atom nil))

(q/defsketch b
         ; Draw the background color to be either black or red depending on the toggle state.
         :draw #(apply q/background (if @a [0 0 0] [255 0 0]))

         ; Negate the state on mouse click
         :mouse-clicked #(swap! a not))

enter image description here

enter image description here

Carcigenicate

Posted 2017-02-05T11:04:12.307

Reputation: 3 295

Can't you replace 255 with 99? Also I'd try removing spaces before/after symbols and seeing if it still works – ASCII-only – 2017-03-28T12:45:34.390

@ASCII-only yes, I could. It made a rant ugly color though iirc, and for some reason I prioritized that at the time. And no, this is as much space as I can remove unfortunately. – Carcigenicate – 2017-03-28T13:35:22.397

@ASCII-only The problem is symbols can contain almost any character, so really the only "safe neighbours" that you can remove spaces around are any kind of bracket, or quotes. Luckily this is a lisp to the former is pretty prevalent. – Carcigenicate – 2017-03-28T13:42:24.990

Hmm maybe [0 0 99] then? (some kind of dark blue) – ASCII-only – 2017-03-28T21:52:12.280

@ASCII-only Actually, I think I can drop the 0s now that I think about it. I think Quil auto pads the front with 0s if the vector is to short. If I remember latest I'll fix it up. Thanks for getting me thinking. – Carcigenicate – 2017-03-29T13:27:02.360

Yeah, (ns c(:require[quil.core :as q]))(def a(atom nil))(q/defsketch b :draw #(q/background(if@a 0 99)):mouse-clicked #(swap! a not)) works, I just read the docs, looks like it doesn't autopad, but it uses the value as a grayscale value. (Plus @a doesn't need a space before it)

– ASCII-only – 2017-03-29T22:12:09.693

@ASCII-only Thanks for the suggestions! I was occupied by the color channel challenge (and work) all day today. I'll try to incorporate your changes tomorrow. – Carcigenicate – 2017-03-30T00:56:31.500

4

Operation Flashpoint scripting language, 273 bytes

To show the checkbox:

createDialog"b"

But first it must be defined:

Save the following to a file named description.ext in the mission folder:

class b{idd=999;movingEnable=1;class controls{class c{idc=457;x=0.1;y=0.1;w=0.1;h=0.1;text="";action="if(ctrlText 457=={})then{ctrlSetText[457,{V}]}else{ctrlSetText[457,{}]}";type=1;style=2;colorText[]={1,1,1,1};font="tahomaB36";sizeEx=0.1;}}}

The checkbox in action:

Steadybox

Posted 2017-02-05T11:04:12.307

Reputation: 15 798

3

AutoIt, 78 Bytes

GUICreate(0)
GUISetState(GUICtrlCreateCheckbox(0,0,0))
Do
Until GUIGetMsg()=-3

Opens a maximized window that can be closed like any other windows window. The maximization and ... on-top-ness is a side effect of the golfing.

enter image description here

mınxomaτ

Posted 2017-02-05T11:04:12.307

Reputation: 7 398

How would I run this on Linux? – dkudriavtsev – 2017-02-07T18:24:12.657

@wat wine AutoIt3.exe script.au3, where the script.au3 file contains this code. – mınxomaτ – 2017-02-07T19:13:54.630

3

Python, 62 bytes

from tkinter import*
a=Tk()
Checkbutton(a).pack()
a.mainloop()

Blue

Posted 2017-02-05T11:04:12.307

Reputation: 26 661

3

Slim, 21 Bytes

input type="checkbox"

Try it here

So yeah. One of slim's major features is that < and > characters are not there. So basically, I get 2 bytes less than the creator of the question!

Spydercrawler

Posted 2017-02-05T11:04:12.307

Reputation: 61

1

HTML still beats it though :) http://codegolf.stackexchange.com/a/109189/44888

– Jan – 2017-02-06T01:40:57.350

remove quotes for better points – dkudriavtsev – 2017-02-07T08:10:11.137

@wat It seems that's not allowed in Slim – Metoniem – 2017-02-08T14:27:36.403

@Metoniem That's excessively ironical. – Right Leg – 2017-02-10T04:36:36.663

3

Bash + zenity, 31

zenity --checklist --column a a

enter image description here

enter image description here

Digital Trauma

Posted 2017-02-05T11:04:12.307

Reputation: 64 644

3

Bash (+yad), 23 bytes

Golfed

yad --form --field :chk

YAD is a program that will display GTK+ dialogs, and return (either in the return code or on standard output) the users input. This allows you to present information, and ask for information from the user, from all manner of shell scripts.

Yad is available in Debian and Ubuntu repositories (and probably others).

Demo

enter image description here

zeppelin

Posted 2017-02-05T11:04:12.307

Reputation: 7 884

3

Java 1.4+, 104 bytes

import java.awt.*;interface g{static void main(String[]a){new Frame(){{add(new Checkbox());show();}};}}

-13 thanks to Kevin Cruijssen.

enter image description here

Groovy, 67 bytes

import java.awt.*;{new Frame(){{add(new Checkbox())setVisible(1)}}}

Magic Octopus Urn

Posted 2017-02-05T11:04:12.307

Reputation: 19 422

Does !0 work instead of 1>0? It does in groovy. – Gurupad Mamadapur – 2017-02-08T11:40:56.617

The exact same thing is 68 bytes in groovy excluding main method signature and ending ;. – Gurupad Mamadapur – 2017-02-08T11:43:35.070

@GurupadMamadapur in groovy it'd just be setVisible(1). In Java 8 it works; in all versions of java though, that is not valid syntax. – Magic Octopus Urn – 2017-02-08T13:47:32.650

Oh okay. I just tried executing the groovy code (V. 2.4.7), I got EOF error. This works though - import java.awt.*;new Frame(){{add(new Checkbox());setVisible(!0)}} (67 bytes) – Gurupad Mamadapur – 2017-02-08T13:57:21.763

1Ah, you're right. It was treating the .*{ as a vectorized closure on java.awt so the ; is required. – Magic Octopus Urn – 2017-02-08T14:01:26.897

1That closure won't execute - Error-Ambiguous expression could be either a parameterless closure expression or an isolated open code block. Also, 1 won't evaluate to true. It raises MethodMissing exception. – Gurupad Mamadapur – 2017-02-08T14:07:29.050

@GurupadMamadapur it *is* a parameter-less closure, it does run but I don't have to add x= and x() as per a sandbox discussion. But you're right about !0 vs 1. If you want it to run, surround the closure with (<closure>)(). – Magic Octopus Urn – 2017-02-08T14:22:07.663

For your Java answer, you are using main but it's not in a class so I'm not sure how it would work... Also you can use String[]a instead of String[] args – Poke – 2017-02-08T14:45:16.253

Uh, you need to wrap the java code in a class, like interface g{ ... } (interface because that will let you drop public from public static void main – Pavel – 2017-02-09T08:15:46.197

java: javac says: reached end of file while parsing. there is a closing bracket missing – phil294 – 2017-06-30T01:06:43.520

Spaces ... >_< ... String[] args=>String[]args. And is it possible to change the name of the argument array? – Zacharý – 2017-09-03T19:43:44.537

This can be import java.awt.*;interface g{static void main(String[]a){new Frame(){{add(new Checkbox());show();}};}} instead (103 bytes). And I don't know Groovy, but maybe it also has a show() method? – Kevin Cruijssen – 2017-11-03T14:11:39.887

@KevinCruijssen only valid in 1.8, invalid modifiers for versions below 1.8. – Magic Octopus Urn – 2017-11-03T14:17:14.400

@MagicOctopusUrn Oh, didn't knew show() was that new. Well, you can still golf String[] args to String[]a. – Kevin Cruijssen – 2017-11-03T14:22:36.223

@KevinCruijssen made a mistake, had the wrong JRE loaded, you're right show is fine. – Magic Octopus Urn – 2017-11-03T14:39:20.140

3

CSS, 46

Assumes a minimal HTML document, not unlike this CSS Hello World submission.

This one avoids the extra "X" of the old 42 char solution, making it conform with the rules.

NOTE: Seems to only work in Firefox (51.0.1)

* :after{content:'X'}*:active :after{all:unset

CSS, 42 (old solution)

:after{content:'X'}:active:after{all:unset

thykka

Posted 2017-02-05T11:04:12.307

Reputation: 131

1Not switching state by click, but switches to other state while muose button is pressed. – Qwertiy – 2017-02-26T21:23:09.980

@Qwertiy, which browser are you using? – thykka – 2017-03-09T07:54:20.583

Chrome 56 on Windows 7 x64. Anyway, I expect it to be in any browser as :active should work only while mouse button is down. What browser do you use with the other result? – Qwertiy – 2017-03-09T09:25:23.457

@Qwertiy, Firefox, as was clearly stated above. – thykka – 2017-03-09T09:29:35.963

Checked at my Firefox 52.0 beta 9 x32 - absolutely the same results as in Chrome. By the way, 42-byte solution has 3 states intead of 2: both Xs are visible, only the fisrst X is visible and both X are invisible. What OS are you using? – Qwertiy – 2017-03-09T09:33:10.340

@Qwertiy That's why it's labeled the "old solution" :) I'm using macOS 10.12.3, and with Firefox 51.0.1 both solutions switch their state with a click – unlike how Chrome behaves. – thykka – 2017-03-09T10:44:57.217

2

Jolf, 19 bytes

"<Ξ♠ΐ♠type=Ξ\t5δbox>

Replace \t with a tab literal and with 0x06. Try it here!

"<Ξ♠ΐ♠type=Ξ\t5δbox>
"...................   string containing...
 <                     "<"
  Ξ♠ΐ♠                 compressed for "input "
      type=            "type="
           Ξ\t5δ       compressed for "CHECK"
                box    "box"
                   >

This displays a checkbox to the output element.

Conor O'Brien

Posted 2017-02-05T11:04:12.307

Reputation: 36 228

2

P5.js 35 26 bytes

function setup(){createCheckbox()}

not really fun, but it works.

setup=s=>createCheckbox()

-9 bytes thanks to Kritixi

satibel

Posted 2017-02-05T11:04:12.307

Reputation: 221

setup=s=>createCheckbox() is shorter – user41805 – 2017-02-06T08:32:35.017

2

FLTK, 48 characters

Function{}{}{Fl_Window{}{}{Fl_Check_Button{}{}}}

Sample run:

bash-4.3$ fluid -c checkbox.fl 

bash-4.3$ fltk-config --compile checkbox.cxx 
g++ -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/freetype2 -I/usr/include/cairo -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -I/usr/include/pixman-1 -I/usr/include/freetype2 -I/usr/include/libpng16 -g -O2 -fvisibility-inlines-hidden -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_THREAD_SAFE -D_REENTRANT -o 'checkbox' 'checkbox.cxx' -Wl,-Bsymbolic-functions -lfltk -lX11

bash-4.3$ ./checkbox

Sample output:

checkbox using FLTK

manatwork

Posted 2017-02-05T11:04:12.307

Reputation: 17 865

2

Racket, 98 bytes

#lang racket/gui
((λ(f)(new check-box%[parent f][label""])(send f show #t))(new frame%[label""]))

At least I have a few bytes on the Clojure answer!

Winny

Posted 2017-02-05T11:04:12.307

Reputation: 1 120

2

C#, 151 139 bytes

This is a longer approach than @satibel his answer, but it's completely different and doesn't require a form. Since I couldn't comment about if I could use key presses instead of mouse clicks (not enough rep.) I'm not 100% sure this is allowed, so let me know!

EDIT: Managed to lose 12 bytes thanks to some of my own improvements, and some are thanks to @TheLethalCoder

Golfed

using c=System.Console;class p{static void Main(){for(int _=1;;_++){c.ReadKey();_%=2;c.BackgroundColor=(System.ConsoleColor)_;c.Clear();}}}

Ungolfed

using System;
class p
{
    static void Main()
    {
        int _ = 0;
        for (int _=1;;_++)
        {
            Console.ReadKey(); //Wait for user input
            _%=2; //Prevent integer from overflowing at some point
            Console.BackgroundColor = (System.ConsoleColor)_; //Set Console background color to either 0 (black) or 1 (darkblue) depending on if our integer is even
            Console.Clear(); //Clear console window to get rid of input. This also causes the whole console window to change color
        }
    }
}

I know it's kind of tricky since it works with keypresses rather than mouseclicks, so let me know if that's allowed. If it's not, I'll happily remove my answer, or write a new one instead!

Metoniem

Posted 2017-02-05T11:04:12.307

Reputation: 387

1You can do it using mouse clicks but the code get's really long – TheLethalCoder – 2017-02-07T12:30:38.920

1

Very very long in fact: http://codegolf.stackexchange.com/a/109383/38550

– TheLethalCoder – 2017-02-07T12:37:35.160

@TheLethalCoder That's an amazing answer. I knew it was possible, but I didn't even dare to try.. GG to you sir! (Can't comment on other answers than my own yet, sorry) – Metoniem – 2017-02-07T14:02:42.467

Amazing that you can do it (I did a lot of googling and then hacking on the result to get it to work and be "short"). Just not amazing with how long it is! And no worries keep posting good answers and you will get there. – TheLethalCoder – 2017-02-07T14:17:54.097

1Improvement points (untested on length but should save you bytes): using c = System.Console; instead of using System; and then do c.ReadKey(); etc, the _++; and _%=2; can be combined using post increment to ++_%=2; (I think that should work although I haven't tested), < 1 instead of == 0, Might be able to assign _ on the BackgroundColor line but you'd have to experiment with it. – TheLethalCoder – 2017-02-07T14:21:49.230

@TheLethalCoder Oh hey, that's interesting. Let me try a few of those! I thought of using c=System.Console; but figured it was longer than writing Console 3 times.. Only I forgot I could then remove using System; – Metoniem – 2017-02-08T10:32:15.743

2

C#, 814 bytes

Out of interest I decided to see how many bytes it would take to use a console application in C# using the mouse to change the checked state. There is no built in support so you have to use unmanaged code and it get's very long, very fast. There might be room for improvement but it's never going to be short.

Note: It does seem to change the checked state when the mouse is pressed and is moved at the same time. But seeing as this is never going to compete I decided to leave that be as a quirk.

using System;using System.Runtime.InteropServices;class P{static void Main(){int m=0,n=0,c=0;var h=GetStdHandle(-10);GetConsoleMode(h,ref m);SetConsoleMode(h,m&=~64);new System.Threading.Thread(()=>{for(;;){var r=new[]{new r()};ReadConsoleInput(h,r,1,ref n);Console.Clear();if(r[0].e==2&&r[0].m.s==1)Console.BackgroundColor=++c%2<1?0:(ConsoleColor)1;}}).Start();}[StructLayout((LayoutKind)2)] struct r{[FieldOffset(0)]public int e;[FieldOffset(4)]public m m;}struct m{int p;public int s;}[DllImport("kernel32.dll")]static extern IntPtr GetStdHandle(int h);[DllImport("kernel32.dll")]static extern bool GetConsoleMode(IntPtr h,ref int m);[DllImport("kernel32.dll")]static extern bool SetConsoleMode(IntPtr h,int m);[DllImport("kernel32.dll")]static extern bool ReadConsoleInput(IntPtr h,[Out]r[]r,int l,ref int n);}

And the formatted version:

using System;
using System.Runtime.InteropServices;

class P
{
    static void Main()
    {
        int m = 0, n = 0, c = 0;

        var h = GetStdHandle(-10);
        GetConsoleMode(h, ref m);
        SetConsoleMode(h, m &= ~64);

        new System.Threading.Thread(() =>
        {
            for (;;)
            {
                var r = new[] { new r() };
                ReadConsoleInput(h, r, 1, ref n);

                Console.Clear();
                if (r[0].e == 2 && r[0].m.s == 1)
                    Console.BackgroundColor = ++c % 2 < 1 ? 0 : (ConsoleColor)1;
            }
        }).Start();
    }

    [StructLayout((LayoutKind)2)]
    struct r
    {
        [FieldOffset(0)]
        public int e;
        [FieldOffset(4)]
        public m m;
    }

    struct m
    {
        int p;
        public int s;
    }

    [DllImport("kernel32.dll")]
    static extern IntPtr GetStdHandle(int h);

    [DllImport("kernel32.dll")]
    static extern bool GetConsoleMode(IntPtr h, ref int m);

    [DllImport("kernel32.dll")]
    static extern bool SetConsoleMode(IntPtr h, int m);

    [DllImport("kernel32.dll")]
    static extern bool ReadConsoleInput(IntPtr h, [Out]r[] r, int l, ref int n);
}

TheLethalCoder

Posted 2017-02-05T11:04:12.307

Reputation: 6 930

2

Vim: 13 Bytes Keystrokes

iq<Esc>:nm a g??<CR>

Press a and toggle between q and d (feels like a lever to me ...)

Explanation:

 iq<Esc>   " insert a q char
 nm a      " create a normal mapping toggled by a
 g??       " rot13 the current line, q -> d -> q ...
 <CR>      " validate the mapping

nobe4

Posted 2017-02-05T11:04:12.307

Reputation: 293

The checkbox has to be activated by the mouse, not the keyboard. I think you can do :nm <LeftMouse> g?? instead – user41805 – 2017-02-08T11:01:07.343

1

Java, 69 bytes

a->{a.removeAll();a.add(new java.awt.Checkbox());a.setVisible(1>0);};

Just a not-so-self-explanatory Consumer<Frame>. It takes a Frame, removes everything in it, puts a checkbox, and then makes sure the Frame is displayed.

user8397947

Posted 2017-02-05T11:04:12.307

Reputation: 1 242

4OP specified a program, not a function. – Pavel – 2017-02-06T12:09:42.807

2Java 8 is also important specification here. – Magic Octopus Urn – 2017-02-06T14:53:23.217

What's your justification for being able to take a Frame as input? – Jakob – 2017-09-03T18:07:36.167

1

Coding, 21 bytes

>input(checkbox)@type

Finally a perfect time to use this language!

Mama Fun Roll

Posted 2017-02-05T11:04:12.307

Reputation: 7 234

1

VBA, 42 bytes

ActiveSheet.CheckBoxes.Add(1,1,8,8).Select

Best I could get with this!

Anoplexian - Reinstate Monica

Posted 2017-02-05T11:04:12.307

Reputation: 149

1

 Emacs Lisp, 58

(and(widget-create'checkbox)(use-local-map widget-keymap))

enter image description here

coredump

Posted 2017-02-05T11:04:12.307

Reputation: 6 292

1

VB.NET, 67 (program) + 37 (additional switches) = 104

VB.NET Windows Forms Application, 67 chars

Class F
Inherits Form
Dim C=New CheckBox With{.Parent=Me}
End Class

Compile it with

vbc.exe *.vb /main:F /imports:System.Windows.Forms

Not using /target:winexe switch. Assume that additional console is ok.

Screenshot

PS: Question about byte count.

Qwertiy

Posted 2017-02-05T11:04:12.307

Reputation: 2 697

Nice. I don't think you need the Inherits Form if you specify you're using Windows Form Application. – Oliver – 2017-02-26T22:05:47.733

@obarakon, but I don't specify that. That's just a file, not the part of solution. In case of winforms solution I wouldn't write main and imports switches by myself. – Qwertiy – 2017-02-26T22:07:34.790

I get the desired form+checkbox just running Class F Dim C=New CheckBox With{.Parent=Me} End Class in an empty WFA project. – Oliver – 2017-02-26T22:10:58.313

@obarakon, let's discuss byte count: http://meta.codegolf.stackexchange.com/q/11650/32091. By the way, in case of winforms app you have partial class because of designer.

– Qwertiy – 2017-02-26T22:20:56.323

1

Javascript, 24 23 21

for(i=0;;)alert(i=!i)

i=0;while(1)alert(i=!i)

i=0;while(1)alert(++i%2)

WARNING: Might cause you to have to force-quit your browser

This one is a neat solution because it actually is not a check box and has none of the logic of a check box, but on the surface behaves very similar to a check box (toggling change of state on button press) due to exploiting the action-based delay in javascript alerts, and meets all of the criteria of rules defined in the challenge.

Try it here (*Might cause you to have to force-quit your browser)

<script>for(i=0;;)alert(i=!i)</script>

Albert Renshaw

Posted 2017-02-05T11:04:12.307

Reputation: 2 955

Wow, this is clever. Usage of text as an image, I like it. – Zacharý – 2017-09-03T19:45:37.407

1

JQuery, 50 Bytes

$("<input type=checkbox>").appendTo(document.body)

Ben Brookes

Posted 2017-02-05T11:04:12.307

Reputation: 19

2Welcome to PPCG! – Martin Ender – 2017-06-30T11:26:48.887

0

SAS, 145

proc pmenu catalog=work.c;menu m;item i dialog=d;dialog d '&1';text #1 @1 'A';checkbox #2 @1 'B';run;quit;%window w menu=work.c.m '';%display w;

This creates and displays a menu with an item that opens a dialogue box with a checkbox. It might be possible to golf it further, but I'm reluctant to try as this has already led to several crashed sessions :(

user3490

Posted 2017-02-05T11:04:12.307

Reputation: 809

0

C# (Windows Form Application), 82 bytes

using System.Windows.Forms;class F:Form{public F(){Controls.Add(new CheckBox());}}

Saved bytes thanks to @Qwertiy

enter image description here

Oliver

Posted 2017-02-05T11:04:12.307

Reputation: 7 160

Full program, not a function. – Qwertiy – 2017-02-26T21:20:39.087

Why do you need namespace? And partial class? – Qwertiy – 2017-02-26T21:45:10.653

Let's discuss byte count: http://meta.codegolf.stackexchange.com/q/11650/32091.

– Qwertiy – 2017-02-26T22:21:22.273

0

C (Win32 Console application), 143 131 bytes

#include<windows.h>
main(){MSG m;for(CreateWindow("button","",1<<28|3,0,0,8,80,0,0,0,0);GetMessage(&m,0,0,0);DispatchMessage(&m));}

Johan du Toit

Posted 2017-02-05T11:04:12.307

Reputation: 1 524

0

C++ with SFML, 401 bytes

-4 bytes thanks to Zacharý

#include<SFML\Graphics.hpp>
#include<cassert>
using namespace sf;int main(){RenderWindow w(VideoMode(300,300),"");Texture a,b;assert(a.loadFromFile("a.png")&&b.loadFromFile("b.png"));Sprite p(a),q(b);bool c=0;while(w.isOpen()){Event e;while(w.pollEvent(e)){if(e.type==Event::Closed)w.close();if(e.type==Event::MouseButtonPressed)c=!c;}w.clear(Color::White);if(c)w.draw(q);else w.draw(p);w.display();}}

Screenshot of the boxes :

Checked :

Checked checkbox

And unchecked :

Unchecked checkbox

This uses the SFML library The checkbox changes state on a sf::Event::MouseButtonPressed event ( left click, right click, middle click... )

If you want to test, after the creation of the render window, you may want to set the framerate limit to 60 fps ( using the line w.setFramerateLimit(60) ), so your computer won't burn.

This requires 2 checkboxes images in the directory where the app runs ( a.png and b.png ). For the reason it uses images, i don't know if my answer is invalid ( standard loophole ? ), but since it's needed to do graphic output...

HatsuPointerKun

Posted 2017-02-05T11:04:12.307

Reputation: 1 891

a.loadFromFile("a.png") && b.loadFromFile("b.png") => a.loadFromFile("a.png")&&b.loadFromFile("b.png"). if (e.type=>if(e.type. Does your code editor auto-format or something? – Zacharý – 2017-09-03T19:41:23.897

Forgot one... VideoMode(300, 300) => VideoMode(300,300) – Zacharý – 2017-09-03T19:46:32.597

@Zacharý Yes, it format every new semi colon. – HatsuPointerKun – 2017-09-03T19:46:53.113