Wednesday, September 9, 2009

Generate Random Number22



namespace RandomNoGenerate2
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}

int min, max, val = 0;
Random rdm;
bool b;
private void Form2_Load(object sender, EventArgs e)
{
for (int i = 1; i <= 30; i++)
{
comboBox1.Items.Add(i);
}

randomnoforcombo5();
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
rdm = new Random();
min = Convert.ToInt32(txt_min.Text);
max = Convert.ToInt32(txt_max.Text);
val = rdm.Next(min, max);
comboBox1.Text = val.ToString();
}

private void link_Check_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
for (int i = 0; i < listBox2.Items.Count; i++)
{
if (comboBox2.Text == listBox2.Items[i].ToString())
{
comboBox2.BackColor = Color.Tomato;
return;
}
}
listBox2.Items.Add(comboBox2.Text);
comboBox2.BackColor = Color.White;
}

private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
{
for (int i = 0; i < listBox3.Items.Count; i++)
{
if (comboBox3.Text == listBox3.Items[i].ToString())
{
comboBox3.BackColor = Color.Tomato;
return;
}
}
listBox3.Items.Add(comboBox3.Text);
comboBox3.BackColor = Color.White;
}

private void comboBox4_SelectedIndexChanged(object sender, EventArgs e)
{
listBox4.Items.Add(comboBox4.Text);
comboBox4.Items.Remove(comboBox4.Text);
}

private void linkLabel5_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
Form2_Load(sender, e);
}
private void randomnoforcombo5()
{
rdm = new Random();
min = Convert.ToInt32(txt_min.Text);
max = Convert.ToInt32(txt_max.Text);
val = rdm.Next(min, max);
comboBox5.Text = val.ToString();
}
}
}

Generate Random Number2




namespace RandomNoGenerate2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

int min,max,val=0;
Random rdm;
bool b;
private void btn_RGenerate_Click(object sender, EventArgs e)
{
rdm = new Random();
min = Convert.ToInt32(txt_min.Text);
max = Convert.ToInt32(txt_max.Text);
val = rdm.Next(min, max);
textBox1.Text = val.ToString();
}

private void btn_Add_Click(object sender, EventArgs e)
{
b = false;
rdm = new Random();
min = Convert.ToInt32(txt_min.Text);
max = Convert.ToInt32(txt_max.Text);
val = rdm.Next(min, max);
textBox1.Text = val.ToString();

for (int i = 0; i < listBox1.Items.Count; i++)
{
if (val == Convert.ToInt32(listBox1.Items[i]))
b=true;
}
if(b)
textBox1.BackColor = Color.Red;
else
textBox1.BackColor = Color.White;

}
}
}

Generate Random Number1




namespace RandomNoGenerate1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void btn_RGenerate_Click(object sender, EventArgs e)
{
Random rdm= new Random();
textBox1.Text = rdm.Next(100, 1000).ToString();
listBox1.Items.Add(textBox1.Text);
}

private void btn_Add_Click(object sender, EventArgs e)
{
//listBox1.Items.Add(textBox1.Text);
}
}

Named and optional parameters in C# 4.0


One commonly heard grievance about C# was how it did not support optional parameters. Well C# 4.0 not only has that, but you can also specify parameters by name.

Consider the following two examples :

public void RepeatText(string text, int count = 3)
{
while (count-- > 0)
{
Console.WriteLine(text);
}

Console.WriteLine();
}

public void RepeatDecoratedText(string text, string decoration = "Mr.", int count = 3)
{
while (count-- > 0)
{
Console.WriteLine("{0} {1}", decoration, text);
}

Console.WriteLine();
}

RepeatText has an optional parameter count with a default value of 3, while RepeatDecoratedText has two optional parameters. The MSIL generated for these methods are as follows (simplified, and not exact) :

.method public void RepeatText(string text, [opt] int32 count)
{
.param [2] = int32(3)

.method public void RepeatDecoratedText(string text, [opt] string decoration, [opt] int32 count)
{
.param [2] = string('Mr.')
.param [3] = int32(3)

[opt] is a parameter attribute that indicates that this parameter is optional and that the default value will be specified by .param entries. These methods can now be called as follows:

RepeatText("Nish");
RepeatText("Ant", 2);

The compiler generates code with the optional values filled in as needed:

RepeatText("Nish", 3); <-- 2nd param set via .param value RepeatText("Ant", 2); <-- No extra work needed You can also now pass an argument by name. This is particularly useful when you want to skip some optional args, but provide others. RepeatDecoratedText("Nish", count: 2); RepeatDecoratedText(count: 2, text: "Ant"); RepeatDecoratedText(decoration: "The super", count: 2, text: "Ant"); That gets compiled as: RepeatDecoratedText("Nish", "Mr." 2); RepeatDecoratedText("Ant", "Mr.", 2); RepeatDecoratedText("Ant", "The Super", 2); So basically, the parameters are either filled in via the named values provided or via the default optional values. Note that you can write optional value methods using attributes too. Example: public void RepeatTextDuplicate(string text, [Optional, DefaultParameterValue(3)] int count) { RepeatText(text, count); } // . . . RepeatTextDuplicate("Nish"); RepeatTextDuplicate("Nishant", 5); The MSIL generated for this method is identical to what you’d have got with the direct C# syntax (used earlier). As with dynamic types, apparently the biggest beneficiaries of these new named and optional parameter features are developers who interop with Office. Just like in VBScript you can now avoid having to provide optional arguments, and can also provide a named argument directly. For others, optional parameters will definitely help reduce the number of method overloads you need to write, and where there are methods that have more than a few optional parameters, the ability to call them by specifying named parameters will result in cleaner and more legible code. referred from voidnish

Tip : Flush the KeyBoard buffer in C#

Q) How to flush the keyboard buffer in a C# console application?

Sol:

Here’s a simple to do this :

private static void FlushKeyboard()
{
while (Console.In.Peek() != -1)
Console.In.Read();
}

Now you can use it as follows :

char x = (char)Console.Read();
FlushKeyboard();
char y = (char)Console.Read();
Console.WriteLine("{0}, {1}", x, y);

Even if you enter more than a single character after the first call to Read(), the second Read() will not be affected.

...

Tuesday, September 8, 2009

How to make the numeric values as left alignment in DataGridView control ?

C#:
How to show the numerical values as Left alignment in the datagridview control as making the positions of the digits (like Thousand hundreds tens ones)

How to compute the numeric values of DataGridView's Column [Field] and result value to be in next row?

C#:
In my application program, i had placed a DataGridView for tabular format. Here what i
need is to calculate the numeric values of a column which is in datagridview and the
result should be in the next row of that column and/or some other textbox named as resulttb.
For Example,[from the below figure] i need to calculate the total sum of the numeric
Field"Execution" like (15+25+2000+500000+10 = 502050)& "Sup_Execution" like
(2+2+50000+100000+100=150104) these total results should be placed in followed row of that
field and also in the resulttb textbox control.




And also i needs to calculate "Total"field(hidden) that the value get from the fields
(Execution) * (Sup_Execution) of row wise.

Followers