I am working on a program to manage a minecraft server with a local UI as well as a remote interface. I have a button on a ribbon bar that will enable or disable the remote interface and a textbox for inputting the port. Currently, I disable the textbox when the networking is enabled, but, disabling does not re-enable the textbox after I set it to true again (and setting a breakpoint reveals it to still be false).
private void NetToggleChecked(object sender, RoutedEventArgs e) {
portTextBox.IsEnabled = false;
if (ButtonPressedByUser) {
var result = MessageBox.Show("Are you sure you want to enable networking with the current settings?" +
" If not properly configured, it may be possible for an attacker to enter your server.",
"Simple Bukkit Wrapper", MessageBoxButton.YesNo, MessageBoxImage.Warning,
MessageBoxResult.No);
if (result == MessageBoxResult.No) {
ButtonPressedByUser = false;
NetworkToggle.IsChecked = false;
ButtonPressedByUser = true;
return;
}
}
Config.NetConf["enabled"] = "true";
int port;
if (!int.TryParse(Config.NetConf["port"], out port)) {
MessageBox.Show("Port could not be parsed (is it a number?)");
ButtonPressedByUser = false;
NetworkToggle.IsChecked = false;
ButtonPressedByUser = true;
return;
}
Net.Listener.StartListening(port);
}
private void NetworkToggleUnchecked(object sender, RoutedEventArgs e) {
portTextBox.IsEnabled = true;
if (ButtonPressedByUser) {
var result =
MessageBox.Show("Are you sure you wish to disable all networking to your server? It will " +
"be impossible to connect to it remotely and any existing connections will be closed.",
"", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
if (result == MessageBoxResult.No) {
ButtonPressedByUser = false;
NetworkToggle.IsChecked = true;
ButtonPressedByUser = true;
return;
}
}
Config.NetConf["enabled"] = "false";
Net.Listener.StopListening();
}
Thank you for any help resolving why the textbox will not enable again.
Old Question but i kept coming across it while searching for an answer so figured i'd post an answer anyways. There is a bug in the ribbonTextbox control that results in isenabled always being false if there is no command associated. There are 2 ways round this from what i have found:
1: Create a new control based on the ribbontextbox and override the isenabledcore property to always return true. As shown here Cannot set RibbonTextBox isEnable to False
2: Create a dummy command and associate it with the control
public static readonly ICommand DummyCommand = new RoutedCommand("Dummy", typeof(Control));
public static void Dummy(Object sender, ExecutedRoutedEventArgs e)
{
// Do nothing its a dummy command
}
public static void CanDummy(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = true;
}
as described in a comment in this link http://blogs.msdn.com/b/wpf/archive/2010/10/21/wpf-ribbon-october-2010-update-details.aspx .
AS i said probably no help to the original poster but i kept coming across it while looking for an answer so it may save someone else a few minutes of googling time.
Related
The student is back! I am trying to self-teach C#, so please pardon my simple but many questions. I appreciate you all.
I am working on a quiz app.What I want but cant seem to achieve is that when "Testing mode" (radio button) is selected, "Number of questions" need to be grayed out.Otherwise, student can select number of questions to attempt.
Here is my code
private void rdotesting_CheckedChanged(object sender, EventArgs e)
{
if (MessageBox.Show("You have selected Testing Mode.Do you want to continue?", "Confirm Choice", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.Yes)
{
MessageBox.Show("Click 'Start' to continue..");
btnclose.Hide();
}
else
{
MessageBox.Show("You Must select an option to continue.");
}
}
//if testing mode, dissable number of questions ,and also the 'Close' button
Like this - see comments
private void rdotesting_CheckedChanged(object sender, EventArgs e)
{
//this event fires when rdotesting is checked or when it is unchecked (change)
//set the enabled state of the nud/button to th opposite of the checked state
//ie when checked = true then enabled = false
numberQsNUD.Enabled = !rdotesting.Checked;
closeButton.Enabled = !rdotesting.Checked;
//if not in test mode, exit to stop the message showing every time
if(!rdotesting.Checked)
return;
if (MessageBox.Show("You have selected Testing Mode.Do you want to continue?", "Confirm Choice", MessageBoxButtons.YesNo, MessageBoxIcon.Information) == DialogResult.No)
rdotesting.Checked = false; //user said no; turn off test mode
simply you can try this code...
private void rdotesting_CheckedChanged(object sender, EventArgs e) {
if(rdotesting.Checked) {
numberQsNUD.Enabled = false;
closeButton.Enabled = false;
} else {
numberQsNUD.Enabled = true;
closeButton.Enabled = true;
}
}
or you can customize this via using this code...
private void EnableComponent(bool check) {
numberQsNUD.Enabled = check;
closeButton.Enabled = check;
}
private void rdotesting_CheckedChanged(object sender, EventArgs e) {
if(rdotesting.Checked) {
EnableComponent(false);
} else {
EnableComponent(true);
}
}
When I try to start my form, it flashes like it never opened.
I have already looked into my code and there is nothing wrong
Here's my code:
public partial class Initialization : Form
{
public Initialization()
{
InitializeComponent();
}
private async Task Wait1(int Milliseconds)
{
await Task.Delay(Milliseconds);
}
private async void Wait(int Millisecondsdew)
{
Wait1(Millisecondsdew);
}
private void Initialization_Load(object sender, EventArgs e)
{
MessageBox.Show("Initialization");
///Setup Stuff///
ShowIcon = false;
ControlBox = false;
FormBorderStyle = FormBorderStyle.FixedDialog;
Text = "";
/// Start Initializing///
richTextBox1.Text = "Initializing....";
Wait(1000);
if (File.Exists(#"C:\Program Files (x86)\Lazy Tools\AdditionalFiles.exe"))
{
richTextBox1.Text = "Initializing.... \n Software Installers 1 Exists";
Wait(1000);
if (File.Exists(#"C:\Program Files (x86)\Lazy Tools\SoftwareInstallers2.exe"))
{
richTextBox1.Text = richTextBox1.Text + "\n Software Installers 2 Exists";
Wait(1000);
}
else
{
MessageBox.Show("Please reinstall software store, \n Software Installers 2 is missing");
}
}
else
{
MessageBox.Show("Please reinstall software store, \n Software Installers 1 is missing");
}
}
}
The async works fine on my first form, but this is the second form. When I call up the second form, it flashes and it like never opened.
Apparently, setting the ControlBox property to false and then setting the Text property to an empty string after the form has loaded causes the form to close (which seems like a bug).
Code to reproduce the issue:
// Using other events like `Form_Shown` or even a `Button_Click` still has the same behavior
private void Form1_Load(object sender, EventArgs e)
{
this.ControlBox = false;
this.Text = "";
}
As a workaround, you may, instead, set the FormBorderStyle property to FormBorderStyle.None:
this.FormBorderStyle = FormBorderStyle.None;
..which will have a similar effect to what you're trying to achieve. This is actually the standard way to hide the title bar.
If you don't want to hide the border or you actually want to use the ControlBox and the Text properties, you can do any of the following:
Set those two properties at design-time. Or...
Move those two lines to the constructor of the form:
public Initialization()
{
InitializeComponent();
this.ControlBox = false;
this.Text = "";
}
Or make sure to set the Text property before the ControlBox property:
private void Initialization_Load(object sender, EventArgs e)
{
this.Text = "";
this.ControlBox = false;
}
How can access to the Location Service API be disabled?
I did receive a letter from the Microsoft Development Center which contains this tip:
Your app must provide in-app settings that allow the user to enable
and disable your app's access to and use of location from the Location
Service API.
Can anyone provide further assistance on how I go about doing this?
Paste this code right after InitializeComponent(); in MainPage.xaml. You will have to add reference to IsolatedStorage by this line using System.IO.IsolatedStorage;.
if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationConsent"))
{
return;
}
else
{
MessageBoxResult result = MessageBox.Show("Allow this app to access your location?", "Location", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = true;
}
else
{
IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = false;
}
IsolatedStorageSettings.ApplicationSettings.Save();
}
Also create a Settings.xaml page with a ToggleSwitch which has the following code:
if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationConsent"))
{
if ((bool)IsolatedStorageSettings.ApplicationSettings["LocationConsent"] == true)
{
locationSwitch.IsChecked = true;
}
else
{
locationSwitch.IsChecked = false;
}
}
else
{
MessageBoxResult result = MessageBox.Show("Allow this app to access your location?", "Location", MessageBoxButton.OKCancel);
if (result == MessageBoxResult.OK)
{
IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = true;
}
else
{
IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = false;
}
IsolatedStorageSettings.ApplicationSettings.Save();
}
private void locationSwitch_Checked(object sender, RoutedEventArgs e)
{
if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationConsent"))
{
IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = true;
IsolatedStorageSettings.ApplicationSettings.Save();
}
}
private void locationSwitch_Unchecked(object sender, RoutedEventArgs e)
{
if (IsolatedStorageSettings.ApplicationSettings.Contains("LocationConsent"))
{
IsolatedStorageSettings.ApplicationSettings["LocationConsent"] = false;
IsolatedStorageSettings.ApplicationSettings.Save();
}
}
And on the page that you use Location / GPS data include the following code:
if ((bool)IsolatedStorageSettings.ApplicationSettings["LocationConsent"] == true)
{
//Do Something
}
else
{
MessageBox.Show("Please enable location services to use this feature. You can turn it on from Settings.");
}
This will surely help. I use the same. Do upvote and mark as answer if this helps you too :)
Does your app use location services and you need to have the ability to disable it OR are you asking in general?
If it's the first then just stop collecting data and disable it in your app. If it's the second then go into the WPmanifest and uncheck it
The code:
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
crawlLocaly1 = new CrawlLocaly();
crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
OptionsDB.Set_localOnly(checkBox2.Checked);
if (checkBox2.Checked)
{
DialogResult dr = crawlLocaly1.ShowDialog(this);
if (dr == DialogResult.Cancel)
{
crawlLocaly1.Close();
}
else if (dr == DialogResult.OK)
{
LocalyKeyWords.Add(crawlLocaly1.getText());
crawlLocaly1.Close();
}
removeExt = true;
}
else
{
removeExt = false;
}
}
This line:
OptionsDB.Set_localOnly(checkBox2.Checked);
Save the state of the checkBox2 if its checked or not. If its checked next time i will run my program i will see the V in the checkBox2 checked box. If i will uncheck the checkBox next time i run my program the box of the checkBox2 will be unchecked.
The problem is when i check the checkBox2 once close my program and run it again since the checkBox is checked now then for some reason it will make this:
DialogResult dr = crawlLocaly1.ShowDialog(this);
Wich will open and show the user a new Form.
But i dont want it to be like that.
I want that if the user checked the checkBox when the program is running the new Form will show up. But if the user is running the program from the beginning and the checkBox is checked dont show the new Form just show that the checkBox is checked !
How should i fix it ?
You need an other boolean flag checkedInThisSession which initially set to false, and just set it to true in a checkbox OnChecked handler, then you can check this state easily. Hope all is clear
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
crawlLocaly1 = new CrawlLocaly();
crawlLocaly1.StartPosition = FormStartPosition.CenterParent;
OptionsDB.Set_localOnly(checkBox2.Checked);
// UPDATED
if (checkedInThisSession && checkBox2.Checked)
{
DialogResult dr = crawlLocaly1.ShowDialog(this);
// ...
}
else
{
removeExt = false;
}
// UPDATED
checkedInThisSession = checkBox2.Checked;
}
// In constructor
checkedInThisSession = false;
checkBox2.Checked = OptionsDB.Get_localOnly();
The CheckedChanged event is fired every time the checkbox is set, also programmatically. So to solve this issue you need to ignore the first time the event is fired. So a boolean could be your solution:
private bool ignore = true;
private void checkBox2_CheckedChanged(object sender, EventArgs e){
if(ignore == false){
//your code here
}
else
ignore = false;
}
I'm using DevExpress XtraReports in a WinForms application, but could equally apply to other reporting tools.
I'd like to perform some logic per-row in the report as it is "rendered", on a row-by-row basis. Specifically I'd like to hide a barcode if the data for the barcode isn't available.
Currently I'm doing the following:
private void xrBarCode2_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
{
var barcode = (XRBarCode)sender;
if (barcode.Text.Trim() == "")
{
barcode.Visible = false;
lblWarning.Visible = true;
}
else
{
barcode.Visible = true;
lblWarning.Visible = false;
}
}
But that just plain smells bad. I'd like to access the current data row in this method and work on the "real" properties of the object, but can't. What is the typical pattern for this in other report generators? Am I even using the correct event? I tried Detail_BeforePrint, but that had no additional information.
Use Detail_BeforePrint in conjunction with GetCurrentColumnValue() like so:
private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) {
if (string.IsNullOrEmpty(GetCurrentColumnValue("BarcodeColumnName"))) {
barcode.Visible = false;
lblWarning.Visible = true;
} else {
barcode.Visible = true;
lblWarning.Visible = false;
}
}