OleDb / OleDbConnection resizing form (bug?) - c#

Can someone help with a problem I can't seem to resolve. I have created a C# form in both VSE2012 express for desktop and VSE2013 express for Desktop, where i have left the form the default size and added one button again totally default. The button is coded to open and close a db connection.
When the button is clicked the form and its contents all shrink by about 25%, i have tried all the default setting with no joy. I even substituted the access db file for a text file and it still happens. Tried this on both window 7 and 8.1
Form1.cs (all the code)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;"
+ #"Data Source="
+ #"..\..\..\x.accdb");
private void button1_Click(object sender, EventArgs e)
{
try
{
myConn.Open();
myConn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
Uneditted Form1.Designer.cs
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(106, 138);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(282, 253);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
}
#endregion

This must be a OleDb or .NET bug. It is only noticeable on very high resolution screens and the simple act of calling OleDb.OleDbConnection.Open causes the form to ignore any DPI or Font scaling.
I've tried moving the call to open the OleDb to a separate class and also to a separate DLL but the results are always the same.
A user posted an example on YouTube here: https://www.youtube.com/watch?v=zdby6gmbX_4
I guess we'll just need to report it to MS
***** Found a way to fix it
If you uninstall the "Microsoft Access Database Engine 2010 Redistributable" 32 bit version and then install the 64 bit version. You then uninstall the 64 bit version and then reinstall the 32 bit version it will now work

Related

Remove the 'button1' field and declare it as a local variable in the relevant methods

i review my code with codacy and codacy says following to me
Remove the 'button1' field and declare it as a local variable in the relevant methods.
The line that codacy mean is
private Button button1;
The method in this case button1_Click
my code is (only a little example because my code is much bigger):
namespace WindowsFormsApp1
{
public class Form1 : Form
{
private Button button1;
public Form1()
{
InitializeComponent();
}
#region Vom Windows Form-Designer generierter Code
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(349, 155);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(800, 450);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
private void button1_Click(object sender, EventArgs e)
{
}
}
}
I dont know how to do that, can anyone explain me what to do here?
Greetings
In form editing tab select this button and set its GenerateMember to false. But take into account that you won't be able to use this button in code behind.
Remove the 'button1' field and declare it as a local variable in the relevant methods.
Don't.
The code you're showing is generated by the Windows Forms Designer. If you modify it, one of two things is likely to happen:
Worse case - You break the form.
Best case - The designer over-writes your changes next time it's invoked.
Neither is ideal, and both are entirely superfluous. It looks like what you have is a static analysis tool which is useful for analyzing code you write, but essentially pointless for code you don't write and is generated by a tool. (You also don't need to analyze code from 3rd party libraries, for example.)
Honestly, just set your static anaysis tools to ignore the generated code. (This same advice applies to unit test coverage metrics. Especially when it comes to the thousands of lines generated by SOAP service clients in Visual Studio. One legacy web service integration can kill your analysis metrics if you take it into account, and there's no real value in doing so.)

When adding existing user control I get "already contains" and "already defines" error

I'm using a C# text book called "Head First C#" and I'm working on the hive simulator, where you import the three BeeControl elements from another project. They work fine in the other project but as soon as I rebuild the solution after importing to the new project and changing the namespace, as per the instructions, I get the following three errors that have me stumped...Keeping in mind that the elements that throw "already contain" and "already defined" errors coexisted peacefully in the project from which they came.
EDIT: I'm adding clarifying code - making it a much longer post. Hope it helps! My first thought is to just remove all the duplications but, again, it worked fin in the project from which it was imported and the book doesn't say anything about it so I don't want to mess up my lesson down the road.
Thanks again for any help!
Error 1 The type 'Hive_Simulator.BeeControl' already contains a
definition for 'components' C:\Users[...]\Hive
Simulator\BeeControl.designer.cs 8 50 Hive Simulator
From BeeControl.designer.cs:
partial class BeeControl
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
}
From BeeControl.cs:
public partial class BeeControl : UserControl
{
private Timer AnimationTimer;
private IContainer components;
}
Error 2 Type
'Hive_Simulator.BeeControl' already defines a member called
'InitializeComponent' with the same parameter types
C:\Users[...]\Hive Simulator\BeeControl.designer.cs 29 22 Hive
Simulator
From BeeControl.designer.cs:
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AnimationTimer = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// timer1
//
this.AnimationTimer.Enabled = true;
this.AnimationTimer.Interval = 150;
this.AnimationTimer.Tick += new System.EventHandler(this.timer1_Tick);
//
// BeeControl
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.Name = "BeeControl";
this.ResumeLayout(false);
}
From BeeControl.cs:
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.AnimationTimer = new System.Windows.Forms.Timer(this.components);
this.SuspendLayout();
//
// AnimationTimer
//
this.AnimationTimer.Enabled = true;
this.AnimationTimer.Interval = 150;
this.AnimationTimer.Tick += new System.EventHandler(this.AnimationTimer_Tick);
//
// BeeControl
//
this.Name = "BeeControl";
this.ResumeLayout(false);
}
Error 3 The type 'Hive_Simulator.BeeControl' already
contains a definition for 'AnimationTimer' C:\Users[...]\Hive
Simulator\BeeControl.designer.cs 52 44 Hive Simulator
From BeeControl.designer.cs:
private System.Windows.Forms.Timer AnimationTimer;
From BeeControl.cs:
private Timer AnimationTimer;

Emgu CV can't play video

I can't play play video with Emgu CV
It's show error
Unable to create capture from 184.avi
Here is the code:
public partial class Form1 : Form
{
//Set the name of pop-up window
String winname = "First Window";
Timer My_Time = new Timer();
int FPS=30;
Capture _capture;
public Form1()
{
InitializeComponent();
//Frame Rate
My_Timer.Interval = 1000 / FPS;
My_Timer.Tick += new EventHandler(My_Timer_Tick);
My_Timer.Start();
_capture = new Capture("184.avi"); // Error this line
}
private void My_Timer_Tick(object sender, EventArgs e)
{
imageBox.Image = _capture.QueryFrame().ToBitmap();
}
I use windows 8 x64 and install emgucv-windows-universal-cuda 2.4.10.1940 It have no opencv_ffmpeg.dll in bin. So I install opencv-2.4.11 and copy all dll from OpenCV bin to paste in Debug in my project. I paste 184.avi to Debug too. But when I run it show error like this. How to play video with Emgu CV?
Your code works fine. I think the problem is that you have not imported the video file to Visual Studio. So try importing the video file to visual studio and also set the properties to copy always. You can see below that i have imported the A.avi file and set it's properties to copy always.
And think you can just use imageBox.Image = _capture.QueryFrame(); instead of imageBox.Image = _capture.QueryFrame().ToBitmap();because you don't need to convert to bitmap with ImageBox.
The code i used is same as yours.
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV;
using Emgu.CV.CvEnum;
using Emgu.CV.Structure;
using Emgu.CV.UI;
using Emgu.CV.Features2D;
using Emgu.CV.Util;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{//Set the name of pop-up window
String winname = "First Window";
Timer My_Time = new Timer();
int FPS = 30;
Capture _capture;
public Form1()
{
InitializeComponent();
//Frame Rate
My_Time.Interval = 1000 / FPS;
My_Time.Tick += new EventHandler(My_Timer_Tick);
My_Time.Start();
_capture = new Capture("A.avi"); // Error this line
}
private void My_Timer_Tick(object sender, EventArgs e)
{
imageBox.Image = _capture.QueryFrame();
}
}
}
You can just provide full path instead of just the file name.
You do not have the video file in the same folder as your program exe.
If you place video file just next to your program exe, it should also work.

My Chromium browser object won't display in Winform

I was using the .Net WebBrowser object but don't like the messy way the compatibility mode works. So I switched to CEFSharp object.
However, the object doesn't display in my form. I looked at the Example Winform project with CEFSharp but it doesn't seem to display it in a winform but runs it as a separate application which isn't want i want.
I figure I am missing some obvious method or property that must be set initially. In the code below I have what I am using for CEFSharp and you will see two lines commented out which is what I was using for .Net WebBrowser that worked.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp.WinForms;
namespace GoogleSiren
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
ChromiumWebBrowser myBrowser = new ChromiumWebBrowser("http://wwww.cnn.com");
//WebBrowser myBrowser = new WebBrowser();
//myBrowser.Navigate("http://www.cnn.com");
myBrowser.Location = new Point(200, 200);
myBrowser.Size = new Size(500, 300);
this.Controls.Add(myBrowser);
}
}
}
Try setting the Size and Location as part of the constructor:
ChromiumWebBrowser myBrowser = new ChromiumWebBrowser("http://wwww.cnn.com")
{
Location = new Point(200, 200),
Size = new Size(500, 300)
};
this.Controls.Add(myBrowser);
My code was missing Cef.Initialize() call. Once I added that it worked fine.

winforms Label flickering

I have a problem with the Label control that is terribly flickering.
Below is some code to reproduce the problem.
How to solve this?
UPDATE: The solution for the previous case (a Form contains a Label directly) was to make the form.DoubleBuffered = true. But this is not a generic solution. For example, what should I do in the case of label inside a SplitContainer? this is my real case.
UPDATED CODE:
DoubleBufferedLabel.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FlickerLabelTest
{
public class DoubleBufferedLabel : Label
{
public DoubleBufferedLabel()
{
DoubleBuffered = true;
}
}
}
DoubleBufferedSplitContainer.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FlickerLabelTest
{
public class DoubleBufferedSplitContainer : SplitContainer
{
public DoubleBufferedSplitContainer()
{
DoubleBuffered = true;
}
}
}
Form1.cs:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace FlickerLabelTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void timer1_Tick(object sender, EventArgs e)
{
label1.Text += "0";
}
}
}
Form1.Designer.cs:
namespace FlickerLabelTest
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
this.label1 = new FlickerLabelTest.DoubleBufferedLabel();
this.splitContainer1 = new DoubleBufferedSplitContainer();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.SuspendLayout();
//
// timer1
//
this.timer1.Enabled = true;
this.timer1.Interval = 1;
this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
//
// label1
//
this.label1.Dock = System.Windows.Forms.DockStyle.Fill;
this.label1.Location = new System.Drawing.Point(0, 0);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(186, 262);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 0);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.label1);
this.splitContainer1.Size = new System.Drawing.Size(284, 262);
this.splitContainer1.SplitterDistance = 94;
this.splitContainer1.TabIndex = 1;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(284, 262);
this.Controls.Add(this.splitContainer1);
this.DoubleBuffered = true;
this.Name = "Form1";
this.Text = "Form1";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Timer timer1;
private DoubleBufferedLabel label1;
private DoubleBufferedSplitContainer splitContainer1;
}
}
Program.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace FlickerLabelTest
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
The problem is with the docking. If you change the Label.Dock from Fill to None, manually set the size of the label to fill the split panel and then anchor it on all sides, it won't flicker.
If you want to see the cause of the flicker, while Dock is still set to Fill, override OnResize in your DoubleBufferedLabel class, start the application, and while it is running set a breakpoint in OnResize. Add a watch for the Size and you'll see it flipping between its design time and runtime sizes.
I tried using a regular SplitContainer and Label in your example, set DoubleBuffer to False on the form, and it did not flicker if I left Dock set to None on the Label.
I think you're looking for this: http://msdn.microsoft.com/en-us/library/3t7htc9c.aspx
example:
class Form1 : Form
{
public Form1() {
this.DoubleBuffered = true;
}
}
Paste this into your form code to help the Dock layout calculations:
protected override void OnLoad(EventArgs e) {
label1.Size = this.ClientSize;
base.OnLoad(e);
}
Not really an answer, but why would you want to update your label each millisecond?
If you meant 1 second, you'd have to set your interval to 1000.
You can solve this by giving the form time to redraw itself and using a larger interval.
Update: turned out, setting DoubleBuffered to true solves the problem. Thanks for csharptest.net for pointing this out and DxCK for correcting me.
Activating Double Buffering on the form will fix the problem. But that is actually an expensive solution. If you just add the following to the form:
SetStyle(ControlStyles.AllPaintingInWmPaint, true);
the flicker will be over too.
The default behavior of the Windows OS it to first let all windows paint their background, and later, let them do actual painting. This is from the old days, when painting letters actually took considerable amount of time. This flag tells it to fold the background paint and the regular paint (for the same window) immediately after each other.
Real Double buffering can be reserved for the cases where you actually do painting yourself (when you override OnPaint).
Stop setting timers to 1ms. No seriously, what you see here is that the Label tries to keep up with it's changes, but fails to do so because they're to frequent. So possible solutions are:
Choose a way to change the Label less often
Activate Double-Buffering on the Form
Why not run your label update function via an asynchronous delegate? Or use System.Threading namespace for a different flavor.
Also, as people before me mentioned, it would be useful if you set the DoubleBuffer property on your form to true (it's no silver bullet though).

Categories