Saturday 16 January 2016

Fill dictionary from reader object in c#


Hello guys.

This is the simple code to fill dictionary from reader object.


Dictionary<String, String> AdminSettings = new Dictionary<String, String>();


Now your database code goes here.


while (reader.Read())

                {

                    AdminSettings = Enumerable.Range(0, reader.FieldCount)

                     .ToDictionary(

                         s => reader.GetName(s),

                         s => Convert.ToString(reader.GetValue(s)));

                }


Thanks…

Monday 1 June 2015

Check String contains purticular value from array or text file comma separated string

Hello All,

string text = System.IO.File.ReadAllText(@"Your_File_Path");

Here file contains comma seperated or single value, like abc or abc,def,... etc

String Str = "Your_String_Goes_Here";

Str holds the text in which we want to chech wheather a purticular value exists or not.

var result = text.ToLower().Split(',').Any(sc => Str.ToLower().Contains(sc));

if (result == true)
{
    // Code here
}
else
{
    // Code here
}

That's it, enjoy...

Thursday 7 May 2015

Call C# function from Javascript in WPF



I was developing a desktop app in WPF and where I need to call C# function from Javascript.
I searched and found a post. I know there are so many posts are available on web but I am sharing this only to add one more resource so no one take time to solve their problem.
Very first, below XAML code.

<Window x:Class="WpfApplication1.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 Name="GrdMain">
       
    </Grid>
</Window>


Now code behind,

public partial class MainWindow : Window
    {
        // This nested class must be ComVisible for the JavaScript to be able to call it.
        [System.Runtime.InteropServices.ComVisible(true)]
        public class ScriptManager
        {
            // Variable to store the form of type Form1.
            private MainWindow ParantWindow;

            // Constructor.
            public ScriptManager(MainWindow form)
            {
                // Save the form so it can be referenced later.
                ParantWindow = form;
            }

            // This method can be called from JavaScript.
            public void MethodToCallFromScript()
            {
                // Call a method on the form.
                ParantWindow.AlertMessage();
            }

            // This method can also be called from JavaScript.
            public void AnotherMethod(string message)
            {
                MessageBox.Show(message);
            }
        }

        public void AlertMessage()
        {
            // Indicate success.
            MessageBox.Show("Hey! worked.");
        }

        public MainWindow()
        {
            InitializeComponent();

            WebBrowser web = new WebBrowser();

            // Set the WebBrowser to use an instance of the ScriptManager to handle method calls to C#.
            web.ObjectForScripting = new ScriptManager(this);

            // Create the webpage.
            String HTMLText = @"<html>
                <head>
                       <title>Test</title>
                </head>
                <body>
                   <input type=""button"" value=""Go!"" onclick=""window.external.MethodToCallFromScript();"" />
                    <br />
                    <input type=""button"" value=""Go Again!"" onclick=""window.external.AnotherMethod('Hello, How are you?');"" />
                </body>
                </html>";

            web.NavigateToStream(new System.IO.MemoryStream(Encoding.ASCII.GetBytes(HTMLText)));
            GrdMain.Children.Add(web);
        }
    }


What happened here, There is no way to communicate between application and web browse object.
So to solve this problem I created one class which contains the object of our main window form. Now we’ll pass that class to web object and in web object our javascript resides.
Hope you like this post.

Enjoy coading…
Thanks.

Tuesday 28 April 2015

Move parent and child window together in windows / WPF application



Hello guys,
Today I am sharing my experience to move parent and child window together. Suppose you have a parent window and on the top of that one child window you need to open.
Now if you move parent window then child must be move together.
Another thing child must be shown under the parent window area. Whenever you move or resize the parent window, the child window must not leave its position. That is the primary requirement.

In Window:
Create one project in that you will get one main form. Open that form and add one button. Now double click on button, so you’ll get its button click event. Now insert the below code to achieve our goal.



private void button1_Click(object sender, EventArgs e)
        {
            Form mainWindow = Form.ActiveForm;

            Form wnd = new Form();
            wnd.Height = 30;
            wnd.Width = mainWindow.Width;
            wnd.Top = (mainWindow.Top + mainWindow.Height) - 50;
            wnd.Left = mainWindow.Left; //0;
            wnd.FormBorderStyle = FormBorderStyle.None;
            wnd.Name = "hi";
            wnd.Owner = mainWindow;
            wnd.ShowInTaskbar = false;
            wnd.BackColor = Color.Red;
            wnd.StartPosition = FormStartPosition.Manual;

            Label lbl = new Label();
            lbl.Text = "lasjdflaj lk asldfj alsk fd;las dfasldjf aslkfj las;kjdf l;aks dflas fd";

            wnd.Controls.Add(lbl);
            wnd.Show();

        }

No insert the below code.

public Form1()
        {
            InitializeComponent();

            this.SizeChanged += new EventHandler(Window_SizeChanged);
            this.LocationChanged += new EventHandler(Window_LocationChanged);
        }

private void Window_SizeChanged(object sender, EventArgs e)
        { ChildRelocate(); }
               
        private void Window_LocationChanged(object sender, EventArgs e)
        { ChildRelocate(); }

        private void ChildRelocate()
        {
            foreach (Form item in Application.OpenForms)
            {
                if (item.Name == "hi")
                {
                    Form mainWindow = Form.ActiveForm;
                    item.Width = mainWindow.Width;
                    item.Top = (mainWindow.Top + mainWindow.Height) - 50;
                    item.Left = mainWindow.Left;
                    item.StartPosition = FormStartPosition.Manual;
                    mainWindow = null;
                }
            }
        }


In WPF:

Same as window create on project in wpf and follow the instructions.
You’ll get one main form and in that add one button and create its click event.
Now insert the below code.
public MainWindow()
        {
            InitializeComponent();
            SizeChanged += new SizeChangedEventHandler(Window_SizeChanged);
            StateChanged += new EventHandler(Window_StateChanged);
            LocationChanged += new EventHandler(Window_LocationChanged);
        }

private void btnMarquee_Click(object sender, RoutedEventArgs e)
        {
            //Marquee marquee = new Marquee();
            //marquee.ShowDialog();

            Window mainWindow = Application.Current.MainWindow;
           
            Window wnd = new Window();
            Grid grid = new Grid();
            wnd.Height = 30;
            wnd.Width = mainWindow.Width; 
            wnd.Top = (mainWindow.Top + mainWindow.Height) - 50;
            wnd.Left = mainWindow.Left;
            wnd.WindowStyle = System.Windows.WindowStyle.None;
            wnd.AllowsTransparency = true;
            wnd.Content = grid;
            wnd.Title = "hi";
            wnd.Owner = this;
            wnd.ShowInTaskbar = false;

           
            System.Windows.Controls.Canvas cn = new Canvas();
            cn.Background = Brushes.Red;
            cn.Height = 30;
           
            System.Windows.Controls.TextBlock txtMarqee = new TextBlock();
            txtMarqee.FontSize = 20;
            cn.Children.Add(txtMarqee);
            txtMarqee.Text = "This is basic example of marquee This is basic example of marquee This is basic example of marquee";

            DoubleAnimation doubleAnimation = new DoubleAnimation();
            doubleAnimation.From = -(System.Windows.SystemParameters.PrimaryScreenWidth);
            doubleAnimation.To = System.Windows.SystemParameters.PrimaryScreenWidth;
            doubleAnimation.RepeatBehavior = RepeatBehavior.Forever;
            doubleAnimation.Duration = new Duration(TimeSpan.FromSeconds(15));
            txtMarqee.BeginAnimation(Canvas.RightProperty, doubleAnimation);

            grid.Children.Add(cn);

              wnd.Show();

        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        { ChildRelocate(); }

        private void Window_StateChanged(object sender, EventArgs e)
        { ChildRelocate(); }

        private void Window_LocationChanged(object sender, EventArgs e)
        { ChildRelocate(); }

        private void ChildRelocate()
        {
            foreach (Window item in System.Windows.Application.Current.Windows)
            {
                if (item.Title == "hi")
                {
                    Window mainWindow = Application.Current.MainWindow;
                    item.Width = mainWindow.Width;
                    item.Top = (mainWindow.Top + mainWindow.Height) - 50;
                    item.Left = mainWindow.Left;
                    mainWindow = null;
                }
            }
        }

In WPF I have created an example which is similar to marqee in HTML.
Enjoy guys. Happy coading…