[Mono-osx] Dynamically add User Control (SubView) defined in the Nib

Tomas Trescak tomi.trescak at gmail.com
Wed Dec 14 05:09:42 EST 2011


Hi

I am trying following code:

SubView sb = new SubView (box.Handle); // subview is the monomac code 
generated to my nib
sb.SetFrameOrigin (new PointF (0, 100));
sb.SetFrameSize (new SizeF(200, 200));
box.AddSubview(sb); // box the the NSView contorl type box

Doing this the whole thing crashes ;(

Tomas

PS: Here is the remaining code

public partial class SubView : MonoMac.AppKit.NSView
{
#region Constructors

// Called when created from unmanaged code
public SubView (IntPtr handle) : base (handle)
{
Initialize ();
}

// Called when created directly from a XIB file
[Export ("initWithCoder:")]
public SubView (NSCoder coder) : base (coder)
{
Initialize ();
}

// Shared initialization code
void Initialize ()
{
}

#endregion
}


public partial class MainWindowController : 
MonoMac.AppKit.NSWindowController
{
#region Constructors

// Called when created from unmanaged code
IntPtr hndl;

public MainWindowController (IntPtr handle) : base (handle)
{
Initialize ();
hndl = handle;
}

// Called when created directly from a XIB file
[Export ("initWithCoder:")]
public MainWindowController (NSCoder coder) : base (coder)
{
Initialize ();
}

// Call to load from the XIB/NIB file
public MainWindowController () : base ("MainWindow")
{
Initialize ();
}

// Shared initialization code
void Initialize ()
{
}

#endregion

//strongly typed window accessor
public new MainWindow Window {
get {
return (MainWindow)base.Window;
}
}

int count = 0;

partial void ClickButton (NSObject sender)
{
label.StringValue = string.Format ("Button clicked {0} times.", ++count);

SubView sb = new SubView (box.Handle);
sb.SetFrameOrigin (new PointF (0, 100));
sb.SetFrameSize (new SizeF(200, 200));
box.AddSubview(sb);
}
}
> ³õÒô¥»¥ó¥±¥ó <mailto:senken at bangumi.tv>
> 14 de diciembre de 2011 10:49
>
> I think "AddSubview" is the right approach. If it doesn't work, try 
> "SetFrameOrigin" to set the subview to a right position. Be noticed 
> that the Mac's screen coordinate is different from Windows or iOS, 
> which starts from the left-bottom corner of the screen (WIN or iOS 
> start from left-top corner).
>
> I'm new to MonoMac too, hope it helps.
> ------------------ Original ------------------
> *From: * "Tomas Trescak"<tomi.trescak at gmail.com>;
> *Date: * Wed, Dec 14, 2011 05:32 PM
> *To: * "mono-osx"<mono-osx at lists.ximian.com>;
> *Subject: * [Mono-osx] Dynamically add User Control (SubView) defined 
> in the Nib
> Dears
>
> I'm very new to the Cocoa - Objective C programming, but I have more
> than 8 years of c# programming.
> My problem is following:
>
> How do I dynamically add a new user control (Cocoa control) to the
> interface.
> The control is defined in the NIB (.nix) file.
> I could not do this nor in the Interface Designer, nor in the program 
> code.
> For the "Groupbox" control I have found a method "AddSubview" which does
> nothing, but I'm just probably not using it right.
>
> Any ideas are greatly appreciated
>
> Have a wonderful day
>
> Tomas
> _______________________________________________
> Mono-osx mailing list
> Mono-osx at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-osx
>
> _______________________________________________
> Mono-osx mailing list
> Mono-osx at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-osx
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-osx/attachments/20111214/d5c7a316/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: compose-unknown-contact.jpg
Type: image/jpeg
Size: 770 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-osx/attachments/20111214/d5c7a316/attachment.jpg 


More information about the Mono-osx mailing list