Sorry. I think I've found it.
I thought the suggestion reference to "RootGrid" was to a VS Named Space, like System.Windows.RootGrid I'm relatively sure they intended me to refer to a Grid in my XAML.
Realizing this probability was a "DUH" moment. So, just replacing RootGrid with my XAML name "GridBoard" will probably solve the issue. I haven't got the close of the Control to work yet, but he naming issue is at least being found.
To answer the question. Help, is a User control file in ScqWander Program. localUCHelp was the name used to create an instance on the Page.
Quick overview: How do I refer to RootGrid?
I get RootGrid "Does not exist in current context"
I am using VS 2019 and have a C# project, using XAML. I wrote a UserControl and am trying code a Button to Close the control (when clicked work from within the control). I got a suggestion which included the sample below. Google has turned up nothing.
The code is within the namespace
public sealed partial class MainPage : Page
void Page_Loaded(object sender, RoutedEventArgs e)
{
ScqWander.Help localUCHelp = new ScqWander.Help();
localUCHelp.HelpUserControlCloseEvent += new EventHandler(BtnPXClose);
RootGrid.Chilren.Add(localUCHelp);
}
I was simply thinking that "RootGrid" reference to which I could refer (as in Using System.Whateverxxxx) when I should have been referring to the Grid I defined in my XAML. I just did not recognize what was being said in the example.
Related
i'm relatively new to WPF and i was tasked to convert an old winforms project into a wpf project.
I am using the function App_Startup as the start for the App and i already created a mainwindow for my GUI. Normally you would just do this in the startup:
void App_Startup(object sender, StartupEventArgs e)
{
MainWindow main = new MainWindow();
main.Show();
}
but this does not work for me because it shows a completely new mainwindow and ignores the one i already made. I understand the issue is that App_Startup happens before the mainwindow and its components even get initialized but there has to be a way i can jump into the mainwindow let it initialize itself and its components and make that the window i start in App_Startup, but I just don't know how to do that.
I'm sorry if this is a really easy to solve problem, but i just don't know how to solve it.
Also i'm sorry if the English sucks it is not my first language.
Thank you very much for your answers and help in advance.
Greetings from Germany
Edit:
I found my mistake! I accidentally set the whole content to one string and it looked like it created a new Window but it was the same window with overwritten content. 🤦♂️🤦♂️🤦♂️🤦♂️🤦♂️🤦♂️🤦♂️🤦♂️🤦♂️🤦♂️
Thank you all for trying to help me, but in the end the problem was very different from what I had written in this question.
First, open your App.xaml, then change StartupUri to what you need.
for changing the startup form in WPF projects, open the App.XML file and change the StartupUri String with your new Window.
That should be the trick.
Greetings from Austria ✌
Another beginners question here, coming from Delphi you always have access to another forms controls but in my early days with C# / Visual Studio I am faced with a problem which is proving more difficult than it should be.
I have been getting started by writing a simple notepad style application, I have my main form and a secondary form used to select a line number.
From my main form, I call the goto line number form like so:
private void mnuGoTo_Click(object sender, EventArgs e)
{
Form gotoForm = new GoToForm();
var dialogResult = gotoForm.ShowDialog();
if (dialogResult == DialogResult.OK)
{
// get the text from gotoForm.editLineNumber.Text
MessageBox.Show(gotoForm.editLineNumber.Text); // doesn't work
}
}
As you can see from the commented code I have a TextBox control called editLineNumber which is on my other form (GoToForm).
My problem (and likely a beginner question) is why does editLineNumber not show in the intellisense menu when I type gotoForm.?
How do I access the editLineNumber control from the form GoToForm?
The error message for the // doesn't work commented line is:
Error CS1061 'Form' does not contain a definition for 'editLineNumber'
and no extension method 'editLineNumber' accepting a first argument of
type 'Form' could be found (are you missing a using directive or an
assembly reference?)
Unless I am missing something obvious, why are controls that exist on another form not publically available to all forms? I understand that C# / Visual Studio is different to Delphi but the way Delphi lets you access and see all controls on all forms without any extra works seems more logical to me. Why does C# / Visual Studio hide controls on secondary forms, for what purpose can this be beneficial?
The editLineNumber control is private. You can change it to be public, but that's discouraged.
Instead, create a property in GoToForm that returns the value you want.
public string LineNumber
{
get { return this.editLineNumber.Text; }
}
Now you can just reference your new property:
if (dialogResult == DialogResult.OK)
{
MessageBox.Show(gotoForm.LineNumber);
}
Especially if you're new to C# and WinForms, don't touch designer code with a 10 foot pole. As Grant Winney said, use a property:
public string GetLineNumberText
{
get { return this.editLineNumber.Text; }
}
It should be mentioned that it's important to be aware of the directional nature of forms. That is to say, if I make Form1 and then define Form2 inside of it, you'll want to be careful how you communicate between the two forms. Properties are nearly always a better alternative than accessing form elements directly - it makes the code very difficult to change otherwise. If you, for example, removed editLineNumber from the other form or renamed it, every instance in the parent form would have to be edited. If you use a property, then you only have to change it in one place.
I plan to add functionalities to TextBox with the following:
public class TextBoxExt : TextBox
{
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
}
}
The question is how can we use this TextBoxExt? Is there anyway to get this class onto the ToolBox so that we can just drag and drop it onto the form? If not, what is the best way to use the TextBoxExt?
Build you project with TextBoxExt, make sure it compiles ok.
With the form that you want TextBoxExt on, open the toolbox, right click and select "choose items"
Browse to you .exe or dll that you compiled in 1)
make sure that TextBoxExt has a tick next to it, press ok
TextBoxExt should appear in the toolbox, drag it onto your form
(There is another way of doing this, opening the designer file and renaming the instances of TextBox to TextBoxExt but manual editing of designer files can be considered hazardous by some)
I know this is super old question, but maybe still useful for someone else that has same problem like me - as it's still on the top Google :)
You might interest to use ToolboxItemAttribute (http://msdn.microsoft.com/en-us/library/system.componentmodel.toolboxitemattribute(v=vs.110).aspx).
I did this at my end to resolve the problem.
[ToolboxItem(true)]
public class PanelTitle : LabelControl {
// Whatever code to override LabelControl here...
}
Rebuild the solution and the extended control should be shown in the toolbox.
Any custom control in your project should show up in the Toolbox automatically. I have found that sometimes the controls won't show until you close a re-open Visual Studio. I assume the issue has something to do with caching of the contents of the Toolbox.
You need to add a constructor to your derived class.
public class TextBoxExt : TextBox
{
public TextBoxExt()
{
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
}
}
Your control should appear in the toolbox for your solution automatically. To have it appear for other projects, you have to do Choose Toolbox items, as others have said.
If you want to provide special design-time functionality, then you will also need to provide some additional designer related attributes and probably your own class derived from ControlDesigner.
I fell into this trap just a couple of hours ago.
I've got a .NET 2.0 Windows Application project with some custom UserControls; it worked fine.
So I decided to order my files in subfolders, to make my project a little bit cleaner.
After that, Visual Studio 2010 designer stopped loading my forms, and ToolBox won't show my controls anymore.
I freaked out, moving back source files in project root, resetting ToolBox, but nothing seemed to work.
After that, I remembered I used ReSharper "Remove Unused References", so I tried to put back unused reference, in particular System.Data: problem solved! :O
I can't say you why, but this worked for me.
Hope my experience can help someone else. :)
Bye,
Nando
I created an empty constructor for my custom implementation of UltraGridBagLayoutPanel. Although david.healed is right it isn't necessary, it is quite useful to put a breakpoint in to check that when the form initialises it is using your class to implement your custom control.
It would have been a lot easier to edit the designer file, but I tried it and changed both the field type for the control and also changed the assignment of the field to a new instance of my custom control.
private Infragistics.Win.Misc.UltraGridBagLayoutPanel ultraGridBagLayoutPanel1;
this.ultraGridBagLayoutPanel1 = new Infragistics.Win.Misc.UltraGridBagLayoutPanel();
to
private Athia.Reports.ultraGridBagLayoutPanel1 ultraGridBagLayoutPanel1;
this.ultraGridBagLayoutPanel1 = new Athia.Reports.ultraGridBagLayoutPanel1();
Doing this destroys Visual Studio every time, and to fix it requires using a text editor to put it back again. Therefore unless anyone can describe what is wrong with my implementation of this approach, perhaps calling the class the same as the control name isn't a great idea, I think the only safe and reliable way to achieve this is as Calanus describes in steps 1 to 5 or as an small deviation from that as Rob Windsor rightly points out restarting VS will bring the control into the Toolbox automatically. Unfortunately for me I then have to change all of the child controls over from the original class to my customised class :-(.
Within the same Solution this should work automatically. However, I have found that if the Target Framework aren't matching the Toolbox does not populate. ( I'm assuming really Reference needs to be of version same or lower than target of Reference. ) ( I did get a warning about non-matching Frameworks )
By making these the same Target Framework, Recompile, Restart VS. the control populated correctly. ( I also added the ToolboxItem(true) Attribute)
I have writen a small applet in Silverlight and, while it works fine on Windows, it seems that on OSX the data binding part of the application (all those NotifyPropertyChanged calls) do not work. Does anyone know why this is? I've tried under both Firefox and Safari with the latest 2.0 download installed.
Your usage of the model object instance in Page seemed odd to me right away. It is not downright incorrect but unusual to me. Some experimentation led me to a working solution, albeit without knowing the cause of the error that happened in the first place. Not many people instantiate objects directly in the DataContext assignment, which is probably why this is not a well-known (and fixed!) defect.
Remove the DependencyObject base class from MyModel.
Make the MyModel instance be a resource of Page, instead of instantiating it directly into the DataContext.
Modify the Button_Click event handler to load the resource, instead of the named Page child object.
All done!
Code snippets for the working solution follow.
Page.xaml
<UserControl.Resources>
<my:MyModel x:Key="TheModel"/>
</UserControl.Resources>
<Grid x:Name="LayoutRoot" Background="White" DataContext="{StaticResource TheModel}">
Page.xaml.cs
private void Button_Click(object sender, RoutedEventArgs e)
{
((MyModel)Resources["TheModel"]).BeginUpdateBitmap();
}
MyModel.cs
public sealed class MyModel : INotifyPropertyChanged
{
Please also include the source code with your question in the future. It would have made this quite a bit simpler.
Did you try using remote sliverlight debugging to the mac? I'd expect getting the debugger setup and turning on 1st chance exceptions has a good shot at showing you the problem.
Outline
OK, I have Google'd this and already expecting a big fat NO!! But I thought I should ask since I know sometimes there can be the odd little gem of knowledge lurking around in peoples heads ^_^
I am working my way through some excercises in a book for study, and this particular exercise is User Controls. I have cobbled together a control and would like to set the DefaultEvent for it (having done this for previous controls) so when I double-click it, the default event created is whatever I specify it to be.
NOTE: This is a standard User Control (.ascx), NOT a custom rendered control.
Current Code
Here is the class & event definition:
[System.ComponentModel.DefaultEvent("OKClicked")]
public partial class AddressBox : System.Web.UI.UserControl
{
public event EventHandler OKClicked;
Current Result
Now, when I double click the the control when it is on a ASPX page, the following is created:
protected void AddressBox1_Load(object sender, EventArgs e)
{
}
Not quite what I was expecting! So, my question:
Is it possible to define a DefaultEvent for a UserControl? Is it a hack? If it's [not] supported, is there a reason?
Side Note: How do we put underscores in code? I cant seem to put and escape char in?
Here is a possible answer, without testing (like martin did).
In reflector, you will see that the DefaultEventAttribute allows itself to be inherited.
In reflector, you see that the UserControl class has it's default event set to the Load event.
So the possible reason is that even though you are decorating your user control with the default event of OKClick, VS might still be thinking that the default event is load, as it's being inherited from UserControl whose default event is Load.
Just a high level guess at what might be happening.
OK, I checked this out, Inheriting from WebControl rather than UserControl.. All worked fine.
Looks like Darren Kopp takes the crown for this one! Thanks for the input!