NinethSense oWnZ mE!: Praveen’s drawing book

Choose a Topic:

Wed
26
Sep '07

Reverse text as you type

Time pass ;)

 
		public string Reverse(string x)
		{
			char[] charArray = new char[x.Length];
			int len = x.Length - 1;
			for (int i = 0; i < = len; i++)
				charArray[i] = x[len-i];
			return new string(charArray);
		}
 
 
		private void txtBox1_KeyUp(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			textBox2.Text = Reverse(textBox1.Text);
		}
'

Reorder listbox items with JavaScript and ASP.NET

Default.aspx:
 
< %@ Page Language=“VB” AutoEventWireup=“false” CodeFile=“Default.aspx.vb” Inherits=“_Default” ValidateRequest=“false”  %>
 
< !DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
 
<html xmlns=“http://www.w3.org/1999/xhtml” >
<head id=“Head1″ runat=“server”>
    <title>Untitled Page</title>
    <style type=“text/css”>
	#lstWishList {
		background-color:lightgrey;
		font:10px notmal arial;
		overflow:visible;;
	}
	
	#divNumbers {
		font:10px notmal arial;
		line-height:13px;
	}
	.imgbutton {
		cursor:pointer;
	}
</style>
<script language=“javascript”>
 
	function LoadNumbers() {
		lst = document.getElementById(“lstWishList”);		
		lst.size = lst.options.length;
		lstNumbers = document.getElementById(“lstWishListNumbers”);		
		div = document.getElementById(“divNumbers”);
		s = “”;
		for (i=1;i<lst .length+1;i++) {
			s += i+“<br>”;
		}
		div.innerHTML = s;
	}
	function ProcessUp(upordown) {
	    if (lst.selectedIndex < 0) return;
		v = (upordown==‘up’)?1:-1;
		lst = document.getElementById(“lstWishList”);
		oldindex = lst.selectedIndex
		newindex = lst.selectedIndex-v
		if (newindex &lt;0 || newindex > lst.options.length-1) return;
		
		
		temp = lst.options[oldindex].text;
		tempv = lst.options[oldindex].value;
		
		lst.options[oldindex].text = lst.options[newindex].text;
		lst.options[oldindex].value = lst.options[newindex].value;
 
		lst.options[newindex].text = temp;
		lst.options[newindex].value = tempv;
		
		lst.selectedIndex = newindex;
	}
	
	function ProcessRemoval() {
	    if (!confirm(“Are you sure you want to remove this item?”)) return;
	    lst = document.getElementById(“lstWishList”);
	    
	    for (i=0;i</lst><lst .options.length;i++) {
	        if (lst.options[i].selected)
	            lst.remove(i);
	    }
	    LoadNumbers();
	}
	
	function PopulateHiddenField(){
	    hid = document.getElementById(“hidListItems”);
	    lst = document.getElementById(“lstWishList”);
	    s = “”;
	    for(i=0;i<lst.options.length;i++) {
	        s += lst.options[i].value + “|” + lst.options[i].text;
	        if (i+1 != lst.options.length) s+= “~”;
	    }
	    hid.value = s;
	}
 
</script>
    
</lst></script></head>
<body>
    <form id=“form1″ runat=“server”>
    <table cellpadding=0 cellspacing=0>
	<tr>
		<td colspan=3 style=“border:solid 1px black”>
<table><tr><td valign=“top” style=“background-color:lightgrey;”>
<div id=“divNumbers”>test</div>
</td><td>
<asp :ListBox ID=“lstWishList” runat=“server”></asp>
</td></tr></table>
</td>
</tr>
 
<tr><td align=center valign=middle><img src=“up.gif” class=“imgbutton” border=1 onclick=“ProcessUp(’up’)”/></td>
<td align=center valign=middle><img src=“down.gif” class=“imgbutton” border=1 onclick=“ProcessUp(’down’)”/></td>
<td align=center valign=middle><img src=“x.gif” class=“imgbutton” border=1 onclick=“ProcessRemoval()”/></td>
</tr>
 
</table>
        <br />
        <asp :Button ID=“Button2″ runat=“server” Text=“Submit Changes” OnClientClick=“PopulateHiddenField()” /><br />
        <br />
        <asp :HiddenField ID=“hidListItems” runat=“server” />
        <br />
        <hr />
        &nbsp;<br />
        Preview of datatable dt :
        <br />
        <br />
        <asp :GridView ID=“GridView1″ runat=“server”>
        </asp>
        
    </form>
</body>
</html>
<script language = “JavaScript”>
	LoadNumbers();
</script>

Default.aspx.vb:
 
Imports System.Data
 
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim dt As New DataTable
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
 
        ‘ Fill sample data in listbox
        Dim dr As DataRow
        dt.Columns.Add(“id”)
        dt.Columns.Add(“title”)
        If (Not IsPostBack) Then
            dr = dt.NewRow() : dr(“id”) = “33″ : dr(“title”) = “My Title” : dt.Rows.Add(dr)
            dr = dt.NewRow() : dr(“id”) = “13″ : dr(“title”) = “My Title blah” : dt.Rows.Add(dr)
            dr = dt.NewRow() : dr(“id”) = “1523″ : dr(“title”) = “asd asdf asda” : dt.Rows.Add(dr)
            dr = dt.NewRow() : dr(“id”) = “1723″ : dr(“title”) = “My Title asdf asdasdasd” : dt.Rows.Add(dr)
            dr = dt.NewRow() : dr(“id”) = “123″ : dr(“title”) = “2222 My Title” : dt.Rows.Add(dr)
            dr = dt.NewRow() : dr(“id”) = “1123″ : dr(“title”) = “122222 My Title” : dt.Rows.Add(dr)
 
            lstWishList.DataValueField = “id”
            lstWishList.DataSource = dt
            lstWishList.DataTextField = “title”
 
            lstWishList.DataBind()
        End If
 
    End Sub
 
 
    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
 
        ‘ You will get list items as a delimeted string in hidden field - hidListItems.Value - re-fill datatable dt
 
        dt.Clear()
        Dim s() As String = hidListItems.Value.Split(“~”)
        Dim i As Integer
        Dim j As Integer
        Dim dr As DataRow
        For i = 0 To s.Length - 1
            Dim s1() As String = s(i).Split(“|”)
            dr = dt.NewRow() : dr(“id”) = s1(0) : dr(“title”) = s1(1) : dt.Rows.Add(dr)
        Next
        lstWishList.Items.Clear()
        lstWishList.DataValueField = “id”
        lstWishList.DataSource = dt
        lstWishList.DataTextField = “title”
 
        lstWishList.DataBind()
 
        ‘Now you have re-ordered values in datatable dt - use it to update database.
        ‘Like - delete all the wishlist items of this customer, then add this dt list 
 
        ‘data preview in grid view
 
        GridView1.DataSource = dt
        GridView1.DataBind()
    End Sub
End Class
Mon
24
Sep '07

Testing for Programmers - Beginners

My new article Testing for Beginners available on CodeProject.

This article provides basic information about software testing for programmers in a beginner’s level.

Explain in detail about subject - testing is out of scope of this article. Just a simple Google search can give you more than enough results. So I wrote this article with just programmers in mind.

CodeProject Link: http://www.codeproject.com/useritems/TestingBeginners.asp

The same I copied to my website also at - http://ninethsense.com/content/view/54/55/
Fri
21
Sep '07

Microsoft XNA - Quick sample

This is a quick sample which demonstates the loading of texture and output of a string.


No worries, 99% of the code is already supplied by Visual Studio ;)


 
#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
#endregion
 
namespace WindowsGame1
{
 
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        ContentManager content;
 
 
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            content = new ContentManager(Services);
        }
 
 
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
 
            base.Initialize();
        }
 
 
        
        protected override void LoadGraphicsContent(bool loadAllContent)
        {
            if (loadAllContent)
            {
            }
 
        }
 
 
 
        protected override void UnloadGraphicsContent(bool unloadAllContent)
        {
            if (unloadAllContent)
            {
                content.Unload();
            }
 
        }
 
 
        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
 
 
            base.Update(gameTime);
        }
 
 
        protected override void Draw(GameTime gameTime)
        {
            graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
 
            string str = “Hello”;
            SpriteBatch f = new SpriteBatch(graphics.GraphicsDevice);
            SpriteFont font = content.Load<spritefont>(“Arial”);
            Vector2 fo = font.MeasureString(str)/2;
            Texture2D texture = content.Load<texture2d>(“mysprite”);
 
            f.Begin();
            f.Draw(texture, fo, Color.Blue);
            f.DrawString(font, str, fo, Color.Red);
            f.End();
 
            base.Draw(gameTime);
        }
    }
}
 
</texture2d></spritefont>
Thu
20
Sep '07

Change attributes of items in a combobox

This was an expiriment program for changing fonts, colors, draw shapes etc.

To alter the items, you need to use comboBox1.DrawMode

 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        
        public Form1()
        {
            InitializeComponent();
        }
 
        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.DrawMode = DrawMode.OwnerDrawVariable;
            comboBox1.DropDownHeight = 200;
            comboBox1.Items.Add(“Hello”);
            comboBox1.Items.Add(“W o r l d”);
            comboBox1.Items.Add(“And MEEEEEEEEEEEEEEEEEeeeeee”);
 
        }
 
        
 
        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            Graphics g = e.Graphics;
            e.DrawBackground();
            if (e.Index == 0)
            {
                g.FillRectangle(Brushes.Yellow, 10, 10, 70, 20);
                g.DrawString(“test”, new Font(“arial”, 10), Brushes.Green, 10, 10);
            }
            if (e.Index == 1)
            {
                g.DrawArc(new Pen(Color.Brown), 20, 20, 30, 30, 30, 120);
                g.DrawString(“test”, new Font(“courier new”, 10), Brushes.Red, 10, 70);
            }
            if (e.Index == 3)
            {
                g.DrawArc(new Pen(Color.Brown), 20, 40, 30, 30, 30, 120);
                g.DrawString(“tesT”, new Font(“courier new”, 10), Brushes.Red, 10, 90);
            }
        }
 
        private void comboBox1_MeasureItem(object sender, MeasureItemEventArgs e)
        {
            switch (e.Index)
            {
                case 0: e.ItemHeight = 50;
                    break;
                case 1: e.ItemHeight = 80;
                    break;
                case 2: e.ItemHeight = 30;
                    break;
            }
        }
    }
}



Inspired from MSDN link - http://msdn2.microsoft.com/en-us/library/system.windows.forms.combobox.drawmode.aspx
Mon
17
Sep '07

Move a control with mouse on form

This is a simple script which demonstates how to move a control (here I used button) by dragging it (without using builtin drag-drop features ;) )


 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        private int x = 0;
        private int y = 0;
        public Form1()
        {
            InitializeComponent();
        }
 
        private void button1_MouseDown(object sender, MouseEventArgs e)
        {
            x = e.X;
            y = e.Y;
        }
 
        private void button1_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                button1.Left = (button1.Left + e.X) - x;
                button1.Top = (button1.Top +e.Y)-y;
            }
        }
    }
}
The CodeProject Microsoft Developer Network Official ASP.NET Forums Microsoft .NET Framework Community Microsoft Most Valuable Professional Kidoos forums Microsoft Visual Studio Developer Home Professional Information Technology Solutions Microsoft Research Home Trivandrum Microsoft Users Group Community Website