Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 11-20 of 58 total

Toggle Text on Button (Using thread timers)

// Works on a seperate thread and changes the color of a button if a significant event happens

void HotShotTick(object obj)
{
if (this.InvokeRequired) { this.Invoke((MethodInvoker)delegate {
this.FillHotshots();
if (this.dsSignificant.HotshotBets.Rows.Count > 0)
{
this.btnHotShotBets.BackColor = Color.Red;
}
else
{
this.btnHotShotBets.BackColor = Color.Green;
}
});
}

Toggle Text on Button (Single thread application)

// description of your code here

 public partial class Form1 : Form
    {
        int iBtn = 0;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            ++iBtn;

            if (iBtn == 3)
            {
                iBtn = 0;
            }

            switch (iBtn)
            {
                case 0: button1.Text = "default";
                    break;
                case 1: button1.Text = "Love";
                    break;
                case 2: button1.Text = "Hate";
                    break;
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            button1.Text = "default";
        }
    }

Include text files in C source

This includes the contents of myfile.txt into the char array text.

Note: This only works if ALL lines in the file are enclosed in "s.

Wrong myfile.txt:
Hello world
Goodbye world



Right myfile.txt:
"Hello world"
"Goodbyle world"
""

char text[] = {
#include "myfile.txt"
}

Elegant way of shorten a text string

this method shortens a plain text string down to complete words contained in given scope (count)

def shorten (string, count = 30)
	if string.length >= count 
		shortened = string[0, count]
		splitted = shortened.split(/\s/)
		words = splitted.length
		splitted[0, words-1].join(" ") + ' ...'
	else 
		string
	end
end

CMD text file rotation 60 generations

// Simple CMD text file rotation 60 generations
@echo off

if not exist %1.txt goto :EOF
if exist %1_62.txt del %1_62.txt

if exist %1_60.txt ren %1_60.txt %1_61.txt
if exist %1_59.txt ren %1_59.txt %1_60.txt
if exist %1_58.txt ren %1_58.txt %1_59.txt
if exist %1_57.txt ren %1_57.txt %1_58.txt
if exist %1_56.txt ren %1_56.txt %1_57.txt
if exist %1_55.txt ren %1_55.txt %1_56.txt
if exist %1_54.txt ren %1_54.txt %1_55.txt
if exist %1_53.txt ren %1_53.txt %1_54.txt
if exist %1_52.txt ren %1_52.txt %1_53.txt
if exist %1_51.txt ren %1_51.txt %1_52.txt
if exist %1_50.txt ren %1_50.txt %1_51.txt
if exist %1_49.txt ren %1_49.txt %1_50.txt
if exist %1_48.txt ren %1_48.txt %1_49.txt
if exist %1_47.txt ren %1_47.txt %1_48.txt
if exist %1_46.txt ren %1_46.txt %1_47.txt
if exist %1_45.txt ren %1_45.txt %1_46.txt
if exist %1_44.txt ren %1_44.txt %1_45.txt
if exist %1_43.txt ren %1_43.txt %1_44.txt
if exist %1_42.txt ren %1_42.txt %1_43.txt
if exist %1_41.txt ren %1_41.txt %1_42.txt
if exist %1_40.txt ren %1_40.txt %1_41.txt
if exist %1_39.txt ren %1_39.txt %1_40.txt
if exist %1_38.txt ren %1_38.txt %1_39.txt
if exist %1_37.txt ren %1_37.txt %1_38.txt
if exist %1_36.txt ren %1_36.txt %1_37.txt
if exist %1_35.txt ren %1_35.txt %1_36.txt
if exist %1_34.txt ren %1_34.txt %1_35.txt
if exist %1_33.txt ren %1_33.txt %1_34.txt
if exist %1_32.txt ren %1_32.txt %1_33.txt
if exist %1_31.txt ren %1_31.txt %1_32.txt
if exist %1_30.txt ren %1_30.txt %1_31.txt
if exist %1_29.txt ren %1_29.txt %1_30.txt
if exist %1_28.txt ren %1_28.txt %1_29.txt
if exist %1_27.txt ren %1_27.txt %1_28.txt
if exist %1_26.txt ren %1_26.txt %1_27.txt
if exist %1_25.txt ren %1_25.txt %1_26.txt
if exist %1_24.txt ren %1_24.txt %1_25.txt
if exist %1_23.txt ren %1_23.txt %1_24.txt
if exist %1_22.txt ren %1_22.txt %1_23.txt
if exist %1_21.txt ren %1_21.txt %1_22.txt
if exist %1_20.txt ren %1_20.txt %1_21.txt
if exist %1_19.txt ren %1_19.txt %1_20.txt
if exist %1_18.txt ren %1_18.txt %1_19.txt
if exist %1_17.txt ren %1_17.txt %1_18.txt
if exist %1_16.txt ren %1_16.txt %1_17.txt
if exist %1_15.txt ren %1_15.txt %1_16.txt
if exist %1_14.txt ren %1_14.txt %1_15.txt
if exist %1_13.txt ren %1_13.txt %1_14.txt
if exist %1_12.txt ren %1_12.txt %1_13.txt
if exist %1_11.txt ren %1_11.txt %1_12.txt
if exist %1_10.txt ren %1_10.txt %1_11.txt
if exist %1_09.txt ren %1_09.txt %1_10.txt
if exist %1_08.txt ren %1_08.txt %1_09.txt
if exist %1_07.txt ren %1_07.txt %1_08.txt
if exist %1_06.txt ren %1_06.txt %1_07.txt
if exist %1_05.txt ren %1_05.txt %1_06.txt
if exist %1_04.txt ren %1_04.txt %1_05.txt
if exist %1_03.txt ren %1_03.txt %1_04.txt
if exist %1_02.txt ren %1_02.txt %1_03.txt
if exist %1_01.txt ren %1_01.txt %1_02.txt

copy %1.txt %1_01.txt

Tabs-to-space function

This Perl function returns its argument with any tabs it contained converted into the appropriate number of spaces.
sub tabs2space($) {
 my $str = shift;
 1 while $str =~ s/\t+/' ' x (length($&)*8 - length($`)%8)/e;
 return $str;
}

Hexadecimal-to-text converter

This script converts all 2-digit hexadecimal numbers (without 0x's) in the standard input into text, though there's probably a better way to do this.
#!/usr/bin/perl -wn
use strict;
s/\s//g;
print chr hex $1 while /([[:xdigit:]]{2})/g;
print "\n";

Binary-to-text converter

Converts all 8-bit binary numbers in the standard input into text, though there's probably a better way to do this
#!/usr/bin/perl -wn
use strict;
s/\s//g;
print chr oct "0b$1" while /([01]{8})/g;
print "\n";

Paragraph formatter

Converts any nonempty lines in a document that don't start with whitespace into more conventional paragraphs
#!/usr/bin/perl -wni
# para.pl
use strict;

/^\s+/ || /^$/ ? print : write;
format ARGVOUT =
        ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
$_
^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~
$_
.

Fast stop word detection in Ruby

Requires BloominSimple (a pure Ruby Bloom filter class).

List of stop words obtained from http://www.dcs.gla.ac.uk/idom/ir_resources/linguistic_utils/stop_words

# Detect stop words QUICKLY
# Uses a bloom filter instead of searching literally through a list of stopwords
# for > 3x speed increase
# 
#    using bloom filter: 2.580000   0.030000   2.610000 (  2.698829)
#  using literal search: 7.850000   0.120000   7.970000 (  8.181684)


require 'bloominsimple'
require 'digest/sha1'
require 'pp'

# Create a simple bloom filter that uses a SHA1 hash (more effective than BloominSimple's default hashing)
b = BloominSimple.new(50000) do |word|
  Digest::SHA1.digest(word.downcase.strip).unpack("VVV")
end

# Add stopwords to the bloom filter!
stopwords = []
File.open('stopwords').each { |a| b.add(a); stopwords << a.downcase.strip }

# Read in a whole dictionary of regular words
words = File.open('/usr/share/dict/words').read.split.collect{|a| a.downcase.strip }

# Define two ways to detect stopwords for comparison..
using_filter = lambda { |word| b.includes?(word) }
using_array = lambda { |word| stopwords.include?(word.downcase.strip) }
techniques = [using_filter, using_array]

# Run stopword comparisons with both techniques
t = techniques.collect { |l| words.collect { |a| l[a] } }

# See how effective the bloom filter has been compared to the literal search
if t[0] == t[1]
  puts "GOOD"
else
  words.zip(t[0],t[1]).each do |x|
    puts x.first if x[1] != x[2]
  end
end

# Now do speed benchmarks..
techniques.each { |l| puts Benchmark.measure { words.each { |a| l[a] } } }
« Newer Snippets
Older Snippets »
Showing 11-20 of 58 total