C# WPF Setting file - c#

I need your help.
I created a file "flick.settings" and a bool variable in it called "prop".
When a user checks one of the checkboxes and hits the button, the code should "remember" that prop's value is true and on the next launch it should appear as checked. So, the value must remain even when I replace .exe file with another copy.
Here's a part of my code:
private void remember_Click(object sender, RoutedEventArgs e)
{
if (this.oneone.IsChecked == true)
{
flick.Default.prop = true;
flick.Default.Save();
}
else if (this.oneone.IsChecked == false)
{
flick.Default.prop = false;
flick.Default.Save();
}
}
And to access the variable:
private void show_Click(object sender, RoutedEventArgs e)
{
if (flick.Default.prop == true)
{
this.oneone.IsChecked = true;
}
else if (flick.Default.prop == false)
{
this.oneone.IsChecked = false;
}
}
But when I change the value in flick.settings and save it, my so called program appears to "remember" that bool value inside of it, not in the flick.settings. So, my question is how can I do it right? Help me, please!

Related

Action perform button visible false does not work

Well, I have this situation, in a program I put a Button whose code is activated with PerformClick (programmatically), that button must be invisible in the interface so I put the value visible=false since the beginning of the program but the action on the event click doesn't perform, but if I put visible = true, the action actually is performed, any ideas of the problem?
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
if(_datosDe == "Insumos")
{
_btnRecargarInsumos.PerformClick();
}
this.Close();
}
_btnRecargarInsumos: is the button and is actually performed in another Form.
private void btnRecargarInsumos_Click(object sender, EventArgs e)
{
objGeneral.regresaDescripciones(ref dsDescripciones);
cbACDescripcion.DataSource = dsDescripciones.Tables[0];
cbACDescripcion.DisplayMember = "Nombre";
cbACDescripcion.ValueMember = "ID";
cbACDescripcion.SelectedIndex = -1;
cbACDescripcion.Text = "";
}
cbACDescripcion: Combobox which will be "reloaded" with the values of the DataSet: dsDescripciones.
The property visible is false since the beginnig of the program, but I also try to set visible=true and just before the method PerformClick() change it, but is the same.
But if I put visible=true since the beginning it works in that way.
If you click a button that's not visible or not enabled, nothing happens, even if you click it programmatically. Here's a workaround that works for me, although it's a bit of a hack:
_btnRecargarInsumos.SuspendLayout();
_btnRecargarInsumos.Visible = true;
_btnRecargarInsumos.PerformClick();
_btnRecargarInsumos.Visible = false;
_btnRecargarInsumos.ResumeLayout();
Why not just put your code in a separate method?
Example:
private StuffToDoAtClick()
{
objGeneral.regresaDescripciones(ref dsDescripciones);
cbACDescripcion.DataSource = dsDescripciones.Tables[0];
cbACDescripcion.DisplayMember = "Nombre";
cbACDescripcion.ValueMember = "ID";
cbACDescripcion.SelectedIndex = -1;
cbACDescripcion.Text = "";
}
//Your Button.Click() code//
private void btnRecargarInsumos_Click(object sender, EventArgs e)
{
StuffToDoAtClick()
}
//Your Datagridview code//
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
if(_datosDe == "Insumos")
{
StuffToDoAtClick();
}
this.Close();
}

Why does TextChanged fire when a RichEditBox gains focus?

I have a RichEditBox in a C# Windows Runtime app. I have set it to set a Boolean flag IsFileUpToDate to false on TextChanged like so:
private void OnTextChanged(object sender, RoutedEventArgs e)
{
if (IsFileUpToDate != false)
{
IsFileUpToDate = false;
}
}
When the page is first navigated to, IsFileUpToDate should be set to true. I have set it like so:
protected override void OnNavigatedTo(NavigationEventArgs e)
{
IsFileUpToDate = true;
}
However, the RichEditBox gains focus immediately when the page loads, and this seems to be causing it set the Boolean to false even though the text hasn't been changed. Why is it doing this? How can I rewrite these commands so that the Boolean is reliably set?
RichEditBox is getting it's text changed on when it is loaded
you can have another flag to make sure it won't change yours on the fisrt time
private void OnTextChanged(object sender, RoutedEventArgs e)
{
if (IsFirstload)
{
IsFirstLoad = false;
return;
}
if (IsFileUpToDate != false)
{
IsFileUpToDate = false;
}
}

Need to send two buttons backwards and forwards

I've got the code, and all seems correct, I've had it reviewed and it seems impossible to find out why the button isn't doing what its coded to do. I'm making a Music player, and when I press the play button, it will be sent to the back and the pause button will become visible, when I next click the pause button, nothing happens and its primary function stops working all together. Here is the code for people to examine.
private void btnPlay_Click(object sender, EventArgs e)
{
try
{
if (_mp3Player != null)
_mp3Player.Play();
btnPlay.SendToBack();
btnPause.BringToFront();
}
catch (Win32Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void btnPause_Click(object sender, EventArgs e)
{
if (_mp3Player != null)
_mp3Player.Stop();
btnPause.SendToBack();
btnPlay.BringToFront();
}
Perhaps it would be best to use visibility?
private void btnPause_Click(object sender, EventArgs e)
{
if (_mp3Player != null)
{
_mp3Player.Stop();
}
btnPause.Visible = False;
btnPlay.Visible = True;
}
Or even the enabled property?
... btnPause.Enabled = false; ...
However I feel you could make it better by having it be the same button, with a value, so just have a value on it of true if it's player or false if it's pause and then in the click event check against that value to determine what it is currently and then just execute the relevant functionality and change the text or image that you have on the button.

Multiple buttons calling a single method

I'm trying to persist CSS user preference to my database according to which of six buttons is selected by the user.
On order to do this, I am trying to assign an integer value to each button click event; whichever is clicked will pass the corresponding integer as a parameter to my data access object to update the database able.
My method reads such:
protected void SetCSS(object sender, EventArgs e)
{
Users setCss = new Users();
if (IsPostBack)
{
if (sender.ToString() == "Blue")
{
setCss.StylePreference = 0;
}
else if (sender.ToString() == "Khaki")
{
setCss.StylePreference = 1;
}
else if (sender.ToString() == "Night")
{
setCss.StylePreference = 2;
}
else if (sender.ToString() == "Pink")
{
setCss.StylePreference = 3;
}
else if (sender.ToString() == "White")
{
setCss.StylePreference = 4;
}
else if (sender.ToString() == "Yellow")
{
setCss.StylePreference = 5;
}
setCss.UserLoginName = Session["eMail"].ToString(); // current user
setCss.SetStylePreference(setCss.UserLoginName, setCss.StylePreference);
}
In each button's click event:
protected void btnBlue_Click(object sender, EventArgs e)
{
SetCSS(btnBlue, null);
}
protected void btnKhaki_Click(object sender, EventArgs e)
{
SetCSS(btnKhaki, null);
}
etc...
I put a watch on the sender object and, when the Pink button is selected, the value assigned to the sender reads
{Text="Pink"}
However, as I step through the if statement in the SetCSS method, when I come to the
else if (sender.ToString() == "Pink")
the condition is not met and, rather than setting the style preference to 3 as it should, the program passes on to the end of the statement, finishing by always assigning a value of 0 to the property.
What am I doing wrong?
Would really appreciate help...
You need to use sender.Id.ToString()
Calling sender.ToString() on an ASP.NET button will return "System.Web.Ui.Button" or something similar.
Paste the code related to how you setup the button and I'll clarify my answer more, as you could need either Id or Text depending on how you're setting the the name on your button.
Realistically, you can refactor this code to be a lot simpler.
You should map the Click event on all of your buttons to SetCSS(). Having a lot of scattered methods to only wrap the call is useless.
Change the if / else block to check for sender.Text
if (sender.Text.ToString() == "Blue")
{
setCss.StylePreference = 0;
}
and do the same for the rest of the statements.

How can i detect if an item in a listbox have been selected more then once?

I have this event code of the listBox:
I tried ot do it this way and it's almost working good.
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
if (recentItems.Contains(listBox1.SelectedItem))
{
itemExist = true;
item = listBox1.SelectedItem.ToString();
this.f1.PlayLightnings();
f1.pdftoolsmenu();
}
else
{
itemExist = false;
item = listBox1.SelectedItem.ToString();
recentItems.Add(listBox1.SelectedItem.ToString());
this.f1.PlayLightnings();
f1.pdftoolsmenu();
}
}
Im using a new bool variable itemExist and check and if the List recentItems wich is don't contain the selectedItem add it.
And if it does exist set the flag to true.
Then in the other code in Form1 im doing:
if (Lightnings_Extractor.Lightnings_Mode.itemExist == true)
{
if (!pdf1.Lightnings.Contains(Lightnings_Extractor.Lightnings_Mode.item))
{
pdf1.Lightnings.Add(Lightnings_Extractor.Lightnings_Mode.item);
}
}
So it's working as i wanted but the problem is that each new item i select in the listBox click on it i have to click on it twice since first time it's not in the recentItems and only on the second click it does in the recentItems and only on the second click it's changing the flag to true.
So how can i solve this problem in the SelectedIndexChanged event ?
I saw now i don't need the code part in Form1 only this code:
private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
{
item = listBox1.SelectedItem.ToString();
this.f1.PlayLightnings();
f1.pdftoolsmenu();
if (item != null && !pdf1.Lightnings.Contains(item.ToString()))
{
pdf1.Lightnings.Add(item.ToString());
}
}

Categories

Resources