I've tried to disable the built-in right click menu of the shockwave flash element in my application, but it doesn't work. Is there a way to disable it in VB? Thanks!
it should disable the mouse right click event:
public partial class Form1 : Form ,IMessageFilter // after the name space
{
private const int WM_LBUTTONDOWN = 0x0201;
public Form1()
{
InitializeComponent();
Application.AddMessageFilter(this);
this.FormClosed += new FormClosedEventHandler(this.Form1_FormClosed);
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
Application.RemoveMessageFilter(this);
}
public bool PreFilterMessage(ref Message m)
{
//Filter out WM_NCRBUTTONDOWN/UP/DBLCLK
if (m.Msg == 0xA4 || m.Msg == 0xA5 || m.Msg == 0xA6) return true;
// Filter out WM_RBUTTONDOWN/UP/DBLCLK
if (m.Msg == 0x204 || m.Msg == 0x205 || m.Msg == 0x206) return true;
return false;
}
This can give you a RightClick Disabled ShockWaveObject "only with 4 simple Steps"
On your "Form" Add a Shockwave Flash control.
1. Create a new Class file named "NoRightClickFlashControl.vb"
Paste the bellow code into NoRightClickFlashControl.vb
Public Class NoRightClickFlashControl
Inherits AxShockwaveFlashObjects.AxShockwaveFlash
Private Const WM_RBUTTONDOWN As Integer = &H204
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
Select Case m.Msg
Case WM_RBUTTONDOWN
' Do nothing on right-click
m.Result = New IntPtr(1)
Return
End Select
MyBase.WndProc(m)
End Sub
End Class
2. Change the Name of your ShockwaveObject "AxShockwaveFlash1" to "NoRightClickFlashControl1" on your Form.
Open your Form.Designer.vb file
3. Change the code in between 'Initialize and SuspendLayout' like bellow :
Private Sub InitializeComponent()
'// Old is : Me.NoRightClickFlashControl1 = New AxShockwaveFlashObjects.AxShockwaveFlash()
'// Change like this
Me.NoRightClickFlashControl1 = New Your_Project_NameSpace.NoRightClickFlashControl
Me.SuspendLayout()
Then at Last, in Form.Designer.vb near the "End Class" file -- change the following :
'// Old is : Friend WithEvents NoRightClickFlashControl1 As AxShockwaveFlashObjects.AxShockwaveFlash
'// Change Like this :
Friend WithEvents NoRightClickFlashControl1 As NoRightClickFlashControl
End Class '// the End Class is just o understand.
All other coding is same to ShockwaveFlashObject - Load Movie, Play
It is Done
Press F5 to run! Enjoy!!!
Related
After clicking a label, the current form is set to hide while another form is opened. It will either open the form if its never been opened, or redisplay the form with all the previous data on it when it has previously been opened. When reopening, it would be more efficient for it to keep the same data, but it can just run the load procedure again as it's reading from a database.
Every time I run this code, it reopens the form but doesn't have all of the previous data which gets filled by the load procedure. It also removes the label that links to the form you just came from.
Private Sub lblHistory_Click(sender As Object, e As EventArgs) Handles lblHistory.Click
Try
frmStudentHistory.ShowDialog()
Catch ex As Exception
frmStudentHistory.ShowInTaskbar = True
frmStudentHistory.WindowState = FormWindowState.Maximized
End Try
Me.WindowState = FormWindowState.Minimized
Me.ShowInTaskbar = False
Me.Hide()
End Sub
Any help would be greatly appreciated!
As Andrew Mortimer pointed out, you can use Application.OpenForms to find any forms that are currently open in the application. This will return the reference to the open form which will have all the existing data still in place. I'm guessing you would like to open the original form when frmStudentHistory closes. To do this add an event handler to the closing event of the frmStudentHistory. Inside that handler add the code to find the existing form and open it.
// This is inside your frmStudentHistory and is the event handler for form closing event
private void frmStudentHistory_OnClosing(object sender, eventargs e){
var returnForm = Application.OpenForms.Item(FormToOpen.Name);
if(returnForm != null){
returnForm.WindowState = FormWindowState.Normal;
}
}
Note: this only works with Minimized windows. If setting the window to Hide or ShowInTaskbar = False the Form will not be found in OpenForms.
I would suggest looking into using MDI Containers (Multiple-Document Interface). It is meant for applications with multiple forms.
To do this designate a Form to be the container (IsMdiContainer = true in Properties window). Then create a public method that all other forms will use to open forms.
C#
public class MdiContainer {
// this goes inside the Form designated as the MdiContainer
public void OpenForm(Form formToOpen){
Form frm = null;
foreach(Form child in this.MDIChildren){
// check if the form already exists as a child in the MDI container
if(child.Name == formToOpen.Name)
frm = child;
}
// if the form doesn't exist get a new instance
if frm == null
frm = FormFactory.GetForm(FormToOpen.Name);
// set form properties and show
frm.MdiParent = this;
frm.WindowState = FormWindowState.Maximized;
frm.BringToFront();
frm.Show();
}
}
VB.Net
Public Class MdiContainer
Public Sub OpenForm(ByVal formToOpen As Form)
Dim frm As Form = Nothing
For Each child As Form In Me.MDIChildren
If child.Name = formToOpen.Name Then frm = child
Next
If frm Is Nothing Then frm = FormFactory.GetForm(FormToOpen.Name)
frm.MdiParent = Me
frm.WindowState = FormWindowState.Maximized
frm.BringToFront()
frm.Show()
End Sub
End Class
I like to use a factory class to get a new form instance when needed.
C#
public class FormFactory{
public static Form GetForm(string formName){
switch(formName.ToLower()){
case Form1.Name.ToLower():
return new Form1;
case Form2.Name.ToLower():
return new Form2;
default:
return new ErrorForm;
}
}
}
VB.Net
Public Class FormFactory
Public Shared Function GetForm(ByVal formName As String) As Form
Select Case formName.ToLower()
Case Form1.Name.ToLower()
Return New Form1()
Case Form2.Name.ToLower()
Return New Form2()
Case Else
Return New ErrorForm()
End Select
End Function
End Class
With this setup we can retrieve Forms that have already been created or create a new form if one does not exist.
C#
public class Form1{
private void lblHistory_Click(..){
MdiContainer.OpenForm(Form2);
}
}
public class Form2{
private void Form2_Closing(...){
MdiContainer.OpenForm(Form1);
}
}
VB.Net
Public Class Form1
Private Sub lblHistory_Click()
MdiContainer.OpenForm(Form2)
End Sub
End Class
Public Class Form2
Private Sub Form2_Closing()
MdiContainer.OpenForm(Form1)
End Sub
End Class
I'm making a Windows Forms application with VB.Net, Visual Studio 2015.
The Form has a WebBrowser control and other controls.
Whenever pressing the TAB key on keyboard, it always focuses on a html element loaded in the WebBrowser control first. Then pressing the TAB key again, the focus is switched between the HTML elements in the WebBrowser control.
Till ending up switching in all HTML elements, the focus doesn't switch to other controls in the Form.
Though I set .TabIndex = 1000 and .TabStop = false in the WebBrowser control, it always focuses on a html element loaded in the WebBrowser control first, always first.
So, I want to disable focusing on the WebBrowser control by pressing the TAB key or to disable the TAB key function in the Form entirely.
I have to get the answer done in VB.NET soon, but for now here's the C# version of it:
First, an extended web browser control, that you'll have to use on the form, with a custom event when the tab key is pressed.
Here we call the TabStop = false to ensure this key gets processed. Similar reasoning on WebBrowserShortcutsEnabled.
Then, we capture on the HTML Body, the key press event.
If the key code is a 9 (tab), we fire our event.
public class WebBrowserExtended : System.Windows.Forms.WebBrowser
{
protected virtual void OnTabKeyEvent(EventArgs e)
{
EventHandler handler = TabKeyEvent;
if (handler != null)
{
handler(this, e);
}
}
public event EventHandler TabKeyEvent;
public WebBrowserExtended() : base()
{
this.TabStop = false;
this.WebBrowserShortcutsEnabled = false;
}
protected override void OnDocumentCompleted(WebBrowserDocumentCompletedEventArgs e)
{
base.OnDocumentCompleted(e);
if (this.Document.Body != null)
this.Document.Body.KeyDown += new HtmlElementEventHandler(Body_KeyDown);
}
private void Body_KeyDown(Object sender, HtmlElementEventArgs e)
{
if (e.KeyPressedCode == 9 && !e.CtrlKeyPressed)
{
this.OnTabKeyEvent(e);
e.BubbleEvent = false;
}
}
}
And here's is your event handler:
private void webBrowser1_TabKeyEvent(object sender, EventArgs e)
{
var controls = new List<Control>(this.Controls.Cast<Control>());
var nextControl = controls.Where(c => c.TabIndex > webBrowser1.TabIndex).OrderBy(c => c.TabIndex).FirstOrDefault();
if (nextControl != null)
nextControl.Focus();
else
controls.OrderBy(c => c.TabIndex).FirstOrDefault().Focus();
}
And here's the VB version of the control:
Public Class WebBrowserExtended
Inherits System.Windows.Forms.WebBrowser
Protected Overridable Sub OnTabKeyEvent(ByVal e As EventArgs)
RaiseEvent TabKeyEvent(Me, e)
End Sub
Public Event TabKeyEvent As EventHandler
Public Sub New()
MyBase.New()
Me.TabStop = False
Me.WebBrowserShortcutsEnabled = False
End Sub
Protected Overrides Sub OnDocumentCompleted(ByVal e As WebBrowserDocumentCompletedEventArgs)
MyBase.OnDocumentCompleted(e)
If Me.Document.Body IsNot Nothing Then
AddHandler Me.Document.Body.KeyDown, AddressOf Body_KeyDown
End If
End Sub
Private Sub Body_KeyDown(ByVal sender As Object, ByVal e As HtmlElementEventArgs)
If e.KeyPressedCode = 9 AndAlso Not e.CtrlKeyPressed Then
Me.OnTabKeyEvent(e)
e.BubbleEvent = False
End If
End Sub
End Class
And the VB event handler:
Private Sub WebBrowser1_TabKeyEvent(sender As Object, e As EventArgs) Handles WebBrowser1.TabKeyEvent
Dim controls = New List(Of Control)(Me.Controls.Cast(Of Control))
Dim nextControl = controls.Where(Function(c)
Return c.TabIndex > WebBrowser1.TabIndex
End Function).OrderBy(Function(c)
Return c.TabIndex
End Function).FirstOrDefault()
If Not controls Is Nothing Then
nextControl.Focus()
Else
controls.OrderBy(Function(c)
Return c.TabIndex
End Function).FirstOrDefault().Focus()
End If
End Sub
Hi i converted the following c# code to vb.net.
public Dropdown(CheckedComboBox ccbParent)
{
this.ccbParent = ccbParent;
InitializeComponent();
this.ShowInTaskbar = false;
this.cclb.ItemCheck += new System.Windows.Forms.ItemCheckEventHandler(this.cclb_ItemCheck);
}
private void cclb_ItemCheck(object sender, ItemCheckEventArgs e)
{
if (ccbParent.ItemCheck != null)
{
ccbParent.ItemCheck(sender, e);
}
}
Visual Basic
Private cclb As CustomCheckedListBox
Public Event ItemCheck As Windows.Forms.ItemCheckEventHandler
Public Sub New(ByVal ccbParent As PlexisCheckedComboBox)
MyBase.New()
Me.ccbParent = ccbParent
InitializeComponent()
Me.ShowInTaskbar = False
AddHandler cclb.ItemCheck, AddressOf cclb_ItemCheck
End Sub
Private Sub cclb_ItemCheck(ByVal sender As Object, ByVal e As
Windows.Forms.ItemCheckEventArgs)
If (Not (ccbParent.ItemCheck) Is Nothing) Then
ccbParent.ItemCheck(sender, e)
End If
End Sub
In the converted vb.net code im getting error in the following line as
""
If (Not (ccbParent.ItemCheck) Is Nothing) Then
ccbParent.ItemCheck(sender, e)
please help me how to resolve it .
Well, as the error message told you, you have to use the RaiseEvent keyword to raise events.
But even then it would not work, since you can't raise events outside of the class the event is declared in (in contrast to C#, where nested classes can raise events of the outer class).
So to solve this add a new method to the outer class called OnItemCheck which raises the ItemCheck event, and call that method in cclb_ItemCheck instead of trying to raise the event directly.
Does anyone know why the Listbox1.Refresh() command may not trigger the ListBox1_DrawItem sub every time?
In Microsoft Visual Basic 2010, a listbox has a forcolor and backcolor property. These properties change the forcolour and backcolor for all the items in the listbox. By default there is no property for the forecolor and backcolor of an individual item on a listbox, I am aware there is on a list view but I would still wish to use a listbox.
I am trying to have the ability to change the forecolor and backcolor properties of individual items in the listbox.
To do this the listbox's draw item sub must be used with the listbox's drawmode property set to OwnerDrawFixed. Then using a brush colour along with the e.graphics the forecolor or backcolor can be changed.
I have seen and followed examples of how to do this for the currently selected item. Such as the one from ehow's website.
What I am tiring to do however is change the colour of the litsbox item as it is added depending on a variable.
Here is my code:
Private Sub listbox_add()
Me.ListBox1.Items.Add(listbox_text(list_num)) ' adds the line to the list box
add_item_colour = True
ListBox1.Refresh()
End Sub
Private Sub ListBox1_DrawItem(ByVal sender As Object, ByVal e As System.Windows.Forms.DrawItemEventArgs) Handles ListBox1.DrawItem
Dim myBrush As Brush = Brushes.Black
e.DrawBackground()
If add_item_colour = True Then
If blue_message = True Then
myBrush = Brushes.Blue
Else
myBrush = Brushes.Black
End If
e.Graphics.DrawString(ListBox1.Items.Item(list_num), ListBox1.Font, myBrush, _
New RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height))
add_item_colour = False
End If
e.DrawFocusRectangle()
End Sub
The listbox_text is a string array that stores the string being added, the list_num is a integer that increments when new items are added to the listbox and the blue_message is a Boolean that it true when I want a blue message and false when I don't.
The problem I seem to be having is that Listbox1.Refresh() command does not seem to be triggering the ListBox1_DrawItem sub every time it is called. I found this by using brake points. Does anyone know why this might be the case and how I could fix it?
Thanks, any help on this would be much appreciated.
First of all I suggest you to use background worker instead of directly writing down your code on UI thread.
Please refer the code below:
public partial class Form1 : Form
{
Brush myBrush = Brushes.Blue;
public Form1()
{
InitializeComponent();
this.backgroundWorker1.DoWork += backgroundWorker1_DoWork;
this.backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
this.backgroundWorker1.RunWorkerAsync(this);
}
private void listBox1_DrawItem(object sender, DrawItemEventArgs e)
{
e.DrawBackground();
e.Graphics.DrawString(listBox1.Items[e.Index].ToString(), listBox1.Font, myBrush, new RectangleF(e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height));
e.DrawFocusRectangle();
}
private void button1_Click(object sender, EventArgs e)
{
this.backgroundWorker1.RunWorkerAsync(button1);
}
private void button2_Click(object sender, EventArgs e)
{
this.backgroundWorker1.RunWorkerAsync(button2);
}
void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
this.listBox1.Refresh();
}
void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (e.Argument == this)
{
listBox1.Items.Add("p1");
listBox1.Items.Add("p2");
}
else if (e.Argument == this.button1)
{
myBrush = Brushes.Red;
listBox1.Refresh();
}
else if (e.Argument == this.button2)
{
myBrush = Brushes.Green;
if (listBox1.SelectedItem == null)
return;
var test = listBox1.Items[listBox1.SelectedIndex];
listBox1.SelectedItem = test;
var g = listBox1.CreateGraphics();
var rect = listBox1.GetItemRectangle(listBox1.SelectedIndex);
listBox1_DrawItem(listBox1, new DrawItemEventArgs(g, this.Font, rect, listBox1.SelectedIndex, DrawItemState.Default));
listBox1.Refresh();
}
}
}
Can I change my button control to toggle-button?
Is there any simple way to change the button property to make it toggle button?
According to this post on OSIX all you need to do is use a CheckBox but set it's appearance to Button.
In code:
CheckBox checkBox1 = new System.Windows.Forms.CheckBox();
checkBox1.Appearance = System.Windows.Forms.Appearance.Button;
(C# code but you see how it works).
But you can do this from the Properties dialog in the designer.
To Change Checkbox to Simple Latching On/Off Button
myCheckBox.Appearance = System.Windows.Forms.Appearance.Button
To Add Custom Toggle (Sliding) On/Off Switch
Right click project in VS and select 'Add' then 'User Control...'
Name your new file "Toggle.vb"
Paste the code below
Switch to your form and drag your 'toggle' control from toolbox to form
Size & settings can be changed like standard control
Colors can be changed in OnPaint method of Toggle Class
VB.net
Imports System.Drawing
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Public Class Toggle
Inherits System.Windows.Forms.UserControl
Private _checked As Boolean
Public Property Checked As Boolean
Get
Return _checked
End Get
Set(ByVal value As Boolean)
If Not _checked.Equals(value) Then
_checked = value
Me.OnCheckedChanged()
End If
End Set
End Property
Protected Overridable Sub OnCheckedChanged()
RaiseEvent CheckedChanged(Me, EventArgs.Empty)
End Sub
Public Event CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
Protected Overrides Sub OnMouseClick(e As MouseEventArgs)
Me.Checked = Not Me.Checked
Me.Invalidate()
MyBase.OnMouseClick(e)
End Sub
Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs)
Me.OnPaintBackground(e)
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias
Using path = New GraphicsPath()
Dim d = Padding.All
Dim r = Me.Height - 2 * d
path.AddArc(d, d, r, r, 90, 180)
path.AddArc(Me.Width - r - d, d, r, r, -90, 180)
path.CloseFigure()
e.Graphics.FillPath(If(Checked, Brushes.DarkGray, Brushes.LightGray), path)
r = Height - 1
Dim rect = If(Checked, New System.Drawing.Rectangle(Width - r - 1, 0, r, r), New System.Drawing.Rectangle(0, 0, r, r))
e.Graphics.FillEllipse(If(Checked, Brushes.Green, Brushes.LightSlateGray), rect)
End Using
End Sub
End Class
C#
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
namespace Your_Project_Name
{
class Toggle : CheckBox
{
public Toggle()
{
SetStyle(ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true);
Padding = new Padding(6);
}
protected override void OnPaint(PaintEventArgs e)
{
this.OnPaintBackground(e);
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
using (var path = new GraphicsPath())
{
var d = Padding.All;
var r = this.Height - 2 * d;
path.AddArc(d, d, r, r, 90, 180);
path.AddArc(this.Width - r - d, d, r, r, -90, 180);
path.CloseFigure();
e.Graphics.FillPath(Checked ? Brushes.DarkGray : Brushes.LightGray, path);
r = Height - 1;
var rect = Checked ? new System.Drawing.Rectangle(Width - r - 1, 0, r, r)
: new System.Drawing.Rectangle(0, 0, r, r);
e.Graphics.FillEllipse(Checked ? Brushes.Green : Brushes.LightSlateGray, rect);
}
}
}
}
This code is from several sources over the years and has some minor tweaks. It appears it exist in various forms on different sites so it's unclear who to attribute
All code was tested in Visual Studio 2017
If i understood correctly, you can achieve this functionality by using a flag. For example:
bool isClicked = false;
and in the clicked event to put the following code to invert the bool:
isClicked = !isClicked;