Drag and Drop event no longer firing - c#

I was having issues with my project with drag and drop no longer working.
I created a new simple project to isolate the issue. (based on this)
I simply created a listview (listView1), changed the background to blue. I then added 3 events from the TaskPaneControl designer which I then added a line of code as follows:
private void listView1_DragDrop(object sender, DragEventArgs e)
{
string test = "";
}
private void listView1_DragEnter(object sender, DragEventArgs e)
{
string test = "";
}
private void listView1_DragOver(object sender, DragEventArgs e)
{
string test = "";
}
I put a breakpoint in each line and expected it to be hit. The breakpoint will hit TaskPane_VisibleChanged but for some reason it won't hit the listview1_DragXXX

I made a change to the designer and in doing so I inadvertently set allowdrop on the user control be set to false. Once I set it to true it worked.

Related

Panel Drag and Drop not working in winForms Application

I am developing a winForm application using c#.NET. On the form i have placed a panel, and i have 2 methods :
private void panel1_DragDrop(object sender, DragEventArgs e){
//some code here
}
private void panel1_DoubleClick(object sender, EventArgs e){
//some code here
}
Whenever i run the program in debug mode(x64), i cannot seem to hit panel1_DragDrop method. I have also tried using a breakpoint , but it is also not being hit.
I have put property AllowDrop=true for this panel, but still nothing is happening.
What could be the possible reason?
Did you tried DragEnter event :
private void panel1_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop)) e.Effect = DragDropEffects.Copy;
}
private void panel1_DragDrop(object sender, DragEventArgs e)
{
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
if (Path.GetExtension(files[0]).ToLower() == ".pdf")//jpg,bmp,docx,....
{
//Code
}
}
Also please check if your application isn't running as another user privilege like Run as Administrator.
"Run as Administrator" prevents drag and drop working.

Action perform button visible false does not work

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();
}

TextChanged event firing as soon as I open the app

So I am making an app in C# where it creates/edits existing .ini files. One of the features I am trying to add is that if I make changes to the .ini file via the c# app I created, and either try to close the app, open another .ini file or create a new file, it should prompt the user if they want to save the file. To accomplish this, I have a flag called dataChanged. In the TextChanged events in for the multiple textboxes, I set dataChanged = true; since changes were made to the file. However, for some reason as soon as I open the app, all the TextChange events fire up so even if I don't enter any values in the various textboxes, when I close the app, it prompts me to save the file (it shouldn't!).
App UI:
User inputs text in the textboxes.
Part of code regarding the 4 textboxes:
private void TextBox_TextChanged(object sender, TextChangedEventArgs e) //ifrs installer
{
dataChanged = true;
}
private void TextBox_TextChanged_1(object sender, TextChangedEventArgs e) //ifrs patchfile
{
dataChanged = true;
}
private void textBox3_TextChanged(object sender, TextChangedEventArgs e)
{
dataChanged = true;
}
private void textBox4_TextChanged(object sender, TextChangedEventArgs e)
{
dataChanged = true;
}
TextChanged event fires even when you set some Text. Apparently you set some initial text when app is loading.
You can subscribe to the event mannually after you set the initial value.
textBox4.TextChanged += textBox4_TextChanged;
or unsubscribe before you set the value and subscribe after that.
textBox4.TextChanged -= textBox4_TextChanged;
textBox4.Text = "Initial Value";
textBox4.TextChanged += textBox4_TextChanged;
Sounds to me like you're programmatically setting the textBoxN.Text properties.
What you might want to do is add an if (appInitialized) around your dataChanged = true; and only set appInitialized to true after the application is loaded, perhaps in your Form_Load event. This way, the initial loading doesn't set your variable. Another option is to only register the TextChanged event after you have already set the initial values. My guess is you registered the event using the designer and as a result it's firing for those initial settings because of where the designer adds event registration. Instead do the
textBox4.TextChanged += textBox4_TextChanged;
// Etc. for each text box
yourself after the .Text properties are set. Again, perhaps in your Form_Load.
I'm guessing you are loading the ini file when the program loads and this triggers to text changed event. I suggest doing something like this.
private void Form1_Load(object sender, EventArgs e)
{
LoadData();
}
private bool _LoadingData = false;
private bool _DataChanged = false;
private void LoadData()
{
try
{
_LoadingData = true;
// Load data
}
finally
{
_LoadingData = false;
}
}
public void DataChanged()
{
if (_LoadingData == false)
{
_DataChanged = true;
}
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
DataChanged();
}

My groupbox is not showing

I have created many groupbox staked one on top of the other and made them to be invisible however ,if i call the first group box it shows but if i call the other groupboxes they will not show
private void buttonFinish_Click(object sender, EventArgs e)
{
groupBox1.Show();// is showing
}
private void buttonFinish_Click(object sender, EventArgs e)
{
groupBox2.Show();// is not showing
}
The second related problem is that if i try an if statement it the selected if statement shows nothing at all
private void buttonFinish_Click(object sender, EventArgs e)
{
if (comboBoxType.Text == "Car" && comboBoxName.Text == "BMW"
" && radioButtonBlack.Checked){
if (checkBoxTwoseater.Checked || radioButtonLeather.Checked ||
radioButtonBooster.Checked ){
groupBox1.show}
I cannot insert an image because i am new
Well, you said they are all on top of each other, so what is likely happening is the second call works just fine, but it is actually appearing behind the first one.
What you need to write is:
private void buttonFinish_Click(object sender, EventArgs e)
{
groupBox1.Show();// is showing
}
private void buttonFinish2_Click(object sender, EventArgs e)
{
groupBox1.Hide();
groupBox2.Show();// is showing now!
}
Note that this code is odd, since both methods appear to be named the same. I changed that to make "compilable" code, but you should check yours to make sure that it isn't causing a problem.
Show just sets the Visible property to true, it does not affect the Z-Order (see MSDN)
The second problem will require you stepping through the code with a debugger and checking all your conditions, you haven't given enough information for us to help.

C# Tabless Control Previous/Back/Return Button failing?

I am hoping someone here can help me, i have a Tabless Control on my windows forms application and basically because the tabs are purposely hidden i have added 2 buttons to each tab "Next" and "Back".
This is the code snippet i have for my "Next" button:
private void nextbutton1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabPage3;
this.toolStripStatusLabel8.Text = System.DateTime.Now.ToString();
}
Which works fine, however when i use the exact same theory on the "Back" button it does not work:
private void backbutton1_Click(object sender, EventArgs e)
{
tabControl1.SelectedTab = tabmain;
this.toolStripStatusLabel1.Text = System.DateTime.Now.ToString();
}
So my question is how does one go to a previous tabpage from a button? I have looked through here and tried all of the links that came up but nothing has worked any ideas?
You should use the SelectedIndex property instead of using concrete TabPage instances. This way it will still work when you decide to change the order of the pab pages or add new pages:
private void previousButton_Click(object sender, EventArgs e)
{
if (tabControl1.SelectedIndex > 0)
{
tabControl1.SelectedIndex--;
}
}
private void nextButton_Click(object sender, EventArgs e)
{
if (tabControl1.SelectedIndex < tabControl1.TabCount - 1)
{
tabControl1.SelectedIndex++;
}
}
Since there is no "Tabless" tab control in .NET Framework I can only assume that it works similar to the standard TabControl. If the solution doesn't work you should give us some information about the actual class you use.
BTW: There is no need to repeat the buttons on each page. Why don't you just put the buttons outside the TabControl?
Also: I see that you use a ToolStripStatusLabel to show the current time. Instead of updating it each time the user clicks somewhere add a Timer to your form. Set its Interval to 1000 and handle its Tick event. Update the label there:
private void timer1_Tick(object sender, EventArgs e)
{
toolStripStatusLabel1.Text = DateTime.Now.ToLongTimeString();
}
This way it updates constantly and again there is no need to repeat anything. You need to call timer1.Start() in the form's constructor.

Categories

Resources