I'm new to c#, and I do know how to dynamically add controls, but I don't know how to set that control as this.[control_name]. Note that here, this is the Form.
This can be done statically with private System.Windows.Forms.[Control_type] [control-name];, but how would i do this from a method so that I can later declare this.[control-name] = [variable]?
Note here, variable would be something like new TextBox();
var txt = new TextBox(); //txt is the variable you are looking for
Form1.Controls.Add(txt); //added it to the form
Now you can access it by txt:
txt.Location = new Point(0,0);
txt.Visible = true;
If you create the control inside a method (as you mentioned in the comments), you can return and use it like below:
public TextBox AddTextBox()
{
var txt = new TextBox();
Form1.Controls.Add(txt);
return txt;
}
var newTxt = AddTextBox();
newTxt.Location = new Point(0,0);
newTxt.Visible = true;
Related
Form2:
private ToolStripMenuItem mHelp;
private ToolStripMenuItem apProposToolStripMenuItem;
public void intializecomponent()
{
this.mHelp = new ToolStripMenuItem();
this.contentsToolStripMenuItem = new ToolStripMenuItem();
this.apProposToolStripMenuItem = new ToolStripMenuItem();
this.mHelp.DropDownItems.AddRange(new ToolStripItem[2]
{
(ToolStripItem) this.contentsToolStripMenuItem,
(ToolStripItem) this.apProposToolStripMenuItem
});
this.mHelp.Name = "mHelp";
this.mHelp.Size = new Size(44, 20);
this.mHelp.Text = "Help";
this.contentsToolStripMenuItem.Name = "contentsToolStripMenuItem";
this.contentsToolStripMenuItem.Size = new Size(122, 22);
this.contentsToolStripMenuItem.Text = "Contents";
this.contentsToolStripMenuItem.Click += new EventHandler(this.contentsToolStripMenuItem_Click);
this.apProposToolStripMenuItem.Image = (Image) componentResourceManager.GetObject("apProposToolStripMenuItem.Image");
this.apProposToolStripMenuItem.Name = "apProposToolStripMenuItem";
this.apProposToolStripMenuItem.Size = new Size(122, 22);
this.apProposToolStripMenuItem.Text = "About";
this.apProposToolStripMenuItem.Click += new EventHandler(this.apProposToolStripMenuItem_Click);
this.Load += new EventHandler(this.DocumentSpace_Load);
}
How to find apProposToolStripMenuItem on the form? I tried to remove a particular ToolStripMenuItem, but it doesn't work and I can't find apProposToolStripMenuItem.
Form1:
ToolStripMenuItem mi = new ToolStripMenuItem("apProposToolStripMenuItem") { Name = "About" };
mi.DropDownItems.RemoveByKey("About");
You can remove it by name like this:
mHelp.DropDownItems.RemoveByKey("apProposToolStripMenuItem");
You can also remove it directly like this:
var about = mHelp.DropDownItems["apProposToolStripMenuItem"]
mHelp.DropDownItems.Remove(about);
Assuming you have access to the MenuStrip or the ToolStrip on the form, then you can use Descendants extension method to find all items, regardless of its location in hierarchy of menus and its parent item. for example:
var item = menuStrip1.Descendants()
.Where(x => x.Name == "printToolStripMenuItem").FirstOrDefault();
item?.GetCurrentParent().Items.Remove(item);
im really new to c# and Xamarin Forms but can someone please explain to me why I'm getting an error in the last line of code below ?
Visual Studio is indicating a curly red underline on the word Children on the last line of this code indicating that there is some syntax error but I don't see why that is an error since I'm just accessing a member of the class ....
var content = new ContentPage();
content.Title = "Appuler";
Label CompanyName = new Label();
CompanyName.HorizontalTextAlignment = TextAlignment.Center;
CompanyName.Text = "Test";
Button NextPage = new Button();
NextPage.Text = "Next Page";
NextPage.Font = Font.SystemFontOfSize(NamedSize.Large);
NextPage.BorderWidth = 1;
NextPage.HorizontalOptions = LayoutOptions.Center;
NextPage.VerticalOptions = LayoutOptions.CenterAndExpand;
content.Content = new StackLayout();
content.Content.VerticalOptions = LayoutOptions.CenterAndExpand;
content.Content.Children.Add(CompanyName);
Refactor the last few lines to the following
var layout = new StackLayout();
layout.VerticalOptions = LayoutOptions.CenterAndExpand;
layout.Children.Add(CompanyName);
content.Content = layout;
ContentPage.Content does not have that Children property as it is a View. StackLayout, however does have a Children property that can be accessed.
populate the layout control's accessible properties and then assign it to the Content property of the ContentPage.
I have created a chart dynamically, is there a way to remove it?
I want to create a button_click to remove the chart. Any advise? Especially this is created locally.
private void createchart()
{
var chartA = new Chart();
chartA.Size = new Size(50,50);
chartA.Left = coord_X2;
chartA.Top = coord_Y2;
var chartArea = new ChartArea();
chartA.ChartAreas.Add(chartArea);
var series = new Series();
series.Name = "TT";
series.ChartType = SeriesChartType.Column;
series.Color = System.Drawing.Color.Green;
chartA.Series.Add(series);
chartA.Series["TT"].Points.AddXY("score", dr["Score"]);
this.Controls.Add(chartA);
}
thanks a mil
Jeff
If you give it a Name like this:
chartA.Name = "myUniqueChartName";
you can remove it by that Name:
this.Controls.Remove(this.Controls["myUniqueChartName"]);
If the Name is not unique the Remove statement will first remove the first control of that name added and on each further call remove the next one added..
I am programatically constructing a block of XAML and would like to provide a name x:Name to a TextElement object (an Underline in this case) but even though the Name prop provides a setter, it can not be set per this MSDN article:
MSDN: TextElement.Name
Gets or sets a unique identification for the object. Name can only be set from initial parsing of XAML.
Here is my code:
public void AddLink(string token, string text, string uri)
{
var elem = new Underline();
elem.Name.Name = token; // <-- I would expect this would work...
elem.Inlines.Add();
if (Container == null)
Container = new Paragraph();
Container.Inlines.Add(elem);
}
I am not sure if I get your question right but to me it seems you are asking to register a name to specific namescope in wpf.
This is what you need:
MSDN: FrameworkElement.RegisterName
Here is an example:
myMainPanel = new StackPanel();
myMainPanel.Background = Brushes.Orange;
button1 = new Button();
button1.Name = "Button1";
// Register button1's name with myMainPanel.
myMainPanel.RegisterName(button1.Name, button1);
button1.Content = "Button 1";
button1.Click += new RoutedEventHandler(button1Clicked);
myMainPanel.Children.Add(button1);
button2 = new Button();
button2.Name = "Button2";
// Register button2's name with myMainPanel.
myMainPanel.RegisterName(button2.Name, button2);
button2.Content = "Button 2";
button2.Click += new RoutedEventHandler(button2Clicked);
myMainPanel.Children.Add(button2);
I'm new to Visual Studio and fixing a small bug in an application.
The combo box which exist is editable with DropDown eventhough it is has databinding. Because If I deleted a value, it would not save the change, I made it into a DropDownList; however, now I do not have the option of using null.
I have searched around and have been reading that I can insert an item before databinding.
I have the bits and peices of the codes from different functions for this combo box. Not sure where to exactly make the change.
if someone could point me in the right direction, that'd be great.
private System.Windows.Forms.ComboBox cmbSecCSR;
//----------------
this.cmbSecCSR = new System.Windows.Forms.ComboBox();
//---------------------
// cmbSecCSR
//
this.cmbSecCSR.AccessibleRole = System.Windows.Forms.AccessibleRole.TitleBar;
this.cmbSecCSR.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
this.cmbSecCSR.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.cmbSecCSR.DataSource = this.csrBindingSource2;
this.cmbSecCSR.DisplayMember = "Name";
this.cmbSecCSR.FormattingEnabled = true;
this.cmbSecCSR.Location = new System.Drawing.Point(112, 26);
this.cmbSecCSR.Margin = new System.Windows.Forms.Padding(0);
this.cmbSecCSR.MaxDropDownItems = 10;
this.cmbSecCSR.Name = "cmbSecCSR";
this.cmbSecCSR.Size = new System.Drawing.Size(184, 21);
this.cmbSecCSR.TabIndex = 2;
this.cmbSecCSR.ValueMember = "Username";
this.cmbSecCSR.TextChanged += new System.EventHandler(this.comboBox_TextChanged);
this.cmbSecCSR.Enter += new System.EventHandler(this.cmbBox_Entered);
//
// csrBindingSource2
//
this.csrBindingSource2.DataMember = "CSR";
this.csrBindingSource2.DataSource = this.productionDS;
//-------------------------
//loadUnboundData();
cmbSecCSR.DataBindings.Add("SelectedValue", productionMasterBindingSource, "CSR2", true, DataSourceUpdateMode.OnPropertyChanged);
My Problem is similar to this: http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/fa277629-a65b-4799-9400-364f6f771739/
which is why I decided to change it to DropDownList;however, I don't know how to add the NULL Value in the DropDownList as it is bounded.