.Net Technical Answers



Here is few of my .NET Technical Answer .
  • How to Pass Winform Textbox Text to Google search text Box? 
ANS:
textbox1.Text = "CodeProject";
string SearchGoogle = "http://www.google.com/#hl=en&q="+ textbox1.Text.Trim() +"&aq=f&oq=&aqi=&fp=leBsIIJAIN0";
System.Diagnostics.Process.Start(SearchGoogle);

  • How to Factorial Number in C#? 
  • ANS:

    protected void Button1_Click(object sender, EventArgs e)
    {
    // Call your fucntion here and pass your input to this method
    String Factorials = YourFactorial(3);
    }

    public String YourFactorial(int n)
    {

    if (n <= 1)
    {
    return " 1! :" + " The result is : 1" ;
    }

    int result = 1;

    String results = "";
    results = "1! + ";
    for (int i = 2; i <= n; i++)
    {
    results =results + i.ToString()+" ! + ";
    result = result * i;

    }

    return results + " The result is :" + result;

    }

  • How to Split Month  and Date of this string "
    ABC_V3_Weekly_Abcdef_from_NOV_05_to_NOV_12" 
    in C#? 
  • ANS:

    String sval = "ABC_V3_Weekly_Abcdef_from_NOV_05_to_NOV_12";
    int Startvale = sval.IndexOf("_from_") + "_from_".Length;
    int EndValue = sval.IndexOf("_to");
    String FinalString = sval.Substring(Startvale, EndValue - Startvale);
    int monthInDigit = DateTime.ParseExact(FinalString.Substring(0, 3), "MMM", CultureInfo.InvariantCulture).Month;
    String result = FinalString.Substring(4, 2) + "/" + monthInDigit.ToString() + "/" + DateTime.Now.ToString("yyyy");
    MessageBox.Show(result.ToString())

    String result1 = sval.Substring(sval.Length-6);//Betweenas(sval, "_to_", "_to");

    • When to use Static Function in C#? 
    • ANS:Check my Article for Static Class and Method Details.
      Basic C# OOP Concept

      Now lets focus on your question
      So you are asking the differance between static method and Public Static Method

      As you know if we declare class or static or method as static we need to create object for to access that methods.
      We can directly access the static method usign there class
      example Yourclass.YourStaticMethod();

      if you declare only static method by default it will be as private so outside class cannot access that method.
      If you declare the static method as public outside class can access that method.

      see the sample console program below for static method and public static method.

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;
      namespace ConsoleApplication1
      {
      public class YourstaticClass
      {
      static void welcomewithprivate()
      {
      Console.WriteLine("iam in Private Static Clss Method");
      }
      public static void WelcomewithPublic()
      {
      Console.WriteLine("iam in Private Static Clss Method");
      }
      }
      class Program
      {
      static void Main(string[] args)
      {
      //This line will be as error due to private static method can not be accessed in another class
      //YourstaticClass.welcomewithprivate();
      // Public Static method
      YourstaticClass.WelcomewithPublic();

      }
      }
      }

  • How to get Selected Datagridview Name on Mouse Down event C#? 
  • ANS:

    private void Datagridview1_MouseDown(object sender, MouseEventArgs e)
    {
    MessageBox.Show("Column Name : " + this.Datagridview1.Columns[Datagridview1.HitTest(e.X, e.Y).ColumnIndex].Name);

    MessageBox.Show("Column Name : " + this.Datagridview1.Columns[Datagridview1.HitTest(e.X, e.Y).ColumnIndex].HeaderText);

    }

  • How to set VScrollBar and HScrollBar with DataGridView using C#? 
  • ANS:

    Set ScrollBars property of your Datagridview to False.
    ScrollBars =None
    Add hScrollBar and vScrollBar to your form.

    In your hScrollBar Scroll Event
      private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
            {
                dataGridView1.HorizontalScrollingOffset = hScrollBar1.Value;
            }

    In your vScrollBar Scroll Event
    private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
            {
              //  int verticalval = dataGridView1.VerticalScrollingOffset;
                dataGridView1.FirstDisplayedScrollingRowIndex = vScrollBar1.Value;      
            } 

  • Asp.Net Program for Display multiple table (2 table) values from database by using gridview?
  • ANS:

    private void bindData()
    {
    SqlConnection con=new SqlConnection();
    con.ConnectionString="Your Connection String";
    con.open();
    Dataset ds;
    SqlDataAdapter da;
    da=new SqlDataAdapter("select S1.Col1,S1.Col2,S1.Col3,S1.Col4,S2.Col2,S2.Col3,S2.Col4 from Table1 s1 inner join Table2 s2 on s1.id=s2.id where s1.dates Between dte1 AND dte2" inner join
    Table2 s2 on s1.id=s2.id where s1.dates Between dte1 AND dte2",con);
    da.fill(ds,"MultiTable");
    Gridview1.DataSource=ds;
    Gridview1.Bind();
    }

  • Winform Create Dynamic Textbox by Button click and fill with Listbox Items?
  • ANS:


TextBox[] ctrl;
private void button1_Click(object sender, EventArgs e)
{
int locx = 0;
int locy = 2;
ctrl = new TextBox[listBox1.Items.Count];
int i = 0;
foreach (var listBoxItem in listBox1.Items)
{
ctrl[i] = new TextBox();
ctrl[i].Name = i.ToString();
ctrl[i].Size = new System.Drawing.Size(50, 20);
ctrl[i].Location = new Point(locx, locy);
ctrl[i].Text = listBoxItem.ToString();
locy = locy + 20;
panel1.Controls.Add(ctrl[i]);
i = i + 1;
}
}


  • Winform how to blink lable control three times in button click.?
  • ANS:In windows form
    place one Button and Lable:
    Button Name as :Button1
    Lable Name as :LBL1
    Timer conral as Timer1

    Declare one public integer variable as ival
    ' ---- public variable
    Dim ival As Integer = 0' ---- Button Click Start the Timer
    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
    LBL1.Visible = True Timer1.Interval = 200 Timer1.Enabled = True ival = 0 Timer1.Start()
    LBL1.Visible = True End Sub' ---- Timer Tick Event blink your label
    Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    If LBL1.Visible = True Then LBL1.Visible = False Else LBL1.Visible = True End If ival = ival + 1 If ival = 7 Then If LBL1.Visible = False Then LBL1.Visible = True End If Timer1.Stop()
    End If
    End Sub