I've a devexpress Navigation menu item that opens a data entry form (user control). The user control has validation rules that compel users not to leave textboxs blank. And, it works pretty good so far.
But, the problem comes when I click on other menu items while the data entry user control is already displayed. This time, the screen just freezes and stucks, and I've to restart the system. What are the possible causes and solutions? Thanks in advance
Here are some code snapshots:
//Here is what I've on the main form. It has a panel control called mainPanel to display the user controls
private XtraUserControl uc;
private void MainForm_Load(object sender, EventArgs e)
{
displayUserControl("Data Entry");
//...
}
private void navigationBar_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
{
displayUserControl(e.Link.Caption);
}
private void displayUserControl(string link)
{
switch (link)
{
case "Data Entry":
uc = new ucDataEntry(); //the data entry user control that freezes the system
break;
case "Setting":
uc = new ucSetting();
break;
case "Chart":
uc = new UCReportChart();
break;
}
mainPanel.Controls.Clear();
mainPanel.Controls.Add(uc);
uc.Dock = DockStyle.Fill;
uc.Show();
}
Any time a menu item is clicked, you are clearing the previous control out of your main panel, and replacing it with a new one. Perhaps it is the validation logic in the ucDataEntry control that is causing the application to hang? (You haven't posted code for that control, so I can't be sure.)
As an aside, by calling mainPanel.Controls.Clear(), you are leaking memory. The documentation for this function states that you must explicitly call the Dispose() method for any controls that are cleared in this manner.
Related
I am using a tab in my program to switch between two forms. I put the code required to switch between forms within the tabPage1_Click event, but it doesn't trigger when the tab is clicked.
I attached the code and properties of the tab. Please let me know if any other information is required to know the problem. Thanks.
private void tabPage1_Click(object sender, EventArgs e)
{
this.Hide();
Home form2 = new Home();
form2.ShowDialog();
this.Close();
}
There are 2 things involved here. Tab control and Tab pages. Tab Control is the parent object which has multiple Tab pages in it.
You have event handler for Tab Page which is tabpage1_Click and not for Tab Control.
tabpage1_Click will be triggered when you click on tab page 1(not on the tab page header).
If you need to capture an event when you click on tab page header use Tab Control click event, something like below.
private void tabControl1_Click(object sender, EventArgs e)
{
//Your code goes here
}
To access the properties of the tab page use tabControl1.SelectedTab
I'm developing some app for Windows phone 8.1, and I have this problem:
Have 1 form with some checkbox, radio buttons and some text fields. When I fill it up, and move to next form with:
Frame.Navigate(typeof(SOME_FORM));
and I have some things to do, and want to come back to 1st form, it's all cleared. How to remember what i fill up on 1st form, when i come back from 2nd?
And I add all controls programmatically, so I can't save it in static variable.
Any help?
In your first page set property NavigationCacheMode to Required
in XAML
NavigationCacheMode="Required"
or in code behind
this.NavigationCacheMode = NavigationCacheMode.Required;
But the question is: when do you add controls to page? if you add control in OnNavigatedTo you should check NavigationMode and don't reload page on back navigation
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if (e.NavigationMode == NavigationMode.Back)
{
//do nothing - filled controls already there
return;
}
//add controls
}
So in the beginning, the user has to input data but in the form. It has textboxes and labels where the answers to the calculations go. I have it so that when it clicks, it computes certain labels and textboxes show up and when the user clicks reset they disappear using:
txtTaxesPaid.Visible = true;
txtTaxesPaid.Visible = false;
My problem is that in the beginning the ones that are not visible. When I click reset they show up. How do I make them not visible in the very beginning?
You can set Textboxes Visible property from Properties window in the designer.Or you can do it in Form Load event programatically:
private void Form1_Load(object sender, EventArgs e)
{
txtTaxesPaid.Visible = false;
}
If you don't see Form Load method just double click your Form in the designer and you will see, or find Load event from Properties > Events window.
Option 1: (shortest and most straight forward)
In VS designer of your form - locate those controls and set the visible property to false.
Option 2:
In your form_load event you can set the initial state of your controls.
Option 3:
You can force to invoke the method of the button which already contain the reset commands from your form_load event.
Option 4: (I think is the preferable choice)
- Create a method for reset..
- call the method from form_load or from your "reset" button or anywhere else you want.
private void ResetControls()
{
txtMyControl.Visible=false;
//here comes more logic for what to do upon reset.
}
private void form_load(...
{
ResetControls();
}
private void btnReset_Click(...
{
ResetControls();
}
I'm trying to make a card game using Windows Application Forms.
The thing is that I don't know how to do the following- for example if i'd have 3 buttons- one of them named, for example, "Play", if i'd click on it, it would open the actual game, but in the same window, it would only make the buttons dissapear, and when i'd click back, it would open the window with buttons again. I don't really know how to explain my problem better, hopefully someone can tell me how to do that.
You don't have to hide / show the buttons. What you can do instead is to make a new form with the cards on it. That Form will pop up after you click the play button.
private void PlayButton_Click(object sender, EventArgs e)
{
// You other functionality goes here
GameForm GF = new GameForm();
GF.Show();
//Or - try this and see the difference
GF.ShowDialog();
}
Good Luck!
In addition to Leez's answer, in your situation, you should think about using container controls rather than handling the visible states of individual controls.
You could put related controls in a Panel, GroupBox or TabControl and set the visible properties of those containers instead.
you can use Visible property of button to do that as follows.
private void button1_Click(object sender, EventArgs e)
{
// You other functionality goes here
button1.Visible = false;
}
I'm working on a windows forms application (C#) where a user is entering data in a form. At any point while editing the data in the form the user can click one of the buttons on the form to perform certain actions. By default the focus goes to the clicked button so the user has to click back on to the control they want to edit in order to continue modifying the data on the form. What I need to be able to do is return the focus to the last edited control after the button click event has been processed. Here's a sample screenshot that illustrates what I'm talking about:
The user can be entering data in textbox1, textbox2, textbox3, etc and click the button. I need the button to return the focus back to the control that most recently had the focus before the button was clicked.
I'm wondering if anyone has a better way of implementing this functionality than what I've come up with. Here's what I'm doing right now:
public partial class Form1 : Form
{
Control _lastEnteredControl;
private void textBox_Enter(object sender, EventArgs e)
{
_lastEnteredControl = (Control)sender;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("Do something here");
_lastEnteredControl.Focus();
}
}
So basically what we have here is a class variable that points to the last entered control. Each textbox on the form is setup so the textBox_Enter method is fired when the control receives the focus. Then, when the button is clicked focus is returned to the control that had the focus before the button was clicked. Anybody have any more elegant solutions for this?
For a bit of 'simplicity' maybe try.
public Form1()
{
InitializeComponent();
foreach (Control ctrl in Controls)
{
if (ctrl is TextBox)
{
ctrl.Enter += delegate(object sender, EventArgs e)
{
_lastEnteredControl = (Control)sender;
};
}
}
}
then you don't have to worry about decorating each textbox manually (or forgetting about one too).
You could do the following
Change the button to a label and make it look like a button. The label will never get focus and you don't have to do all the extra coding.
I think what you're doing is fine. The only thing I could think of to improve it would be to store each control into a stack as they are accessed. That would give you a complete time line of what was accessed.
Your approach looks good. If you want to avoid having to add an the event handler to every control you add, you could create a recursive routine to add a GotFocus listener to every control in your form. This will work for any type of control in your form, however you could adjust it to meet your needs.
private void Form_OnLoad(object obj, EventArgs e)
{
AddGotFocusListener(this);
}
private void AddGotFocusListener(Control ctrl)
{
foreach(Control c in ctrl.Controls)
{
c.GotFocus += new EventHandler(Control_GotFocus);
if(c.Controls.Count > 0)
{
AddGotFocusListener(c);
}
}
}
private void Control_GotFocus(object obj, EventArgs e)
{
// Set focused control here
}
Your implementation looks good enough -- what I do want to know is why you want to do this in the first place? Won't it be preferrable for the focus to cycle back to the first entry? Is the data in the last text box so malleable that once they click the button it is "remembered"? Or do you have some sort of operation that the button does to that specifici text box data -- in that case shouldn't the focus go to a subsequent control instead?
I'm interested in finding out why you want to do this in the first place.
Yeah, I admit the requirement is a bit unusual. Some of the information that the users will be entering into this application exists in scans of old documents that are in a couple of different repositories. The buttons facilitate finding and opening these old docs. It's difficult to predict where the users will be on the form when they decide to pull up a document with more information to enter on the form. The intent is to make the UI flow well in spite of these funky circumstances.
Create a class called CustomTextBox that inherits from TextBox. It has a static variable called stack. When the textbox loses focus push onto the stack. When you want to find the last focused control then just pop the first item from the stack. Make sure to clear the static Stack variable.