Archive

Archive for September, 2007

Reverse text as you type

September 26th, 2007 No comments

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);
}

VN:F [1.9.18_1163]
Rating: 0.0/5 (0 votes cast)
Categories: C#

Reorder listbox items with JavaScript and ASP.NET

September 26th, 2007 No comments

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">



test



 
Preview of datatable dt :





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

VN:F [1.9.18_1163]
Rating: 0.0/5 (0 votes cast)
Categories: ASP.NET, Web Design

Testing for Programmers – Beginners

September 24th, 2007 No comments

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/

VN:F [1.9.18_1163]
Rating: 0.0/5 (0 votes cast)
Categories: Architecture, General

Microsoft XNA – Quick sample

September 21st, 2007 No comments

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("Arial");
Vector2 fo = font.MeasureString(str)/2;
Texture2D texture = content.Load("mysprite");

f.Begin();
f.Draw(texture, fo, Color.Blue);
f.DrawString(font, str, fo, Color.Red);
f.End();

base.Draw(gameTime);
}
}
}

VN:F [1.9.18_1163]
Rating: 0.0/5 (0 votes cast)
Categories: C#, DOTNET, General

Change attributes of items in a combobox

September 20th, 2007 No comments

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

VN:F [1.9.18_1163]
Rating: 0.0/5 (0 votes cast)
Categories: C#

Move a control with mouse on form – .NET

September 17th, 2007 5 comments

This is a simple C# 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;
            }
        }
    }
}
VN:F [1.9.18_1163]
Rating: 4.8/5 (24 votes cast)
Categories: C#, DOTNET, General