tags:

views:

47

answers:

1

Hi, Im wpf application,i have preset.xaml.cs,in that one function is there like adding text to listbox. But i want to do that function in toolbar.xaml.cs. How can i refer controls in preset.xaml in toolbar.xaml.cs.

presetbox is listbox used in preset.xaml I want to add text for that box from toolbar.xaml.cs.

Pls help me.Im new to C#.

+2  A: 

Somewhere else in your project, create a utility class, for example:

public static class MyUtils {
    /* your method here */
}

Now you should be able to use it from both places as MyUtils.SomeMethod; note that it should be a static method, so you'll need to pass the textbox in as a parameter.

Marc Gravell
public static class MyUtils{ public static void AddPresetmenu(ListBox menubox,string pMenu) { menubox.Items.Add(pMenu); } } In toolbar.xaml.csi coded likepublic void Add(string menu) { MyUtils.AddPresetmenu(menubox, menu); }But it shows error The name 'menubox' does not exist in the current context....menubox is the control used in trest.xaml.How can i refer this?
Anu
If menubox is definited in xaml, it will need an x:Name attribute in order to be referenced in code.
TabbyCool
Yes in presettabxaml the listbox is as <ListBox x:Name="menubox" Margin="0,5,0,0" >But still i cannot refer in toolbar.xaml.cs.The same error occurs.
Anu
@Anu - my WPF is limited, but with a name that *should* work...
Marc Gravell
Are you trying to directly access menubox from toolbar.xaml.cs when it is a property of presettab.xaml? That won't work - you'll need to access presettab.xaml.cs from within toolbar.xaml.cs which you don't seem to be doing currently. Does the toolbar.xaml.cs class have an instance of presettab.xaml.cs as a property from which to access menubox?
TabbyCool
No,i dont have any instance of presettab.xaml.cs in toolbar.xaml.csI dont know how to do.Can u send any sample?pls
Anu