I am building an app which displays a curve in 2D like for example the Math.Sin curve. The user is prompted to enter how many dots he wants to display on the plot. When he chooses the number of dots and enters the parameters for them - a new window is opened where the plot is displayed.
My question is, if there is a way, I can get back to the previous window and enter new parameters which will generate a new curve BUT display it together with the first one? My solution for now is for only one curve being displayed. Any time the Plot window is opened - a new instance of it is created, so I suppose I have to find a way to use the same instance since the window is not closed, only hidden, but I dont know how.
Checkout the following image:
As you have already assumed, you can begin by using the same instance of the Form which displays the PlotView. You could expose a method Update in the PlotDisplayWindow Form, which would then update the plot view with new points. For example, in your parent form.
PlotDisplayWindow plotDisplay;
private void RefreshPlot(object sender, EventArgs e)
{
var dataPoints = GetNewDataPoints();
if (plotDisplay == null)
{
plotDisplay = new PlotDisplayWindow();
plotDisplay.Show();
}
plotDisplay.Update(dataPoints);
}
In your PlotDisplayWindow Form, you could initialize your Model when loading the Window for the first time and then, use the Update method to add more points to the Plot View. For example:
private void PlotDisplayWindow_Load(object sender, EventArgs e)
{
this.plotView1.Model = new PlotModel { Title = "Example 1" };
}
public void Update(IEnumerable<DataPoint> points)
{
this.plotView1.Model.Series.Add(new LineSeries { ItemsSource = points });
this.plotView1.InvalidatePlot(true);
}
The PlotView.InvalidatePlot(true) would ensure the plot is refreshed and the newly added points are displayed.
Related
I have a main page which has a play, options and exit button, both the play and exit button work as I followed a tutorial on it however for the options button I do not know how to navigate it. What I am planning to do is to create another class called optionsMenu.cs and have like a credits screen or a how to play guide on it.
These are the codes that I have for my options button.
var optionsGameButton = new Button(buttonTexture, buttonFont)
{
Position = new Vector2(300, 250),
Text = "Options",
};
optionsGameButton.Click += OptionsGameButton_Click;
_components = new List<Component>()
{
titleGameButton,
playGameButton,
optionsGameButton,
exitGameButton,
};
Finally I know I have to write a code in this area to be able to get the options button working but I don't know which kind of code to put.
private void OptionsGameButton_Click(object sender, EventArgs e)
{
Console.WriteLine("Credits");
}
If you're set on using click events, then you'll need to create a new instance of your options page and set it as the content of your control object within OptionsGameButton_Click. On the options page you'll need to set the xmlns to your optionsMenu.cs file.
I think you could also set the content of your control object by passing it as a parameter within the method itself, but I've only done this using the ICommand interface.
In my Excel Add-In, I have created two task panes - with each ones visibility being from two different values, requiring both to be in a return statement, however it will only allow me to return one of the values. These are 'taskPaneValue' and 'taskPaneValue2'.
How do I go about having both returned in the 'get' statement.
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
taskPaneControl2 = new FileChooser();
taskPaneValue2 = this.CustomTaskPanes.Add(taskPaneControl2, "File Chooser");
taskPaneValue2.VisibleChanged += new EventHandler(taskPaneValue_VisibleChanged);
taskPaneValue2.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionFloating;
taskPaneValue2.Height = 600;
taskPaneValue2.Width = 600;
taskPaneValue2.DockPositionRestrict = Office.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoChange;
//These three lines of code start by initiating the TaskPane control (namely aLaCarteMenu())
//It then goes on to set the name of the menu "A La Carte Menu" which appears on the top left of the window before stating its visibility.
taskPaneControl1 = new aLaCarteMenu();
taskPaneValue = this.CustomTaskPanes.Add(taskPaneControl1, "A La Carte Menu");
taskPaneValue.VisibleChanged +=
//The following four lines of code are used to display the visiblity of the AddIn.
//The docking position is set to float, with a resolution of 980x1920. This is designed for a 1080p screen, however still working on changing it to fit screens dynamically.
new EventHandler(taskPaneValue_VisibleChanged);
taskPaneValue.DockPosition = Office.MsoCTPDockPosition.msoCTPDockPositionFloating;
taskPaneValue.Height = 980;
taskPaneValue.Width = 1920;
//This line of code sets the position to be restricted to what has been set above (floating). This allows for the pane to be moved around the screen, as well as to be resized.
//This stops the pane from locking on to the right, left, top or bottom sections of the Excel Window.
taskPaneValue.DockPositionRestrict = Office.MsoCTPDockPositionRestrict.msoCTPDockPositionRestrictNoChange;
}
private void taskPaneValue_VisibleChanged(object sender, System.EventArgs e)
{
Globals.Ribbons.ManageTaskPaneRibbon.toggleButton1.Checked = taskPaneValue.Visible;
Globals.Ribbons.ManageTaskPaneRibbon.toggleButton2.Checked = taskPaneValue2.Visible;
}
public Microsoft.Office.Tools.CustomTaskPane TaskPane
{
get
{
return taskPaneValue2;
}
}
The final 'get' statement is the one I wish to return both variables.
Use a Tuple or create a class that has all the properties you are looking for and make that the return type of the function.
You can also create a method with out parameter modifier, if it'still possible to you. Please, check the following link: https://msdn.microsoft.com/en-us/library/ee332485.aspx
I started out C# very recently and sorry if this question sounds dumb.
How do I add a Listbox in a Form that pops out from a Button click?
Note: The Form isn't the one that's added from the Solution Explorer whereby I can just drag a Listbox from the Toolbox to my Form.
So what I want is to create a ListBox in my file drawer1Form where I can add additional items. Thanks for the help in advance!:)
private void drawer1button_Click(object sender, EventArgs e) // Drawer 1 Button
{
drawer1Form df1 = new drawer1Form();
df1.StartPosition = FormStartPosition.CenterScreen;
df1.Show();
}
public partial class drawer1Form : Form // Creates drawer1Form
{
public drawer1Form()
{
Text = "Drawer 1 ";
}
}
Pretty much the same way as you'd do with any other object.
In the class of your form add a
private ListBox myAwesomeListBox;
Then in the button event handler add something like this:
myAwesomeListBox = new ListBox();
myAwesomeListBox.SuspendLayout();
// set all the properties that you want
myAwesomeListBox.Name = "myAwesomeListBox";
myAwesomeListBox.Location = new Point(...); // place it somewhere
myAwesomeListBox.Size = new Size(...); // give it a size
// etc...
df1.Controls.Add(myAwesomeListBox);
myAwesomeListBox.ResumeLayout();
This should be it.
I highly advise you to do it through the designer first though, and then take a look at the generated code in the form's .Designer.cs file, you'll have a very good understanding after reading through that.
I am new to C#. I am using windows forms and I have Form1 which contains 2 buttons ( one to create user control at run time and the other creates buttons on user control at run time).
This code creates user control and FlowLayoutPanel (to organize button position) if you click add_UserControl button. And then it creates buttons on FlowLayoutPanel if you click Add_Buttons button and it is all done at run time.
Now in Form1 let's say I created user control and FlowLayoutPanel and then created 5 buttons , how can I save the properties/details of this user control with its FlowLayoutPanel and 5 buttons in SQL database so I can use them later when I run the program? I have been thinking about an idea and I reached the internet but no luck.
Any idea? Please help me. Thank you
public partial class Form1 : Form
{
FlowLayoutPanel FLP = new FlowLayoutPanel();
UserControl uc = new UserControl();
private void add_UserControl_Click(object sender, EventArgs e)
{
uc.Height = 700;
uc.Width = 900;
uc.BackColor = Color.Black;
Controls.Add(uc); //add UserControl on Form1
FLP.Height = 600;
FLP.Width = 800;
FLP.BackColor = Color.DimGray;
uc.Controls.Add(FLP); // add FlowLayoutPanel to UserControl
}
private void Add_Buttons_Click(object sender, EventArgs e)
{
//####### add buttons to FlowLayoutPanel ############
Button dynamicButton = new Button();
dynamicButton.Height = 50;
dynamicButton.Width = 200;
dynamicButton.BackColor = Color.Green;
dynamicButton.ForeColor = Color.Blue;
dynamicButton.Text = "";
FLP.Controls.Add(dynamicButton);
}
}
OK, First you need to create a class that will represent one of the buttons with the properties you need.
class MyButton
{
public string ButtonText {get;set;}
}
Everytime you click and create a button, you actually create an object of this class and add it to a collection or list. Then you would have some other code watching over the collection, and every time it gets a new entry, it creates a new button and sets its Button text to the text property. when a member of list is gone, it removes the button.
If you need more properties to be remembered (color, size, font, ...) you add them to the class as well. If you need for other controls, as well, .... you can always create common parent controls.
Simple.
If you want to be able to reload it, you could define the MyButton class as serializable and store it in xml file, and upon build, reload it.
You should watch into WPF and it's MVVM pattern. It's pretty much similar to it. Also have a look into command pattern, usefull pattern when it commes to this.
You can remember the FlowLayoutsPanels in one SQL table and in another table you could save the buttons which belong to these FlowLayoutPanels.
On Form Load or Application Load, you could check if there are already FlowLayoutPanels and correspending Buttons do exist in the SQL db and if yes then create them, else do nothing.
Basically, what I'm trying to do is to create a window with list of images and large image view box. If user points at one of the images in the list, imagebox should display it, otherwise it should display live feed from a webcam.
I'm trying to achieve that by using ImageGrabbed event of EmguCV.Capture, and a field that stores index of the currently viewed image. Camera feed works alright, however, when I move the mouse over listbox, image view merely stops updating. It stays stuck at the last frame it saw, and doesn't display the image I'm pointing at. I've checked that the list has images in it, that index is correct, and that image coming from the camera is different from ones saved in the list. Also, same behavior happens when I use vanila PictureBox.
See the code (stripped of extra parts) below.
Initialization:
int ViewedFrame = -1;
var Frames = new List<Image<Rgb, byte>>();
// This list will have some images added to it along the way.
// They will be displayed in a custom drawn ListBox bound to the list.
Camera = new Emgu.CV.Capture(cameraid);
Camera.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH, width);
Camera.SetCaptureProperty(Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT, height);
Camera.ImageGrabbed += UpdateCamera;
Camera.Start();
Frame capture:
private void UpdateCamera(object sender, System.EventArgs ev)
{
if (ViewedFrame == -1)
Preview.Image = Camera.RetrieveBgrFrame();
else
// !!! this line has no apparent effect, image in the ImageBox is still the same
Preview.Image = Frames[ViewedFrame];
}
Mouse handling:
private void FrameList_MouseMove(object sender, MouseEventArgs e)
{
int index = FrameList.IndexFromPoint(e.Location);
if (index == ListBox.NoMatches)
ViewedFrame = -1;
else if (index != ViewedFrame)
{
ViewedFrame = index;
}
}
private void FrameList_MouseLeave(object sender, EventArgs e)
{
ViewedFrame = -1;
}
UPD: I tried adding Preview.Update() and Preview.Refresh() into the same branch as Frames[ViewedFrame], and after the conditional operator. In first case it doesn't have any effect. In second case nothing is shown at all. In case the ImageBox can't keep up updating itself, I enabled doublebuffering, but that didn't help either. Moreover, after I hover the mouse over an image in the list, the camera stream breaks too. The code above is the only one that's somewhat working.