I have a question!
I'm currently making a notepad app and everything was going well until now.
The way it's set up:
There's a panel with the menu buttons (File, Edit, Themes, & Format) and a panel at the bottom with all of the options under each tab. Both the menu button and sub-buttons are labels.
When you click a menu button, the bottom panel will show the buttons categorized under that section by making all labels there invisible, and then making the appropriate ones visible.
Everything seems to work fine except for the 2 buttons under Format (Font & Font Color). They show up, but do not do anything when clicked. Below is the code for it.
private void FontButton_Click(object sender, EventArgs e)
{
FontDialog fd = new FontDialog();
if(fd.ShowDialog() == DialogResult.OK)
{
richTextBox1.Font = fd.Font;
}
}
private void FontColor_Click(object sender, EventArgs e)
{
ColorDialog cd = new ColorDialog();
if(cd.ShowDialog() == DialogResult.OK)
{
richTextBox1.ForeColor = cd.Color;
}
}
private void Label4_Click(object sender, EventArgs e)
{
Config();
FontButton.Visible = true;
FontColor.Visible = true;
}
private void Config()
{
New.Visible = false;
Open.Visible = false;
Save.Visible = false;
Light.Visible = false;
Dark.Visible = false;
FontButton.Visible = false;
FontColor.Visible = false;
Undo.Visible = false;
Cut.Visible = false;
Copy.Visible = false;
Paste.Visible = false;
}
Is the code wrong? Is it because the labels are stacked?
If someone could lend a helping hand, that'd be great! Thanks in advance.
Related
I am using AxMSTSCLib to develop a Windows App for creating RDP connections.
The steps listed below caused my remote desktop display disappear:
Start an RDP connection with full-screen mode
Click "restore down" button from connection bar
Click "maximize" button again to re-enter full-screen mode
Click "minimize" button
Then my app disappears, I can't see it in taskbar (but still be listed / running in taskmanager)
If I skip steps 2 & 3, it won't disappear when I click "minimize" button from connection bar, it's really weird.
I post some part of my code here, hope anyone can help me to find out the problem.
public partial class RdpShowForm : Form
{
public AxMSTSCLib.AxMsRdpClient9NotSafeForScripting oRemote;
public RdpShowForm()
{
InitializeComponent();
oRemote = new AxMSTSCLib.AxMsRdpClient9NotSafeForScripting();
((System.ComponentModel.ISupportInitialize)(this.oRemote)).BeginInit();
oRemote.Dock = System.Windows.Forms.DockStyle.Fill;
oRemote.Enabled = true;
oRemote.Name = "WindowsVM";
this.Controls.Add(oRemote); // Controls contains 'panel1' and 'oRemote'
((System.ComponentModel.ISupportInitialize)(this.oRemote)).EndInit();
oRemote.CreateControl();
this.panel1.Dock = System.Windows.Forms.DockStyle.Fill;
}
private void RdpShowForm_Load(object sender, EventArgs e)
{
NewRdpConnectionInstance();
}
private void NewRdpConnectionInstance()
{
oRemote.Server = 'xxx';
...
oRemote.FullScreen = true;
oRemote.AdvancedSettings7.DisplayConnectionBar = true;
oRemote.AdvancedSettings7.ConnectionBarShowMinimizeButton = true;
oRemote.AdvancedSettings7.ConnectionBarShowRestoreButton = true;
oRemote.OnConnected += rdpClient_OnConnected;
oRemote.OnLeaveFullScreenMode += rdpClient_OnLeaveFullScreenMode;
oRemote.Connect();
oRemote.Show();
oRemote.Focus();
}
private void rdpClient_OnConnected(object sender, EventArgs e)
{
this.panel1.Visible = false;
this.Visible = false;
}
private void rdpClient_OnLeaveFullScreenMode(object sender, EventArgs e)
{
this.Visible = true;
}
protected override void WndProc(ref Message m)
{
if (m.Msg == WM_SYSCOMMAND)
{
if (m.WParam.ToInt32() == MAXIMIZE)
{
this.Visible = false;
oRemote.FullScreen = true;
oRemote.Show();
}
}
base.WndProc(ref m);
}
}
I've tried some methods, but none of them worked.
oRemote.Bounds = Screen.PrimaryScreen.WorkingArea;
oRemote.IsAccessible = true;
this.ShowInTaskbar = true;
or
this.Visible = ture; // do not hide the main form
or
if (m.WParam.ToInt32() == MAXIMIZE)
{
oRemote.FullScreen = true;
oRemote.Show();
oRemote.Update();
oRemote.Refresh();
}
So far, the only solution that came into my mind is to disable the restore button, like this:
oRemote.AdvancedSettings7.ConnectionBarShowRestoreButton = false;
This way isn't fix the problem, just avoid the issue to be triggered.
I would appreciate any help.
Since AxMSTSCLib has a restore down issue, I turn to use a form to handle its fullscreen mode. In this way, I can get extensive control over full-screen mode behavior, write code to tell the container what to do when the restore down button is called.
// enable container-handled full-screen mode
oRemote.AdvancedSettings7.ContainerHandledFullScreen = 1;
Then, customize OnRequestGoFullScreen, OnRequestLeaveFullScreen, OnRequestContainerMinimize.
oRemote.OnRequestGoFullScreen += corey_OnRequestGoFullScreen;
oRemote.OnRequestLeaveFullScreen += corey_OnRequestLeaveFullScreen;
oRemote.OnRequestContainerMinimize += corey_OnRequestContainerMinimize;
private void corey_OnRequestGoFullScreen(object sender, EventArgs e)
{
// set the form to fullscreen
}
private void corey_OnRequestLeaveFullScreen(object sender, EventArgs e)
{
// set the form back to non-fullscreen mode
}
private void corey_OnRequestContainerMinimize(object sender, EventArgs e)
{
// minimize the form
}
The method I posted here can be a workaround to this issue.
When I currently save, it saves the checkboxes (but only the color and it's contents, but not if it was checked for some reason), but of course, since I only have 1 setting for each of the properties of the check box (like color, if it is checked(which doesn't work), and it's content), the last checkbox to be added by the user overwrites every other checkbox.
The other problem is that it only creates 1 checkbox, and I don't know how to create the exact number of checkboxes the user has created, with code.
Yesterday I almost spent the whole day looking for solutions, and today I have only spent some hours, maybe two.
Properties.Settings.Default.CheckBoxes is a bool value, to tell it if it's checked or unchecked. Properties.Settings.Default.CheckBoxText
I started to code in c# some days ago, so my code is probably sloppy. Here is my code:
private void DoneBtn_Click(object sender, RoutedEventArgs e)
{
playSimpleSound();
TextRange descText = new TextRange(descTextBox.Document.ContentStart, descTextBox.Document.ContentEnd);
CheckBox c = new CheckBox();
c.Name = "CheckBoxs";
c.IsChecked = false;
c.Content = TitleTextBox.Text + "\n" + descText.Text;
c.Foreground = new SolidColorBrush(Colors.SkyBlue);
checkBoxPanel.Children.Add(c);
checkBoxScroll.Content = checkBoxPanel;
CreateTaskPanel.Visibility = Visibility.Collapsed;
Properties.Settings.Default.CheckBoxText = (string)c.Content;
}
private void MainWindow1_Loaded(object sender, RoutedEventArgs e)
{
if (Properties.Settings.Default.Value == true)
{
CheckBox c = new CheckBox();
c.IsChecked = Properties.Settings.Default.CheckBoxes;
c.Content = Properties.Settings.Default.CheckBoxText;
c.Foreground = Properties.Settings.Default.CheckBoxColor;
checkBoxPanel.Children.Add(c);
}
Properties.Settings.Default.Value = true;
}
private void MainWindow1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
Properties.Settings.Default.Save();
}
I wrote a windows form application which can change my GateWay automatically by executing two Batch files.
Problem is that I want to show "check.ico" icon beside the item in ContextMenu list when I click on it.
Here is my code :
public Form1()
{
InitializeComponent();
}
Image MenuStripImage = Image.FromFile(#"C:\Users\ALIENWARE\Documents\Visual Studio 2015\Projects\routerSelect\routerSelect\Check.ico");
NotifyIcon TrayIcon;
ContextMenuStrip TrayMenu;
private void Form1_Load(object sender, EventArgs e)
{
Image MenuStripExit = Image.FromFile(#"C:\Users\ALIENWARE\Documents\Visual Studio 2015\Projects\routerSelect\routerSelect\exit.ico");
this.WindowState = FormWindowState.Maximized;
this.ShowInTaskbar = false;
this.Hide();
TrayIcon = new NotifyIcon();
TrayMenu = new ContextMenuStrip();
TrayMenu.Items.Add("GreenPAcket").Click += new EventHandler(GreenpacketSelect);
//if (DLinkSelectcliced == true && GreenPacketSelected == false)
//{
TrayMenu.Items.Add(MenuStripImage);
//}
TrayMenu.Items.Add("D-Link DSL 2890AL 5GHz").Click += new EventHandler(DLinkSelect);
//if (GreenPacketSelected == true && DLinkSelectcliced == false)
//{
TrayMenu.Items.Add(MenuStripImage);
//}
TrayMenu.Items.Add("Exit", MenuStripExit).Click += new EventHandler(Exit);
TrayIcon.ContextMenuStrip = TrayMenu;
TrayIcon.Visible = true;
TrayIcon.Icon = new Icon(#"C:\Users\ALIENWARE\Documents\Visual Studio 2015\Projects\WindowsFormsApplication2\WindowsFormsApplication2\router.ico");
}
private void Exit(object sender, EventArgs e)
{
System.Windows.Forms.Application.Exit();
}
// bool DLinkSelectcliced = false;
private void DLinkSelect(object sender, EventArgs e)
{
// DLinkSelectcliced = true;
Process Proc = new Process();
Proc.StartInfo.FileName = (#"C:\Users\ALIENWARE\Documents\Visual Studio 2015\Projects\routerSelect\routerSelect\Dlink.lnk");
Proc.Start();
TrayMenu.Items.Add(MenuStripImage);
// GreenPacketSelected = false;
}
// bool GreenPacketSelected = false;
private void GreenpacketSelect(object sender, EventArgs e)
{
// GreenPacketSelected = true;
Process Proc = new Process();
Proc.StartInfo.FileName = (#"C:\Users\ALIENWARE\Documents\Visual Studio 2015\Projects\routerSelect\routerSelect\GreenPacket.lnk");
Proc.Start();
TrayMenu.Items.Add(MenuStripImage);
// DLinkSelectcliced = false;
}
If check.ico is a check mark, you don't need it for ContextMenuItem because it has option to show check mark, just enable CheckOnClick property for that item.
But if you want to show your custom icon instead, you must first convert it to image
You can do it yourself or use this method below to let C# do it for you
Use method here to convert icon to bmp image:
http://www.dotnetfunda.com/codes/show/967/icon-to-bitmap-in-csharp-winforms
Or simply create .png/.bmp version of this icon instead.
But first you should add this icons as resources to your project because if this icons will not exist in the location you gave it won't work (so you won't be able to use it on another PC).
To do it open solution explorer, right click on project and select properties, then go to resources, select icons, click Add Resource and add all icons.
Then to load icons from resources you simply type:
notifyIcon1.Icon = Properties.Resources.iconName
To show icon as image in your ContextMenuItem method in the link like this:
menu1ToolStripMenuItem.Image = ConvertFromIconToBitmap(Properties.Resources.iconName, new Size(16,16))
Btw, I glanced at your code and I think there is a lot of problems there...
I have a Form which holds a DataGridView, this Form also loads with an invisible Form which only holds another DataGridView. The second DGV is used to display more information on the items in the first DGV.
The second DGV should only be shown when the user clicks inside the 7th Cell of any row in the first DGV. I have already managed to get it to hide when I click other cells, but I can't seem to get it to hide when I click outside the DataGridView. I have already tried the Leave, RowLeave and LostFocus events without success. I think it is because as soon as the second DataGridView is displayed, it gets the focus and this somehow messes with the event.
Here is my code:
public class Form1
{
Form schedules = new Form();
DataGridView backups = new DataGridView();
public Form1()
{
this.schedules.Visible = false;
backups.DataBind();
}
private void backups_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex != -1 && e.ColumnIndex == 7)
{
if (this.schedules.getData(Convert.ToInt32(backups.Rows[e.RowIndex].Cells[0].Value)))
{
this.schedules.Owner = this;
this.schedules.Visible = true;
this.schedules.changePosition(Cursor.Position);
}
else
{
this.schedules.Visible = false;
}
}
else
{
this.schedules.Visible = false;
}
}
}
public class Schedules : Form
{
DataGridView grdSchedules = new DataGridView();
public Schedules()
{
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Visible = false;
this.AutoSize = true;
this.grdSchedules.RowHeadersVisible = false;
this.grdSchedules.AllowUserToAddRows = false;
this.grdSchedules.ScrollBars = ScrollBars.None;
this.grdSchedules.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
this.grdSchedules.AllowUserToResizeColumns = false;
this.grdSchedules.AllowUserToResizeRows = false;
this.grdSchedules.AllowUserToDeleteRows = false;
}
}
private void Form1_Click(object sender, EventArgs e)
{
this.schedules.Visible = false;
}
Users tend to click on the biggest window they see to close popups. You can also do the same with the secondary form; or even add a close button to it.
I think you would want to combine Form Click and Grid Leave event to make it work.
private void Form1_Click(object sender, EventArgs e)
{
detailForm.Visible = false;
}
private void dataGridView1_Leave(object sender, EventArgs e)
{
detailForm.Visible = false;
}
Now if a user clicks outside Grid on form or directly into a different control, then your detail form should be hidden.
Hope it helps.
Im not very familiar with refactoring but I feel like I need to do some changes in this code...
I have 4 buttons in my app. Each one calls an method that receives data from an online xml and use this data to populate a listbox.
I kept myself repeating code for all those methods, although I see they differ sometimes.
Here is a little explanation of my logic:
The buttons btnA_Click and btnB_Click get the same data, but from differente years.
The same goes for the other 2 buttons. They get the same data(not the same as btnA
and btnB) but from different years too.
Buttons:
private void btnA_Click(object sender, RoutedEventArgs e)
{
string webService = #"xml from the web";
table.OpenReadAsync(new Uri(webService));
btnA.IsEnabled = false;
btnB.IsEnabled = false;
progressBar1.Visibility = System.Windows.Visibility.Visible;
}
private void btnB_Click(object sender, RoutedEventArgs e)
{
string webService = #"xml from the net";
table.OpenReadAsync(new Uri(webService));
btnA.IsEnabled = false;
btnB.IsEnabled = false;
progressBar1.Visibility = System.Windows.Visibility.Visible;
}
private void btnSTA_Click(object sender, RoutedEventArgs e)
{
string webService = #"xml from the web;
stats.OpenReadAsync(new Uri(webService));
btnSTA.IsEnabled = false;
btnSTB.IsEnabled = false;
progressBar3.Visibility = System.Windows.Visibility.Visible;
}
private void btnSTB_Click(object sender, RoutedEventArgs e)
{
string webService = #"xml from the web;
stats.OpenReadAsync(new Uri(webService));
btnSTA.IsEnabled = false;
btnSTB.IsEnabled = false;
progressBar3.Visibility = System.Windows.Visibility.Visible;
}
Methods:
void table_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled)
{
XElement xml = XElement.Load(e.Result);
var table = LINQ Statement...
listBox1.ItemsSource = table;
btnClassificacaoSerieA.IsEnabled = true;
btnClassificacaoSerieB.IsEnabled = true;
progressBar1.Visibility = System.Windows.Visibility.Collapsed;
}
}
void stats_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error == null && !e.Cancelled)
{
XElement xml = XElement.Load(e.Result);
var stats = LINQ Statement...
listBox3.ItemsSource = stats;
btnSTA.IsEnabled = true;
btnSTB.IsEnabled = true;
progressBar3.Visibility = System.Windows.Visibility.Collapsed;
}
}
Im a little lost here. What could I do to make this code more Object Oriented?
Thx!
Your click events seem to mostly be the same... Make a sub that does that stuff and call the sub from the click event. See if you can put any differences in parameters.
Also your code for hiding / showing the progress bar and enable / disable of buttons is pretty much the same. stuff that in to a sub with a param that lets you say if you want to hide or show.