Share via

How to Call Class.

MiPakTeh 1,476 Reputation points
2021-06-05T10:27:15.617+00:00

Hi All ,

somebody show me how to call Class with click button.

Thank.102644-asking-1.txt

Developer technologies | C#
Developer technologies | C#

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.

0 comments No comments

Answer accepted by question author

Karen Payne MVP 35,606 Reputation points Volunteer Moderator
2021-06-05T14:21:33.29+00:00

Hello,

To work with a list of Student in a ListBox I've wrote just enough to show adding new students and selecting the current Student in the ListBox is any. The BindingList and implementation of INotifyPropertyChanged assist with displaying data and notify of changes.

public class Student : INotifyPropertyChanged
{
    private string _name;

    public string Name
    {
        get => _name;
        set
        {
            _name = value;
            OnPropertyChanged();
        }
    }
    public Student Highest(Student o1, Student o2) => 
        string.Compare(
            o1.ToString(), 
            o2.ToString(), StringComparison.Ordinal) > 0 ? o1 : o2;

    public override string ToString() => Name;
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

}

Form code

public partial class Form1 : Form
{
    private readonly BindingList<Student> _bindingList = new BindingList<Student>();
    public Form1()
    {
        InitializeComponent();

        listBox1.DataSource = _bindingList;
    }

    private void AddButton_Click(object sender, EventArgs e)
    {
        if (!string.IsNullOrWhiteSpace(NameTextBox.Text))
        {
            _bindingList.Add(new Student() { Name = NameTextBox.Text });
        }
    }

    private void CurrentButton_Click(object sender, EventArgs e)
    {
        if (listBox1.SelectedIndex <= -1) return;

        var student = _bindingList[listBox1.SelectedIndex];
        MessageBox.Show(student.Name);
    }
}

EDIT

Here is how to change the current Student done in a mock up

private void EditButton_Click(object sender, EventArgs e)
{
    if (listBox1.SelectedIndex <= -1) return;
    _bindingList[listBox1.SelectedIndex].Name = "Karen";
    listBox1.Invalidate();
}

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Duane Arnold 3,216 Reputation points
    2021-06-05T13:26:28.377+00:00

    it's more like how do you do method invocation on a class that has been instanced into an object or invoking a static method in the class. A class is a type.

    https://learn.microsoft.com/en-us/dotnet/csharp/methods

    You should understand the basic principles of OO.

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/

    OO is OO it's the same principles in Java or .NET C#

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.