Okay, I have a form called settings that I'm using to set hot key's and other stuff through out my form but the issue is once set you need to restart the app for them to take affect, my code is this:
In main class:
public void setHotkeys()
{
if (Properties.Settings.Default.F4 == true)
{
Pvt.HookedKeys.Add(Keys.F4);
MessageBox.Show("checked F4");
}
if (Properties.Settings.Default.F5 == true)
{
Pvt.HookedKeys.Add(Keys.F5);
MessageBox.Show("checked F5");
}
Pvt.KeyDown += new KeyEventHandler(ghk_startPvt);
Group.KeyDown += new KeyEventHandler(ghk_startGroup);
}
Settings form class for saving / setting the settings:
private void settingsPwn4g3_Load(object sender, EventArgs e)
{
settingsLoad();
}
public void saveFields()
{
Properties.Settings.Default.autoAttach = Autoattach.Checked;
Properties.Settings.Default.F5 = F5.Checked;
Properties.Settings.Default.F4 = F4.Checked;
Properties.Settings.Default.Save();
}
public void settingsLoad()
{
Autoattach.Checked = Properties.Settings.Default.autoAttach;
F5.Checked = Properties.Settings.Default.F5;
F4.Checked = Properties.Settings.Default.F4;
}
private void button1_Click(object sender, EventArgs e)
{
pwn4g3 Pwn4g3 = new pwn4g3();
saveFields();
Pwn4g3.setHotkeys();
}
Not to mention this way of doing it is very annoying for over 200 settings this can take days. Any other ideas? I would like to use a variable for set hotkey's then have something where they can press the hot key and set it, rather then 200 check box's for every key on the keyboard.
Related
Is there a way to reload a form variables?
string a = ""
public form1()
{
InitializeComponent();
}
private void form1_load(object sender, EventArgs e)
{
a = "Something";
textbox1.Text = a;
}
Is there a way to reset the value of "a" to empty and the text displayed on the textbox? I can create a function to set the value of "a" to empty and empty the textbox but as I have many variables, I want to avoid doing that as I might miss one that will cause a bug. I was wondering if there is a way to do that.
I don't want to reinitialize the form as its kinda slow to reload the form graphics again. Thanks for any help.
I would work like this:
public struct Snapshot
{
public string a;
public int? b;
}
private Snapshot _initial;
private void form1_Load(object sender, EventArgs e)
{
_initial = this.CreateSnapshot();
}
private Snapshot CreateSnapshot()
{
return new Snapshot()
{
a = textbox1.Text,
b = int.TryParse(textbox2.Text, out int x) ? (int?)x : null
}
}
private void button1_Click(object sender, EventArgs e)
{
this.RestoreSnapshot(_initial);
}
private void RestoreSnapshot(Snapshot snapshot)
{
textbox1.Text = snapshot.a;
textbox2.Text = snapshot.b.HasValue ? snapshot.b.Value.ToString() : "";
}
Your _initial Snapshot happens at form1_Load to save where the form was at at the beginning. You then just call this.RestoreSnapshot(_initial); whenever you want to revert your values.
The advantage of this kind of approach is that it allows you to make multiple snapshots and implement an "undo" function and, because you have separated your form from your data, it makes testing easier.
Have you considered using a wrapper around your properties, with change notification? You can then use data binding to make the value and the text boxes update together. See the following:
public partial class Form1 : Form
{
public NotifyProperty<string> A { get; } = new NotifyProperty<string>();
public NotifyProperty<double> B { get; } = new NotifyProperty<double>();
public void Reset()
{
A.Value = "Something";
B.Value = 3.14d;
}
public Form1()
{
InitializeComponent();
textBox1.DataBindings.Add("Text", A, "Value", true, DataSourceUpdateMode.OnPropertyChanged);
textBox2.DataBindings.Add("Text", B, "Value", true, DataSourceUpdateMode.OnPropertyChanged);
Reset();
}
private void simpleButton1_Click(object sender, EventArgs e)
{
Reset();
}
}
public class NotifyProperty<T> : INotifyPropertyChanged
{
private T _value;
public T Value
{
get => _value;
set
{
if (_value != null && _value.Equals(value)) return;
_value = value;
PropertyChanged?.Invoke(this,new PropertyChangedEventArgs("Value"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
When Reset() is called, both the values in A and B and textBox1 and textBox2 will be updated. Also, when you are obtaining the results from the form, you can use A and B (you don't need to use the textboxes, because when the user types text, A and B will be updated automatically).
First Form
public partial class FrmCasher : XtraForm
{
public static FrmCasher instanceC;
public SimpleLabelItem num;
public FrmCasher()
{
InitializeComponent();
num = lableTableID; // We want to change lableTableID's text value
instanceC = this;
}
}
Second Form
public partial class FrmHall : DevExpress.XtraEditors.XtraForm
{
private void FrmHall_FormClosing(object sender, FormClosingEventArgs e)
{
FrmCasher.instanceC.num.Text = "Our new String value...";
}
}
I am working with yFiles.Net component which I use for representing dependencies between objects in some SQL database (graph representation).
I need to create tooltip which will appear when I point cursors on some object. That tooltip not contains just text, it contains images also. I know how to make custom class which extends default Tooltip class and then to override methods for drawing custom tooltips.
What i do not know is how to show tooltip when I point on some object on graph, and how to dispose it when I move cursor from object on graph? Can someone help me with this please?
If I understand you correctly, you want to intercept the
ItemHoverInputMode.HoveredItemChanged Event.
I have not tested this nor worked with yFiles before, but according to documentation this should work:
// 'gc' is of type yWorks.yFiles.UI.GraphControl.
var ihim = new ItemHoverInputMode();
ihim.HoveredItemChanged += YourEvenHandler;
gc.InputModes.Add(ihim);
Then check the element in the event handler and display or hide the tooltip.
private CustomTooltip m_tooltip;
private MouseHoverInputMode m_mouseHoverMode;
private void SetupToolTips(GraphEditorInputMode mode)
{
m_tooltip = new CustomTooltip(m_model.TooltipImages);
ItemHoverInputMode itemHoverMode = new ItemHoverInputMode();
itemHoverMode.HoverItems = GraphItemTypes.Node | GraphItemTypes.Edge;
mode.ItemHoverInputMode = itemHoverMode;
m_mouseHoverMode = new MouseHoverInputMode(m_tooltip, textProvider);
mode.MouseHoverInputMode = m_mouseHoverMode;
mode.ItemHoverInputMode.HoveredItemChanged += new EventHandler<HoveredItemChangedEventArgs>(ToolTipEvent);
}
private void ToolTipEvent(object sender, HoveredItemChangedEventArgs e)
{
m_tooltip.Item = e.Item;
}
private void textProvider(object sender, ToolTipQueryEventArgs e)
{
if (m_tooltip.Item is INode || m_tooltip.Item is IEdge)
{
e.ToolTip = " ";
}
}
public class CustomTooltip : ToolTip
{
private void OnPopup(object sender, PopupEventArgs e)
{
}
private void OnDraw(object sender, DrawToolTipEventArgs e)
{
}
}
I am trying to update "Player's health." when "Player" attacks a target. Player's health is reduced based on the target's return damage.
My Game class (where the label is):
public partial class Game : Form
{
public static Wizard wizard;
public static Assasin assasin;
public Game()
{
InitializeComponent();
}
private void Battle_Click(object sender, EventArgs e)
{
Battle battle = new Battle();
battle.Show();
}
public void Game_Load(object sender, EventArgs e)
{
NameLabel.Text = HeroMaker.className;
if (HeroMaker.wizardChoosen)
{
wizard = new Wizard(HeroMaker.className);
HealthLabel.Text = wizard.Health.ToString();
DamageLabel.Text = wizard.AttackDamage.ToString();
HealthBar.Maximum = wizard.Health;
HealthBar.Value = wizard.Health;
}
}
}
My Battle class (when attacking is happening) :
public partial class Battle : Form
{
Creature troll = CreaturesFactory.CreateCreature(CreatureType.Troll);
public Battle()
{
InitializeComponent();
}
private void AttackTroll_Click(object sender, EventArgs e)
{
Game.wizard.Health -= troll.ReturnDamage;
//TODO: Update the "HealthLabel value."
}
}
The problem is that when I attack the troll, Player's health is decreasing but its not updating on the label. Thanks in advance.
You just need to update the label:
private void AttackTroll_Click(object sender, EventArgs e)
{
Game.wizard.Health -= troll.ReturnDamage;
//TODO: Update the "HealthLabel value."
HealthLabel.Text = wizard.Health.ToString();
//any other things that need to be updated
}
You can also bind the label value like in this question.
Similarly, you can hook up an event such as OnWizardHealthChange that updates the label value whenever the HP changes. This way, you won't need to remember to add HealthLabel.Text = wizard.Health.ToString(); everywhere the health changes. There's an example of this in the question that I linked.
EDIT:
You can try looking in the code-behind where the label is created to see its access modifier (is it public?)
Or, you can try this:
Label healthLabel = (Label)Application.OpenForms["FormName"].Controls.OfType<Label>().First(x=> x.Name == "LabelName");
Note that I haven't tested it but you should be able to at least get the label using this code and then update the value there. Here is a good discussion on accessing controls (such as your label) in another form.
I want to make a function to work in toggle(switch) mode when i press a key and i really can't figure how to do it. I tried lots of ways and only the "RegisterHotKey" method is working fine. But "RegisterHotKey" is overwriting the mapped key from the game and this is not what i need. So i'm trying to use "GetKeyState". The code below it's only working for one position no matter what i change...:
private void mw_KeyDown(object sender, KeyEventArgs e){
bool sw = (toggle = !toggle);
int tog = (GetKeyState(Key.Tab));
if ((tog & 1) == 1)
{
if (sw)
{
System.Windows.MessageBox.Show("go to second position...!");
}
}
else
{
System.Windows.MessageBox.Show("go to first position...!");
}
}
Any idea or suggestion how can i do this ?
Thank you,
Solution provided by Sergey Alexandrovich Kryukov from CodeProject.
Link: solution
public partial class MainWindow : Window
{
bool toggle;
public MainWindow()
{
InitializeComponent();
MainWindow.KeyDown += (sender, eventArgs) => { if (eventArgs.Key == Key.F7) toggle = !toggle; };
}
private void MainWindow_KeyDown(object sender, EventArgs e)
{
if (toggle)
{
//System.Windows.MessageBox.Show("go to second position...!");
}
else
{
//System.Windows.MessageBox.Show("go to first position...!");
}
}
}
I've got code and i know I'm 99% of the way there. C# coding in MS VS2008.
Basically I have a form that has 4 radio buttons and a Continue button. the user clicks one of the radio buttons and clicks continue, and this all works fine.
However, I want to use the value entered by the user (i.e. if they click the first button, I want a variable equal to 1, 2nd button equals 2 and so on). I tried doing this in various points but the only place I can get it to run is in the private void btnOkClick line, which means I can use the values outside this void, which is what I really want.
I've tried playing around with setting some enums and such (commented out in the code below), but I can't quite get it. I know I must be close but my novice-ness is truly showing as I keep reading posts and can't quite grasp it.
In short, I want to be able to have other classes in my VS2008 project be able to reference whatever value the user selected in the initial form.
namespace AmortClient
{
public partial class frmLoadACTFCST : Form
{
public frmLoadACTFCST()
{
InitializeComponent();
//set the parent of the form to the container
//this.MdiParent = parent;
}
//public enum ACTFCST
//{
// ACT = 1,
// FCST = 2,
// PLAN = 3,
// FiveYearPlan2012=4
//}
//private ACTFCST _actfcst = ACTFCST.ACT;
//public ACTFCST actfcst
//{
// get { return _actfcst; }
// set { _actfcst = value; }
//}
private void frmLoadACTFCST_Load(object sender, EventArgs e)
{
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void btnActual_CheckedChanged(object sender, EventArgs e)
{
}
private void btnForecast_CheckedChanged(object sender, EventArgs e)
{
}
private void btnPlan_CheckedChanged(object sender, EventArgs e)
{
}
private void btn5YrPlan2012_CheckedChanged(object sender, EventArgs e)
{
}
private void btnContinue_Click(object sender, EventArgs e)
{
string ACTFCSTtext = "";
int dataTypeKey = 0;
if (btnActual.Checked)
{
ACTFCSTtext = btnActual.Text;
dataTypeKey = 1;
}
else if (btnForecast.Checked)
{
ACTFCSTtext = btnForecast.Text;
dataTypeKey = 2;
}
else if (btnPlan.Checked)
{
ACTFCSTtext = btnPlan.Text;
dataTypeKey = 3;
}
else if (btn5YrPlan2012.Checked)
{
ACTFCSTtext = btn5YrPlan2012.Text;
dataTypeKey = 4;
}
string msg = "";
msg = ACTFCSTtext + " " + dataTypeKey;
//btn5YrPlan2012
MessageBox.Show(msg);
Close();
}
}
}
Your dataTypeKey and ACTFCSTtext variables need to be declared as instance variables for your Form object if you want to access them from any other methods within your form. If you want to use them with some other form, you can pass them either as constructor arguments, or set some properties of said other form.
So you'd declare them just after the class declaration if you want them to be instance variables. They should still be private, meaning they can only be accessed from within your frmLoadACTFCST class.
public partial class frmLoadACTFCST : Form
{
private string ACTFCSTtext = "";
private int dataTypeKey = 0;
...
EDIT: if you want to access variables from one object in a different object (or static class), your options are as follows...
1) Declare your variables as public instance variables (same as shown above but public; these are known as Properties when you give them getter and setter methods). Your class that needs access to these variables would need to have a reference to the class that owns the variables.
Example:
FormA has a public property named SomeString.
FormB needs to access SomeString.
FormB needs a reference to FormA, and would access the variable as...
formAReference.SomeString
2) Pass the values of the variables as arguments to some method for the class that needs access.
Example:
FormA has a private instance variable named SomeString.
FormB needs access to SomeString.
If FormA instantiates FormB, it can pass the value of SomeString to FormB's constructor...
//From within FormA's code
FormB formB = new FormB(SomeString);
//FormB's constructor
public FormB(string someString)
{
this.someString = someString;
}
Maybe there is a smarter way to do it.
public partial class frmLoadACTFCST : Form
{
public frmLoadACTFCST()
{
InitializeComponent();
actfcst = ACTFCST.ACT;
btnActual.Tag = ACTFCST.ACT;
btnActual.Checked = true;
btnForecast.Tag = ACTFCST.FCST;
btnPlan.Tag = ACTFSCT.PLAN;
btn5YrPlan2012.Tag = ACTFCST.FiveYearPlan2012;
}
public enum ACTFCST
{
ACT = 1,
FCST = 2,
PLAN = 3,
FiveYearPlan2012=4
}
public static ACTFCST actfcst { get; private set; }
private void CheckedChanged(object sender, EventArgs e)
{
// All the buttons uses this Click-event.
actfcst = (sender as Button).Tag as ACTFCST;
}
private void btnContinue_Click(object sender, EventArgs e)
{
MessageBox.Show(actfcst.ToString());
Close();
}
}
The point is that all the buttons calls CheckedChanged when clicked.
Using a static means that others can access the value using something like this:
frmLoadACTFCST.ACTFCST value = frmLoadACTFCST.actfcst;
// Do something based on value.
I hope this helps you in yoyr quest.
If you select a control in design view, the properties window contains an item named "Modifiers". You can make the control public here.
A better way would be to create a new public property on your form that yields the value of the currently selected radio button.