I need show droppeddown combobox after start program.
I need in dropdown style only, not simple style.
This is simple fragment of my program:
private void Form1_Shown(object sender, EventArgs e)
{
CB1.Items.Add("1");
CB1.DropDownStyle = ComboBoxStyle.DropDown;
CB1.DroppedDown = true;
}
But I found the watch sign as cursor till I click on Form in any place.
I guessed that my Form have not fully active state and wait for something.
When I click Form (or combobox or any control) by LBM, it activated fully and all works fine.
Of course the combobox is dropup then, so I need click combobox twice.
Đ•ell me please what is correct initialization of such style combobox without "Cursor = Cursors.Default;"
You can simply wait until cursor is the default:
while (Cursor.Current != Cursors.Default)
{
Application.DoEvents();
}
CB1.Items.Add("1");
CB1.DropDownStyle = ComboBoxStyle.DropDown;
CB1.DroppedDown = true;
Application.DoEvents simply process messages from the window queue, so you can process message until you get that cursor is the default. In that moment, you can drop down your control without problem.
If you prefer, create a extension method for the Form:
public static class FormExtends
{
public static void WaitToDefaultCursor(this Form form)
{
while (Cursor.Current != Cursors.Default)
{
Application.DoEvents();
}
}
}
And use it:
this.WaitToDefaultCursor();
CB1.Items.Add("1");
CB1.DropDownStyle = ComboBoxStyle.DropDown;
CB1.DroppedDown = true;
NOTE: I use Cursor.Default but not to change the cursor. The form is processing messages and it's difficult to select a good moment to drop down the control.
Related
I'm using WinForm to do my little CRM.
I got MainWindow form and on this form 3 panels ( 1 Top/ 1Left/ the other part is filled up by 7 Control User )
All the Control User are on top of each other, and when i click on some button( attached to the left panel) the called CU is brought to the front.
public void BtnContact_Click(object sender, EventArgs e)
{
contactControl1.Visible = true;
contactControl1.BringToFront();
panelBar.Height = BtnContact.Height;
panelBar.Top = BtnContact.Top;
employe1.Visible = false;
comptaControl1.Visible = false;
histoControl1.Visible = false;
alerteControl1.Visible = false;
voyageursControl1.Visible = false;
parametresControl1.Visible = false;
}
I don't want all the CU to load at the start of the App, but i want them to be launch when i click on the button on the left. And let say if i opened one and now opening a new one it close the one who was opened.
If i have no choice to open everything ( which i doubt ) how can i choose the one i want to open first or second etc ??
Thank you
I don't want all the CU to load at the start of the App
Use properties tab and make visible=false or use formload event to prevent all cu loading at the start.
private void Form1_Load(object sender, System.EventArgs e)
{
//use code to make anything visible or invisible
}
you can put all controls that should be shown or hidden on the panel.
This is called the other part is filled up by 7 Control User in the question, so just use Panel to store all those controls.
Then you can introduce new field on your form to store current control index that is shown and to write button click that will check the index and activate next control
int currentIndex = -1;
public void BtnContact_Click(object sender, EventArgs e)
{
currentIndex++;
if (currentIndex >= panelFor7Control.Controls.Count)
currentIndex = 0;
foreach (Control c in panelFor7Control.Controls)
c.Visible = false;
Control control2show = panelFor7Control.Controls[currentIndex] ;
control2show.Visible = true;
}
Button BtnContact has to be pressed to activate next control on the panel.
PS I suppose that you are trying to load data for the visible control only. You can use Control.VisibleChanged to check is data loaded and to load if control is visible and data was not loaded.
First you need to make instance of your UC, something like this:
public partial class YourUC : UserControl
{
public static YourUC _instance;
public static YourUC Instance
{
get
{
if (_instance == null)
_instance = new YourUC();
return _instance;
}
}
}
Then you need to call and show UC when button is clicked
private void btn_Click(object sender, EventArgs e)
{
if (!pnlControls.Controls.Contains(YourUC.Instance))
{
pnlControls.Controls.Add(YourUC.Instance);
YourUC.Instance.Dock = DockStyle.Fill;
YourUC.Instance.BringToFront();
}
else
{
YourUC.Instance.BringToFront();
}
}
You it sounds like you want to have a "pages" navigation, I'm not use to work with WinForm, but perhaps this post could help you
What is the most efficient way to create a Win form with multiple pages?
It seems that you have to manage what Control you want to display manually.
Note that passing Controls from Visible to Hidden doesn't unload them
I have a simple Winform example that demonstrates what happens.
There is a GroupBox with two RadioButtons. The two buttons share a validating event handler. Also on the form is a Button that does nothing. No events are connected. Finally there is a CheckBox that controls the state of a passing validation. Checked it passes and unchecked it fails.
When the program starts and the user clicks on either RadioButton the validating event does not fire. Then when I click on any control other than the current button the Validating Event fires. The NOP button gives me something to click besides the CheckBox.
In this test the CheckBox represents the status of passing the validation.
This will not work because once I uncheck the CheckBox and then click a radio button the focus is forever stuck. You can't get the focus to the CheckBox to change its state.
The reason is the Validating event is always called when the focus is leaving and not when the RadioButton is clicked. It sees that "valdating" fails and cancels the event.
This is obviously the wrong approach. What should I be doing to test at the time of the initial click? The Click event happens after the state has changed.
What event should I use so that I can test validation before changing the RadioButton state? Then I can leave the button and fix the issue before trying again.
This example is a simplified test that shows my dead end. My real world example is the two RadioButtons select one of two similar tables in a DataGridView. The two tables are related and I want them to be on the same TabPage. When the user selects the alternate table I want to do a validation/confirmation before switching away. If the confirmation fails I want to cancel the radio button.
// Validate is called when the radio button is clicked and when it leaves the box
using System.ComponentModel;
using System.Windows.Forms;
namespace ValidateRadioButton
{
public partial class Form1 : Form
{
int count;
public Form1()
{
InitializeComponent();
}
private void radiobutton_Validating(object sender, CancelEventArgs e)
{
if (sender is RadioButton) {
// If box not checked, cancel radioButton change
// This becomes a Hotel California test, once unchecked you
// can never leave the control
e.Cancel = !chkAllowChange.Checked;
}
count++;
//Display the number of times validating is called in the title bar
//Demonstrates when the event is called
Text = count.ToString();
}
}
}
I have reproduced your form in a test project and I think I see what is going on.
Setting the Checked property of CancelEventArgs to true will prevent whatever control set it from losing focus until input is "corrected". As you are aware, the Validating event is triggered whenever a control loses focus. The user becomes "stuck" as you say because the only way they can "correct" their input is to modify the check box, which they cannot get to because of the Validating event which fires on the radio button.
A solution which I came up with was moving the Validated event to the GroupBox, instead of the radio buttons:
private void groupBox_Validating(object sender, CancelEventArgs e)
{
if (sender is RadioButton)
{
e.Cancel = !checkBox1.Checked;
}
count++;
//Display the number of times validating is called in the title bar
//Demonstrates when the event is called
Text = count.ToString();
}
Be sure to remove the Validating event handler from the radio buttons.
For clarity, I have set my form up in this manner:
The result is now I cannot click the 'OK' until I have ticked the check box. I have preserved your Text = count.ToString() assignment so that you can see that the form now calls Validating only as it should.
I ended up using the CheckChanged and Click events for the RadioButtons.
I track the currently active RadioButton and do the validation in the CheckChanged event. Then in the Click event, if the validation failed I restore the previous active RadioButton. Otherwise we can continue to the purpose of the RadioButton.
This solution works for two or more RadioButtons in a group and it works when using the keyboard.
using System.ComponentModel;
using System.Windows.Forms;
namespace ValidateRadioButton
{
public partial class Form1 : Form
{
// RadioButton that is currently active
RadioButton ActiveRadioButton;
bool cancelingChange;
public Form1()
{
InitializeComponent();
activeRadioButton = this.radioButton1;
}
private void radioButton_CheckedChanged(object sender, System.EventArgs e)
{
// Each click fires two events
// One for the RadioButton loosing its check and the other that is gaining it
// We are interested in the one gaining it
if (sender is RadioButton) {
RadioButton radioButton = (RadioButton)sender;
if (radioButton.Checked) {
// If this button is changing because of a canceled check
// do not validate
if (!cancelingChange) {
cancelingChange = !ValidateData();
if (!cancelingChange) {
// Mark this as the active value
activeRadioButton = radioButton;
}
}
}
}
}
private void radioButton_Click(object sender, System.EventArgs e)
{
// This is called after the RadioButton has changed
if (cancelingChange) {
// Check theRadioButton that was previously checked
activeRadioButton.Checked = true;
cancelingChange = false;
}
else {
// Do the thing the RadioButton should do
// If using separate events they all must start with the condition above.
this.Text = ((RadioButton)sender).Name;
}
}
/// <summary>
/// Validate state of data
/// </summary>
/// <returns></returns>
private bool ValidateData()
{
bool result = chkAllowChange.Checked;
if (!result) {
result = MessageBox.Show("Do you want to save your data?", "CheckBox unchecked", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) == DialogResult.OK;
if (result) {
// This is where I would save the data
chkAllowChange.Checked = true;
}
}
return result;
}
}
}
I don't know if it is called an argument (i.e. textbox1.text = "Hello";).
I have a control and there is a text box in it. It has a dropdown box that opens when the text is changed. But when I update the text in the text box that box drops down.
I need a way to make it so it only drops down if someone manually does it.
TBAddressBar.ABText.Text = getCurrentBrowser().Source.ToString();
and
public void ABText_TextChanged(object sender, TextChangedEventArgs e)
{
if (sender == 1*)
{
ABDropDown.Visibility = Visibility.Visible;
}
else
{
ABDropDown.Visibility = Visibility.Collapsed;
}
}
If someone manually does it, presumably they are using keypresses to do so. In that case, use KeyDown or KeyUp events to show the dropdown instead.
What I have done in the past is use a boolean variable that I set when I update my textboxes programically to bypass the TextChangedEvent.
i.e.
bool loading;
....
loading =true;
TBAddressBar.ABText.Text = getCurrentBrowser().Source.ToString();
loading = false;
public void ABText_TextChanged(object sender, TextChangedEventArgs e)
{
if(loading) return;
....
}
Simple, just remove the code from your TextChanged Event.
Anyway you got the basic idea.. Now do your dropdown logic in KeyPress event, since it accepts only characters and not the modifiers. So it behaves closer to your requirement. Not that you cant handle the same using KeyDown and KeyUp, you can, but more code..
Is it somehow possible to disable one (or more) tabs of tab control? At some point I need to make user stay on the active tab and prevent him from leaving... I know I can disable the whole TabControl component, but that disables also all components on active tab...
I also tried to use the Selecting method of TabControl:
private void TabControl_Selecting(object sender, TabControlCancelEventArgs e) {
e.Cancel = PreventTabSwitch;
}
This works, prevents user from switching (if PreventTabSwitch==true), but since all tabs look active and just don't react it's confusing...
There is no Enabled property for individual tab pages, so I don't know what else to do...
Thanks a lot for in advance for all tips.
IIRC, this is the only way to prevent a user from switching tabs.
I presume you are preventing them from leaving as validation on the form has failed? Using the ErrorProvider component would provide some sort of visual cue that they need to do something before switching tabs.
I've had a similar need once (I wanted the active tab to have different background color and some other stuff) and ended up creating new Controls that inherited from TabControl & TabPage where I used OwnerDraw to alter the look.
What you are doing is the right way to go according to MSDN but it does suggest that another option is to hide/show the pages as needed.
TabControl - Disable/Enable tab page at
http://social.msdn.microsoft.com/forums/en-US/winforms/thread/985b41c3-a1de-4744-8875-63262d4c2718/
MSDN Search for "tabcontrol disabled tabpage" at http://social.msdn.microsoft.com/Search/en-US?query=tabcontrol+disabled+tabpage&ac=8
The user cannot click on tabs to navigate, but they can use the two buttons ( Next , Back ). The user cannot continue to the next if the //conditions are no met
private int currentTab = 0;
private void frmOneTimeEntry_Load(object sender, EventArgs e)
{
tabMenu.Selecting += new TabControlCancelEventHandler(tabMenu_Selecting);
}
private void tabMenu_Selecting(object sender, TabControlCancelEventArgs e)
{
tabMenu.SelectTab(currentTab);
}
private void btnNextStep_Click(object sender, EventArgs e)
{
switch(tabMenu.SelectedIndex)
{
case 0:
//if conditions met GoTo
case 2:
//if conditions met GoTo
case n:
//if conditions met GoTo
{
CanLeaveTab:
currentTab++;
tabMenu.SelectTab(tabMenu.SelectedIndex + 1);
if (tabMenu.SelectedIndex == 3)
btnNextStep.Enabled = false;
if (btnBackStep.Enabled == false)
btnBackStep.Enabled = true;
CannotLeaveTab:
;
}
private void btnBackStep_Click(object sender, EventArgs e)
{
currentTab--;
tabMenu.SelectTab(tabMenu.SelectedIndex - 1);
if (tabMenu.SelectedIndex == 0)
btnBackStep.Enabled = false;
if (btnNextStep.Enabled == false)
btnNextStep.Enabled = true;
}
If you want to cancel the change of a tab, you can use the Deselecting event. There you can cancel the change by setting property Cancel of the provided TabControlCancelEventArgs to true.
I have a button and on click of that i show a popup which has a listbox.
popup named - popComboList
Listbox named - lstComboBoxResult
I am giving a focus to a listbox but at initial on a click of a button the listbox doesn't get focus-(this happens only once at initial, when i first time click button) After the second click it works.
private void bnOpen_Click(object sender, RoutedEventArgs e)
{
if (IsDesignTime)
return;
lstComboBoxResult.Width = tbComboValue.ActualWidth + bnOpen.ActualWidth;
if (!popComboList.IsOpen)
{
SetPopupPosition(popComboList);
popComboList.IsOpen = true;
lstComboBoxResult.Focus();
}
else
{
popComboList.IsOpen = false;
}
}
This is a bit of a guess, but try calling UpdateLayout() after opening the pop-up, but before calling Focus(). It's possible that the listbox is not fully initialized and therefore unable to accept focus until it has become visible for the first time.