h1.post-title { color:orange; font-family:verdana,Arial; font-weight:bold; padding-bottom:5px; text-shadow:#64665b 0px 1px 1px; font-size:32px; } -->

Pages

Use of DataTimePicker MinData and MaxDate Property

In this Project, I am giving code for using MinDate and MaxDate for One DateTimepicker Control.The main purpose of this project is selecting a Financial year based on Country. In my Project , 1 Forms ,One ListBox,One Button,One ComboBox and One DateTimePicker. Form Name: frmFinancialYear.csListBox: lstFinalcialYear Button: btncallDateTimePicker:dtpfinanceDateComboBox:CmbCountry
public static string selectedValue; public static string countryname;
enum CountryName { India, America }
private void frmFinancialYear_Load(object sender, EventArgs e) { CmbCountry.Items.Add(CountryName.India.ToString()); CmbCountry.Items.Add(CountryName.America.ToString()); }
private static string[] Splitvalues(string str) { result= str.Split('-'); return result; }
private void countrywisedatevalue(string str,string[] selectedyear) { switch (str) { case "India": startingYear = "04/01/" + selectedyear[0]; endingYear = "03/31/" + selectedyear[1]; dtpfinanceDate.MinDate = Convert.ToDateTime(startingYear); dtpfinanceDate.MaxDate = Convert.ToDateTime(endingYear); dtpfinanceDate.Value = dtpfinanceDate.MinDate; break; case "America": startingYear = "01/01/" + selectedyear[0]; endingYear = "12/31/" + selectedyear[0]; dtpfinanceDate.MinDate = Convert.ToDateTime(startingYear); dtpfinanceDate.MaxDate = Convert.ToDateTime(endingYear); dtpfinanceDate.Value = dtpfinanceDate.MinDate; break; } }
private void btncall_Click(object sender, EventArgs e) { selectedValue = lstFinalcialYear.SelectedItem.ToString(); countryname =CmbCountry.Text; countrywisedatevalue(countryname, Splitvalues(selectedValue)); }

Dynamic Property with XML

XML and retrieve Data from XML to Dynamic Property Step: 1 In First step, we will discuss about How to create Dynamic Property in C#.net. Properties are GroupBox, ComboBox, TextBox and CheckBox. Here these are the Declaration part of Dynamic Properties.
Public static System.Windows.Forms.GroupBox gb; Public static System.Windows.Forms.TextBox txt; Public static System.Windows.Forms.RadioButton chk; Public static System.Windows.Forms.ComboBox cmb;
Here these are the Declaration String
Public static string txt1, txt2; Public static string txt3; Public static string text;
Here this is the Declaration String - To store a XML Path Public static string ModifiedPath = Path.GetDirectoryName(Application.ExecutablePath) + "\\Folder\\dataText"; I have 3 buttons in my Form (Buttons are not created dynamically).Drag from Property window. Button1 (Text) - - Property Button2 (Text) - - Save Button3 (Text) - - Display. In First button click event:
private void button1_Click(object sender, EventArgs e) { gb = new GroupBox(); gb.Name = "GroupName"; gb.Text = "Virtual COM Port"; gb.Font = new System.Drawing.Font("Microsoft Sans Serif", 9F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); gb.Width = 750; gb.Height = 150; gb.Left = 10; gb.Top = 15; this.Controls.Add(gb); int i; for (i = 0; i < 3; i++) { cmb = new ComboBox(); cmb.Name = "combobox" + i.ToString(); cmb.Text ="Combo"+i.ToString(); cmb.Width = 100; cmb.Height = 20; cmb.Width = 75; cmb.Height = 20; cmb.Left = 15; cmb.Top = 20 + (cmb.Height * i) + (10 * i); //initial + height + padding cmb.DropDownStyle = ComboBoxStyle.DropDownList; cmb.Items.Add("COM5"); cmb.Items.Add("COM6"); txt = new TextBox(); txt.Name = "Textbox" + i.ToString(); txt.Text = "Testtext" + i.ToString(); txt.Width = 150; txt.Height = 20; txt.Left = 150; txt.Top = 20 + (txt.Height * i) + (10 * i); chk = new RadioButton(); chk.Name = "RB" + i.ToString(); chk.Text = "Enabled"; chk.Width = 100; chk.Height = 20; chk.Left = 320; chk.Top = 20 + (chk.Height * i) + (10 * i); //initial + height + padding gb.Controls.Add(cmb); gb.Controls.Add(txt); gb.Controls.Add(chk); } } // Create XML: private static void createxml(String xmlpath) { int index; string sFilename = xmlpath; XmlDocument xmldoc = new XmlDocument(); XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", null, null); xmldoc.AppendChild(xmldec); XmlElement xmlele = xmldoc.CreateElement("Root"); xmldoc.AppendChild(xmlele); for (index = 0; index < 3; index++) { string targetComboBox = "Combobox" + index; //Try to find the textbox int ComboBoxIndex = gb.Controls.IndexOfKey(targetComboBox); if (ComboBoxIndex != -1) { ComboBox foundComboBox = (ComboBox)gb.Controls[ComboBoxIndex]; Userprefrenece.txt2 = foundComboBox.Text; } string targetTextBox = "Textbox" + index; //Try to find the textbox int textBoxIndex = gb.Controls.IndexOfKey(targetTextBox); if (textBoxIndex != -1) { TextBox foundTextBox = (TextBox)gb.Controls[textBoxIndex]; Userprefrenece.txt1 = foundTextBox.Text; } string targetCheckBox = "RB" + index; //Try to find the textbox int targetCheckBoxs = gb.Controls.IndexOfKey(targetCheckBox); if (targetCheckBoxs != -1) { RadioButton foundCheckBox = (RadioButton)gb.Controls[targetCheckBoxs]; if (foundCheckBox.Checked == true) { Userprefrenece.txt3 = true; } else { Userprefrenece.txt3 = false ; } } String str = "port" + index.ToString(); XmlElement port = xmldoc.CreateElement(str); xmlele.AppendChild(port); port.InnerText = Userprefrenece.txt2 + "-" + Userprefrenece.txt1 + "-" + Userprefrenece.txt3; } xmldoc.Save(sFilename); MessageBox.Show("Saved Successfully"); } private static void readxml(String xmlpath) { string sFilename = xmlpath; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(sFilename); XmlElement rootElem = xmlDoc.DocumentElement; //Gets the root element, in your xml its "Root" for (int i = 0; i < rootElem.ChildNodes.Count ; i++) { string name = rootElem.ChildNodes[i].Name; string value = rootElem.ChildNodes[i].InnerText; string[] words = value.Split('-'); txt1 = words[0]; txt2 = words[1]; txt3 = words[2]; gb.Controls["Combobox" + (i + 0).ToString()].Text = txt1; gb.Controls["Textbox" + (i + 0).ToString()].Text = txt2; gb.Controls["RB" + (i + 0).ToString()].Text = txt3; RadioButton cb = gb.Controls["RB" + i.ToString()] as RadioButton; cb.Checked = (txt3 == "True"); gb.Controls["RB" + (i + 0).ToString()].Text = "Enabled"; } } private void button2_Click(object sender, EventArgs e) { createxml(ModifiedPath + "\\UserPreferences.xml"); } private void button3_Click(object sender, EventArgs e) { readxml(ModifiedPath + "\\UserPreferences.xml"); }

Cross Word Application in C#.Net

Purpose of this Application: “A crossword is a word puzzle that normally takes the form of a square or a rectangular grid of white and shaded squares.” Wikipedia I am very much interested in the Cross Words Games [CWG]. So I developed this Application. 
private static bool chkEmpty;
private static bool  IdetifyContNull(System.Windows.Forms.TextBox txt)
{
    if (txt.Text == "")
    {
        chkEmpty = true;
    }
    else
    {
        chkEmpty = false;
    }
    return chkEmpty;
} 
Application Processing:
                   Two Command buttons in the Application. They are Question Button and Answer Button.
 Question Button:
  • Clicks “Question” Button.
  • Question will display in the RichTextBox.
Answer Button:
  • ·         Check Answer.
  • ·         Display Message Box.

Now we will go to Coding Section.
 Step by Step Processing
Create a Project in the VS C#.2008.
From name: frmCrossword.cs.
Controls in Application:
    Label, Textboxes, Panel and Buttons.
Class File(s):CheckAnswer.cs
CheckAnswer.cs – Code:
class CheckAnswer
    {
        public  static bool chkAnswer1;
        public static bool chkAnswer2;
        public static bool chkAnswer3;
        public static bool chkAnswer4;
        public static bool chkAnswer5;
        public static bool chkAnswer6;
        public static bool chkAnswer7;
        public static bool chkAnswer8;
        public static bool chkAnswer9;
        public static bool chkLeftToRightQ1(string str)
        {
            if (str == "ABSTRACT")
            {
                chkAnswer1 = true;
            }
            else
            {
                chkAnswer1 = false;
            }
            return chkAnswer1;
        }

        public static bool chkLeftToRightQ18(string str)
        {
            if (str == "SBYTE")
            {
                chkAnswer2 = true;
            }
            else
            {
                chkAnswer2 = false;
            }
            return chkAnswer2;
        }

        public static bool chkLeftToRightQ35(string str)
        {
            if (str == "STRING")
            {
                chkAnswer3 = true;
            }
            else
            {
                chkAnswer3 = false;
            }
            return chkAnswer3;
        }
        public static bool chkLeftToRightQ41(string str)
        {
            if (str == "NEW")
            {
                chkAnswer4 = true;
            }
            else
            {
                chkAnswer4 = false;
            }
            return chkAnswer4;
        }
        public static bool chkTopToBottomQ1(string str)
        {
            if (str == "AS")
            {
                chkAnswer5 = true;
            }
            else
            {
                chkAnswer5 = false;
            }
            return chkAnswer5;
        }

        public static bool chkTopToBottomQ4(string str)
        {
            if (str == "TRY")
            {
                chkAnswer6 = true;
            }
            else
            {
                chkAnswer6 = false;
            }
            return chkAnswer6;
        }

        public static bool chkTopToBottomQ5(string str)
        {
            if (str == "RETURN")
            {
                chkAnswer7 = true;
            }
            else
            {
                chkAnswer7 = false;
            }
            return chkAnswer7;
        }
        public static bool chkTopToBottomQ19(string str)
        {
            if (str == "BASE")
            {
                chkAnswer8 = true;
            }
            else
            {
                chkAnswer8 = false;
            }
            return chkAnswer8;
        }

        public static bool chkTopToBottomQ39(string str)
        {
            if (str == "NULL")
            {
                chkAnswer9 = true;
            }
            else
            {
                chkAnswer9 = false;
            }
            return chkAnswer9;
        }

    }
frmCrossword.cs -- Code   
private void button1_Click(object sender, EventArgs e)
     {
          CrosswordQuestions();
         richTextBox1.ReadOnly = true;
     } 
  private void CrosswordQuestions()
  {
      richTextBox1.SelectedText = Environment.NewLine + "Left To Right";
      richTextBox1.ForeColor = Color.Blue;
      richTextBox1.SelectedText = Environment.NewLine + "1)An ___________ class can contain either abstract methods or non abstract methods";
      richTextBox1.SelectedText = Environment.NewLine + "18) ___________ keyword denotes an integral type that stores values according to the size and range.";
      richTextBox1.SelectedText = Environment.NewLine + "35)AlphaNumeric.";
      richTextBox1.SelectedText = Environment.NewLine + "41) ______ keyword can be used as an operator or as a modifier Last two letters order changed.";
      richTextBox1.SelectedText = Environment.NewLine + "Top To Bottom";
      richTextBox1.SelectedText = Environment.NewLine + "1) Which keyword retunr null if a cast will not success?";
      richTextBox1.SelectedText = Environment.NewLine + "4) One of the Exception Handling Statement.";
      richTextBox1.SelectedText = Environment.NewLine + "5) Terminates execution of the method in which it appears and returns control to the calling method.";
      richTextBox1.SelectedText = Environment.NewLine + "19) ____________ keyword is used to access members of the base class from within a derived class.";
      richTextBox1.SelectedText = Environment.NewLine + "39) default value of reference-type variables.First two letters order changed.";
} 
private void button2_Click(object sender, EventArgs e)
 {
     chkStatusofTextBox();
 } 
private static bool chkEmpty;
private static bool  IdetifyContNull(System.Windows.Forms.TextBox txt)
{
    if (txt.Text == "")
    {
        chkEmpty = true;
    }
    else
    {
        chkEmpty = false;
    }
    return chkEmpty;
} 
      private void  chkStatusofTextBox()
      {
          bool chkTxt1 = IdetifyContNull(textBox1);
          bool chkTxt2 = IdetifyContNull(textBox2);
          bool chkTxt3 = IdetifyContNull(textBox3);
          bool chkTxt4 = IdetifyContNull(textBox4);
          bool chkTxt5 = IdetifyContNull(textBox5);
          bool chkTxt6 = IdetifyContNull(textBox6);
          bool chkTxt7 = IdetifyContNull(textBox7);
          bool chkTxt8 = IdetifyContNull(textBox8);
          bool chkTxt9 = IdetifyContNull(textBox24);
          bool chkTxt10 = IdetifyContNull(textBox20);
          bool chkTxt11 = IdetifyContNull(textBox31);
          bool chkTxt12 = IdetifyContNull(textBox30);
          bool chkTxt13 = IdetifyContNull(textBox29);
          bool chkTxt14 = IdetifyContNull(textBox28);
          bool chkTxt15 = IdetifyContNull(textBox27);
          bool chkTxt16 = IdetifyContNull(textBox14);
          bool chkTxt17 = IdetifyContNull(textBox12);
          bool chkTxt18 = IdetifyContNull(textBox10);
          bool chkTxt19 = IdetifyContNull(textBox38);
          bool chkTxt20 = IdetifyContNull(textBox37);
          bool chkTxt21 = IdetifyContNull(textBox36);
          bool chkTxt22 = IdetifyContNull(textBox35);
          bool chkTxt23 = IdetifyContNull(textBox34);
          bool chkTxt24 = IdetifyContNull(textBox33);
          bool chkTxt25 = IdetifyContNull(textBox48);
          bool chkTxt26 = IdetifyContNull(textBox47);
          bool chkTxt27 = IdetifyContNull(textBox46);
          bool chkTxt28 = IdetifyContNull(textBox44);
          bool chkTxt29 = IdetifyContNull(textBox42);
          bool chkTxt30 = IdetifyContNull(textBox50);
if ((chkTxt1) || (chkTxt2) || (chkTxt3) || (chkTxt4) || (chkTxt5) || (chkTxt6) || (chkTxt7) || (chkTxt8) || (chkTxt9) || (chkTxt10) || (chkTxt11) || (chkTxt12) || (chkTxt13) || (chkTxt14) || (chkTxt15) || (chkTxt16) || (chkTxt17) || (chkTxt18) || (chkTxt19) || (chkTxt20) || (chkTxt21) || (chkTxt22) || (chkTxt23) || (chkTxt24) || (chkTxt25) || (chkTxt26) || (chkTxt27) || (chkTxt28) || (chkTxt29) || (chkTxt30))
          {
              MessageBox.Show("Please give all values in the Input Box");
          }
          else
          {
              checkAnswer();
          }
     
      } 
private void checkAnswer()
{
    // Left To Right Q1
    string userValue1 = getTextValue(textBox1).ToUpper();
    string userValue2 = getTextValue(textBox2).ToUpper();
    string userValue3 = getTextValue(textBox3).ToUpper();
    string userValue4 = getTextValue(textBox4).ToUpper();
    string userValue5 = getTextValue(textBox5).ToUpper();
    string userValue6 = getTextValue(textBox6).ToUpper();
    string userValue7 = getTextValue(textBox7).ToUpper();
    string userValue8 = getTextValue(textBox8).ToUpper();
    // Left To Right Q2
    string userValue9 = getTextValue(textBox31).ToUpper();
    string userValue10 = getTextValue(textBox30).ToUpper();
    string userValue11 = getTextValue(textBox29).ToUpper();
    string userValue12 = getTextValue(textBox28).ToUpper();
    string userValue13 = getTextValue(textBox27).ToUpper();
    // Left To Right Q3
    string userValue14 = getTextValue(textBox38).ToUpper();
    string userValue15 = getTextValue(textBox37).ToUpper();
    string userValue16 = getTextValue(textBox36).ToUpper();
    string userValue17 = getTextValue(textBox35).ToUpper();
    string userValue18 = getTextValue(textBox34).ToUpper();
    string userValue19 = getTextValue(textBox33).ToUpper();
    // Left To Right Q4
    string userValue20 = getTextValue(textBox18).ToUpper();
    string userValue21 = getTextValue(textBox17).ToUpper();
    string userValue22 = getTextValue(textBox16).ToUpper();
    // Top To Bottom Q1
    string userValue23 = getTextValue(textBox1).ToUpper();
    string userValue24 = getTextValue(textBox24).ToUpper();
    // Top To Bottom Q2
    string userValue25 = getTextValue(textBox4).ToUpper();
    string userValue26 = getTextValue(textBox21).ToUpper();
    string userValue27 = getTextValue(textBox29).ToUpper();
    // Top To Bottom Q3
    string userValue28 = getTextValue(textBox5).ToUpper();
    string userValue29 = getTextValue(textBox20).ToUpper();
    string userValue30 = getTextValue(textBox28).ToUpper();
    string userValue31 = getTextValue(textBox12).ToUpper();
    string userValue32 = getTextValue(textBox36).ToUpper();
    string userValue33 = getTextValue(textBox44).ToUpper();
    // Top To Bottom Q4
    string userValue34 = getTextValue(textBox30).ToUpper();
    string userValue35 = getTextValue(textBox14).ToUpper();
    string userValue36 = getTextValue(textBox38).ToUpper();
    string userValue37 = getTextValue(textBox46).ToUpper();
    // Top To Bottom Q5
    string userValue38 = getTextValue(textBox34).ToUpper();
    string userValue39 = getTextValue(textBox10).ToUpper();
    string userValue40 = getTextValue(textBox42).ToUpper();
    string userValue41 = getTextValue(textBox50).ToUpper();
    /// Verify Answer
    string userAnswer1 = userValue1 + userValue2 + userValue3 + userValue4 + userValue5 + userValue6 + userValue7 + userValue8;
    string userAnswer2 = userValue9 + userValue10 + userValue11 + userValue12 + userValue13;
    string userAnswer3 = userValue14 + userValue15 + userValue16 + userValue17 + userValue18 + userValue19;
    string userAnswer4 = userValue20 + userValue21 + userValue22;
    string userAnswer5 = userValue23 + userValue24;
    string userAnswer6 = userValue25 + userValue26 + userValue27;
    string userAnswer7 = userValue28 + userValue29 + userValue30 + userValue31 + userValue32 + userValue33;
    string userAnswer8 = userValue34 + userValue35 + userValue36 + userValue37;
    string userAnswer9 = userValue38 + userValue39 + userValue40 + userValue41;
    /// Check Answer from Left TO Right
    bool chkAns1 = CheckAnswer.chkLeftToRightQ1(userAnswer1);
    bool chkAns4 = CheckAnswer.chkLeftToRightQ18(userAnswer2);
    bool chkAns3 = CheckAnswer.chkLeftToRightQ35(userAnswer3);
    bool chkAns2 = CheckAnswer.chkLeftToRightQ41(userAnswer4);
    /// Check Answer from Top To Bottom
    bool chkAns5 = CheckAnswer.chkTopToBottomQ1(userAnswer5);
    bool chkAns8 = CheckAnswer.chkTopToBottomQ4(userAnswer6);
    bool chkAns9 = CheckAnswer.chkTopToBottomQ5(userAnswer7);
    bool chkAns6 = CheckAnswer.chkTopToBottomQ19(userAnswer8);
    bool chkAns7 = CheckAnswer.chkTopToBottomQ39(userAnswer9);
   if ((chkAns1) && (chkAns2) && (chkAns3) && (chkAns4) && (chkAns5) && (chkAns6) && (chkAns7) && (chkAns8) && (chkAns9))
    {
        MessageBox.Show("Answers are  Right");
    }
    else
    {
        MessageBox.Show("Answers are Wrong");
    }
}
private static string str; private static string getTextValue(TextBox txt) { str = txt.Text; return str; }