How to count string in string

clock August 13, 2008 00:35 by author mderaeve

I was looking for a c# function to count the number of occurrences of a string in a string. But I couldn't find any by default.

So I wrote my own little function. Migth come in handy!

In this scenario I had a string that contains line feeds, I had to know how many to correctly calculate the length of the string. Here is the function that does the work:

public static int Count(string src, string find)
        {
            int ret = 0;
            int len = find.Length;
            for (int i=0; i < src.Length-len;i++)
            {
                if (src.Substring(i,len) == find)
                {
                    ++ret;
                }
            }
            return ret;
        }

So I was looking for "\r\n" in a string:

int countNewLines = Count(day.Remark,"\r\n");
 

It's Simple and easy, any person could find this, but I like to blog it so next time I won't have to look for it again.



How to change Option item index with javascript

clock August 7, 2008 17:19 by author mderaeve

Again a small script that enables you to order items in a listbox or (html) optoin. Just include this script in your page:

function move(index,to,list) 
{
    var total = list.options.length-1;
    if (index == -1) return false;
    if (to == +1 && index == total) return false;
    if (to == -1 && index == 0) return false;
    var items = new Array;
    var values = new Array;
    for (i = total; i >= 0; i--) 
    {
        items[i] = list.options[i].text;
        values[i] = list.options[i].value;
    }
    for (i = total; i >= 0; i--) 
    {
        if (index == i) 
        {
            list.options[i + to] = new Option(items[i],values[i], 0, 1);
            list.options[i] = new Option(items[i + to], values[i + to]);
            i--;
        }
        else 
        {
            list.options[i] = new Option(items[i], values[i]);
        }
    }
    list.focus();
}

 

When you are using asp.Net you can set this script from the code behind like this:

btnUp.Attributes.Add("onclick", "move(" + lstOrder.ClientID + 
".selectedIndex, -1,"+lstOrder.ClientID+")");
btnDown.Attributes.Add("onclick", "move(" + lstOrder.ClientID + 
".selectedIndex,+1," + lstOrder.ClientID + ")");

 

You can now click on the up and down arrows to move an item in the list.

         

I had lots of help from this site: http://javascript.internet.com/forms/selection-order.html



Short if structure

clock August 6, 2008 20:19 by author mderaeve

It keeps slippin my mind, so here an example, making it easy to find:

string test = i<0?"yes":"no";
 

If i < 0 then it returns string "yes" else "no".

Syntax:  Res = Coparison ? returnIfTrue : returnIfFalse ;



Send mail from web part (using spcontext)

clock August 1, 2008 23:40 by author mderaeve

Don't start with the System.Net.Mail namespace, because Sharepoint has a better way to do this:

using Microsoft.SharePoint.Utilities;

SPUtility.SendEmail(SPContext.Current.Web, false, false, EmailTO, 
"SOP review request", message.ToString());



Looking for nice Style for your web part buttons for WSS?

clock July 31, 2008 11:00 by author mderaeve

I made some webparts that are using htmlbuttons to perform some actions. At first I kept the ugly default button style. But when I found some spare time I start looking for some nice CSS classes to put on my buttons. I chose to use style of SharePoint itself, making sure the buttons will change their looks when I change the theme.

Here is the result of which I'm very pleased.

Before:

After:

The Yes button is the hoover style, the No button is the normal style. The colors used are the same as the Tab on top. Here's the code from my webpart:

_myNoReview = new HtmlButton();
_myNoReview.InnerText = "Yes";
_myNoReview.Attributes.Add("class", "ms-SPButton ms-WPAddButton");
_myNoReview.Attributes.Add("onmouseover", "this.className='ms-SPButton 
ms-WPAddButton ms-WPAddButtonHover';");
_myNoReview.Attributes.Add("onmouseout", "this.className='ms-SPButton ms-WPAddButton';");
 
_myNoReview.ServerClick += new EventHandler(_mybutton_click);
Controls.Add(_myNoReview);

 



Search

Categories





Locations of visitors to this page

About

Mark Deraeve

Blogroll

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

This site was created by Jetro Wils and Deraeve Mark powered by BlogEngine

© Copyright 2008

Sign in