I have WPF application that holds some buttons. One button is named ChangeTime so when clicked, dialog opens and there is hh/mm/ss what you can change. I want to store picked time in variable.
My problem is that I only want store that new time if user really changed time. Currently when user presses button, event triggers and new value is stored. Also it shouldn't store new value when dialog is closed without touching Time field.
Essentially I want to change BtnWasClicked to true when time is actually changed. Poor choice of variable name indeed.
Any way to prevent it?
private DateTimePicker _timePortionDateTimePicker;
private DateTime _pickedTime;
private bool btnWasClicked = false;
private bool timeChanged = false;
private void TimeIsChanged(object sender, EventArgs e)
{
timeChanged = true;
if (timeChanged)
{
btnWasClicked = true;
}
}
private void BtnChangeTime_OnClick(object sender, EventArgs e)
{
Form timeDialog = new Form();
_timePortionDateTimePicker = new DateTimePicker();
_timePortionDateTimePicker.Format = DateTimePickerFormat.Time;
_timePortionDateTimePicker.ShowUpDown = true;
_timePortionDateTimePicker.ValueChanged += new
EventHandler(TimeIsChanged);
timeDialog.Controls.Add(_timePortionDateTimePicker);
timeDialog.ShowDialog();
_pickedTime = _timePortionDateTimePicker.Value;
}
You don't actually need TimeIsChanged. You can just check whether _timePortionDateTimePicker.Value is different from _pickedTime in BtnChangeTime_OnClick.
Form timeDialog = new Form();
_timePortionDateTimePicker = new DateTimePicker();
_timePortionDateTimePicker.Format = DateTimePickerFormat.Time;
_timePortionDateTimePicker.ShowUpDown = true;
// start with the previous chosen time
_timePortionDateTimePicker.Value = _pickedTime;
timeDialog.Controls.Add(_timePortionDateTimePicker);
timeDialog.ShowDialog();
if (_pickedTime != _timePortionDateTimePicker.Value) {
// time has changed, assign to _pickedTime and other variables
_pickedTime != _timePortionDateTimePicker.Value;
timeChanged = true;
btnWasClicked = true;
} // otherwise do nothing
Related
When I currently save, it saves the checkboxes (but only the color and it's contents, but not if it was checked for some reason), but of course, since I only have 1 setting for each of the properties of the check box (like color, if it is checked(which doesn't work), and it's content), the last checkbox to be added by the user overwrites every other checkbox.
The other problem is that it only creates 1 checkbox, and I don't know how to create the exact number of checkboxes the user has created, with code.
Yesterday I almost spent the whole day looking for solutions, and today I have only spent some hours, maybe two.
Properties.Settings.Default.CheckBoxes is a bool value, to tell it if it's checked or unchecked. Properties.Settings.Default.CheckBoxText
I started to code in c# some days ago, so my code is probably sloppy. Here is my code:
private void DoneBtn_Click(object sender, RoutedEventArgs e)
{
playSimpleSound();
TextRange descText = new TextRange(descTextBox.Document.ContentStart, descTextBox.Document.ContentEnd);
CheckBox c = new CheckBox();
c.Name = "CheckBoxs";
c.IsChecked = false;
c.Content = TitleTextBox.Text + "\n" + descText.Text;
c.Foreground = new SolidColorBrush(Colors.SkyBlue);
checkBoxPanel.Children.Add(c);
checkBoxScroll.Content = checkBoxPanel;
CreateTaskPanel.Visibility = Visibility.Collapsed;
Properties.Settings.Default.CheckBoxText = (string)c.Content;
}
private void MainWindow1_Loaded(object sender, RoutedEventArgs e)
{
if (Properties.Settings.Default.Value == true)
{
CheckBox c = new CheckBox();
c.IsChecked = Properties.Settings.Default.CheckBoxes;
c.Content = Properties.Settings.Default.CheckBoxText;
c.Foreground = Properties.Settings.Default.CheckBoxColor;
checkBoxPanel.Children.Add(c);
}
Properties.Settings.Default.Value = true;
}
private void MainWindow1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Properties.Settings.Default.Save();
}
I am making a voting app and I want the user to be allowed to vote just one time/day. I have a main page and on this page, multiple buttons navigating to different pages (where the user can vote).On the voting "page" I have a button that when is pressed, it uploads the vote into a database, and after it is disabled for some time. However, if I go back and forth (voting page -> main page -> voting page) the button is once again enabled. How can I solve this, so wherever I navigate the button will remain disabled? I write down the code that I use to disable the button on the voting page, but as I said, whenever I leave the page the button will be enabled again.
public void ImageButton_Clicked(object sender, EventArgs e)
{
Timer aTimer = new Timer();
myButton.IsEnabled = false;
aTimer.Interval = 5000; //ms
aTimer.Enabled = true;
aTimer.Elapsed += ATimer_Elapsed;
}
public void ATimer_Elapsed(object sender, ElapsedEventArgs e)
{
Device.BeginInvokeOnMainThread(() => { myButton.IsEnabled = true; });
}
The Xaml definition of the button.
<ImageButton
x:Name="myButton"
Clicked="ImageButton_Clicked" >
I guess that you want to store data when leaving page using Navigation, if yes, I suggest you can use Xamarin.Essentials: Preferences to save current user data.
For MainPage:
private void btn1_Clicked(object sender, EventArgs e)
{
Navigation.PushAsync(new Page15());
}
For VotePage:
public partial class Page15 : ContentPage
{
public DateTime dt;
public Page15()
{
InitializeComponent();
DateTime currentdate = DateTime.Now;
bool haskey = Preferences.ContainsKey("user1");
if(haskey)
{
string date = Preferences.Get("user1", "");
if (date != null)
{
dt = Convert.ToDateTime(date);
TimeSpan timediff = currentdate - dt;
if(timediff.Seconds<10)
{
btnvote.IsEnabled = false;
}
}
}
}
private void btnvote_Clicked(object sender, EventArgs e)
{
dt = DateTime.Now;
//save count into database or local
btnvote.IsEnabled = false;
}
protected override void OnDisappearing()
{
base.OnDisappearing();
string clickdate = dt.ToString();
Preferences.Set("user1", clickdate);
}
}
In my code, I disable Button within 10 seconds of voting, you can use timediff.Days;timediff.Minutes to change datetime difference.
The button requires two clicks to fire up the event. Here is an image and the code.There is a combobox which triggers the button with different items, but when I click the button to show an item in a panel on the page, I have to click it twice so it can trigger the event. After selecting an item once by twice-clicking it, every next time i click it works with one click, just like it should.
Here is the image of the combobox which triggers the button
And there is the code :
namespace Carbon
{
public partial class ucAnaliza : MetroFramework.Controls.MetroUserControl
{
static ucAnaliza _instance;
public static ucAnaliza Instance3
{
get
{
if (_instance == null)
_instance = new ucAnaliza();
return _instance;
}
}
public MetroFramework.Controls.MetroPanel MetroAnaliza
{
get { return mPanelAnaliza; }
set { mPanelAnaliza = value; }
}
public ucAnaliza()
{
InitializeComponent();
}
private void ucAnaliza_Load(object sender, EventArgs e)
{
}
private void mPotvrdiElementi_Click(object sender, EventArgs e)
{
switch (((ComboBox)mDropAnaliza).SelectedItem.ToString())
{
case "Главна рамка":
_instance = this;
ucGlavna uc = new ucGlavna();
uc.Dock = DockStyle.Bottom;
mPanelAnaliza.Controls.Add(uc);
break;
case "Челна рамка":
_instance = this;
ucCelna uc2 = new ucCelna();
uc2.Dock = DockStyle.Bottom;
mPanelAnaliza.Controls.Add(uc2);
break;
case "Подолжна рамка":
_instance = this;
ucPodolzna uc3 = new ucPodolzna();
uc3.Dock = DockStyle.Bottom;
mPanelAnaliza.Controls.Add(uc3);
break;
}
}
}
}
Here is the code from the designer for the button :
// mPotvrdiElementi
//
this.mPotvrdiElementi.BackColor = System.Drawing.Color.Transparent;
this.mPotvrdiElementi.CausesValidation = false;
this.mPotvrdiElementi.Cursor = System.Windows.Forms.Cursors.Hand;
this.mPotvrdiElementi.ForeColor = System.Drawing.SystemColors.MenuBar;
this.mPotvrdiElementi.Image = global::Carbon.Properties.Resources.Checked_Checkbox_24px;
this.mPotvrdiElementi.ImageAlign = System.Drawing.ContentAlignment.MiddleRight;
this.mPotvrdiElementi.ImageSize = 24;
this.mPotvrdiElementi.Location = new System.Drawing.Point(758, 34);
this.mPotvrdiElementi.Name = "mPotvrdiElementi";
this.mPotvrdiElementi.Size = new System.Drawing.Size(80, 25);
this.mPotvrdiElementi.Style = MetroFramework.MetroColorStyle.Orange;
this.mPotvrdiElementi.TabIndex = 4;
this.mPotvrdiElementi.TabStop = false;
this.mPotvrdiElementi.Text = "Потврди";
this.mPotvrdiElementi.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
this.mPotvrdiElementi.UseCustomBackColor = true;
this.mPotvrdiElementi.UseCustomForeColor = true;
this.mPotvrdiElementi.UseSelectable = true;
this.mPotvrdiElementi.UseStyleColors = true;
this.mPotvrdiElementi.Click += new System.EventHandler(this.mPotvrdiElementi_Click);
I know it is a long time ago but I was having the same problem...
But I found a solution to the problem and is working every time and not killing the usability.
private int focusFlag = 0;
private void MainForm_MouseEnter(object sender, EventArgs e)
{
if (focusFlag < 1)
{
this.FocusMe();
++focusFlag;
}
}
This will not always try to focus on that form when trying to go to other forms or something else, it will just focus once and that is enough... after that it will behave normally :)
It seems the MetroForm doesn´t get Focus until you click within the form and it is just a bug from the developers of the MetroFramework when using certain Metro Controls within the Form.
I have seen others posting the same problem when they are using the MetroFramework.
Hopefully this will help.
I have a Form which holds a DataGridView, this Form also loads with an invisible Form which only holds another DataGridView. The second DGV is used to display more information on the items in the first DGV.
The second DGV should only be shown when the user clicks inside the 7th Cell of any row in the first DGV. I have already managed to get it to hide when I click other cells, but I can't seem to get it to hide when I click outside the DataGridView. I have already tried the Leave, RowLeave and LostFocus events without success. I think it is because as soon as the second DataGridView is displayed, it gets the focus and this somehow messes with the event.
Here is my code:
public class Form1
{
Form schedules = new Form();
DataGridView backups = new DataGridView();
public Form1()
{
this.schedules.Visible = false;
backups.DataBind();
}
private void backups_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1 && e.ColumnIndex == 7)
{
if (this.schedules.getData(Convert.ToInt32(backups.Rows[e.RowIndex].Cells[0].Value)))
{
this.schedules.Owner = this;
this.schedules.Visible = true;
this.schedules.changePosition(Cursor.Position);
}
else
{
this.schedules.Visible = false;
}
}
else
{
this.schedules.Visible = false;
}
}
}
public class Schedules : Form
{
DataGridView grdSchedules = new DataGridView();
public Schedules()
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Visible = false;
this.AutoSize = true;
this.grdSchedules.RowHeadersVisible = false;
this.grdSchedules.AllowUserToAddRows = false;
this.grdSchedules.ScrollBars = ScrollBars.None;
this.grdSchedules.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.grdSchedules.AllowUserToResizeColumns = false;
this.grdSchedules.AllowUserToResizeRows = false;
this.grdSchedules.AllowUserToDeleteRows = false;
}
}
private void Form1_Click(object sender, EventArgs e)
{
this.schedules.Visible = false;
}
Users tend to click on the biggest window they see to close popups. You can also do the same with the secondary form; or even add a close button to it.
I think you would want to combine Form Click and Grid Leave event to make it work.
private void Form1_Click(object sender, EventArgs e)
{
detailForm.Visible = false;
}
private void dataGridView1_Leave(object sender, EventArgs e)
{
detailForm.Visible = false;
}
Now if a user clicks outside Grid on form or directly into a different control, then your detail form should be hidden.
Hope it helps.
I'm currently working in VS 2012.
.NET 4.5 and working on an mmc snap-in. (i know right?!)
so i followed this topic:
Is there a simple way to implement a Checked Combobox in WinForms
as i want something similar to the scheduled task manager.
But that solution does not seem to work for me.
the listview pops up but when i try to click on a checkbox in my listview.
It gives me a big middle finger and closes my dropdown.
is there any way i can suppress the combobox's "focus lost" close event?
i can, not hide the list but then it never hides.
For Example:
// designer class
//
// comboBox1
//
this.comboBox1.DrawMode = System.Windows.Forms.DrawMode.OwnerDrawFixed;
this.comboBox1.DropDownHeight = 1;
this.comboBox1.DropDownWidth = 1;
this.comboBox1.FormattingEnabled = true;
this.comboBox1.IntegralHeight = false;
this.comboBox1.Location = new System.Drawing.Point(256, 371);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(238, 21);
this.comboBox1.TabIndex = 5;
this.comboBox1.DropDown += new System.EventHandler(this.comboBox1_DropDown);
this.comboBox1.DropDownClosed += new System.EventHandler(this.comboBox1_DropDownClosed);
//
// lstWeekDays
//
this.lstWeekDays.CheckBoxes = true;
this.lstWeekDays.Location = new System.Drawing.Point(50, 63);
this.lstWeekDays.Name = "lstWeekDays";
this.lstWeekDays.Size = new System.Drawing.Size(263, 97);
this.lstWeekDays.TabIndex = 13;
this.lstWeekDays.Tag = "lstlstWeekDays";
this.lstWeekDays.UseCompatibleStateImageBehavior = false;
this.lstWeekDays.View = System.Windows.Forms.View.SmallIcon;
this.lstWeekDays.Visible = false;
// Code behind
public Form1()
{
InitializeComponent();
this.lstWeekDays.Items.Add("Monday");
this.lstWeekDays.Items.Add("Tuesday");
this.lstWeekDays.Items.Add("Wednesday");
this.lstWeekDays.Items.Add("Thursday");
this.lstWeekDays.Items.Add("Friday");
this.lstWeekDays.Items.Add("Saturday");
this.lstWeekDays.Items.Add("Sunday");
}
private void comboBox1_DropDown(object sender, EventArgs e)
{
lstWeekDays.Visible = true;
}
private void comboBox1_DropDownClosed(object sender, EventArgs e)
{
lstWeekDays.Visible = false;
}
Add the checkboxes to this list instead of the panel.