cant figure out how to create a get set statement - c#

I try to make a get set statement but it doesn't work. I have no idea why.
Basically I have a form named form1 that contains a combo-box with names and a button.
The button opens a new form form2 that has a text box. I want to be able to add the text in the text box to the combo box in form1. I searched the web a bit and found that in order to do so i need to create get and set statement instead of playing with the privacy of the controls.
The set and get statement doesn't work. It says only assignment call increment decrement and new object can be used as statement.
Much appreciate your help.
public string GuideNameFunc(){
Get { return GuideName.Text; }
set { GuideName.Text = value; }
}

Just remove it () from your code.
public string GuideNameFunc{
get { return GuideName.Text; }
set { GuideName.Text = value; }
}

public string GuideNameField{
get { return GuideName.Text; }
set { GuideName.Text = value; }
}

Related

asp.net DropDownList set SelectedValue NOT working

I have a user-control contains a DropDownList, and this user-control is used inside another one. I am passing the selected value to the drop-down from the outer user control to the inner one, and I am tracking the value passed (during debugging) and the value is passed correctly, but at least the value is NOT set correctly!
Here is the code of the outer user-control
public int SelectedPhoneNumberCountryCode
{
get { return ucRecoveryPhoneNumber.SelectedPhoneNumberCountryCode; }
set { ucRecoveryPhoneNumber.SelectedPhoneNumberCountryCode = value; }
}
And this is the code of the inner user-control (ucRecoveryPhoneNumber)
public int SelectedPhoneNumberCountryCode
{
get { return int.Parse(DdlCountryPhoneCodes.SelectedValue); }
set { DdlCountryPhoneCodes.SelectedValue = value.ToString(); }
}
At the Page_Load, the value is passed correctly until the last step, but it is not set correctly! and the drop-down is always selected as the first value (default one).
Q What is wrong with this code? or What should be done to make it work?

XtraGrd.GetRow(XtraGrd.FocusedRowHandle) doesn't get the value of the focused TextBox

In my WinForm application when I want to add a row in xtragrid, I have a problem with getting the current value of the focused textbox.
Assume I have a textBox bind to Model.VchType.Title , Before I click Save button my focus is on txtTitle and I typed "title1" on it.
This is my code for Save button event:
Model.VchType row = xtraGrd.GetRow(xtraGrd.FocusedRowHandle) as Model.VchType;
I get null for row.Title after it hits the break point in this line of code.
And this problem only occurs when right before I click on save button focus is on txtTitle.
-------- UPDATE ------------
Here is some of code of model:
[System.ComponentModel.DataAnnotations.Schema.Table("vwVchType", Schema = "Sle")]
[Serializable]
public class VchType : Entity
{
private int _ID;
[System.ComponentModel.DataAnnotations.Schema.Column]
[RnDisplayName(typeof(Rnw.Sle.Properties.Resources), "ID")]
public override int ID
{
get
{
return _ID;
}
set
{
_ID = value;
}
}
private string _Title;
[System.ComponentModel.DataAnnotations.Schema.Column]
[RnDisplayName(typeof(Rnw.Sle.Properties.Resources), "Title")]
public string Title
{
get
{
return _Title;
}
set
{
_Title = value;
}
}
}
Also I created columns by designer.
I fill a bindingSource and set the property of the datasource of grid to this bindingsource in designer.
And I don't think problem is column name , because if before I click save button I focus on another controller, It works fine and I get value for row.Title.
You need to call
((GridView)xtraGrid.FocusedView).PostEditor();
or
gridView.PostEditor()this will save the current value to the editor EditValue.
Then you need to call view.UpdateCurrentRow() to validate the focused row and save its values to the data source.
So you need something like this
((GridView)xtraGrid.FocusedView).PostEditor();
((GridView)xtraGrid.FocusedView).UpdateCurrentRow();
Model.VchType row = xtraGrd.GetRow(xtraGrd.FocusedRowHandle) as Model.VchType;
You can focus another forms object before you save your data. So call:
anyControl.Select();
Before you save. This will close the open editor from your Textbox and post the changes to your DataSource. Normally this should be done by PostEditor(); which sometimes seems to lack.

Change Textbox text from another frame(container)

I see here lot of similar question, but I still not find answer that help me in situation.
I have two frame(lets say FrameChild), one is "in" another(practically FrameChild is in this frame, lets say FrameMain).
When I insert all parameters in FrameChild and tap on button witch is on bottom of FrameMain I call method that return string...
Now when i get string i need to change textbox text in FrameChild
I have tray many way.
First idea was something like:
FrameChild frm = new FrameChild;
frm.textbox.text = "somestring";
But nothing happen.
Than i thing use some property.
in FrameChield:
public string setTicNo
{
set
{
textBox.Text = value;
}
}
in FrameMain:
FrameChild frm = new FrameChild;
frm.setTicNo = "somestring";
When i debbuging I get value, but textbox still is empty...
On the end I try to bind textbox text on setTicNo;
public string setTicNo
{
get
{
return setTicNo;
}
set
{
setTicNo = value;
}
}
Xaml:
Text = {Binding setTicNo, Mode=TwoWay,UpdateSourceTrigger=Explicit}
(here i try use more bindings, but every time i get infinite loop.
Please help , I not have more ideas..
Thanx
Did you try building a single view model and bind it to both frames, if it was passed by ref which is the default it will change the value once you do.
A side note implement a INOTIFYPROPERTYCGANGED in the View model

How to assign triggers to variables in C#?

When coding in Visual Basic, I'm able to define triggers to variables like: Whenever the value of the variable is changed, this triggers a function, which in turn changes the value of another variable. I wonder if there is a way to do the same in C#; that is, I want to define a trigger to a variable, fired every time when the value of that variable is changed.
Below code is how I do it in VB. When running this App, whenever a new user logs in, the App assigns the username like ActiveUserName = "SomeUserName", and this in turn automatically updates the FormMain.StatusLabelUserName.Text in the form. So, is there a way to achieve this trigger in C#?
Public Property ActiveUserName() As String
Get
ActiveUserName = FormMain.StatusLabelUserName.Text
End Get
Set(ByVal Value As String)
FormMain.StatusLabelUserName.Text = Value
End Set
End Property
I believe you're looking for Properties
private int localMember;
public int publicProperty
{
get
{
return localMember;
}
set
{
localMember = value;
//Do whatever you want here.
}
}
The "get" block is run any time you access the value. The "set" block is run any time you assign a value.
This is handled by raising events. This post describes how to do it well.
This is a simple property. Here's the C# syntax:
public string ActiveUserName
{
get { return FormMain.StatusLabelUserName.Text; }
set { FormMain.StatusLabelUserName.Text = value; }
}
Properties can work the same in C#
public string ActiveUserName
{
get{ return FormMain.StatusLabelUserName.Text;}
set{FormMain.StatusLabelUserName.Text = value; /*do more stuff here; */}
}
This can get rather messy and somewhat rigid however. You may consider using events to accomplish the same thing.
Use INotifyPropertyChanged event.
Properties and the set brackets.
INotifyPropertyChanged could be implemented for WPF databindings for exemple.

Design time error in form for object with

I have an control that inherits from another control (TxTextControl). I have a SelectedText property that basicaly wraps the base SelectedText property, which is apparently needed because my control is implementing an interface with that property. The code is this:
public string SelectedText
{
get
{
return base.Selection.Text; // Error here (#1042)
}
set
{
if (base.Selection == null)
{
base.Selection = new TXTextControl.Selection(0, 0);
}
base.Selection.Text = value;
}
}
When I drop this control on a form, no problems. It compiles and runs. Everything looks great. However, when I save, close then reopen the form, the form designer shows this error:
Object reference not set to an instance of an object.
1. Hide Call Stack
at Test.FormattedTextBox2.get_SelectedText() in C:\Projects\Test\FormattedTextBox2.cs:line 1042
Anyone know what is going on? I'm about to pull out my last hair...
UPDATE:
darkassisin93's answer wasn't exactly correct, but that was because my posted code wasn't exactly accurate. I needed to test if base.Selection was null before attempting to access a property of that object. In any case, that answer got me headed in the right direction. Here is the actual solution:
public string SelectedText
{
get
{
string selected = string.Empty;
if (base.Selection != null)
{
selected = base.Selection.Text;
}
return selected;
}
set
{
if (base.Selection == null)
{
base.Selection = new TXTextControl.Selection(0, 0);
// Have to check here again..this apparently still
// results in a null in some cases.
if (base.Selection == null) return;
}
base.Selection.Text = value;
}
}
Try replacing
return base.SelectedText;
with
return base.SelectedText ?? string.Empty;
It's most likely because the base class's SelectedText property is set to null.

Categories