Zedgraph Control accessible from any function - c#

I have this function:
public void MainFormLoad(object sender, EventArgs e)
{
GraphPane myPane = GRAPH.GraphPane;
}
Where myPane is a reference to GraphPane (GRAPH is name of ZedGraphControl which is displayed in GUI)
And now I want to change things like name of "x" or "y" axis, title, colors etd. or whatever you can change, but based on events. For example: I have textbox where I can write text and this text will be displayed in graph as title after textbox_textchanged_event trigger like this:
void TitleTextChanged(object sender, EventArgs e)
{
myPane.Title.Text = textbox1.Text;
}
There will be more functions like this to change properties of the graph. But this is not working.
Is there a way to come around this?
I have also tried this:
void TitleTextChanged(object sender, EventArgs e)
{
GRAPH.GraphPane.Title.Text = textbox1.text.Text;
}
but no help at all.
Please help, any advices are welcome.
**ANSWER:
So far, i have found this solution:
public void MainFormLoad(object sender, EventArgs e)
{
EditGraph(GRAPH);
}
This is the event that handles text change in text box:
public void TB_GRAPH_TITLE_VALUETextChanged(object sender, EventArgs e)
{
//GraphPane myPane2 = GRAPH.GraphPane;
changedGraphTitle = true;
EditGraph(GRAPH);
}
This is the function that find what is changed and update it:
public void EditGraph(ZedGraphControl zgc)
{
GraphPane myPane = zgc.GraphPane;
if(changedGraphTitle)
{
myPane.Title.Text = TB_GRAPH_TITLE_VALUE.Text;
changedGraphTitle = false;
zgc.Refresh();
}
}
"bool changedGraphTitle = false" must be declared also.**

If I've understood your question correctly, here's my simple code to update Zedgraph Axis Titles by a single ButtonClick event.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZedGraph;
namespace updateZedGraph
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
myPane = zedGraphControl1.GraphPane;
}
GraphPane myPane;
private void btn_UpdateChart_Click(object sender, EventArgs e)
{
// Update x Axis Text
myPane.XAxis.Title.Text = textBox1.Text;
// Update x Axis Text
myPane.YAxis.Title.Text = textBox2.Text;
// Refresh Chart
zedGraphControl1.Invalidate();
}
}
}
hope that helps..

Related

ComboBox menu repeat bug after typing in textbox in Winform app

I have a primitive app with 2 comboBoxes. They work fine after first starting the app.
However after typing in text in the search bar and pressing enter, the comboboxes loop their contents.
It happens after I type in the textbox, even if I do not press enter. Every time I press a key another repeat list of options appends to the comboBox.
How do I prevent this comboBox malfunction? Here is my code.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
//Nick Knapp
//CSCI 363 Fall 2019
namespace c363_hw3_2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
this.Text = "Library Literature Search";
this.BackColor = Color.White;
comboBox2.Items.Add("All");
comboBox2.Items.Add("Books");
comboBox2.Items.Add("Papers");
comboBox2.Items.Add("Films");
comboBox2.Items.Add("CDs");
comboBox2.Items.Add("Other");
comboBox2.SelectedIndex = 0;
comboBox1.Items.Add("Title");
comboBox1.Items.Add("Author");
comboBox1.Items.Add("Publisher");
comboBox1.Items.Add("ISBN");
comboBox1.SelectedIndex = 0;
// this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckEnter);
//this.Controls.Add(textBox1);
this.ActiveControl = textBox1;
textBox1.KeyPress += new KeyPressEventHandler(keypressed);
}
private void keypressed(Object o, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
textBox1.Text = "";
e.Handled = true;
}
}
private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
{
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
I think your code is correct but have you ever tried these 2 method after calling combo boxes?
comboBox1.ResetText();
comboBox1.Items.Clear();```
i think it works
private void keypressed(Object o, KeyPressEventArgs e)
{
if (e.KeyChar == (char)Keys.Return)
{
textBox1.Text = "";
comboBox1.Items.Clear();
e.Handled = true;
}
}

Multiline Textbox Suggestion/Append property

I am currently in need of a multi-line textbox that can do suggest/append of the desired list items so far I am able to get the suggestion but the only problem is now is that it is working abnormally, i.e. showing suggestion for white spaces, when I press enter in order to autocomplete it gives me multiple strings of that characters, also there is a problem that due to my logic it only show suggestion to the strings that are at the end of the textbox, not the one that is inserted in between.
So far my code is following
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace multiline_text_suggest
{
public partial class Form1 : Form
{
ListBox listBox = new ListBox();
public Form1()
{
InitializeComponent();
Controls.Add(listBox);
listBox.Hide();
textsplit();
}
string intellisenes;
private void textsplit() {
listBox1.Items.Clear();
foreach (string c in textBox.Text.Split()) {
listBox1.Items.Add(c);
}
intellisenes = listBox1.Items[listBox1.Items.Count-1].ToString().Trim();
}
void listBox_SelectedIndexChanged(object sender, KeyEventArgs e)
{
if (e.KeyValue == (decimal)Keys.Enter)
{
textBox.Text += ((ListBox)sender).SelectedItem.ToString();
textBox.Focus();
listBox.Hide();
}
}
private void textBox1_KeyUp(object sender, KeyEventArgs e)
{
textsplit();
listBox.SetBounds(textBox.Left, textBox.Top + textBox.Height, textBox.Width + 20, 40);
listBox.KeyDown += listBox_SelectedIndexChanged;
List<string> list = new List<string>(){"king","kong"};
var localList = list.Where(z => z.StartsWith(intellisenes.Trim())).ToList();
if (localList.Any() && !string.IsNullOrEmpty(textBox.Text) && !string.IsNullOrWhiteSpace(textBox.Text))
{
listBox.DataSource = localList;
listBox.Show();
listBox.Focus();
}
}
private void textBox_TextChanged(object sender, EventArgs e)
{
textsplit();
}
}
}
Forgive me for any mistakes since I am just a noob in coding.

C# Hotkey Box (AHK Hotkey Style)

I am trying within C# to create a similar Hotkey function as in AHK. Just like in any video game, you click in a box, press your hotkey and get it registered.
That's what I am trying to do with the textBox:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Keybinder
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
KeyPreview = true;
textBox1.ReadOnly = true;
textBox1.Focus();
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "HELLO";
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
char key = e.KeyChar;
string keystring = Char.ToString(key);
textBox1.Text = keystring;
}
}
}
However, the problem is, that I need to turn off the basic functions of the textBox, but I don't know how. For example: the cursor is still active and I can highlight the text in it.
You can set the property of the text box, Enabled to false, and then put the background color white and the font black, so it doesn´t seems like it was disabled, but you can not focus nor click on ir.
textBox1.Enabled = false;
By the way, I have a really simple library, that may be usefull.
https://github.com/PabloHorno/HotKeyDialog
Once you have the library referenced you can use it like this
or in a more simple way
var hotKey = new HotKey();
hotKey = HotKeyMessageBox.Show("Title", "A little description");
hotKey.ToString();
Or you can check if the user closes the box
HotKey hotkey = new HotKey();
if (HotKeyMessageBox.Show("Title", "Please press the key combination", out hotkey) == DialogResult.OK)
label.Text = "You have pressed the keys: " + hotkey.ToString();
else
label.Text = "You have closed the dialog. There is no input";
Hope it helps!
Why are you using a TextBox, if you do not need its functionality?
Instead of turning off it's functions, you can create a simple custom control and put it on your form. Something like:
public class KeyInput : UserControl
{
public string KeyString { get; set; } = "HELLO";
public KeyInput() : base()
{
BorderStyle = BorderStyle.Fixed3D;
}
protected override void OnKeyPress(KeyPressEventArgs e)
{
base.OnKeyPress(e);
KeyString = e.KeyChar.ToString();
Invalidate();
}
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
e.Graphics.DrawString(KeyString, Font, SystemBrushes.ControlText, 0, 0);
}
}

Tagging an array of bitmaps with corresponding string values

I am creating a "rock paper scissors lizard spock" game for school. I am stuck on the part of the assignment where we need to tag the bitmap images with the corresponding names using the tag property. I have created an array of names, and an array of bitmap images.
I am unsure of how to use the tag property to do this. The exact instructions are:
Create an array of string objects, and initialize it to contain the string values "rock", "paper", "scissors", "lizard", "spock" Add code to each of the bitmaps with the corresponding string values. (ex. bitmap "properties.resources.rock" should be tagged with the string "rock".
private void Form1_Load(object sender, EventArgs e)
{
string[] names =
{
"rock",
"paper",
"scissors",
"lizard" ,
"spock"
};
Bitmap[] bitmaps =
{
Properties.Resources.rock,
Properties.Resources.paper,
Properties.Resources.scissors,
Properties.Resources.lizard,
Properties.Resources.spock,
};
}
I've tried adding rock.Tag = properties.resources.rock.
I've tried names[0].tag = properties.resources.rock.
I've also tried properties.resources.rock.Tag.
The professor hasn't shown us how to use the tag property yet, so I'm sure I'm just missing something obvious. I am new to coding and any help is appreciated.
My full code is here, though it is very incomplete.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Lab5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
string[] names =
{
"rock",
"paper",
"scissors",
"lizard" ,
"spock"
};
Bitmap[] bitmaps =
{
Properties.Resources.rock,
Properties.Resources.paper,
Properties.Resources.scissors,
Properties.Resources.lizard,
Properties.Resources.spock,
};
// Following array will not actually be used
// PictureBox[] pics = new PictureBox[bitmaps.Length];
for (int i = 0; i < bitmaps.Length; i++)
{
PictureBox pic = new PictureBox();
pic.Image = bitmaps[i];
pic.Location = new Point(20 + (i * 100), 20);
pic.SizeMode = PictureBoxSizeMode.AutoSize;
Controls.Add(pic);
pic.Click += clickHandler;
}
}
private void displayImages()
{
// Move code from form1_load to here
}
//click handler for every picture
private void clickHandler(object sender, EventArgs e)
{
MessageBox.Show("You clicked a picture box");
}
private void playAgainButton_Click(object sender, EventArgs e)
{
// call display images here
}
}
}
All winforms controls, including PictureBox, have a Tag property that can be set to any object. Presumably, your professor wants you to use that to link the pictures to their corresponding names.
Add this line to your for loop where you are initializing the PictureBoxes:
pic.Tag = names[i];
Then in your click handler you can show the name of the picture that was clicked like this:
private void clickHandler(object sender, EventArgs e)
{
PictureBox pic = (PictureBox)sender; // get the control that was clicked on
string name = (string)pic.Tag; // retrieve the name from the Tag property
MessageBox.Show("You clicked " + name);
}

C#: Drag drop controls on surface

Is there a way that one can make a control, such as a textbox, drag-droppable in C#?
I want the user to have the ability to click and hold the control with the mouse and drag it around on its surface and drop it anywhere within that surface.
Anyone has any idea how to implement this?
This answer helped me a lot. It's working great on any type of Control and Container.
If your control is moving within one container (e.g. panel), you can override OnMouseDown / OnMouseMove events, and adjust the Location property of the control.
Based on your question, it does not seem that you need full drag-and-drop (moving data between different controls or even applications).
if you're trying to drag an item from outside the silverlight container, then your best bet is to check out silverlight 4 beta
public MainPage()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainPage_Loaded);
// wire up the various Drop events
InstallButton.Drop += new DragEventHandler(InstallButton_Drop);
InstallButton.DragOver += new DragEventHandler(InstallButton_DragOver);
InstallButton.DragEnter += new DragEventHandler(InstallButton_DragEnter);
InstallButton.DragLeave += new DragEventHandler(InstallButton_DragLeave);
}
void InstallButton_Drop(object sender, DragEventArgs e)
{
IDataObject foo = e.Data; // do something with data
}
This used to be so easy in VB6. But now we really only have what used to be called OleDrag.
Anyway, the following code should show you how. You just need a single label (dragDropLabel), and set the AllowDrop property of the form (DragDropTestForm) to True.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace DragDropTest
{
public partial class DragDropTestForm : Form
{
// Negative offset to drop location, to adjust for position where a drag starts
// on a label.
private Point _labelOffset;
// Save the full type name for a label, since this is used to test for the control type.
private string labelTypeName = typeof(Label).FullName;
public DragDropTestForm()
{
InitializeComponent();
}
private void dragDropLabel_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_labelOffset = new Point(-e.X, -e.Y);
}
}
private void dragDropLabel_MouseMove(object sender, MouseEventArgs e)
{
const double minimumDragDistance = 4;
const double minimumDragDistanceSquared = minimumDragDistance * minimumDragDistance;
if (e.Button == MouseButtons.Left)
{
// Minimum n pixel movement before drag starts.
if (((Math.Pow(_labelOffset.X - e.X, 2)) + Math.Pow(_labelOffset.Y - e.Y, 2)) >= minimumDragDistanceSquared)
{
dragDropLabel.DoDragDrop(dragDropLabel, DragDropEffects.Move);
}
}
}
private void DragDropTestForm_DragOver(object sender, DragEventArgs e)
{
IDataObject data = e.Data;
string[] formats = data.GetFormats();
if (formats[0] == labelTypeName)
{
e.Effect = DragDropEffects.Move;
}
}
private void DragDropTestForm_DragDrop(object sender, DragEventArgs e)
{
IDataObject data = e.Data;
string[] formats = data.GetFormats();
if (formats[0] == labelTypeName)
{
Label label = (Label) data.GetData(formats[0]);
if (label == dragDropLabel)
{
Point newLocation = new Point(e.X, e.Y);
newLocation.Offset(_labelOffset);
dragDropLabel.Location = this.PointToClient(newLocation);
}
}
}
}
}

Categories