Focusing a ReportViewer control - c#

I'm trying to set a focus on my ReportViewer control. There are no reports binded to it or whatsoever. I have this code on my Form1_Load event:
private void Form1_Load(object sender, EventArgs e)
{
this.reportViewer1.RefreshReport();
this.reportViewer1.Select();
this.reportViewer1.Focus();
if(this.reportViewer1.Focused)
{
Console.WriteLine(true);
}
else
{
Console.WriteLine(false);
}
}
After the form loads, the else block is being executed. Why does that keep happening even though I already made a call to the .Focus() and .Select() functions?

Related

C# Windows Form OnLoad() Event not updating DataGridView.DataSource

So I have this code:
private void frmSell_Load(object sender, EventArgs e)
{
dgvProducts.DataSource = _data.AllProducts();
}
Where _data.AllProducts() returns a List.
The only problem is that line will only work when outside the _Load() event, for example, it will perfectly work when placed inside a buttonĀ“s OnClick()
How can I load de products in the _Load event?

How to trigger DrillThrough with Report Viewer

I'm using ReportViewer with my WPF application.
I'm trying to trigger a function within my c# code and the button will be on the ReportViewer.
I'm wondering how do I trigger the DrillThrough?
void DemoDrillThroughEventHandler(object sender, DrillthroughEventArgs e)
{
MessageBox.Show("Drillthrough worked");
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
try
{
this._reportViewer.Drillthrough += new DrillthroughEventHandler(DemoDrillThroughEventHandler);
this._reportViewer.Reset();
....
this._reportViewer.LocalReport.Refresh();
this._reportViewer.RefreshReport();
}
}
Sometimes there is a method what rises event (OnSomething, to example, in winforms there is Button.PerformClick). Otherwise you can put code from event handler in separate function and call it directly.
Simplest solution to call it
DemoDrillThroughEventHandler(this._reportViewer, new DrillthroughEventArgs());
or even (depends on what is going on inside)
DemoDrillThroughEventHandler(null, null);

Is there a better way to close buttons on click?

I'm a beginner and have an assignment in which I must program the game of NIM. I begin with 15 "tokens" and at each turn a maximum of three can be removed, or "hidden". So far I am hiding these tokens on click by doing the following.
private void button1_Click(object sender, EventArgs e)
{
button1.Visible = false;
}
private void button2_Click(object sender, EventArgs e)
{
button2.Visible = false;
}
I simply copied and pasted that multiple times and changed the button numbers so that my buttons will close on click. This might be obvious, but is there a more efficient way to do this, instead of having 15 button close methods?
You can use the same click event for every single button, and make use of the sender object, casting it to Button:
private void buttonsToClose_Click(object sender, EventArgs e)
{
((Button)sender).Visible = false;
}
Then just add that handler to every single button you want to close itself on click.
Note, though, that this will throw an InvalidCastException if you or anyone else uses this handler on an object that is not a Button, so if you're actually going to use this code I would add some sort of conditional to check the real type of the sender.
Additionally, you could reuse this for any Control object by casting sender to Control instead, given that Button inherits from Control, and all Control objects have the Visible property. Here's an example, with a conditional to guard against an invalid cast:
private void controlToMakeInvisible_Click(object sender, EventArgs e)
{
if (sender.GetType() == typeof(Control))
{
((Control)sender).Visible = false;
}
}
A final note - it seems from your post like you may have a slight misunderstanding about the way events are created and wired in with objects in Windows Forms. If you go into the Designer, add a click event, and see it pop into your Form code as follows:
private void button1_Click(object sender, EventArgs e)
the name of this method has no bearing on its function. The button1 part of button1_Click doesn't actually have any logical linkage with the Button button1 - it's just the default name assigned by the Designer. The actual assignment of the method button1_Click to the Button.Click event is auto-generated into your Form's Designer.cs method.
The point of this is that if you copy and paste button1_Click and change every incidence of button1 with button2, like so:
private void button2_Click(object sender, EventArgs e)
{
button2.Visible = false;
}
it's not going to fire when button2 gets clicked. In actual fact, it's never going to fire at all, because the method hasn't actually been connected to any controls/events.
just call your event in a foreach loop.
private void Form1_Load(object sender, EventArgs e)
{
foreach (var button in Controls.OfType<Button>())
{
button.Click += button_Click;
}
}
void button_Click(object sender, EventArgs e)
{
((Control) sender).Visible = false;
}
if you change:
Controls.OfType<Button>()
to
Controls.OfType<Control>()
it will set visible to false for any Control. so you can control what item you want the event to be raised for easily.
OfType summary: Filters the elements of an IEnumerable based on a specified type.

SelectAll not working in Dialog WPF

I'm not sure what's going wrong but I can't select the Text of my TextBox in a dialog.
I added a FirstTimeLoadedHandler to Loaded in my view where I select the text:
public ParticipantView()
{
InitializeComponent();
Loaded += FirstTimeLoadedHandler;
}
private void FirstTimeLoadedHandler(object sender, RoutedEventArgs e)
{
SurnameBox.Focus();
Keyboard.Focus(SurnameBox);
SurnameBox.SelectAll();
}
However my textbox is in Keyboard-Focus, but not selected at all. I'm not sure why it's not working.
I thought it has something to do with my databinding but the data should be received before Loaded fires or am I wrong?
Your code would work fine with two small changes. The first is that obviously, selecting all on a TextBox that has no text in it will have no effect. Secondly, if you focus it after calling SelectAll, you'll have more luck. Try this:
private void FirstTimeLoadedHandler(object sender, RoutedEventArgs e)
{
SurnameBox.Text = "This text is selected";
SurnameBox.SelectAll();
Keyboard.Focus(SurnameBox);
}

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