Showing posts with label c# array. Show all posts
Showing posts with label c# array. Show all posts

Sunday, 22 August 2010

source code or examples of how to use listboxes


//source code or examples of how to use listboxes

//try to develop same form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsProject
{
public partial class Form7 : Form
{
public Form7()
{
InitializeComponent();
}
private void Form7_Load(object sender, EventArgs e)
{
//source code or examples of how to use listboxes
listBox1.Items.Add("AP");
listBox1.Items.Add("Tamilnadu");
listBox1.Items.Add("Karnataka");
listBox1.Items.Add("Kerala");
listBox1.Items.Add("Orissa");
listBox1.Items.Add("Maharastra");
string[] cols = { "Red", "Blue", "Green", "White", "Black", "Yellow", "Pink" };
checkedListBox1.Items.AddRange(cols);
}
private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(Convert.ToInt32(e.KeyChar) == 13)
if(comboBox1.FindString(comboBox1.Text) == -1)
comboBox1.Items.Add(comboBox1.Text);
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(comboBox1.Text);
MessageBox.Show(comboBox1.SelectedIndex.ToString());
MessageBox.Show(comboBox1.SelectedItem.ToString());
}
private void button2_Click(object sender, EventArgs e)
{
foreach(object obj in listBox1.SelectedItems)
MessageBox.Show(obj.ToString());
}
private void button3_Click(object sender, EventArgs e)
{
string str = null;
foreach (object obj in checkedListBox1.CheckedItems)
str += obj.ToString() + ", ";
str = str.Substring(0, str.Length - 2);
int pos = str.LastIndexOf(",");
if (pos != -1)
{
str = str.Remove(pos, 1);
str = str.Insert(pos, " and");
}
MessageBox.Show(str);
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}

source code for assing values in jagged array

//source code for assing values in jagged array
using System;
class JagDemo
{
static void Main()
{
int[][] arr1 = new int[4][];
arr1[0] = new int[6];
arr1[1] = new int[8];
arr1[2] = new int[7];
arr1[3] = new int[5];
//Assigning values to jagged array
for(int i=0;i{
for(int j=0;jarr[i][j] = j + 1;
}

source code for assinging and printing values in jagged array

//c# source code for assinging and printing values in jagged array
using System;
class JagDemo
{
static void Main()
{
int[][] arr1 = new int[4][];
arr1[0] = new int[6];
arr1[1] = new int[8];
arr1[2] = new int[7];
arr1[3] = new int[5];
//Assigning values to jagged array
for(int i=0;i{
for(int j=0;jarr[i][j] = j + 1;
}
//Printing values from jagged array
for(int i=0;i{
foreach(int x in arr[i])
Console.Write(x + " ");
Console.WriteLine();
}
}
}

Assigning values and Printing values from 2d array

//source code for Assigning values and Printing values from 2d array
using System;
class TDArray
{
static void Main()
{
int[,] arr = new int[4,5];
int a = 5;
//Assigning values to 2d array;
for(int i=0;i{
for(int j=0;j{
arr[i,j] = a;
a += 5;
}
}
//Printing values from 2d array:
for(int i=0;i{
for(int j=0;jConsole.Write(arr[i,j] + " ");
Console.WriteLine();
}
}
}

source code forAssigning values to 2d array

//source code forAssigning values to 2d array;
using System;
class TDArray
{ //source code for array
static void Main()
{
int[,] arr = new int[4,5];
int a = 5;
// source code forAssigning values to 2d array;
for(int i=0;i{
for(int j=0;j{
arr[i,j] = a;
a += 5;
}
}