views:

206

answers:

3

I'm sure this is something very simple but I can't figure it out. I've searched here and on msdn and have been unable to find the answer. I need to be able to set the richtextboxes selection via richtextbox.Selection.Select(TextPointer1, Textpointer2). Thanks for your help!

+1  A: 

You cant access the texbox from another window as it is private to that window you can however work around this by exposing the RichTextBox as a public property on your window (hack)

public RichTextBox RichTextBox {
  get{
    //the RichTextBox would have a property x:Name="richTextbox" in the xaml
    return richTextBox;
  }
}
Aran Mulholland
Actually, this hack would only be needed if windows are in different assemblies, since the field access is internal by default, and even that can be changed with x:FieldModifier attribute.MSDN:Under the standard build configuration for a WPF application project that uses XAML, partial classes, and code-behind, the specified x:Name becomes the name of a field that is created in the underlying code when XAML is processed, and that field holds a reference to the object. By default, the created field is internal. You can change field access by specifying the x:FieldModifier attribute.
Miroslav Popovic
I have tried changing the richtextbox x:FieldModifier attribute to public but it still is not visible from the second window. What could I be doing wrong? I am using the visual studio 2010 beta 2 would this make any difference?
Justin
According to MSDN info for x:Name, it should work for .NET 4.0 also. Are you sure you're using instance of window1 and not the class name to access the control? To get the instance of window, look at Ian's answer. Can you post the code you have for accessing the richtextbox?
Miroslav Popovic
Thankyou! I was using the class name and not the instance.
Justin
A: 

You should be able to access controls on Window1 from Window2 code behind, if that's what you want. Generated fields are internal by default.

All you need is to name the control on Window1, like this:

<RichTextBox x:Name="richtextbox" ... />

In Window2 code behind:

var window = new Window1(); // or use the existing instance of Window1
window.richtextbox.Selection.Select(TextPointer1, Textpointer2);

A better option would be to encapsulate select operation in a method in code behind of Window1, to avoid giving away internal. Then you would have:

// Window1.cs
public void Select(int param1, int param2)
{
    richtextbox.Selection.Select(param1, param2);
}

// Window2.cs
var window = new Window1(); // or use the existing instance of Window1
window.Select(TextPointer1, Textpointer2);
Miroslav Popovic
A: 

Application.Current contains a collection of all windows in you application, you can get the other window with a query such as

var window2 = Application.Current.Windows
    .Cast<Window>()
    .FirstOrDefault(window => window is Window2) as Window2;

and then you can reference the control from your code, as in

var richText = window2.MyRichTextBox
Ian Oakes