-->

Wednesday, November 11, 2015

WPF Window attribute | Properties examples

Introduction
In this article, we will learn many more attributes or you can say properties of window tag in WPF. First of, I will take Width and Height properties of Window Tag. Width defines Horizontal size of window and Height vertically. Both takes numbers. If you do some changes in these attribute then your window will resize. Now, come to Next attribute that is Title, Define the label of title bar, which is the Top most bar of your window. Title is aligned in center bydefault. 


Icon: If you want to put image in left corner which is realted to your project like suppose your project window is related to login, now you can put login image into your title bar in left corner.

ResizeMode : Change the window size at run time, if you want. Minimize and Maximize button appear by using this arrtibute. Also you can hide both buttons, if you assign "NoResize" value in it. By using this value, hide minimize and maximize button as well as resize arrow. 

ShowInTaskbar : The default value of it is 'True'. It means when your application will run  then your application icon will display into your taskbar. If you assign False then does not appear in Taskbar.

SizeToContent : Define the window size which is totaly depend on your content. If you assign 'width' as a value in it then your window's width resized. Similarly in case of Height.

TopMost : Your window will always appear at top position. The Default value of it is false.

Tuesday, November 10, 2015

Example domainUpDown control in windows form c#

Introduction
In this article, we will see, How to use domainUpDown control in windows form c#. This control is used to choose single item at time, In this we have multiple options. If you see this control then you notice that this control is similar to Numeric UpDown control. But NumericUpDown control display numbers only. domainUpDown control display string like display month's name and many more things. In this article we will see many more interesting things which are related to domainUpDownControl.
Example domainUpDown control in windows form c#

How to use doaminUpDown Control
  1. First of all add domainUpDown control from ToolBox
  2. Copy this code paste into your code file

    private void Form2_Load(object sender, EventArgs e)
        {
            DomainUpDown.DomainUpDownItemCollection collection = this.domainUpDown1.Items;
            collection.Add("Jan");
            collection.Add("Feb");
            collection.Add("Mar");
            collection.Add("Apr");

            //How to set the text default in DomainUpDown

            this.domainUpDown1.Text = "Jan";

        }


The Last line of code explain the default value of domainUpDown Control. 
In the Second example, I will explain you how to retrieve value from domainUpDown Control using SelectedItemChanged.

        private void domainUpDown1_SelectedItemChanged(object sender, EventArgs e)
        {
            MessageBox.Show(domainUpDown1.Text);
        }

When, first time the page is load then appear message box on your screen with the text message of selected item of domainUpDown Control.

Now, Learn how to add domainUpDown control using code file. Also learn how to fire SelectedItemChanged event using code file.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication3
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            DomainUpDown dynamicallyadd = new DomainUpDown();
            dynamicallyadd.Location = new System.Drawing.Point(12, 50);
            dynamicallyadd.Name = "domainupdown";
            dynamicallyadd.Width = 250;
            dynamicallyadd.Height = 100;
            dynamicallyadd.Items.Add("Apple");
            dynamicallyadd.Items.Add("mango");
            dynamicallyadd.Items.Add("Grapes");
            dynamicallyadd.Text = "Apple";
            this.Controls.Add(dynamicallyadd);
            dynamicallyadd.SelectedItemChanged += new System.EventHandler(Itemchanged);
        }

        private void Itemchanged(object sender, EventArgs e)
        {
           // throw new NotImplementedException();
            DomainUpDown item = sender as DomainUpDown;
            MessageBox.Show(item.Text);
        }
    }
}



Now, In this last section of the domainUpDown Control, we will learn how to bind it with Database table. If you bind it with SQL server then  first to add EDMX file in the project.

      private void Form1_Load(object sender, EventArgs e)
        {
            BankingSystemEntities bs = new BankingSystemEntities();
            var item = bs.userAccounts.ToArray();
            foreach (var item1 in item)
            {
                domainUpDown1.Items.Add(item1.Account_No.ToString());
            }



Monday, November 9, 2015

How to get cell value from dataGridView in windows form c#

In this article, I will teach you, How to get cell value from dataGridView in windows form c#. First of all bind the dataGridView with any Data Source then you can access cell value from it. I bind it with many data source, check this link. In previous article, I get the cell value from selected row of  dataGrid but on this time, we set the default row. In this article, I will give an example of cell click event.


private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
     
            decimal accno = Convert.ToDecimal(dataGridView1.Rows[e.RowIndex].Cells[0].Value);
            MessageBox.Show(accno.ToString());
}

Here, Cells[0] return the first column's cell value from dataGridView.

Friday, November 6, 2015

LOAD IMAGE WITH IMAGE DATATYPE INTO TABLE IN ASP.NET WEBFORM

In this article, I will teach you, How to load image into database table with image type in ASP.NET. In previous article i already explained how to save image with their path.So, If you want to do this task then first of all design a table with image datatype.

Database table

LOAD IMAGE WITH IMAGE DATATYPE INTO TABLE IN ASP.NET WEBFORM


Source code 

<form id="form1" runat="server">
    <div>
 
        <asp:FileUpload ID="FileUpload1" runat="server" />
 
    </div>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
    </form>

Code behind page

using System.Drawing;
using System.IO;
protected void Button1_Click(object sender, EventArgs e)
    {
        BankingSystemEntities1 BS = new BankingSystemEntities1();
        Bitmap bitmap;
        if (FileUpload1.HasFile)
        {
            FileUpload1.SaveAs(Server.MapPath("~/image/" + FileUpload1.FileName));
            bitmap = new Bitmap(Server.MapPath("~/image/" + FileUpload1.FileName));
            MemoryStream ms = new MemoryStream();
            bitmap.Save(ms, bitmap.RawFormat);


            userAccount uacc = new userAccount();
            uacc.Account_No = 123456789;
            uacc.Picture = ms.ToArray();
            BS.userAccounts.Add(uacc);
            BS.SaveChanges();

         
        }
    }

Code generrates the following output



Wednesday, November 4, 2015

Get data From Database table using EDMX in ASP.NET

In this article, I will show you, how to get data from Database table using EDMX file. Actually EDMX reduces lots of codes, you can see this article which contains lots of codes. EDMX file provides model as well as context class. By using public property of context class, we can communicate with the table.  Before copy this code, first to add EDMX file in solution.

Get data From Database table using EDMX in ASP.NET


Source code

   <form id="form1" runat="server">  
   <div>  
     <asp:GridView ID="GridView1" runat="server">  
     </asp:GridView>  
   </div>  
   </form>  


Code Behind 

   private void loadgrid()  
   {  
     //throw new NotImplementedException();  
     dbEntities dbe = new dbEntities();  
     var item = dbe.usertables.ToList();  
     GridView1.DataSource = item;  
     GridView1.DataBind();  
   }  

Monday, November 2, 2015

Insert data into Database table WPF

In this article, I will show you how to insert data into Database table using EDMX file. So, before copy this code first to add EDMX file in the project with full configuration. In the source code, we have two text block, two text box and one button control.
Description
I explained, example of login form in WPF, WPF listview with item template binding, Start learning with WPF,


Source code (XAML)
 <Window x:Class="WpfApplication7.MainWindow"  
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
     Title="MainWindow" Height="350" Width="525">  
   <Grid>  
     <TextBlock HorizontalAlignment="Left" Margin="75,68,0,0" TextWrapping="Wrap" Text="Username" VerticalAlignment="Top"/>  
     <TextBlock HorizontalAlignment="Left" Margin="75,124,0,0" TextWrapping="Wrap" Text="Password" VerticalAlignment="Top"/>  
     <TextBox Name="t1" HorizontalAlignment="Left" Height="23" Margin="166,70,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>  
     <TextBox Name="t2" HorizontalAlignment="Left" Height="23" Margin="166,123,0,0" TextWrapping="Wrap" Text="TextBox" VerticalAlignment="Top" Width="120"/>  
     <Button Content="SAVE" HorizontalAlignment="Left" Margin="166,173,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>  
   </Grid>  
 </Window>  

Code behind code

   private void Button_Click(object sender, RoutedEventArgs e)  
     {  
       dbEntities dbe = new dbEntities();  
       usertable ut = new usertable();  
       ut.Username = t1.Text;  
       ut.Password = t2.Text;  
       dbe.usertables.Add(ut);  
       dbe.SaveChanges();  
     }  
Code Generates the following output



Get last record from Database using LINQ in WPF

Today, I faced a problem which is related to banking application like account number. When I load the application to create new account for new customer then account number automatically increased by one from previously stored account. So, I want to pick last field value of Database table.


Get last record from Database using LINQ in WPF


Let's see the example:

  <Grid>  
     <Button Content="Button" HorizontalAlignment="Left" Margin="70,79,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>  
   </Grid>  

Code behind code

   private void Button_Click(object sender, RoutedEventArgs e)  
     {  
       DatabaseEntities dbe = new DatabaseEntities();  
       var getlastrec = dbe.users.ToArray().LastOrDefault();  
       MessageBox.Show(getlastrec.Id.ToString());  
     }  

Here DatabaseEntities is the context class. In this context class, we have public properties that is users. Through this we can get all records from user table, which is exist in model folder. Before doing this code, must to add EDMX file
© Copyright 2013 Computer Programming | All Right Reserved