Click here to Skip to main content
15,886,788 members
Home / Discussions / C#
   

C#

 
GeneralRe: To filter datagridview using like IN and NOT IN Pin
polachan23-Feb-12 0:13
polachan23-Feb-12 0:13 
AnswerRe: To filter datagridview using like IN and NOT IN Pin
polachan23-Feb-12 0:23
polachan23-Feb-12 0:23 
AnswerRe: To filter datagridview using like IN and NOT IN Pin
AntDC23-Feb-12 5:37
AntDC23-Feb-12 5:37 
QuestionOptions to check logical equivalence of Boolean expressions Pin
raj_2822-Feb-12 20:16
raj_2822-Feb-12 20:16 
AnswerRe: Options to check logical equivalence of Boolean expressions Pin
Abhinav S22-Feb-12 20:54
Abhinav S22-Feb-12 20:54 
QuestionHow can I to manipulate dynamically created controls? Pin
NWNewelll22-Feb-12 10:42
NWNewelll22-Feb-12 10:42 
AnswerRe: How can I to manipulate dynamically created controls? Pin
PIEBALDconsult22-Feb-12 11:04
mvePIEBALDconsult22-Feb-12 11:04 
AnswerRe: How can I to manipulate dynamically created controls? Pin
BobJanova22-Feb-12 22:57
BobJanova22-Feb-12 22:57 
How I'd do this is:


  • Maintain a list of data objects, containing the data you want:
    class Person : BaseNotifier {
     private string name;
     public string Name { get { return name; } set { name = value; Notify('Name'); } }
     private string state;
     public string State { get { return state; } set { state = value; Notify('State'); } }
    }

    (BaseNotifier is a simple implementation of INotifyPropertyChanged, see end of post), and in your main class:

    List<Person> people = new List<Person>();

  • Create a custom control that can be bound to a person:
    class PersonPanel : Panel, INotifyPropertyChanged {
     TextBox nameTextBox;
     ComboBox stateCombo;
     // for the sake of example, probably would be held elsewhere or loaded from database
     public static readonly string[] States = { "NSW", "SA", "WA", "NT", "ACT", "TAS" };
    
     public PersonPanel(){
      Width = 200;
      Height = 30;
      Controls.Add(nameTextBox = new TextBox());
      nameTextBox.Left = nameTextBox.Top = 5;
      // ... etc
    
      nameTextBox.TextChanged += (s, e) => Notify("Name");
      stateCombo.SelectedIndexChanged += (s, e) => Notify("State");
     }
     
     public string Name {
      get { return nameTextBox.Text; }
      set { nameTextBox.Text = value; }
     }
      
     public string State {
      get { return (string)stateCombo.SelectedItem; }
      set { stateCombo.SelectedItem = value; }
     }
    
     public event PropertyChangedEventHandler PropertyChanged;
    
     private void Notify(string property){
      PropertyChangedEventHandler h = PropertyChanged;
      if(null != h) h(this, new PropertyChangedEventArgs(property));
     }
    }

    The notification is so two way data binding can work.

  • When you add a new person, create and bind one of these to a new Person object
    void AddNewPerson(){
     Person person = new Person();
     if(people.Count > 0) person.State = people[people.Count - 1].State;
     people.Add(person);
    
     PersonPanel panel = new PersonPanel();
     panel.DataBindings.Add("Name", person, "Name");
     panel.DataBindings.Add("State", person, "State");
     Panel.Controls.Add(panel);
    }



This gives you a nice separation of the data part and the UI, allowing you to change view driver or easily store the data in a database or file system.


BaseNotifier is a simple implementation of INotifyPropertyChanged to avoid typing the same thing several times:

public abstract class BaseNotifier : INotifyPropertyChanged {
 public event PropertyChangedEventHandler PropertyChanged;

 protected void Notify(string property){
  PropertyChangedEventHandler h = PropertyChanged;
  if(null != h) h(this, new PropertyChangedEventArgs(property));
 }
}

AnswerRe: How can I to manipulate dynamically created controls? Pin
GParkings22-Feb-12 23:00
GParkings22-Feb-12 23:00 
AnswerRe: How can I to manipulate dynamically created controls? Pin
BillWoodruff24-Feb-12 15:00
professionalBillWoodruff24-Feb-12 15:00 
QuestionHow can we access to an object of a form from another form? Pin
Fred 3422-Feb-12 9:59
Fred 3422-Feb-12 9:59 
AnswerRe: How can we access to an object of a form from another form? Pin
PIEBALDconsult22-Feb-12 10:09
mvePIEBALDconsult22-Feb-12 10:09 
GeneralRe: How can we access to an object of a form from another form? Pin
Fred 3422-Feb-12 11:53
Fred 3422-Feb-12 11:53 
AnswerRe: How can we access to an object of a form from another form? Pin
Wes Aday22-Feb-12 10:32
professionalWes Aday22-Feb-12 10:32 
GeneralRe: How can we access to an object of a form from another form? Pin
Fred 3422-Feb-12 11:51
Fred 3422-Feb-12 11:51 
AnswerRe: How can we access to an object of a form from another form? Pin
Luc Pattyn22-Feb-12 12:12
sitebuilderLuc Pattyn22-Feb-12 12:12 
AnswerRe: How can we access to an object of a form from another form? Pin
hellono122-Feb-12 20:50
hellono122-Feb-12 20:50 
AnswerRe: How can we access to an object of a form from another form? Pin
Quintain26-Feb-12 15:03
Quintain26-Feb-12 15:03 
QuestionRegarding AttributeUsageAttribute.AllowMultiple Pin
PIEBALDconsult22-Feb-12 8:11
mvePIEBALDconsult22-Feb-12 8:11 
QuestionHow to check that dsn which is created used for Sql Server or Access using C# Pin
Awadhendra123422-Feb-12 2:16
Awadhendra123422-Feb-12 2:16 
AnswerRe: How to check that dsn which is created used for Sql Server or Access using C# Pin
PIEBALDconsult22-Feb-12 2:56
mvePIEBALDconsult22-Feb-12 2:56 
GeneralRe: How to check that dsn which is created used for Sql Server or Access using C# Pin
Awadhendra123422-Feb-12 3:24
Awadhendra123422-Feb-12 3:24 
GeneralRe: How to check that dsn which is created used for Sql Server or Access using C# Pin
PIEBALDconsult22-Feb-12 4:00
mvePIEBALDconsult22-Feb-12 4:00 
QuestionDetect Marked Checkbox using OCR Pin
Zeyad Jalil22-Feb-12 2:12
professionalZeyad Jalil22-Feb-12 2:12 
AnswerRe: Detect Marked Checkbox using OCR PinPopular
enhzflep22-Feb-12 4:44
enhzflep22-Feb-12 4:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.