Showing posts with label C-Sharp. Show all posts
Showing posts with label C-Sharp. Show all posts

Wednesday, March 11, 2015

UserControl What a Wonderful Future in C# C-Sharp You should be using

Working on a big project that contains a lot of functionality, modules and futures really gets stressing and gets out of control.  So the best approach is to divide code into modules and structure them to be unique and reusable.



The following is a screen shot or the main application I put together for the example purpose.  It contain a text  box that is displays all output logs.
You can send text logs from "Add Text Test" command button,
you can also, type specific text in the two text boxes and send them to the Output Log Text box.




 I created this user control, a text box and a command button that can be used to type in your text log and press the button to update/send to the main Output Log text box.



Overall Concept

The idea, is when creating main application GUI, you create new user control that can be used later and almost standalone. So it is self contained and can be reused.
The following diagram shows an application that has a text control and three other control groups that consists of a text entry and a command button.
these command buttons can be used multiple times in the same application with different needs and functionality.



The first goal is to create the control group and define events and properties you need to use for this user control. In the user control we using there are:
  • Event that occurs when user clicks on the Command button
  • Properties related to the Text Entry
    • Set: modify the contents of the Text Entry
    • Get: read the contents of the Text Entry
Command Button Events are handled as follow:
  1. User Control Class:
    1.  Add new public delegate 
      • public delegate void Update_Output_Log_Event(string message, Boolean next_line);
    2.  Create new event of same delegate type 
      • public event Update_Output_Log_Event Update_Output_Log;
    3. In the button click add a call to that event
      • Update_Output_Log(Message_Log_TextBox.Text, true);
  2. Main Application Class
    1. Add the User Control to the GUI of the main form
      • I will rename it my_User_Control_Log1
    2. Create function that has the same parameters as the delegate in User Control
      • public void Update_Output_Log(string message, Boolean add_new_Line)
    3. Register the event to the new function
      • my_User_Control_Log1.Update_Output_Log += new my_User_Control_Log.Update_Output_Log_Event(Update_Output_Log);


Properties are handled as follow:


  1. User Control Class:
    1. Add description and category to make it look nicer!
      • [Description("Retrieves and sets the Message Log Text"), Category("Data")]
    2. Create new variable that represents the data type we need to access
      • public string update_Messsage_Text
    3. Add get and set properties for that variable
      • get { return Message_Log_TextBox.Text; }
      • set { Message_Log_TextBox.Text = value; }
      • These two properties will modify/read from the Text Box Message_Log_TextBox.Text
  2. Main Application Class:
    1. when needed you can set or get text values using the following
    2. Output_Log_TextBox.AppendText(my_User_Control_Log1.Update_Message_Text);
    3. my_User_Control_Log1.Update_Message_Text = "Test New Text...";




Here are all the details on how to do that step by step...












Thursday, May 2, 2013

How to Auto scroll TextBox, ListBox, ListView C# C-Sharp


Programming In C# with Visual Studio


Sometimes you need to add this little trick in your code to ease using it. this trick is to force the TextBox, ListBox, ListView, or DataGridView to scroll down to the last item was entered.


Here are some info on how to do an Auto-scroll for your program controls

TextBox auto-scroll

textBox1.SelectionStart = textBox1.Text.Length;
textBox1.ScrollToCaret();

ListBox auto-scroll

listBox1.SelectedIndex = listBox1.Items.Count - 1;
listBox1.SelectedIndex = -1;

ListView auto-scroll

listView1.EnsureVisible(listView1.Items.Count - 1);TreeView autoscroll
treeView1.Nodes[treeView1.Nodes.Count - 1].EnsureVisible();

DataGridView auto-scroll

dataGridView1.FirstDisplayedCell =
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0];