-->

Wednesday, December 16, 2015

Example of Context menu in WPF

Context Menu Introduction:
Context menu means a window appear when we click on right button of mouse on selected Text. Today, we will design context menu with the help of WPF. Here, we will take a Input control with context menu also context menu contain MenuItem.



XAML Code
<Grid>
        <RichTextBox >
            <RichTextBox.ContextMenu>
                <ContextMenu>
                    <MenuItem Command="Cut">
                        <MenuItem.Icon>
                            <Image Source="apple.png"/>
                        </MenuItem.Icon>
                    </MenuItem>
                    <MenuItem Command="Copy">
                        <MenuItem.Icon>
                            <Image Source="grapes.png"/>
                        </MenuItem.Icon>
                    </MenuItem>
                </ContextMenu>              
               
            </RichTextBox.ContextMenu>
                 
           
        </RichTextBox>

    </Grid>
Suppose, your richTextBox is disabled and you want to show context menu on disabled item then use this code:

<RichTextBox IsEnabled="False" ContextMenuService.ShowOnDisabled="True">

Saturday, December 12, 2015

AWT Frame close using close Button

In AWT Java we have a Frame class which is inherit from window class. If you are a beginner in AWT Java then you know that Frame doesn't close when we press close button of it. So, In last we pressed stop debugging button in Netbeans IDE. If you want to close Frame using Close button, which is see in top right corner of Frame then you should implement code for it. 


Complete Java Code


package awtframe;

import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;


public class AWTFrame {

    public AWTFrame()
    {
        Frame f1=new Frame("Welcome to Closing");
        f1.setSize(500,500);
        f1.setVisible(true);
       
        f1.addWindowListener(new WindowAdapter(){
           
            @Override
            public void windowClosing(WindowEvent e)
            {
              System.exit(0);
            }
           
        });
       
       
    }
    public static void main(String[] args) {
     
       AWTFrame at=new AWTFrame();
       
    }
   
}

Here, we have windowClosing ( ) method to close Frame using close button.

Thursday, December 10, 2015

How to pass data from controller to view in ASP.NET MVC

If you want to pass value from controller to view in asp.net MVC, we have two methods. The first method of it is ViewBag and last is ViewData, both are used to pass data from controller to view. ViewBag object creates a dynamic variable and ViewData creates a dynamic key like ViewState in ASP.NET.  Learn, How to use ViewBag in MVC for single variable as well as List<String> type variable. 


In controller class, we used ViewBag.Dynamic_variable = "Value" and in View we used @ViewBag.Dynamic_Variable_Name. Also, learn how to pass value from controller to view using ViewData.



Both objects doesnot generate compile time error.

Tuesday, December 8, 2015

Password Box example in WPF

If you want to design login control then must to set the special character in password box. Today, we will learn, how to design password box in WPF. In WPF, we have a PasswordBox tag in XAML , through this we can add Password box in WPF. I have a simple login control through you can see password control.



The complete code of login control

<Window x:Class="WpfApplication8.password"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="password" Height="300" Width="300">
    <Grid> 
        <StackPanel Margin="20">
            <Label>Password Box Example</Label>
            <WrapPanel>
                <Label>Username:</Label>
                <TextBox Width="100"/>
            </WrapPanel>
            <WrapPanel>
                <Label>Password:</Label>
                <PasswordBox PasswordChar="x" MaxLength="8" Width="100"/>
            </WrapPanel>
            
            
        </StackPanel>
        
    </Grid>
</Window>

Code Generates the following output:


Monday, December 7, 2015

WPF common "enable all" checkbox in the top

Introduction
In this WPF article, I will show you, single checkbox enable all other remaining checkboxes; if it is unchecked then remaining them also unchecked. To do this task, first of add single checkbox for all other checkboxes in the stack panel. Now, add other child checkboxes in nested stack panel. look at, simple xaml code view.



<Window x:Class="WpfApplication8.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>
        <StackPanel Margin="10">
            <Label FontWeight="Bold">Technical Skills</Label>
            <StackPanel Margin="20,10">
                <CheckBox IsThreeState="True" Name="AllSelect" Checked="AllSelect_Checked" Unchecked="AllSelect_Checked">Enable All</CheckBox>
                <StackPanel Margin="20,10">
                    <CheckBox Name="singlecheckjava" Checked="singlecheckjava_Checked" Unchecked="singlecheckjava_Checked">Java</CheckBox>
                    <CheckBox Name="singlecheckcpp" Checked="singlecheckjava_Checked" Unchecked="singlecheckjava_Checked">C++</CheckBox>
                    <CheckBox Name="singlecheckphp" Checked="singlecheckjava_Checked" Unchecked="singlecheckjava_Checked">PHP</CheckBox>          
             
                 
                 
                </StackPanel>
            </StackPanel>      
         
         
        </StackPanel>
    </Grid>
</Window>
Here, we have checked and unchecked events. Both are calling same function in "Allselect" checkbox. In the child checkboxes also we have checked and unchecked event , they are calling same method. So in this program we have two method that is "AllSelect_Checked" and "singlecheckjava_Checked".

Code Behind Code 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfApplication8
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void AllSelect_Checked(object sender, RoutedEventArgs e)
        {
            bool allcheckbox = (AllSelect.IsChecked == true);
            singlecheckjava.IsChecked = allcheckbox;
            singlecheckcpp.IsChecked = allcheckbox;
            singlecheckphp.IsChecked = allcheckbox;

        }

        private void singlecheckjava_Checked(object sender, RoutedEventArgs e)
        {
            AllSelect.IsChecked = null;
            if ((singlecheckjava.IsChecked == true) && (singlecheckcpp.IsChecked == true) && (singlecheckphp.IsChecked == true))
                AllSelect.IsChecked = true;
            if ((singlecheckjava.IsChecked == false) && (singlecheckcpp.IsChecked == false) && (singlecheckphp.IsChecked == false))
                AllSelect.IsChecked = false;
        }
    }
}

Friday, December 4, 2015

How to add window media player in ASP.NET

Introduction
In this article, I will show you how to play mp4 and WMV file in asp.net. To do this task first to install Silverlight into your computer by the following link. After installed Silverlight, add System.Web.Silverlight.dll file reference in the bin folder. Add media player component in ToolBox by using right click on toolBox.


Right click on toolbox--> choose Items...--->.Net Framework Components--> Click on Browse Button also select System.Web.Sliverlight from uncompressed folder.



Drag Media player control from ToolBox to Web Form. Set Required property that is mentioned below.
Media Source = ~/movie/first.mp4
Here, we have movie folder.

Download System.Web.Silverlight.dll file.

Code generates the following output

How to add window media player in ASP.NET

Thursday, December 3, 2015

Add multiple TextBox dynamically using Button click in ASP.NET C#

Introduction
In this article, I will show you how to add multiple TextBox by using button click. When we press the button then add TextBox in the panel. Similarly again when we press the button then again TextBox add just after previous one. I will give you an example of add multiple TextBox dynamically in ASP.NET.




Source code

<body>
    <form id="form1" runat="server">
    <div>

    </div>
    </form>
</body>

Code Behind page

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class DynamicallyAdd : System.Web.UI.Page
{
    Panel panelforTextBox;
    protected void Page_Load(object sender, EventArgs e)
    {
        Literal literal;
        Label labelTextBox;

        panelforTextBox = new Panel();
        panelforTextBox.ID = "panelforTextBox";
        panelforTextBox.BorderWidth = 2;
        panelforTextBox.Width = 400;
        this.form1.Controls.Add(panelforTextBox);

        literal = new Literal();
        literal.Text = "<br/>";

        this.form1.Controls.Add(literal);

        labelTextBox = new Label();
        labelTextBox.Text = "Dynamic TextBox <br/>";


        panelforTextBox.Controls.Add(labelTextBox);
        Button addbutton = new Button();
        addbutton.ID = "addbutton";
        addbutton.Text = "Add New TextBox";
        addbutton.Click += new System.EventHandler(clicktoaddTextBox);
        this.form1.Controls.Add(addbutton);

        if (IsPostBack)
        {
            controlcreate("DynamicText", "TextBox");
        }

    }

    private void controlcreate(string p1, string p2)
    {
        string[] controls = Request.Form.ToString().Split('&');
        int count = findcontrolcount(p1);
        if(count>0)
        {
            Literal lit;
            for (int k = 0; k <=count; k++)
            {
                for (int i = 0; i < controls.Length; i++)
                {
                    if (controls[i].Contains(p1+"-"+k.ToString()))
                    {
                        string controlname = controls[i].Split('=')[0];
                        string controlvalue = controls[i].Split('=')[1];
                        controlvalue = Server.UrlDecode(controlvalue);
                        if(p2=="TextBox")
                        {
                            TextBox txt = new TextBox();
                            txt.ID = controlname;
                            txt.Text = controlvalue;
                            panelforTextBox.Controls.Add(txt);
                            lit = new Literal();
                            lit.Text = "<br/>";
                            panelforTextBox.Controls.Add(lit);

                        }
                        break;
                    }
                }
            }
        }
    }

    private void clicktoaddTextBox(object sender, EventArgs e)
    {
        //throw new NotImplementedException();
        Button button = (Button)sender;
        if (button.ID=="addbutton")
        {
            int count = findcontrolcount("DynamicText");
            TextBox text = new TextBox();
            text.ID = "DynamicText-" + Convert.ToString(count + 1);
            panelforTextBox.Controls.Add(text);
            Literal lit = new Literal();
            lit.Text = "<br/>";
            panelforTextBox.Controls.Add(lit);
        }


    }

    private int findcontrolcount(string p)
    {
        //throw new NotImplementedException();
        string str = Request.Form.ToString();
        return ((str.Length - str.Replace(p, "").Length) / p.Length);

    }

}
© Copyright 2013 Computer Programming | All Right Reserved