DevExpress Set XtraTab Selected Page - c#

I was trying to set the XtraTab Selected page in the constructor it self as follows,
public frmInquiryManagement()
{
InitializeComponent();
tabInquiryManagement.SelectedTabPage = xtraTabPage3;
}
But it doesn't work that way. Please help me to do this.

XtraTabControl does not provide this method to directly set a selected page. What i see is, to implement this method, we need to implement a loop internally.
for(int i = 0; i < xtraTabControl1.TabPages.Count; i ++)
if(xtraTabControl1.TabPages[i].Name == "someName"){
xtraTabControl1.SelectedTabPage = xtraTabControl1.TabPages[i];
break;
}
Else this is one of the clear solutions :
tabpage1.Show();
tabpage1.pageVisible=true;
xtraTabControl1.tabPages[0].selected=true;
Hope it helps.
Cheers.

If you want to achieve that you must use PageEnabled property from XtraTabPage component.
xtraTabPage3.PageEnabled = true;

private void Form1_Load(object sender, EventArgs e)
{
SelectTabByTitle("xtraTabPage3",xtraTabControl1);
}
private void SelectTabByTitle(String tabTitle, XtraTabControl tabControl)
{
if (tabControl != null)
{
XtraTabPage pageToSelect = (from curPage in tabControl.TabPages
where curPage.Text == tabTitle
select curPage).FirstOrDefault();
if (pageToSelect != null)
{
tabControl.SelectedTabPage = pageToSelect;
}
}
}

xtraTabControl1.SelectedTabPageIndex = 0;, where 0 is the index of the page you wish to display.

Your code should normally work, but it seems that the problem is that the Form has not loaded yet and the Tabs are not visible yet. Try setting the SelectedTabPage on the Load event, or the Shown event

Use the following code in the Form's Load event handler:
tabInquiryManagement.SelectedTabPage = xtraTabPage3;
tabInquiryManagement.MakePageVisible(xtraTabPage3);

Related

TabControl AddingTab event

I have a TabControl in which I want to prevent adding existing TabPage (they are identified by a name) and instead set the SelectedTabPage to this precise tab.
I wish to know if there are an event that triggers right before a page is being added to the TabControl. If not, would using the event CollectionChanged of the TabPages (list) be a correct alternative ?
I believe the event you're looking for is the Control.ControlAdded event:
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.controladded.aspx
If that also detects when things inside the tab pages themselves are added, you should be able to filter out everything but TabPage controls using the ControlEventArgs.Control property in your event handler.
To reject adding a control will be a little more complicated. Since this event seems to only be raised after the control gets added, you'll need to do something like this:
void onControlAdded(object sender, ControlEventArgs e) {
var tab = e as TabPage;
if (tab == null)
return;
this.myTabControlObject.TabPages.Remove(tab);
}
This should remove the tab, but it will likely slow the tab adding process considerably.
Try something like this, I am checking the TabControl page Collection for a page with the same name as the Page that is trying to be added, if it exists I am setting focus to the existing instance, otherwise adding the new page to the TabControl. See if something like this works for you.
private void button1_Click(object sender, EventArgs e)
{
TabPage tp = new TabPage();
tp.Name = tabPage1.Name;
var temp =tabControl1.Controls.Find(tp.Name,true);
if( temp.Length > 0)
{
tabControl1.SelectedTab = (TabPage) temp[0];
}
else
tabControl1.Controls.Add(tp);
}
Anything having to do with the ControlCollection will most likely be triggered after the control has been added.
From above link:
You can determine if a Control is a member of the collection by passing the control into the Contains method. To get the index value of the location of a Control in the collection, pass the control into the IndexOf method. The collection can be copied into an array by calling the CopyTo method.
If you want you could cleanup your code some by adding an ExtensionMethod to your TabControl Check for an existing page, set focus or add from there.
Example:
namespace ExtensionMethods
{
public static class MyExtensions
{
public static bool AddPage(this TabControl tc, TabPage tp)
{
var matchedPages = tc.Controls.Find(tp.Name, false);
if ( matchedPages.Length > 0)
{
tc.SelectedTab = (TabPage)matchedPages[0];
return true;
}
else
{
tc.TabPages.Add(tp);
tc.SelectedTab = tp;
return false;
}
}
}
}
Usage:
tabControl1.AddPage(tp);

LookUpEdit not selecting newly entered value when it's a double

I have 2 LookUpEdit controls from DevExpress on my form. Both use an ObservableCollection as it's datasource, one being of type string and the other of type double. The LookUpEdit control has an event called ProcessNewValue which fires when, you guessed it, a new value is entered in the control. I've added some code in this event to add the newly added value to the ObservableCollection and it automatically selects it once done. This works as expected for the string LooUpEdit but when I try it with the double LookUpEdit`, it adds it to the collection but then it clears out the control.
Here's the code to load the controls, which gets called in Form_Load():
void InitControls()
{
double[] issueNumbers = new double[5];
issueNumbers[0] = 155;
issueNumbers[1] = 156;
issueNumbers[2] = 157;
issueNumbers[3] = 158;
issueNumbers[4] = 159;
ObservableCollection<double> issues = new ObservableCollection<double>(issueNumbers);
lookupIssues.Properties.DataSource = issues;
DevExpress.XtraEditors.Controls.LookUpColumnInfoCollection colInfo = lookupIssues.Properties.Columns;
colInfo.Clear();
colInfo.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Column"));
colInfo[0].Caption = "Issue ID's";
string[] stringNumbers = Array.ConvertAll<double, string>(issueNumbers, Convert.ToString);
ObservableCollection<string> issuesString = new ObservableCollection<string>(stringNumbers);
lookupStringValue.Properties.DataSource = issuesString;
colInfo.Clear();
colInfo.Add(new DevExpress.XtraEditors.Controls.LookUpColumnInfo("Column"));
colInfo[0].Caption = "String Issue ID's";
}
And here's the ProcessNewValue event for both (I've renamed them to try to make it easier to see which does what):
private void OnProcessNewValue_Double(object sender, DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs e)
{
ObservableCollection<double> source = (ObservableCollection<double>)(sender as LookUpEdit).Properties.DataSource;
if (source != null)
{
if ((sender as LookUpEdit).Text.Length > 0)
{
source.Add(Convert.ToDouble((sender as LookUpEdit).Text));
(sender as LookUpEdit).Refresh();
}
}
e.Handled = true;
}
private void OnProcessNewValue_String(object sender, DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs e)
{
ObservableCollection<string> source = (ObservableCollection<string>)(sender as LookUpEdit).Properties.DataSource;
if (source != null)
{
if ((sender as LookUpEdit).Text.Length > 0)
{
source.Add((sender as LookUpEdit).Text);
(sender as LookUpEdit).Refresh();
}
}
e.Handled = true;
}
As you can see, the code it identical with the exception of one converting text to a double before adding it to the collection.
Anyone know why the double value gets added to the collection but the control doesn't automatically select it like it does with a string collection? I've even tried to hard-code the newly added value right after e.Handled = true; but it still doesn't select it. What's weird is that if I run it through the debugger, I can step through and see that the lookupIssues control indeed gets the newly added value AND it's Text property is set to it, but as soon as the event terminates, the control clears it out.....really strange.
Any help is greatly appreciated!
BTW, I can add a link to a sample project that duplicates the problem but you would need to have DevExpress v12.2.6 controls installed in order to compile the project.
I posted this to the DevExpress team as well and they were gracious enough to provide the solution:
I agree that this discrepancy appears confusing as-is. The reason for the discrepancy is LookUpEdit.ProcessNewValueCore makes a call to RepositoryItemLookUpEdit.GetKeyValueByDisplayValue which returns a null value from the LookUpListDataAdapter because no implicit conversion exists from double to string. You may resolve the discrepancy with the following change to your ProcessNewValue handler:
private void OnProcessNewValue_Double(object sender, DevExpress.XtraEditors.Controls.ProcessNewValueEventArgs e)
{
ObservableCollection<double> source = (ObservableCollection<double>)(sender as LookUpEdit).Properties.DataSource;
if (source != null) {
if ((sender as LookUpEdit).Text.Length > 0) {
double val = Convert.ToDouble((sender as LookUpEdit).Text);
source.Add(val);
e.DisplayValue = val;
(sender as LookUpEdit).Refresh();
}
}
e.Handled = true;
}
The control now behaves as expected. I hope this can help someone else out :)

XtraReports dual label binding

I am trying to do a simple conditional statement binding in XtraReports. I have my main reports bound to my dataset, my fields (GoalAmount, GoalName, GoalNumber, GoalStart, GoalEnd).
Now GoalNumber or GoalAmount are populated. It's always one or another. So I want to do something like -
Private void Detail_BeforePrint(object sender, PrintEventArgs e) {
if ([GoalNumber] != null) {
xrLabelGoal.Text = [GoalNumber].ToString()
}
else {
xrLabelGoal.Text = [GoalAmount].ToString()
}
xrCWPerct.Text = Convert.ToString(Convert.ToInt32(xrLabelGoal.Text)/Convert.ToInt32(xrLabelCurrentValue.Text);
}
Thanks for the help.
I found the answer -
xrLabelGoal.Text = ((DataRowView)GetCurrentRow()).Row["goalnumber"].ToString();
Turns out I was missing the System.Data which allowed me to use the DataRowView. This fixed it.

Windows Forms: How to extend a button?

How can I extend a Button?
I want a Button that has an additional property IsSwitchOffable.
Do I have to write an extra custom control?
EDIT:
I want that the button is usable as a standard WindowsFormsButton.
This includes that I can add the button at design time!
Extending a button control is no different then extending any class in C#. Simply do the following:
class ToggleButton : Button {
// Add your extra code
}
You need to create a class that inherits the System.Windows.Forms.Button class and adds your property and associated behavior.
After compiling your project, your new class will appear in the toolbox.
I know this is old and has been answered - however,
Why make life difficult?
Each control has a Tag property which you can easily set to IsSwitchedOffable - or better English CanBeDisabled
Far easier.
In my puzzle application 2d button's location is to be changed... So i need extra facilities...
My button ButObj extends Button class
Public Class ButObj : Button
{
Point initloc;
Public ButObj(Point loc)
{ this.Location=initloc=loc ; }
Public bool isNearto(ButObj X)
{
if (this.Location.X==X.Location.X || this.Location.Y==X.Location.Y)
return true;
else return false;
}
Public bool isSettled()
{
if(this.Location==initloc)
return true ;
else return false;
}
Public void Replace (ButObj X)
{
Point temp ;
temp=this.Location;
this.Location=X.Location;
X.Location=temp;
}
}
Following code is written in form 1_load ()
ButObj[ ][ ] B=new ButObj[4][4];
char c='A';
for (int i=0;i<4;i++)
for (int j=0;j<4;j++)
{ B[i][j]=new ButObj(new Point (i*100+10,j*100+10));
B[j][i].Text = ""+c++;
B[i][j].Font =new Font ("Arial", 24);
this.Controls.Add (B[i][j]);
B[i][j].MouseClick += new MouseEventHandler(MouseClick); }
Coding in mouse click event
private void MouseClick(Object sender, EventArgs e)
{
ButObj b=(ButObj)sender;
if (b.isNearto(B[3][3]))
b.Replace(B[3][3]);
\\ checking after replace
if(AllSolved());\\game over
}
bool AllSolved()
{
for (int i=0;i<4;i++)
for (int j=0;j<4;j++)
if (!B[i][j].isSettled)
return false ;
return true;
}

Persisting User Input for Dynamic Control in SharePoint Web Part

Edit at bottom with solution
I've seen a similar question to this posted before and have tried the suggestions, but I must be missing something. My basic problem is this: I have a select box where the user can select a filter which may or may not have constraints built into it (requires the user to input further data so the filter knows how to filter). Since it's unknown just how many constraints will exist for the filter, I'm trying to load them in dynamically and add them to a placeholder panel that I have. The correct number of constraints load just fine and dandy, but when the user inputs text and hits submit, after the page reloads none of the values persist. Here's the appropriate code (I can provide more if needed):
I have these as class variables for my Web Part:
Panel constraintPanel;
HtmlInputText[] constraints;
Label[] constraintLabels = null;
Inside an override CreateChildControls I initialize the panel:
constraintPanel = new Panel();
I build in the dynamic input boxes in an overridden OnPreRender (Note: I've heard people say to do it in OnInit, OnLoad, or OnPreRender, but OnPreRender is the only one that doesn't make the whole Web Part error out):
protected override void OnPreRender(EventArgs e)
{
buildConstraints();
base.OnPreRender(e);
}
private void buildConstraints()
{
if (!viewSelect.SelectedValue.Equals(INConstants.NoFilterOption))
{
string[,] constraintList = docManager.GetFilterConstraints(viewFilterSelect.SelectedValue);
if (constraintList != null)
{
this.constraints = new HtmlInputText[constraintList.Length / 2];
this.constraintLabels = new Label[constraintList.Length / 2];
for (int constraintCount = 0; constraintCount < constraintList.Length / 2; constraintCount++)
{
Label constraintLabel = new Label();
constraintPanel.Controls.Add(constraintLabel);
constraintLabel.Text = constraintList[constraintCount, 0];
this.constraintLabels[constraintCount] = constraintLabel;
HtmlInputText constraint = new HtmlInputText();
constraintPanel.Controls.Add(constraint);
constraint.ID = "constraint_" + constraintCount;
constraint.MaxLength = 12;
constraint.Style.Add("FONT-FAMILY", "Verdana");
constraint.Style.Add("FONT-SIZE", "11");
this.constraints[constraintCount] = constraint;
}
}
}
}
And then finally inside an overridden RenderWebParts I have (note: I've also tried looping through the arrays constraints and constraintLabels to render the controls, but it made no difference):
...
constraintPanel.RenderBeginTag(output); // not sure if this is needed
if (constraints != null && constraints.Length > 0)
{
foreach (Control tempControl in constraintPanel.Controls)
{
if (tempControl is Label)
{
output.WriteLine("<tr>");
output.WriteLine("<td width='2%' nowrap><font class='search-header'>");
tempControl.RenderControl(output);
output.WriteLine(" ");
}
else if (tempControl is HtmlInputText)
{
tempControl.RenderControl(output);
output.WriteLine("</td>");
output.WriteLine("<td width='*' nowrap></td>");
output.WriteLine("</tr>");
}
}
}
constraintPanel.RenderEndTag(output); // not sure if this is needed
...
I appreciate any help, as this is truly driving me crazy.
Edit with solution:
I've been able to get it working. I needed to override the OnLoad event and wrap my calls from there in a try-catch block. For some reason the initial page load throws an exception when trying to run, which causes the entire page to not display. I also forgot to add my constraintPanel to the Controls list.
Here's the code in OnLoad for information's sake:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
try
{
viewsBuildConstraints();
}
catch (Exception)
{
}
}
Try marking your webpart with the INamingContainer interface and make sure to give all controls an ID. Furthermore, HtmlInput COntrols do not have a viewstate i believe, which would cause them to "forget" the input after a postback. Could you try using actual TextBox controls?

Categories