I'm having a problem extending the standard WebControls.Button control. I need to override the text property, but I receive the error message:
cannot override inhereted member 'System.Web.UI.WebControls.Button.Text.get' because it is not marked virtual, abstract or override
I used the following code for a LinkButton, and that worked perfectly:
public class IconLinkButton : LinkButton
{
private string _icon = "";
public string Icon
{
get
{
return _icon;
}
set
{
_icon = value;
}
}
public override string Text
{
get
{
return "<i class=\""+Icon+"\"></i> " + base.Text;
}
set
{
base.Text = value;
}
}
}
However, doing the same thing for a standard Button kicks up the error I described above.
public class IconButton : Button
{
private string _icon = "";
public string Icon
{
get
{
return _icon;
}
set
{
_icon = value;
}
}
public virtual string Text
{
get
{
return "<i class=\"" + Icon + "\"></i> " + base.Text;
}
set
{
base.Text = value;
}
}
}
How can I fix this?
This is because LinkButton has a virtual Text property.. whilst Button does not.
You can hide the base functionality entirely by using new:
public class IconButton : Button {
public new string Text {
// implementation
}
}
Using new hides the inherited member completely.
Related
This is the XAML of the radio. Nothing else is editing this. Once this is set it is not changing. But somehow no matter what it is setting the XML to "false".
Here is how I save the XML file (works just fine).
There are 3 radio buttons, as you can see, that I am trying to get set to false or true but they all just get saved as false.
<RadioButton x:Name="sx80" Content="Cisco SX80" HorizontalAlignment="Left" Margin="701,244,0,0" VerticalAlignment="Top" GroupName="codecType" TabIndex="17" FontWeight="Normal" Height="25" Width="95" Padding="0,2"/>
class SaveXml
{
public static void savedata(object obj, string filename)
{
XmlSerializer sr = new XmlSerializer(obj.GetType());
TextWriter writer = new StreamWriter(filename);
sr.Serialize(writer, obj);
writer.Close();
}
}
Here is the main class that tells it what information we are saving to the XML file.
public class information
{
private string city;
private string chairCount;
private string stateSelect;
private string HostNameIPTyped;
private string VTCmac;
private string vtcUser;
private string vtcPass;
private string VTCserial;
private string AssetTag;
private string SIPURI;
private string SystemName;
private string firstName;
private string lastName;
private string contactPhone;
private string provisionerName;
private string provisionerInitials;
private string provisionDate;
private bool sx80;
private bool codecPlus;
private bool codecPro;
public string postcity
{
get { return city; }
set { city = value; }
}
public string postchairCount
{
get { return chairCount; }
set { chairCount = value; }
}
public string poststateSelect
{
get { return stateSelect; }
set { stateSelect = value; }
}
public string postHostNameIPTyped
{
get { return HostNameIPTyped; }
set { HostNameIPTyped = value; }
}
public string postVTCmac
{
get { return VTCmac; }
set { VTCmac = value; }
}
public string postvtcUser
{
get { return vtcUser; }
set { vtcUser = value; }
}
public string postvtcPass
{
get { return vtcPass; }
set { vtcPass = value; }
}
{ e164 = value; }
}
public string postVTCserial
{
get { return VTCserial; }
set { VTCserial = value; }
}
public string postAssetTag
{
get { return AssetTag; }
set { AssetTag = value; }
}
public string postSIPURI
{
get { return SIPURI; }
set { SIPURI = value; }
}
public string postSystemName
{
get { return SystemName; }
set { SystemName = value; }
}
public string postfirstName
{
get { return firstName; }
set { firstName = value; }
}
public string postlastName
{
get { return lastName; }
set { lastName = value; }
}
public string postcontactPhone
{
get { return contactPhone; }
set { contactPhone = value; }
}
public string postprovisionerName
{
get { return provisionerName; }
set { provisionerName = value; }
}
public string postprovisionerInitials
{
get { return provisionerInitials; }
set { provisionerInitials = value; }
}
public string postprovisionDate
{
get { return provisionDate; }
set { provisionDate = value; }
}
public bool postsx80
{
get { return sx80; }
set { sx80 = value; }
}
public bool postcodecPlus
{
get { return codecPlus; }
set { codecPlus = value; }
}
public bool postcodecPro
{
get { return codecPro; }
set { codecPro = value; }
}
}
The code you posted doesn't show any data binding on the RadioButton or how you've set your DataContext. But you said in the comments that the strings are working so I assume you've set the DataContext somewhere. If you can update your question to show how your Window/View is bound to the information object it will be easier to give you a more accurate solution. You also said the following in one of your comments:
Yes, it is actually being saved as false. If it didn't find a value it would just show nothing. :-) <postsx80>false</postsx80>
The default value for a bool is actually false, so even if no value is retrieved from your RadioButton, your XML file will still show false.
Your RadioButton's would normally be bound like this, depending on how your DataContext is set. Notice the Binding in the IsChecked property. The Mode=TwoWay means that the UI can set the value of the property and not just read it:
<RadioButton x:Name="sx80" Content="Cisco SX80" IsChecked="{Binding Info.postsx80, Mode=TwoWay}" />
In the code behind of this Window I have created a public property called Info which contains an instance of your information class. The RadioButton above is bound the the postsx80 property of this information instance so you would need to pass this instance to your savedata method like below.
public partial class MainWindow : Window
{
public information Info { get; set; } = new information(); // The UI is bound to this instance
public MainWindow()
{
InitializeComponent();
this.DataContext = this; // I've set the Window's DataContext to itself
}
private void Button_Click(object sender, RoutedEventArgs e)
{
SaveXml.savedata(Info, "somefile.xml");
}
}
You should also implement INotifyPropertyChanged which will notify the UI when a property's value has changed. For example your information class could look like this:
// You will need to add the following namespaces
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace YourAppsNamespace
{
public class information : INotifyPropertyChanged // Implement the INotifyPropertyChanged interface
{
private bool sx80;
public bool postsx80
{
get { return sx80; }
set {
sx80 = value;
OnPropertyChanged(); // Notify the UI that this property's value has changed
}
}
// This code raises the event to notify the UI which property has changed
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string name = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));
}
}
}
You would need to add OnPropertyChanged() to the setters of all of your properties.
You also mentioned in the comments that you don't know how to use auto properties. An auto property is basically a shorter way to write a property when there are no additional actions which need to be performed when getting or setting a value. For example, this:
private bool someBool;
public bool SomeBool
{
get { return someBool; }
set { someBool = value; }
}
Would just become:
public bool SomeBool { get; set; }
There is no need to create the private variable or define the body of the getter and setter. This is handled automatically for you. This is only suitable if you don't need to perform any additional actions in the getter or setter. So in my example above where we need to call OnPropertyNotifyChanged() in the setter, you wouldn't be able to use an auto property.
An additional tip is that you can simply type prop in Visual Studio and press Tab twice to insert an auto property without having to type it out yourself. You then simply change the data type, press Tab again to move to the name and change that. The same can be done for a full property like the ones you wrote by typing propfull.
I would like to have a control that allows a property to be shown if another property's value is set to a specific value. The following is a much simplified example of what I would like:
public class CustomButton : Control
{
private ButtonType _bType = ButtonType.OnOff;
private Int32 _minPress = 50; // 50 mS
public ButtonType Button_Type
{
get { return _bType; }
set { _bType = value; }
}
public Int32 Minimum_Press_Time // Only for momentary buttons
{
get { return _minPress; }
set { _minPress = value; }
}
}
public enum ButtonType
{
Momentary,
OnOff
}
On adding CustomButton to a Windows.Forms form, the Minimum_Press_Time will only show in the Properties window if Button_Type is changed to ButtonType.Momentary.
Is such a thing possible?
Yes, its possible to get close but it looks a little strange. I've done this on some controls before. Here is a full example of what you would need to do:
public partial class CustomButton : Control
{
private ButtonType _buttonType = ButtonType.OnOff;
private CustomButtonOptions _options = new OnOffButtonOptions();
[RefreshProperties(System.ComponentModel.RefreshProperties.All)]
public ButtonType ButtonType
{
get { return _buttonType; }
set
{
switch (value)
{
case DynamicPropertiesTest.ButtonType.Momentary:
_options = new MomentaryButtonOptions();
break;
default:
_options = new OnOffButtonOptions();
break;
}
_buttonType = value;
}
}
[TypeConverter(typeof(ExpandableObjectConverter))]
public CustomButtonOptions ButtonOptions
{
get { return _options; }
set { _options = value; }
}
public CustomButton()
{
InitializeComponent();
}
}
public enum ButtonType
{
Momentary,
OnOff
}
public abstract class CustomButtonOptions
{
}
public class MomentaryButtonOptions : CustomButtonOptions
{
public int Minimum_Press_Time { get; set; }
public override string ToString()
{
return Minimum_Press_Time.ToString();
}
}
public class OnOffButtonOptions : CustomButtonOptions
{
public override string ToString()
{
return "No Options";
}
}
So basically what is happening is you are using an ExpandableObjectConverter to convert an abstract type to a set of options. You then use the RefreshProperties attribute to tell the property grid that it will need to refresh the properties after this property changes.
This is the easiest way I've found to come as close to what you are asking for as possible. The property grid doesn't always refresh the right way so sometimes there will be a "+" sign next to an options set with no expandable properties. Use the "ToString" in the properties to make the display on the property grid look intelligent.
I'm attempting to override Form.Text in order to modify the Title prior to appearing on the form.
As a proof of concept I created this class, which will be used in place of directly inheriting from Form:
public class FormWithVersionNumber : Form
{
[SettingsBindable(true)]
public override string Text
{
get
{
return "tester";
}
}
}
I would've expected all forms that inherit from this to have the Title "tester" but instead it is always blank. I've been through with breakpoints, and can't see any reason why this should happen. So what is the reason?
Because the actual Title is not retreived from Text but from the internal property WindowText in Control.
Here's an example of how you can do:
public partial class FormWithVersionNumber : Form
{
public override sealed string Text
{
get
{
return base.Text + " 1.0.0.0";
}
set
{
base.Text = value + " 1.0.0.0";
}
}
public FormWithVersionNumber()
{
InitializeComponent();
Text = "Some Title";
}
}
I need some help here.
I've created a child class called MyEditorRow from DevExpress EditorRow, and added 3 properties
public class myEditorRow : EditorRow
{
public myEditorRow()
{
}
private string inRowDescription = null;
public string RowDescription
{
get { return inRowDescription; }
set { inRowDescription = value; }
}
private bool inRequired = false;
public bool Required
{
get { return inRequired; }
set { inRequired = value; }
}
private bool inInherits = false;
public bool Inherits
{
get { return inInherits; }
set { inInherits = value; }
}
Second part of the code somewhere in the program adds instance of MyEditorRow to DevExpress VGrid Control.
vgcGrid.Rows.Add(Row);
My question is this: How can I link MyEditorRow class with DevExpress VGrid Control FocusedRowChanged event, so I can get my custom properties when row focus changes.
Thanks
The e.Row parameter is of the BaseRow type. So, to obtain an instance of the MyEditorRow object in the FocusnedRowChanged event handler, use the following code:
private void vGridControl1_FocusedRowChanged(object sender, DevExpress.XtraVerticalGrid.Events.FocusedRowChangedEventArgs e) {
if(e.Row is myEditorRow) {
myEditorRow row = ((myEditorRow)e.Row);
// your code here
}
}
I am trying to hide a property from Intellisense for the Text property of the TextBox control.
I tried the following, but I get a compile error complaining that the Text property is not set as virtual in the base class. I am not trying to remove the property, just hide it from Intellisense. Any ideas?
public class MyTextBox:TextBox
{
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
public override string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
}
}
}
YOu can use new keyword to hide it if its not virtual.
Like this:
public class MyTextBox:TextBox
{
[EditorBrowsable(EditorBrowsableState.Never)]
[Browsable(false)]
public new string Text
{
get
{
return base.Text;
}
set
{
base.Text = value;
}
}
}