Well, I have this situation, in a program I put a Button whose code is activated with PerformClick (programmatically), that button must be invisible in the interface so I put the value visible=false since the beginning of the program but the action on the event click doesn't perform, but if I put visible = true, the action actually is performed, any ideas of the problem?
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
if(_datosDe == "Insumos")
{
_btnRecargarInsumos.PerformClick();
}
this.Close();
}
_btnRecargarInsumos: is the button and is actually performed in another Form.
private void btnRecargarInsumos_Click(object sender, EventArgs e)
{
objGeneral.regresaDescripciones(ref dsDescripciones);
cbACDescripcion.DataSource = dsDescripciones.Tables[0];
cbACDescripcion.DisplayMember = "Nombre";
cbACDescripcion.ValueMember = "ID";
cbACDescripcion.SelectedIndex = -1;
cbACDescripcion.Text = "";
}
cbACDescripcion: Combobox which will be "reloaded" with the values of the DataSet: dsDescripciones.
The property visible is false since the beginnig of the program, but I also try to set visible=true and just before the method PerformClick() change it, but is the same.
But if I put visible=true since the beginning it works in that way.
If you click a button that's not visible or not enabled, nothing happens, even if you click it programmatically. Here's a workaround that works for me, although it's a bit of a hack:
_btnRecargarInsumos.SuspendLayout();
_btnRecargarInsumos.Visible = true;
_btnRecargarInsumos.PerformClick();
_btnRecargarInsumos.Visible = false;
_btnRecargarInsumos.ResumeLayout();
Why not just put your code in a separate method?
Example:
private StuffToDoAtClick()
{
objGeneral.regresaDescripciones(ref dsDescripciones);
cbACDescripcion.DataSource = dsDescripciones.Tables[0];
cbACDescripcion.DisplayMember = "Nombre";
cbACDescripcion.ValueMember = "ID";
cbACDescripcion.SelectedIndex = -1;
cbACDescripcion.Text = "";
}
//Your Button.Click() code//
private void btnRecargarInsumos_Click(object sender, EventArgs e)
{
StuffToDoAtClick()
}
//Your Datagridview code//
private void dataGridView1_DoubleClick(object sender, EventArgs e)
{
if(_datosDe == "Insumos")
{
StuffToDoAtClick();
}
this.Close();
}
Related
So, in my WinForms application, I need to display several "pages" (that are groupboxes holding textbox, buttons, maskedbox and etc) on one main page on my application. Firstly I tried to use a "user controller", but it didn't work out because the name of the controllers didn't match the names that I passed to my database connection for example. So I tried to place these groupboxes one on top of another one via the "location property". All working just fine, but now I have a significant problem when I decide to make a few changes to these groupboxes controllers. It's very hard to access them and I wonder if there's another way to do so because it looks very amateur approach... How do I achieve this kind of functionality without placing groupboxes on top of another one? And I also wonder, is it correct to do it with groupbox or should I use panel?
An example of the approach:
private void btnCadastrarBeneficiario_Click(object sender, EventArgs e)
{
groupBoxUsuarioCadastro.Visible = false;
groupBoxClienteCadastro.Visible = false;
groupBoxHospitalCadastro.Visible = false;
GroupBoxMonitoramento.Visible = false;
groupBoxBeneficiarioCadastro.Visible = true;
}
I use the visibility property so I can show (or not) the groupbox and make them behave like an actual page.
---EDIT----
In order to make myself clear I created a minimal version of what I'm trying to show here:
namespace WinFormsApp1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
groupBox2.Visible = false;
groupBox3.Visible = false;
groupBox1.Visible = true;
}
private void button2_Click(object sender, EventArgs e)
{
groupBox1.Visible = false;
groupBox3.Visible = false;
groupBox2.Visible = true;
}
private void button3_Click(object sender, EventArgs e)
{
groupBox1.Visible = false;
groupBox2.Visible = false;
groupBox3.Visible = true;
}
}
}
This is the code of a minimal application similar to what I'm doing. It works just fine, but I wonder if there's another way to do it, a more efficient, organized, and sophisticated way.
I would tend to do something like this:
private void button1_Click(object sender, EventArgs e)
{
HideAllBut(groupBox1);
}
private void button2_Click(object sender, EventArgs e)
{
HideAllBut(groupBox2);
}
private void HideAllBut(GroupBox groupToShow)
{
var groups = new[] {groupBox1, groupBox2};
foreach(var gb in groups)
{
gb.Visible = (gb == groupToShow);
}
}
You can obviously extend that to as many controls of whatever kind you like.
Currently I have it so that when you select the text box it will highlight the text in it but what I want it to do is only do this for the first time that it is selected so that it will not delete the text that the user is typing each time. Here is what I am using to highlight the text:
private void txtName_Focus(object sender, EventArgs e)
{
bool isFirstTime = true;
if (isFirstTime == true){
txtName.SelectionStart = 0;
txtName.SelectionLength = txtName.Text.Length;
}
isFirstTime = false;
}
bool isFirstTime = true; this is your problem. It is being initialized to true every time the focus event is being called. Move bool isFirstTime; to be a member of your class and initialize it to true once in the declaration, constructor or the form load event
Maybe something like this:
bool txtNameWasFocused=false;
private void txtName_Focus(object sender, EventArgs e)
{
if(!txtNameWasFocused){
txtNameWasFocused=true;
txtName.SelectionStart = 0;
txtName.SelectionLength = txtName.Text.Length;
}
}
If you need this in many places, you might think of a derived text box with this special behaviour...
I have a Windows Form, DataGridView and two buttons.
When I will press the button1 it changes a value of RowHeadersVisible to true.
When I will press the button2 it changes a value of RowHeadersVisible to false.
public Form1()
{
InitializeComponent();
dataGridView1.RowHeadersVisible = false;
}
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.RowHeadersVisible = true;
}
private void button2_Click(object sender, EventArgs e)
{
dataGridView1.RowHeadersVisible = false;
}
I cannot find any kind of events about "RowHeadersVisible" value changing in "DataGridView" class. As I mentioned "CellFormatting" event works for this action but it appears often, almost for all kind of action made in datagridview1.
I think we might create a custom event handler in order to make different decisions.
When "RowHeadersVisible" changes the value to false I need to call another function inside "CustomEvent".
private void CustomEvent(object sender, EventArgs e)
{
SomeFunction();
}
On the other hand "DataGridTableStyle" class has the event "RowHeadersVisibleChanged".
So How to solve this problem?
.NET 4.5, you should get help.
Dissolve it in the following way.
Represents the table drawn by the System.Windows.Forms.DataGrid control at run time.
// Instantiate the EventHandler.
public void AttachRowHeaderVisibleChanged()
{
myDataGridTableStyle.RowHeadersVisibleChanged += new EventHandler (MyDelegateRowHeadersVisibleChanged);
}
// raise the event when RowHeadersVisible property is changed.
private void MyDelegateRowHeadersVisibleChanged(object sender, EventArgs e)
{
string myString = "'RowHeadersVisibleChanged' event raised, Row Headers are";
if (myDataGridTableStyle.RowHeadersVisible)
myString += " visible";
else
myString += " not visible";
MessageBox.Show(myString, "RowHeader information");
}
// raise the event when a button is clicked.
private void myButton_Click(object sender, System.EventArgs e)
{
if (myDataGridTableStyle.RowHeadersVisible)
myDataGridTableStyle.RowHeadersVisible = false;
else
myDataGridTableStyle.RowHeadersVisible = true;
}
i have an application here in winforms that am trying to make. This is how i want it to happen: whenever the user clicks on register visitor button the registration form should be opening. works fine. here is the function that is called in that case:
private void Register_Visitor_Load(object sender, EventArgs e)
On this form i have a textfield placed which i want to disable when the form loads. i wrote a line which disables the textbox on form load:
textbox1.enabled = false;
i placed the above line in the load function which is working fine. now i want to enable my textbox1 based on the checkbox checked. for this i wrote the code:
CheckState state = checkBox1.CheckState;
switch (state)
{
case CheckState.Checked:
{
textBox1.Enabled = true;
break;
}
case CheckState.Indeterminate:
case CheckState.Unchecked:
{
break;
}
now when i place the code above in the page load function nothing happens which is surely going to happen as that function is only called on form load. what am not getting is where to place the checkbox code so that my textbox is enable on runtime. other function are in response to button but what i want here it to instantly enable the textfield on runtime when the user checks the checkbox. kindly explain me how am i going to accomplish this!
You can use CheckStateChanged event; so whatever reason the checkBox1 is checked/unchecked/grayed you'll have the textBox1 properly enabled/disabled
private void checkBox1_CheckStateChanged(object sender, EventArgs e) {
textBox1.Enabled = (checkBox1.CheckState == CheckState.Checked);
}
you are placing code at wrong event.
Instead of placing in pageload place that code on chekchange event of checkbox.
That will help you.
private void chkDisable_CheckedChanged(object sender, EventArgs e)
{
if (((CheckBox)sender).Checked)
{
textBox1.Enable=true;
}
else
{
textBox1.Enable=false;
}
}
Place the above code inside the function which handles the event for check box.
In your case it is checkchanged status.
You can try this:
private void checkBox1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
textBox1.Enabled = false;
}
else
{
textBox1.Enabled = true;
}
}
I did a hybrid of some of the above answers and it worked perfectly. I wanted the state of a button to be disabled upon loading the form, but then enabled if the user checks a box, here's the code:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
button1.Enabled = (checkBox1.CheckState == CheckState.Checked);
}
private void Form1_Load(object sender, EventArgs e)
{
if (checkBox1.Checked)
{
button1.Enabled = true;
}
else
{
button1.Enabled = false;
}
}
I don't know how to fix or handle this issue. But I currenlty have 2 links as following:
NavigateUrl="~/Admin/ManageProducts.aspx?IsMeal=true and False.
When it is set to TRUE I want txtDescription to be visible, and when set to FALSE I wan't txtDescription to be invisible.
IsMeal is a BIT in my database. So I need to define somehow that, when ManageProducts.aspx?IsMeal=true then txtDescription should be visible, and reverse
FALSE = invisible
But how do I manage this?
In your Page_Load() method you can add the following:
protected void Page_Load(object sender, EventArgs e) {
txtDescription.Visible = Convert.ToBoolean(Request.QueryString("IsMeal"));
}
In the ManageProducts.aspx.cs file place this:
protected void Page_Load(object sender, EventArgs e) {
if (Request.QueryString["IsMeal"] != null) {
if (Boolean.Parse(Request.QueryString["IsMeal"])) {
txtDescription.Visible = true;
}
else {
txtDescription.Visible = false;
}
}
}
Wouldn't you just set the property accordingly:
bool isMeal = Convert.ToBoolean(Request.QueryString["IsMeal"]);
txtDescription.Visible = isMeal;