Forum Xamarin.Mac

NSTextView as a group

codergcoderg Member ✭✭

I want to group NSTextview and access/change their values based on "Tag"? Is this possible?

Eg: I need something like radio button. Where there are 5 textfields, all five will have same name but different Tag value.

Answers

  • ChrisHamonsChrisHamons USForum Administrator, Xamarin Team Xamurai

    Often, but not always you really want a NSTextField, which is a simpler version of NSTextView.

    You can likely assign a common delegate instance for each one and use a tag you set to distinguish (and do whatever custom logic you want).

  • codergcoderg Member ✭✭
    edited March 31

    Hi Chris,

    How can I create an array of NSTextfield and add all the textfields to an NSBox programmatically?

  • codergcoderg Member ✭✭
    edited March 31

    I was able to add NSTextFIeld programmatically. But why am I getting "Object reference not set to an instance of an object" error while trying to print the "StringValue" value of my Texfield created programmatically?

    NSTextField[] t = new NSTextField[1];
    public override void ViewDidLoad()
    {
    base.ViewDidLoad();

            t[0] = new NSTextField();
            t[0].Frame = new CoreGraphics.CGRect(20, 1, 200, 20);
            t[0].Tag = 0;
            t[0].Identifier = "0";
            t[0].StringValue = "yahoo";
            t[0].Bordered = true;
            t[0].Changed += new EventHandler(testt);
            this.myNSBox.AddSubview(t[0]);
    

    }

    public void testt(Object sender, EventArgs e) {
    NSTextField check = sender as NSTextField;
    //var check = sender as NSTextField; this is also not working
    Console.WriteLine(check.StringValue);
    }

    System.NullReferenceException: Object reference not set to an instance of an object
    at myProject.ViewController.testt (System.Object sender, System.EventArgs e) [0x00008] in
    /path to my folder /ViewController.cs:125
    at AppKit.NSTextField+_NSTextFieldDelegate.Changed (Foundation.NSNotification notification) [0x00011] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.10.0.17/src/Xamarin.Mac/NSTextField.g.cs:1093
    at at (wrapper managed-to-native) AppKit.NSApplication.NSApplicationMain(int,string[])
    at AppKit.NSApplication.Main (System.String[] args) [0x00040] in /Library/Frameworks/Xamarin.Mac.framework/Versions/6.10.0.17/src/Xamarin.Mac/AppKit/NSApplication.cs:100
    at myProject.MainClass.Main (System.String[] args) [0x00007] in
    /path to my folder /Main.cs:57

Sign In or Register to comment.