In C# win forms- I would like to add controls to form from other class.
How can I do it?
I tried to pass the form as formal parameter to function in the other class, but how can I attach it to the form?
class Class1
{
System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
}
In addition, I have Form1.cs
I would like to add txt to Form1.
In addition, I would like to set the properties of txt from Class1, and it failed..
Thanks!
This should work:
class Class1
{
System.Windows.Forms.TextBox txt = new System.Windows.Forms.TextBox();
public void AddTextBoxToForm(Form form)
{
form.Controls.Add(txt);
txt.Text = "Hello World! I've been added to a form.";
}
}
You may also set properties like Location and Size of the TextBox. Note that it will be a bad idea to add the TextBox to different forms, though.
If you have any errors, your question should be more specific about what "failed" means.
In general, all controls of a Form should rather be members of that Form and not be defined in other classes.
Related
Can a WPF user control be written in F#?
Lets say I have a standard WPF/C# user control as:
public class DataGridAnnotationControl : UserControl
{
static DataGridAnnotationControl()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(DataGridAnnotationControl), new FrameworkPropertyMetadata(typeof(DataGridAnnotationControl)));
}
public DataGridAnnotationControl()
{
BorderBrush = Brushes.Black;
Background = Brushes.AliceBlue;
BorderThickness = new Thickness(20, 20, 20, 20);
}
public string LastName
{
get { return (string)GetValue(LastNameProperty); }
set { SetValue(LastNameProperty, value); }
}
public static readonly DependencyProperty LastNameProperty =
DependencyProperty.Register("LastName", typeof(string), typeof(DataGridAnnotationControl), new PropertyMetadata(string.Empty));
}
How is this coded in F#?
TIA
In general, creating the user control in F# (without a library) will typically be quite different than in C#.
The main issue is you can't use partial classes, so the designer will not function. Even if you ditch the designer, the typical workflow with XAML files does not work properly. To do this in "pure" F#, you typically need to write the UI in code vs doing it in XAML and allowing the generated InitializeComponent() method to wire things together.
However, one way to get there in a more "natural" method is to use FsXaml. It allows you to write user controls directly which become usable in a similar way to C# developed ones. This is done via a type provider and overriding the default information.
As we know that by default the winform PropertyGrid is able to edit properties of a predefined class. However, some times we might need to edit dynamic created objects. Refer to the code below:
ParamForm.Show(new { Firstname = "John", Lastname = "Herby" })
The ParamForm window contains 2 controls, a PropertyGrid and a Button. It is designed to be able to edit dynamic objects which contains string or boolean fields only.
public static dynamic Show(dynamic args)
{
var frm = new ParamForm(args);
frm.ShowDialog();
return frm.Result;
}
public ParamForm(dynamic args)
{
InitializeComponent();
propertyGrid.SelectedObject = ag;
}
The problem is that the Firstname & Lastname displayed in PropertyGrid control is grayed out and cannot be edited. So how to make the PropertyGrid able to edit dynamic created objects?
Anonymous types have read only property descriptors (used by the property grid) by design (see here for more on this: Non-read only alternative to anonymous types).
You can however use tricks such as the DynamicTypeDescriptorWrapper class demonstrated here: Fun with C# 4.0’s dynamic that implement the ICustomTypeDescriptor Interface
I would like to create a file dialog form that works like the one in MS Word. Specifically, you can select an existing file, create a new one, or delete a file, all within the file dialogue window. OpenFileDialog is a read-only deal. You can't create a new file with that control. SaveFileDialog can't be used to select an existing file. Is there a control that will do all of the above?
To create your custom dialog you should follow these steps:
Create a new form, call it MyFolderDialog
In the new form add the controls you need. For example, in order to let the user browse the file system, you can add a treeView and populate it using the classes System.IO.FileInfo and System.IO.FolderInfo.
Create a property in your form that reflects the user choice, something like public string SelectedFolder
Add a "Confirm" button and a "Cancel" button to your form
In the cancel button click event handler add the code this.DialogResult = DialogResult.Cancel;
In the confirm button click event handler add the code this.DialogResult = DialogResult.Ok;
Call your custom dialog like that
private string PickAFolder()
{
string selectedFolder = string.Empty;
using(var f = new MyFolderDialog())
{
if(f.ShowDialog() == DialogResult.Ok)
{
selectedFolder = f.SelectedFolder;
}
}
return selectedFolder;
}
Note that when the form is shown using ShowDialog method, the form won't dispose when it will be closed, so you can still read his properties after you confirmed. So it a good practice dispose it manually when you don't need it anymore. An elegant way to do it is to use the using keyword.
I hope this helps.
I am a beginner in WPF and trying to learn,So Sorry for this type of query.
I have added a property named GetName in WPF Form1 as below:
public string GetName
{
get { return this.uname.Text; }
set { this.uname.Text = value; }
}
Trying to retrieve the GetName property in WPF Form2 as below:
public Home()
{
InitializeComponent();
Form1 mn = new Form1 ();
MessageBox.Show(mn.GetName.ToString());
}
But I am getting prompt with blank. What I am doing wrong here. Please help.
You create a new Form:
Form1 mn = new Form1 ();
Then you request the Text value of what I assume is a TextBox control to be shown in a MessageBox:
MessageBox.Show(mn.GetName.ToString());
I would not expect GetNameto return a value, as when a TextBox control is created, unless specified, it will not have a value.
Between creating the Form and showing the MessageBox, you do not show your Form, for example:
mn.Show();
You need to show your Form first, input a value into your uname control and then request its value.
Calling ToString() on GetName is redundant, just use:
MessageBox.Show(mn.GetName);
I am trying to create an application bar in code for WinPhone7. The XAML that does it goes like this:
<PhoneApplicationPage.ApplicationBar>
<shellns:ApplicationBar Visible="True" IsMenuEnabled="True">
<shellns:ApplicationBar.Buttons>
<shellns:ApplicationBarIconButton IconUri="/images/appbar.feature.search.rest.png" />
</shellns:ApplicationBar.Buttons>
</shellns:ApplicationBar>
</PhoneApplicationPage.ApplicationBar>
So I thought I'd just rewrite it in C#:
var appbar = new ApplicationBar();
var buttons = new List<ApplicationBarIconButton>();
buttons.Add(new ApplicationBarIconButton(new Uri("image.png", UrlKind.Relative));
appbar.Buttons = buttons; //error CS0200: Property or indexer 'Microsoft.Phone.Shell.ApplicationBar.Buttons' cannot be assigned to -- it is read only
The only problem is that Buttons property does not have a set accessor and is defined like so:
public sealed class ApplicationBar {
//...Rest of the ApplicationBar class from metadata
public IList Buttons { get; }
}
How come this can be done in XAML and not C#? Is there a special way that the objects are constructed using this syntax?
More importantly, how can I recreate this in code?
appbar.Buttons.Add(new ApplicationBarIconButton(new Uri("image.png", UrlKind.Relative));
Add directly to the Buttons property.
It probably uses Buttons.Add instead of assigning to the Buttons property.
The ApplicationBar.Buttons member has an Add function (see this)
var appBarButton =
new ApplicationBarIconButton(new Uri("image.png", UrlKind.Relative)
appBar.Buttons.Add(appBarButton);