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);
Related
This question already has answers here:
How to save a List<string> on Settings.Default?
(4 answers)
How Can I Save a List of objects in setting file?
(1 answer)
How to store a list of objects in application settings
(5 answers)
Closed 1 year ago.
I'm trying to make a winforms app where you can get an overview of your project, by entering a description, name and image. I store those three different lists of strings and images, and then I tried to save it, but it doesn't work.
private void save_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Name = new textList();
Properties.Settings.Default.Description = new textList();
Properties.Settings.Default.Image = new imageList();
foreach (var item in names)
{
Properties.Settings.Default.Name.Add(item.Text);
}
foreach (var item in description)
{
Properties.Settings.Default.Description.Add(item.Text);
}
foreach (var item in pictureBox)
{
Properties.Settings.Default.Image.Add(item.BackgroundImage);
}
Properties.Settings.Default.Save();
}
private void Oversigt_Load(object sender, EventArgs e)
{
Properties.Settings.Default.Reload();
Properties.Settings.Default.Name = new textList();
Properties.Settings.Default.Description = new textList();
Properties.Settings.Default.Image = new imageList();
foreach (var item in Properties.Settings.Default.Image)
{
var pic = new PictureBox();
pic.Size = new Size(192, 108);
pic.BackgroundImageLayout = ImageLayout.Zoom;
pic.BackgroundImage = item;
pic.Location = new Point(47, 209);
Controls.Add(pic);
pictureBox.Add(pic);
}
foreach(var item in Properties.Settings.Default.Name)
{
var name = new Label();
name.AutoSize = true;
name.Text = item;
name.Font = new Font("Microsoft Sans Serif", 17);
name.ForeColor = Color.White;
Controls.Add(name);
names.Add(name);
}
foreach(var item in Properties.Settings.Default.Description)
{
var desc = new Label();
desc.AutoSize = false;
desc.Size = new Size(192, 63);
desc.Text = item;
desc.Font = new Font("Microsoft Sans Serif", 10);
desc.ForeColor = Color.White;
Controls.Add(desc);
description.Add(desc);
}
arrangeProject();
}
class textList : List<string>
{
}
class imageList : List<Image>
{
}
I tried making a setting that was just a normal string and that worked fine!
I also tried putting a breakpoint after save_Click, but I couldn't see anything there. Not sure if I did it right tho. I know there are version of the application, but that was set to 1.0.0.0 by default:
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
So I'm not sure where to go from here
My problem is this: I have a form which is created at runtime within it's own function which takes 2 parameters to retrieve a text value from a dataGridView and display it in a textBox. The issue is that when I call this function on it's own it works fine however when called from another runtime-created form control event, no text value is returned but the form is still displayed. This is the code which creates and shows the form:
private void commentsForm(int x, int y)
{
using (Form frmDisplayText = new Form())
{
TextBox txt = new TextBox();// <- Decleration
Button btnAccept = new Button();
Button btnRevert = new Button();
Button btnClose = new Button();
frmDisplayText.Size = new Size(550, 350);// <- Size
txt.Size = new Size(510, 250);
btnAccept.Size = new Size(216, 30);
btnRevert.Size = new Size(216, 30);
btnClose.Size = new Size(66, 30);
txt.Location = new Point(12, 12);// <- Location
btnAccept.Location = new Point(12, 269);
btnRevert.Location = new Point(234, 269);
btnClose.Location = new Point(456, 269);
string dataToOutput = dataGridView1.Rows[x].Cells[y].Value.ToString();// <- Text
txt.Text = dataToOutput;
frmDisplayText.Text = "Comments";
btnAccept.Text = "Accept";
btnRevert.Text = "Revert";
btnClose.Text = "Cancel";
txt.Multiline = true;// <- Other
btnAccept.Font = new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Regular);
btnRevert.Font = new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Regular);
btnClose.Font = new Font(FontFamily.GenericSansSerif, 12.0F, FontStyle.Regular);
btnAccept.Click += (_, args) =>// <- Event Handlers
{
string txtText = txt.Text;
using (var conn = new SqlConnection(connection))
{
string SQL = "UPDATE Comments SET [" + dataGridView1.Rows[x].Cells[y].OwningColumn.HeaderText.ToString() + "] = #val2 WHERE ID = #val3";
using (var cmd = new SqlCommand(SQL, conn))
{
cmd.Parameters.AddWithValue("#val2", txt.Text);
cmd.Parameters.AddWithValue("#val3", dataGridView1.Rows[x].Cells[0].Value.ToString());
try
{
conn.Open();
cmd.ExecuteNonQuery();
dataGridView1.Rows[x].Cells[y].Value = txt.Text;
MessageBox.Show("Success");
}
catch (SqlException ex)
{
MessageBox.Show(ex.ToString());
}
}
}
};
btnRevert.Click += (_, args) =>
{
txt.Text = dataToOutput;
};
btnClose.Click += (_, args) =>
{
frmDisplayText.Close();
};
frmDisplayText.Controls.Add(txt);// <- Add Controls
frmDisplayText.Controls.Add(btnAccept);
frmDisplayText.Controls.Add(btnRevert);
frmDisplayText.Controls.Add(btnClose);
frmDisplayText.ShowDialog();
}
}
And here is when it is called:
btnAddComment.Click += (_, args) =>
{
commentsForm(x, y);
};
Any ideas as to why the text property is null when called form here?
Thanks all
There is an addition to the row index which was throwing off the data to retrieve.
So I want to draw a grid with 3 columns so that I can have:
Cancel Title Save
But at the moment I am getting
Cancel Title Save
The code I have is:
var modalContentPage = sender as ModalContentPage;
var context = Android.App.Application.Context;
var actionBar = ((Activity)Context).ActionBar;
LinearLayout gridView = new LinearLayout(context);
gridView.SetGravity(GravityFlags.FillHorizontal|GravityFlags.CenterVertical);
LinearLayout.LayoutParams textViewParameters =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MatchParent, LinearLayout.LayoutParams.MatchParent);
Android.Widget.Button cancelButton = new Android.Widget.Button(context);
cancelButton.Text = "Cancel";
cancelButton.Click += delegate
{
modalContentPage.CancelCommand.Execute(null);
};
gridView.AddView(cancelButton, LayoutParams.WrapContent);
TextView modelTitle = new TextView(context);
modelTitle.Text = actionBar.Title;
modelTitle.TextFormatted = actionBar.TitleFormatted;
modelTitle.Gravity = GravityFlags.Center;
modelTitle.TextSize = 25;
modelTitle.SetTypeface(Android.Graphics.Typeface.Default, Android.Graphics.TypefaceStyle.Bold);
modelTitle.SetTextColor(Android.Graphics.Color.White);
gridView.AddView(modelTitle);
Android.Widget.Button saveButton = new Android.Widget.Button(context);
saveButton.Text = "Save";
saveButton.Click += delegate
{
modalContentPage.SaveCommand.Execute(null);
};
saveButton.Background.SetColorFilter((Resources.GetColor(Resource.Color.accent)),Android.Graphics.PorterDuff.Mode.Multiply);
saveButton.Gravity = GravityFlags.Center;
gridView.AddView(saveButton,LayoutParams.WrapContent);
ActionBar.LayoutParams actionbarParams =
new ActionBar.LayoutParams(ActionBar.LayoutParams.MatchParent, ActionBar.LayoutParams.MatchParent);
actionBar.SetCustomView(gridView, actionbarParams);
actionBar.SetDisplayShowCustomEnabled(true);
actionBar.SetIcon(new ColorDrawable(Color.Transparent.ToAndroid()));
actionBar.SetDisplayHomeAsUpEnabled(false);
All i needed to add was:
modelTitle.LayoutParameters = new TableRow.LayoutParams(0, LayoutParams.WrapContent, 1f);
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 trying to use the Microsoft ribbon control programatically using C#. Everything is fine but I'm unable to bind the command through the RibbonCommand. Can anyone give me an example of how to do this? My actual code is:
Ribbon rbn = new Ribbon();
RibbonTab file = new RibbonTab();
file.Name = "file";
file.Label = "FILE";
RibbonTab edit = new RibbonTab();
edit.Name = "edit";
edit.Label = "Edit";
RibbonGroupPanel rbgp = new RibbonGroupPanel();
RibbonGroup rbg = new RibbonGroup();
RibbonButton rbtn = new RibbonButton();
rbtn.Content = "New";
RibbonCommand rcomnd = new RibbonCommand();
rcomnd.LabelTitle = "NEW";
rcomnd.ToolTipDescription = "THIS IS NEW";
rcomnd.LargeImageSource = imgSource;
rcomnd.Execute(rbtn, rbtn);
rbtn.IsEnabled = true;
//rcomnd.SmallImageSource = imgSource;
rcomnd.CanExecute +=new CanExecuteRoutedEventHandler(rcomnd_CanExecute);
rcomnd.Executed +=new ExecutedRoutedEventHandler(rcomnd_Executed);
CommandBinding cmdb = new CommandBinding(ApplicationCommands.New);
cmdb.Command = ApplicationCommands.New;
cmdb.Executed +=new ExecutedRoutedEventHandler(cmdb_Executed);
CommandBind.Add(cmdb);
//rcomnd.Executed += new ExecutedRoutedEventHandler(OnAddNewEntry);*/
rbtn.Click +=new System.Windows.RoutedEventHandler(rbtn_Click);
rbtn.Command = rcomnd;
But the bindings are not working and the button is not enabled.
Check this tutorial from the "Adding Commands" section. It may be good to read it from the start.