Tuesday, May 12, 2009

Export DataGridView into .csv file

Following example I am trying to save the DataGridView's Data into .csv file

Please create a Windows Application and Add a DataGridView and a button on Form1, to fill the data initially please add following code in Form1_Load

image

Double click on the Button and add following code in Button1_Click, in this code we are reading all columns and rows and forming a string and saving in data.csv file

image

Now we are ready with our code, just build, run and test.

Tags: Export DataGridView to .csv file, Windows Forms, C#, .NET

Handle Click Event of Eclipse: Windows Application

Normally we dont have any event for the Drawing objects like circle, eclipse ect., so here I am trying to draw a eclipse using graphics and try to give clicking effect and handing that event.

Declare the event handler and deligate to handle the events

image

Initialise the Click Event for eclipse we are drawing in Form_Load

image

Draw eclipse in Form paint method

image

To give clicking effect we need to draw a similar eclipse when you 
press the mouse, also check the bounds where mouse is being clicked, please add following code in Form1_MouseDown

image

And add following code in Form1_MouseUp

image

Following event will be called when you click the eclipse, so please add following code block

image

Now we are ready our code just build, run and test.

Tags: Drawing in C#, Eclipse, Handling Events

Saturday, May 9, 2009

Getting the Selected Value of Combo Box in DataGridView

In following example I am try to get the Selected value of Combo Box which is in DataGridView.
Create a Windows Applicaiton and Add a DataGridView in the Form1

We need to initialise DataGridViewEditingControlShowingEventHandler of DataGridView to handle its control events, so please use follwoing code in Forms Constructor

image

In the form load we will fetch all the data and assign to DataGridView

image

Now catch the control edit event in the DataGridView and assign the SelectIndexChanged event to each Combo Box in DataGridView

image

Add Follwoing Combo Box's SelectedIndexChanged which we assigned to all the combo boxes in DataGridView

image

Now we are ready with our code lets build, run and test.

Output:

image

Tags: Getting the Selected Value of Combo Box in DataGridView, DataGridView

Wednesday, May 6, 2009

Reading Outlook Contacts in VB.NET

In this article I am trying to read the Outlook contact into a Combo Box and selecting a contact I am displaying his/ her details in below Label controls, so lets start building example.

Create a Windows Forms Application and Add Label and Combo Box and change the lable text to Select Contact.

To use the Outlook object in our project we need include a dll from the add references, now add a reference and select "Microsoft.Office.Interop.Outlook" dll from the list.

After adding the dll, now we will import the name space for that use following import

To access Outlook we need to create a object for Outlook via its interface and to get current session we need a NameSpace object to create these object use following code

Imports

Now in the Form Load we will initialise the objects and fill the combo box with contact names, for that in the Form_Load use following code

FormLoad

Once we get the list of contacts in the Combo box use will select a Contact on selecting a contact we will display his or her details in the labels we put on the form for that we need to add following coding in the ComboBox_SelectedIndexChanged event, basically we are searching through all contacts and get the selected contact and display his or her details

We need to clear the object memory we used in the project in Form_Disposed event

ComboBoxSelect

Now we are ready with our code just build, run and test

Output

Tags: Reading Outlook Contacts using VB.NET

Tuesday, May 5, 2009

Reading ini File using VB.NET

Following fucntion reads INI file by Section and Keys and get the value of the Section. Following function uses GetPrivateProfileStringW API function to read the INI file

Private Declare Unicode Function GetPrivateProfileString Lib "kernel32" _
    Alias "GetPrivateProfileStringW" (ByVal lpApplicationName As String, _
    ByVal lpKeyName As String, ByVal lpDefault As String, _
    ByVal lpReturnedString As String, ByVal nSize As Int32, _
    ByVal lpFileName As String) As Int32

Public Overloads Sub ReadINIFile(ByVal INIPath As String, _
ByVal SectionName As String, ByVal KeyName As String(), _
ByRef KeyValue As String())
    Dim Length As Integer
    Dim strData As String
    strData = Space$(1024)
    For i As Integer = 1 To KeyName.Length - 1
        If KeyName(i) <> "" Then
            'This will read the ini file using Section Name and Key
            Length = GetPrivateProfileString(SectionName, KeyName(i), KeyValue(i), _
            strData, strData.Length, LTrim(RTrim((INIPath))))
            If Length > 0 Then
                KeyValue(i) = strData.Substring(0, Length)
            Else
                KeyValue(i) = ""
            End If
        End If
    Next
End Sub


Testing above function
Create a Sample INI file using below text in C:\Logs\test.ini
[Sample Section 1]
SampleKey1=SAMPLEVALUE1
SampleKey2=SAMPLEVALUE2
[Sample Section 2]
SampleKey3=SAMPLEVALUE3
SampleKey4=SAMPLEVALUE4

And use following code
Dim KeyName(2) As String
Dim KeyValues(2) As String
KeyName(1) = "ServerName"
KeyName(2) = "DBName"
ReadINIFile("C:\Logs\test.ini", "TEST Server", KeyName, KeyValues)
MessageBox.Show(KeyValues(1) & " " & KeyValues(2))

Hope this helps

Monday, May 4, 2009

Validate All TextBoxes Using JavaScript

Using following function you can find out all text boxes in the page are entered a value or not, no need to manually write each text box name to validate.

<script language="javascript" type="text/javascript">
function validate() {
    // Get all input controls
    var arrTextBox = document.getElementsByTagName("input");
    var retValue = 1;
    // Loop through all controls
    for(i=0;i<arrTextBox.length;i++)
    {
        // Check whether control is textbox and has a value
        if (arrTextBox[i].type == "text" && arrTextBox[i].value == "") {
            retValue = 0;
        }
    }
    //based on the validation return the value
    if (retValue == 0) {
        alert("Validation Failed");
        return false;
    }
    else {
        alert("Validation Success");
        return true;
    }
}
<script>


Tags : Validate JavaScript, All TextBoxes, document.getElementByTagName

Saturday, May 2, 2009

Microsoft Tech-Ed 2009 At Hyderabad

 

Microsoft’s Tech-Ed is going to happen in Hyderabad this year, you can here latest technology news from Microsoft speakers who are actually involved in developing that particular technology.

Tech-Ed is one place where you can make yourself ready for next generation technology, today's economic scenario you need to be ahead of others so this Tech-Ed will help you to stay ahead of others.

You can know about the event at  Tech-Ed 2009

You can see what is the agenda at Day 1 Day 2 Day 3

You can register for the event at Register

Tags: Tech-Ed 2009, ASP.NET