I've added a toggle button to my ribbon and now, I'd like to send its state to an other object. The code is below.
public void Butt_Click(Office.IRibbonControl control)
{
something.SendButtVal(control.State);
}
Of course, control.State doesn't work. I suspect that I'll need to cast control to type ToggleButton or something like that but intellisense gives me nothing...
I can see that the API for the interface IRibbonControl is here but I'd like to see a list of implementing classes, like in JavaDocs. How do I get there?
EDIT
Please note that I'm asking about a ribbon component. See the method signature. It's a toggle button on a ribbon. The problem is that I don't get Checked property and (probably) need to cast control to correct type.
You need to alter your signature. It's not correct for a toggle button. See here for details.
public void ToggleButtonOnAction(IRibbonControl control, bool pressed)
{
MessageBox.Show("ToggleButton was switched " + pressed ? "on" : "off");
}
Related
Hello i want to disable / enable button with check box.
myproject:
If I understood correctly you question, there is a property called Enabled on the Control.
Here is a link to that property documentation
Update: (Just to make more clear the answer based on comments)
It belogns to the Winforms API as this property is part of the Namespace of System.Windows.Forms
As most (if not every) control inherits from Control Class and this Class has the Enabled property which states that "Enabled
Gets or sets a value indicating whether the control can respond to user interaction."
So, every Control that inherits from this Class can use that property.
Hello if you want do get your button enabled or disabled try something like this.
first set the button.enable to false:
button.enabled = false;
then:
if(checkbox.checked == true)
{
button.enabled = true;
}
else
{
//let the user know with messagebox that he needs to accept the terms
}
I want a button on my winform to be enabled or disabled based on the return value of a particular function. Basically, I'm trying the following code in various places where the function will possible return a different value:
btnNewNotices.Enabled = isSelectedPrinterValid();
this.btnNewNotices.Refresh()
However, this is not working. Why is it when I call the refresh method after the enabled property is changed, that the button does not become enabled? I have to close the form and reopen it before the button properly disables. What is the best way to accomplish what I need here without having to bounce the form?
you can add a Databinding for the Enabled property.
if your method is implemented in your Form then you can define a Property
public bool IsSelectedPrinterValid
{
get{ return this.isSelectedPrinterValid(); }
}
And add a Databinding as following:
btnNewNotices.DataBindings.Add("Enabled", this, "IsSelectedPrinterValid");
You can refresh your value:
btnNewNotices.DataBindings[0].ReadValue();
The common way is to implement a ViewModel containing all Properties and Methods you need and bind your controls to these.
Short answer, but it's pretty straight forward.
Using .Refresh(); causes the button to repaint itself, and resets the Enabled property. There's no reason to use it in your context. Just remove it.
btnNewNotices.Enabled = isSelectedPrinterValid();
I'm thinking about the best way to validate user input.
Let's imagine some TextBoxes, CheckBoxes or whatever .NET control you please, where the user input has to be validated as OK or NOK. As soon as the user's filled up all required fields he submits via a button.
Now I have to know which fields were previously confirmed as OK and which as NOK. By now I've always handled such cases by declaring a global bool variable for every control to tell me so. But I don't like that...
I'm pretty sure there must be another way! What I would like to do is expanding these .NET controls with a OK or NOK property called status or similar. Can you do that? And if so how do you do it? Is something like that already existing?
Thank you for your response!
You have some useful features in windows forms to perform validation and show error messages including:
IDataErrorInfo Interface
Validating Event of Controls
ErrorProvider Component
ValidateChildren Method and AutoValidate Property of Form
Using above options:
You can perform validation when you are using data-binding to model classes.
You van perform validation when you don't use data-binding.
You can show error messages and an error icon near the controls which are in invalid states.
You can decide to prevent the focus change from invalid controls or let the focus change.
You can show a validation summary for your form.
You can also apply DataAnnotations Validations in Windows Forms
IDataErrorInfo Interface
In cases which you have some model classes, the best fit for validation and providing error messages in windows forms is implementing IDataErrorInfo. It's supported by data-binding mechanisms and some windows forms control like DataGridView and ErrorProvider.
To keep things simple you can write validation rules in your class and return error messages using IDataErrorInfo properties. Even if you want to apply a more advanced scenario like using validation engines, at last it's better to implement IDataErrorInfo to gain most consistency with widows forms.
You will use an ErrorProvider to show error messages. It's enough to bind it to your data source and it shows errors automatically.
Validating Event of Controls
In cases that you don't have model classes and all validations should be done against controls, the best option is using Validating event of controls. There you can set e.Cancel = true to set the control state as invalid. Then you can prevent focus change or use the state of control in getting validation summary.
In this case you will use an ErrorProvider to show errors. It's enough to set an error for a control in Validating event this way: errorProvider1.SetError(control1, "Some Error") or you can set an empty error message to remove validation error.
ErrorProvider Component
In both cases when you use databinding or when you use Validating event, as mentioned above, ErrorProvider shows and error icon with a tooltip that shows error message for you near the controls. (DataGridView uses its own mechanism to show errors on rows and cells, without using an ErrorProvider.)
You can also use the component to get a validation summary for your form using GetError method of the component which return the error message of each control.
ValidateChildren Method and AutoValidate Property of Form
You can use ValidateChildren method of form or your container control to check if there is a validation error for your controls or not.
Based on the value of AutoValidate property of your form, it prevents focus change or let the focus change from invalid controls.
Save the names of your controls to be validated into an array and then just loop through them. You can also set a validation function onto them, if you want to.
var elements = new[] {
new { Control = textBox1 },
new { Control = textBox2 }
};
foreach (var elem in elements)
{
elem.Control.BackColor = string.IsNullOrWhiteSpace(elem.Control.Text) ? Color.Yellow : Color.White;
}
Wrap your Elem array into class objects to add a "ok" property.
It really depends how deep you want to delve into that rabbit hole...
You need to decide on the validation statuses - if it's simply a case of Yes/No, then Boolean/bool will suffice, otherwise you should consider creating an enumeration to hold your validation statuses.
You will need to decide whether you want to extend the controls that require validation, or just use the control's Tag property to store the validation status (personally I think that using Tag to do this is hideous).
An Example:
// Provides your validation statuses.
public enum ControlValidation
{
Ok,
NotOk
}
// Provides a contract whereby your controls implement a validation property, indicating their status.
public interface IValidationControl
{
ControlValidation ValidationStatus { get; private set; }
}
// An example of the interface implementation...
public class TextBox : System.Windows.Forms.TextBox, IValidationControl
{
public ControlValidation ValidationStatus { get; private set; }
...
protected override void OnTextChanged(EventArgs e)
{
ValidationStatus = ControlValidation.Ok;
}
}
All winforms components have a "spare" property which you can use: Tag. It's an object and you can assign whatever to it: it's not used for anything by the framework, and it's useful for cases like this.
If this is going to be generalized, you can just derive your controls and add your properties, but for a one-time single-property, Tag could perfectly work.
// OK
myTextBox.Tag = true;
// NOK
myTextBox.Tag = false;
// Undefined
myTextBox.Tag = null;
To check:
if(myTextBox.Tag is bool)
{
var isOk = (bool)myTextBox.Tag;
if(isOk)
{
// It's OK
} else {
// It's NOK
}
} else {
// It's undefined
}
All that said, I use Tag for simple things and simple logics. If you plan to have more properties or it's a generalized thing... either use the validation mechanisms explained in the other answers, or derive your controls:
public class MyTextBox : System.Windows.Forms.TextBox
{
public bool ValidationOK { get; set; }
}
And change the controls to MyTextBox (if you already have them, open the designer.cs file and change all instances of System.Windows.Forms.TextBox to <yourNamespace>.MyTextBox), etc.
I have a Windows Phone 8 project that converts values (i.e.: Celsius to Fahrenheit). There are two TextBox UI elements, one of which is read-only. The user can change the first TextBox to input the value to be converted. He can also press a button to "swap" the two TextBoxes so that he can do the reverse conversion. When the user presses the button, the value from the second TextBox goes into the first TextBox (and vice versa). But it's not the user who changed the value, it's the code who did.
I asked around (on IRC) and researched the subject, but I am a beginner and couldn't understand most of what I have found.
I heard that a simple solution would be to use Data Bindings. I researched the subject, and from what I read, Data Bindings can't solve my problem (correct me if I'm wrong).
I also tried to create a subclass of TextBox, hoping that I could hook in some custom event to it and go further in that direction. But I did not understand how to link the custom TextBox to the UI (in XAML). The way I created the subclass is to just create a new class and add TextBox as the parent. I know there is a template in VS to create a new User Control, and I tried it, but I couldn't understand what I was doing (or what I was supposed to do).
So I have two questions: Am I looking at the problem from the right angle? If yes, how do I create a custom TextBox and link it to the UI? If not, how could I solve my problem?
If your question is how to distinguish if the text got changed by the user or by the code then its simple.
Assuming that when the user types something you'd like to perform method A but when the code changes the text you'd like to perform method B:
In both cases you will need to override the TextBox.TextChanged() event handler.
You will also need a flag variable to tell you if the swap button was pressed or not.
The event handler should be something like this:
{
if (swap_pushed)
{
Method_B();
swap_pushed = false;
}
else
{
Method_A();
}
}
And finally your event handler for swap Button.Click() should be like this:
{
swap_pushed = true;
}
How do you remove the dotted line that appears on buttons when they are selected (either via tab or by clicking them)?
This question is for winforms - any help is appreciated.
Edit: I apologize for the duplicate question. I did search for an answer, but I did not know this issue was due to the 'focus' of the button. As I result I was not finding the appropriate answers.
This happens because your Button gains focus. It is possible to remove it but that means giving the focus to something else when your button's focus Enter event is triggered.
private void button1_Enter(object sender, EventArgs e)
{
// give focus to something else
}
The problem with that is that you lose the ability to use the keyboard in order to select the button (using tab).
Moreover, a more correct approach would be to give focus to the last control that had focus instead of passing it fixed one.
have you tried to remove the focus from the button.
just call Focus(); when the button is clicked.
create custom control add ShowFocusCues and build to use
Example
public class button : System.Windows.Forms.Button
{
protected override bool ShowFocusCues
{
get
{
return false;
}
}
}
Look for button border settings.
I do not get this border, if I set the BorderSize to 0 in the FlatAppearance section
From Remove button border on tab c# winforms
You can set the ShowFocusRectangle peoperty to false.
The only answer here that really works without having to hack (moving focus to another control) is Wongsathon Tuntanakan's answer.
I refer to his answer and, as a bit of extra, I've converted his code to VB:
Public Class YourButtonClass
Inherits System.Windows.Forms.Button
Protected Overrides ReadOnly Property ShowFocusCues As Boolean
Get
Return False
End Get
End Property
End Class