I have a WebBrowser
private WebBrowser wb = new WebBrowser();
and when I click on a Button, it should open a PrintPreviewDialog and show me the WebBrowser.
So when the user clicks on a button, this code is being executed:
private void PrintPreviewCommandExecute()
{
wb.DocumentText = ReportPage;
wb.ClientSize = new System.Drawing.Size(450000, 750000);
wb.MinimumSize = new System.Drawing.Size(450000, 750000);
wb.Size = new System.Drawing.Size(450000, 750000);
wb.DocumentCompleted += wb_PreviewDocumentCompleted;
}
void wb_PreviewDocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
wb.ShowPrintPreviewDialog();
}
ReportPage contains a HTML string. As you can see, I'm trying to set the Dialog size somehow (with ridiculous big numbers...), but the window size is not being changed.
So when the Dialog is being shown, it looks like this:
http://i.imagebanana.com/img/s474xiyk/Unbenannt.png
How can I change the size of the PreviewDialog (in the top left corner)?
Related
I have used toolstripdropdown in my Windows form to show list of buttons on click of another button.
var td = new ToolStripDropDown
{
AutoSize = true,
DropShadowEnabled = false,
BackColor = Color.Transparent,
Margin = Padding.Empty,
Padding = Padding.Empty
};
var host = new ToolStripControlHost(panel)
{
BackColor = Color.Transparent,
Margin = Padding.Empty,
Padding = Padding.Empty
};
td.Items.Add(host);
The panel contains list of buttons to be displayed. To show the panel to user, on button(Show) click following line is called.
td.Show(pointonScreen);
By default, AutoClose is set to true. So whenever user clicks anywhere in the form, the toolstripdropdown is getting closed. This is ok.
My requirements:
Click Show button
Display the toolstripdropdown by calling td.show() and close the popup if td.Visible
Again click the Show button
toolstripdrown should be closed
Click anywhere in the form, toolstripdropdown should be closed if it is visible
What is happening now is, on step 3, before the button click event is raised, toolstripdropdown is getting closed. So again the dropdown is being displayed.
Is there any other way to achieve my requirements?
You should handle Closing event of the dropdown and set a flag if the dropdown is closing by click on the button which opened it. Then when you click on button, check the flag and if there wasn't a flag, show dropdown and set the flag, otherwise close the dropdown and clear the flag:
ToolStripDropDown td;
private void Form1_Load(object sender, EventArgs e)
{
td = new ToolStripDropDown { /*...*/};
var host = new ToolStripControlHost(this.panel1){ /*...*/};
td.Items.Add(host);
td.Closing += td_Closing;
}
void td_Closing(object sender, ToolStripDropDownClosingEventArgs e)
{
if (e.CloseReason == ToolStripDropDownCloseReason.AppClicked)
if (this.button1.Bounds.Contains(this.PointToClient(MousePosition)))
{
td.Tag = true;
return;
}
td.Tag = null;
}
private void button1_Click(object sender, EventArgs e)
{
if (td.Tag == null)
{
td.Show(Cursor.Position);
td.Tag = true;
}
else
{
td.Close();
td.Tag = null;
}
}
In Win Forms I have these three test methods. First to create a button, second to create a tab control with two tabs and third to move created button to first tab.
private void button1_Click(object sender, EventArgs e)
{
Button przycisk = new Button();
przycisk.Location = new Point(24, 250);
przycisk.Name = "nowy";
przycisk.Text = "utworzony";
przycisk.Width = 131;
przycisk.Height = 23;
Controls.Add(przycisk);
}
private void button2_Click(object sender, EventArgs e)
{
TabControl zakladki = new TabControl();
zakladki.Location = new Point(208, 160);
zakladki.Name = "zakl";
zakladki.Height = 150;
zakladki.Width = 208;
zakladki.TabPages.Add("zakladka1", "pierwsza");
zakladki.TabPages.Add("zakladka2", "druga");
Controls.Add(zakladki);
}
private void button3_Click(object sender, EventArgs e)
{
TabControl zakladki = (TabControl)Controls.Find("zakl", false).FirstOrDefault();
int numerZakladki = 1;
foreach (TabPage zakladka in zakladki.TabPages)
{
Control kt = Controls["nowy"];
kt.Location = new Point(10, 10); // System.NullReferenceException
zakladka.Controls.Add(kt);
numerZakladki++;
}
}
I'm having a hard time to understand the behavior upon trying to change the referenced button location. The code above throws System.NullReferenceException, but when I do
if (kt != null)
{
kt.Location = new Point(10, 10);
}
it works as expected. Can anyone explain it to me ?
The new TabControl contains two Tabs.
If you move the button to the first Tab, the control at the Main form is null.
Code without Loop:
private void button3_Click(object sender, EventArgs e)
{
TabControl zakladki = (TabControl)Controls.Find("zakl", false).FirstOrDefault();
Control kt = Controls["nowy"];
kt.Location = new Point(10, 10);
zakladki.TabPages[0].Controls.Add(kt);
}
Your nowy controls are added to the root control (Form I guess), but you are trying to find them in the tab pages under zakl. You have to either add the controls to the tab page, or find them in the root control, just like you did with zakl.
You move the "nowy" button from the Form.Controls to the first TabPage's Controls collection. This removes the control from the first collection, so the code throws the exception on the next iteration. A Control can have only a singleParent.
Either create the Button for each tab separately (instead of moving it), or add the Button to the first tab only (without the foreach loop).
I am new to WPF and I've read a lot of answers, but none of them worked for me.
I have a button with a Click method. When I click on it I want my program to show a new Window, only with an image inside, which URL is previously given. Can you show me the simplest way to create such onclick method ?
This code will start you up:
private void button1_Click(object sender, EventArgs e)
{
Form form = new Form();
form.Show();
PictureBox pb = new PictureBox();
pb.ImageLocation = "https://www.google.com/images/srpr/logo11w.png";
pb.Size = form.Size;
pb.SizeMode = PictureBoxSizeMode.Zoom;
form.Controls.Add(pb);
}
I have a problem about showing data in RichTextBox by selected tab, but I don't need to show in the selected tab, I need show by RichTextBox for run.
private void btn_Runserver_Click(object sender, EventArgs e)
{
AddTab();
StartCMD();
}
private void AddTab()
{
TabPage newTab = new TabPage((string)cbConfig.SelectedItem);
RichTextBox rtb = new RichTextBox();
rtb.Dock = DockStyle.Fill;
rtb.ScrollBars = System.Windows.Forms.RichTextBoxScrollBars.Vertical;
rtb.BorderStyle = System.Windows.Forms.BorderStyle.None;
rtb.BackColor = System.Drawing.Color.White;
rtb.ReadOnly = true;
newTab.Tag = rtb;
newTab.Name = (string)cbConfig.SelectedItem;
newTab.Controls.Add(rtb);
tabControl.Controls.Add(newTab);
tabControl.SelectTab(newTab);
}
private void build_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
string strMessage = e.Data;
if (tabControl.InvokeRequired)
{
tabControl.Invoke(new Action(() =>
{
RichTextBox rtb = (RichTextBox)tabControl.SelectedTab.Tag;
rtb.AppendText(strMessage + Environment.NewLine);
rtb.Select(rtb.Text.Length - 1, 0);
rtb.ScrollToCaret();
}));
}
}
Code RunCMD Form
proc.OutputDataReceived += build_ErrorDataReceived;
proc.BeginOutputReadLine();
Now my problem is that if I run the program and data show in RichTextbox by selected tab, but I need to show data in RichTextbox On RichText Tab Safe
Someone said "change RichTextBox rtb = (RichTextBox)tabControl.SelectedTab.Tag;, don't use SelectedTab", but I not know how to change it accordingly.
I believe you are adding RichTextBox in one event and adding the text to it in another event. So there is a post back while adding the text. In this case, you need to re-add the controls which are dynamically created. Refer This Link which explains how to handle dynamic controls in asp.net.
I need to create thumbnails list of pictures located on web.
I would like also to add CheckBox to make thumbnails choose able.
I'm trying to load pictures from urls to ListBox
// from form design file:
System.Windows.Forms.ListBox listBoxPictures;
// from main file
foreach (Photo albumPhoto in album.Photos)
{
PictureBox albumsImg = new PictureBox();
albumsImg.LoadAsync(albumPhoto.URL); // URL is string
CheckBox selectedPhotoCheckBox = new CheckBox();
listBoxPictures.Items.Add(albumsImg);
listBoxPictures.Items.Add(selectedPhotoCheckBox);
}
It doesn't work, there are no images appear in ListBox.
What am I doing wrong?
How can I make a scrollabe image list in C# Windows Form?
what is wrong is that you have to wait load completed of picture
private void button1_Click(object sender, EventArgs e)
{
//make your loop here
pictureBox1.WaitOnLoad = false;
pictureBox1.LoadCompleted += new AsyncCompletedEventHandler(pictureBox1_LoadCompleted);
pictureBox1.LoadAsync(albumPhoto.URL);
}
void pictureBox1_LoadCompleted(object sender, AsyncCompletedEventArgs e)
{
//now your picture is loaded, add to list view
CheckBox selectedPhotoCheckBox = new CheckBox();
listBoxPictures.Items.Add(albumsImg);
listBoxPictures.Items.Add(selectedPhotoCheckBox);
}